Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / resourceloader / ResourceLoaderTest.php
blob4fc7378a8aefdb46453fac78a726f5c42ba62bb4
1 <?php
3 class ResourceLoaderTest extends ResourceLoaderTestCase {
5 protected function setUp() {
6 parent::setUp();
8 // $wgResourceLoaderLESSFunctions, $wgResourceLoaderLESSImportPaths; $wgResourceLoaderLESSVars;
10 $this->setMwGlobals( array(
11 'wgResourceLoaderLESSFunctions' => array(
12 'test-sum' => function ( $frame, $less ) {
13 $sum = 0;
14 foreach ( $frame[2] as $arg ) {
15 $sum += (int)$arg[1];
17 return $sum;
20 'wgResourceLoaderLESSImportPaths' => array(
21 dirname( dirname( __DIR__ ) ) . '/data/less/common',
23 'wgResourceLoaderLESSVars' => array(
24 'foo' => '2px',
25 'Foo' => '#eeeeee',
26 'bar' => 5,
28 ) );
31 /* Provider Methods */
32 public static function provideValidModules() {
33 return array(
34 array( 'TEST.validModule1', new ResourceLoaderTestModule() ),
38 /* Test Methods */
40 /**
41 * Ensures that the ResourceLoaderRegisterModules hook is called when a new
42 * ResourceLoader object is constructed.
43 * @covers ResourceLoader::__construct
45 public function testCreatingNewResourceLoaderCallsRegistrationHook() {
46 $resourceLoaderRegisterModulesHook = false;
48 $this->setMwGlobals( 'wgHooks', array(
49 'ResourceLoaderRegisterModules' => array(
50 function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
51 $resourceLoaderRegisterModulesHook = true;
54 ) );
56 $resourceLoader = new ResourceLoader();
57 $this->assertTrue(
58 $resourceLoaderRegisterModulesHook,
59 'Hook ResourceLoaderRegisterModules called'
62 return $resourceLoader;
65 /**
66 * @dataProvider provideValidModules
67 * @depends testCreatingNewResourceLoaderCallsRegistrationHook
68 * @covers ResourceLoader::register
69 * @covers ResourceLoader::getModule
71 public function testRegisteredValidModulesAreAccessible(
72 $name, ResourceLoaderModule $module, ResourceLoader $resourceLoader
73 ) {
74 $resourceLoader->register( $name, $module );
75 $this->assertEquals( $module, $resourceLoader->getModule( $name ) );
78 /**
79 * @covers ResourceLoaderFileModule::compileLessFile
81 public function testLessFileCompilation() {
82 $context = $this->getResourceLoaderContext();
83 $basePath = __DIR__ . '/../../data/less/module';
84 $module = new ResourceLoaderFileModule( array(
85 'localBasePath' => $basePath,
86 'styles' => array( 'styles.less' ),
87 ) );
88 $styles = $module->getStyles( $context );
89 $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
92 /**
93 * Strip @noflip annotations from CSS code.
94 * @param string $css
95 * @return string
97 private function stripNoflip( $css ) {
98 return str_replace( '/*@noflip*/ ', '', $css );
102 * What happens when you mix @embed and @noflip?
103 * This really is an integration test, but oh well.
105 public function testMixedCssAnnotations( ) {
106 $basePath = __DIR__ . '/../../data/css';
107 $testModule = new ResourceLoaderFileModule( array(
108 'localBasePath' => $basePath,
109 'styles' => array( 'test.css' ),
110 ) );
111 $expectedModule = new ResourceLoaderFileModule( array(
112 'localBasePath' => $basePath,
113 'styles' => array( 'expected.css' ),
114 ) );
116 $contextLtr = $this->getResourceLoaderContext( 'en', 'ltr' );
117 $contextRtl = $this->getResourceLoaderContext( 'he', 'rtl' );
119 // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
120 // the @noflip annotations are always preserved, we need to strip them first.
121 $this->assertEquals(
122 $expectedModule->getStyles( $contextLtr ),
123 $this->stripNoflip( $testModule->getStyles( $contextLtr ) ),
124 "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
126 $this->assertEquals(
127 $expectedModule->getStyles( $contextLtr ),
128 $this->stripNoflip( $testModule->getStyles( $contextRtl ) ),
129 "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
134 * @dataProvider providePackedModules
135 * @covers ResourceLoader::makePackedModulesString
137 public function testMakePackedModulesString( $desc, $modules, $packed ) {
138 $this->assertEquals( $packed, ResourceLoader::makePackedModulesString( $modules ), $desc );
142 * @dataProvider providePackedModules
143 * @covers ResourceLoaderContext::expandModuleNames
145 public function testexpandModuleNames( $desc, $modules, $packed ) {
146 $this->assertEquals( $modules, ResourceLoaderContext::expandModuleNames( $packed ), $desc );
149 public static function providePackedModules() {
150 return array(
151 array(
152 'Example from makePackedModulesString doc comment',
153 array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ),
154 'foo.bar,baz|bar.baz,quux',
156 array(
157 'Example from expandModuleNames doc comment',
158 array( 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ),
159 'jquery.foo,bar|jquery.ui.baz,quux',
161 array(
162 'Regression fixed in r88706 with dotless names',
163 array( 'foo', 'bar', 'baz' ),
164 'foo,bar,baz',
166 array(
167 'Prefixless modules after a prefixed module',
168 array( 'single.module', 'foobar', 'foobaz' ),
169 'single.module|foobar,foobaz',
174 public static function provideAddSource() {
175 return array(
176 array( 'examplewiki', '//example.org/w/load.php', 'examplewiki' ),
177 array( 'example2wiki', array( 'loadScript' => '//example.com/w/load.php' ), 'example2wiki' ),
178 array(
179 array( 'foowiki' => '//foo.org/w/load.php', 'bazwiki' => '//baz.org/w/load.php' ),
180 null,
181 array( 'foowiki', 'bazwiki' )
183 array(
184 array( 'foowiki' => '//foo.org/w/load.php' ),
185 null,
186 false,
192 * @dataProvider provideAddSource
193 * @covers ResourceLoader::addSource
195 public function testAddSource( $name, $info, $expected ) {
196 $rl = new ResourceLoader;
197 if ( $expected === false ) {
198 $this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
199 $rl->addSource( $name, $info );
201 $rl->addSource( $name, $info );
202 if ( is_array( $expected ) ) {
203 foreach ( $expected as $source ) {
204 $this->assertArrayHasKey( $source, $rl->getSources() );
206 } else {
207 $this->assertArrayHasKey( $expected, $rl->getSources() );
211 public static function fakeSources() {
212 return array(
213 'examplewiki' => array(
214 'loadScript' => '//example.org/w/load.php',
215 'apiScript' => '//example.org/w/api.php',
217 'example2wiki' => array(
218 'loadScript' => '//example.com/w/load.php',
219 'apiScript' => '//example.com/w/api.php',
225 * @covers ResourceLoader::getLoadScript
227 public function testGetLoadScript() {
228 $this->setMwGlobals( 'wgResourceLoaderSources', array() );
229 $rl = new ResourceLoader();
230 $sources = self::fakeSources();
231 $rl->addSource( $sources );
232 foreach ( array( 'examplewiki', 'example2wiki' ) as $name ) {
233 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
236 try {
237 $rl->getLoadScript( 'thiswasneverreigstered' );
238 $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
239 } catch ( MWException $e ) {
240 $this->assertTrue( true );