Merge "mediawiki.jqueryMsg: Prevent default action for functions as external link"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderTest.php
1 <?php
2
3 class ResourceLoaderTest extends ResourceLoaderTestCase {
4
5 protected static $resourceLoaderRegisterModulesHook;
6
7 protected function setUp() {
8 parent::setUp();
9
10 // $wgResourceLoaderLESSFunctions, $wgResourceLoaderLESSImportPaths; $wgResourceLoaderLESSVars;
11
12 $this->setMwGlobals( array(
13 'wgResourceLoaderLESSFunctions' => array(
14 'test-sum' => function ( $frame, $less ) {
15 $sum = 0;
16 foreach ( $frame[2] as $arg ) {
17 $sum += (int)$arg[1];
18 }
19 return $sum;
20 },
21 ),
22 'wgResourceLoaderLESSImportPaths' => array(
23 dirname( dirname( __DIR__ ) ) . '/data/less/common',
24 ),
25 'wgResourceLoaderLESSVars' => array(
26 'foo' => '2px',
27 'Foo' => '#eeeeee',
28 'bar' => 5,
29 ),
30 ) );
31 }
32
33 /* Hook Methods */
34
35 /**
36 * ResourceLoaderRegisterModules hook
37 */
38 public static function resourceLoaderRegisterModules( &$resourceLoader ) {
39 self::$resourceLoaderRegisterModulesHook = true;
40
41 return true;
42 }
43
44 /* Provider Methods */
45 public static function provideValidModules() {
46 return array(
47 array( 'TEST.validModule1', new ResourceLoaderTestModule() ),
48 );
49 }
50
51 /* Test Methods */
52
53 /**
54 * Ensures that the ResourceLoaderRegisterModules hook is called when a new
55 * ResourceLoader object is constructed.
56 * @covers ResourceLoader::__construct
57 */
58 public function testCreatingNewResourceLoaderCallsRegistrationHook() {
59 self::$resourceLoaderRegisterModulesHook = false;
60 $resourceLoader = new ResourceLoader();
61 $this->assertTrue( self::$resourceLoaderRegisterModulesHook );
62
63 return $resourceLoader;
64 }
65
66 /**
67 * @dataProvider provideValidModules
68 * @depends testCreatingNewResourceLoaderCallsRegistrationHook
69 * @covers ResourceLoader::register
70 * @covers ResourceLoader::getModule
71 */
72 public function testRegisteredValidModulesAreAccessible(
73 $name, ResourceLoaderModule $module, ResourceLoader $resourceLoader
74 ) {
75 $resourceLoader->register( $name, $module );
76 $this->assertEquals( $module, $resourceLoader->getModule( $name ) );
77 }
78
79 /**
80 * @covers ResourceLoaderFileModule::compileLessFile
81 */
82 public function testLessFileCompilation() {
83 $context = self::getResourceLoaderContext();
84 $basePath = __DIR__ . '/../../data/less/module';
85 $module = new ResourceLoaderFileModule( array(
86 'localBasePath' => $basePath,
87 'styles' => array( 'styles.less' ),
88 ) );
89 $styles = $module->getStyles( $context );
90 $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
91 }
92
93 /**
94 * @dataProvider providePackedModules
95 * @covers ResourceLoader::makePackedModulesString
96 */
97 public function testMakePackedModulesString( $desc, $modules, $packed ) {
98 $this->assertEquals( $packed, ResourceLoader::makePackedModulesString( $modules ), $desc );
99 }
100
101 /**
102 * @dataProvider providePackedModules
103 * @covers ResourceLoaderContext::expandModuleNames
104 */
105 public function testexpandModuleNames( $desc, $modules, $packed ) {
106 $this->assertEquals( $modules, ResourceLoaderContext::expandModuleNames( $packed ), $desc );
107 }
108
109 public static function providePackedModules() {
110 return array(
111 array(
112 'Example from makePackedModulesString doc comment',
113 array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ),
114 'foo.bar,baz|bar.baz,quux',
115 ),
116 array(
117 'Example from expandModuleNames doc comment',
118 array( 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ),
119 'jquery.foo,bar|jquery.ui.baz,quux',
120 ),
121 array(
122 'Regression fixed in r88706 with dotless names',
123 array( 'foo', 'bar', 'baz' ),
124 'foo,bar,baz',
125 ),
126 array(
127 'Prefixless modules after a prefixed module',
128 array( 'single.module', 'foobar', 'foobaz' ),
129 'single.module|foobar,foobaz',
130 ),
131 );
132 }
133
134 public static function fakeSources() {
135 return array(
136 'examplewiki' => array(
137 'loadScript' => '//example.org/w/load.php',
138 'apiScript' => '//example.org/w/api.php',
139 ),
140 'example2wiki' => array(
141 'loadScript' => '//example.com/w/load.php',
142 'apiScript' => '//example.com/w/api.php',
143 ),
144 );
145 }
146
147 /**
148 * @covers ResourceLoader::getLoadScript
149 */
150 public function testGetLoadScript() {
151 $this->setMwGlobals( 'wgResourceLoaderSources', array() );
152 $rl = new ResourceLoader();
153 $sources = self::fakeSources();
154 $rl->addSource( $sources );
155 foreach ( array( 'examplewiki', 'example2wiki' ) as $name ) {
156 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
157 }
158
159 try {
160 $rl->getLoadScript( 'thiswasneverreigstered' );
161 $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
162 } catch ( MWException $e ) {
163 $this->assertTrue( true );
164 }
165 }
166 }
167
168 /* Hooks */
169 global $wgHooks;
170 $wgHooks['ResourceLoaderRegisterModules'][] = 'ResourceLoaderTest::resourceLoaderRegisterModules';