Merge "Sanitizer::escapeId: Decode entity before replacing spaces"
[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 * What happens when you mix @embed and @noflip?
95 * This really is an integration test, but oh well.
96 */
97 public function testMixedCssAnnotations( ) {
98 $basePath = __DIR__ . '/../../data/css';
99 $testModule = new ResourceLoaderFileModule( array(
100 'localBasePath' => $basePath,
101 'styles' => array( 'test.css' ),
102 ) );
103 $expectedModule = new ResourceLoaderFileModule( array(
104 'localBasePath' => $basePath,
105 'styles' => array( 'expected.css' ),
106 ) );
107
108 $contextLtr = self::getResourceLoaderContext( 'en' );
109 $contextRtl = self::getResourceLoaderContext( 'he' );
110
111 $this->assertEquals(
112 $expectedModule->getStyles( $contextLtr ),
113 $testModule->getStyles( $contextLtr ),
114 "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
115 );
116 $this->assertEquals(
117 $expectedModule->getStyles( $contextLtr ),
118 $testModule->getStyles( $contextRtl ),
119 "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
120 );
121 }
122
123 /**
124 * @dataProvider providePackedModules
125 * @covers ResourceLoader::makePackedModulesString
126 */
127 public function testMakePackedModulesString( $desc, $modules, $packed ) {
128 $this->assertEquals( $packed, ResourceLoader::makePackedModulesString( $modules ), $desc );
129 }
130
131 /**
132 * @dataProvider providePackedModules
133 * @covers ResourceLoaderContext::expandModuleNames
134 */
135 public function testexpandModuleNames( $desc, $modules, $packed ) {
136 $this->assertEquals( $modules, ResourceLoaderContext::expandModuleNames( $packed ), $desc );
137 }
138
139 public static function providePackedModules() {
140 return array(
141 array(
142 'Example from makePackedModulesString doc comment',
143 array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ),
144 'foo.bar,baz|bar.baz,quux',
145 ),
146 array(
147 'Example from expandModuleNames doc comment',
148 array( 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ),
149 'jquery.foo,bar|jquery.ui.baz,quux',
150 ),
151 array(
152 'Regression fixed in r88706 with dotless names',
153 array( 'foo', 'bar', 'baz' ),
154 'foo,bar,baz',
155 ),
156 array(
157 'Prefixless modules after a prefixed module',
158 array( 'single.module', 'foobar', 'foobaz' ),
159 'single.module|foobar,foobaz',
160 ),
161 );
162 }
163
164 public static function fakeSources() {
165 return array(
166 'examplewiki' => array(
167 'loadScript' => '//example.org/w/load.php',
168 'apiScript' => '//example.org/w/api.php',
169 ),
170 'example2wiki' => array(
171 'loadScript' => '//example.com/w/load.php',
172 'apiScript' => '//example.com/w/api.php',
173 ),
174 );
175 }
176
177 /**
178 * @covers ResourceLoader::getLoadScript
179 */
180 public function testGetLoadScript() {
181 $this->setMwGlobals( 'wgResourceLoaderSources', array() );
182 $rl = new ResourceLoader();
183 $sources = self::fakeSources();
184 $rl->addSource( $sources );
185 foreach ( array( 'examplewiki', 'example2wiki' ) as $name ) {
186 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
187 }
188
189 try {
190 $rl->getLoadScript( 'thiswasneverreigstered' );
191 $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
192 } catch ( MWException $e ) {
193 $this->assertTrue( true );
194 }
195 }
196 }
197
198 /* Hooks */
199 global $wgHooks;
200 $wgHooks['ResourceLoaderRegisterModules'][] = 'ResourceLoaderTest::resourceLoaderRegisterModules';