Merge "Cache redirects from Special:Redirect"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderFileModuleTest.php
index 20d4b54..fbddfb6 100644 (file)
@@ -320,7 +320,7 @@ class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase {
                $testModule = new ResourceLoaderFileModule( [
                        'localBasePath' => $basePath,
                        'styles' => [ 'bom.css' ],
-                       ] );
+               ] );
                $testModule->setName( 'testing' );
                $this->assertEquals(
                        substr( file_get_contents( "$basePath/bom.css" ), 0, 10 ),
@@ -372,4 +372,211 @@ class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase {
                        'Using less variables is significant'
                );
        }
+
+       public function providerGetScriptPackageFiles() {
+               $basePath = __DIR__ . '/../../data/resourceloader';
+               $base = [ 'localBasePath' => $basePath ];
+               $commentScript = file_get_contents( "$basePath/script-comment.js" );
+               $nosemiScript = file_get_contents( "$basePath/script-nosemi.js" );
+               $config = RequestContext::getMain()->getConfig();
+               return [
+                       [
+                               $base + [
+                                       'packageFiles' => [
+                                               'script-comment.js',
+                                               'script-nosemi.js'
+                                       ]
+                               ],
+                               [
+                                       'files' => [
+                                               'script-comment.js' => [
+                                                       'type' => 'script',
+                                                       'content' => $commentScript,
+                                               ],
+                                               'script-nosemi.js' => [
+                                                       'type' => 'script',
+                                                       'content' => $nosemiScript
+                                               ]
+                                       ],
+                                       'main' => 'script-comment.js'
+                               ]
+                       ],
+                       [
+                               $base + [
+                                       'packageFiles' => [
+                                               'script-comment.js',
+                                               'script-nosemi.js' => [ 'main' => true ]
+                                       ],
+                                       'deprecated' => 'Deprecation test',
+                                       'name' => 'test-deprecated'
+                               ],
+                               [
+                                       'files' => [
+                                               'script-comment.js' => [
+                                                       'type' => 'script',
+                                                       'content' => $commentScript,
+                                               ],
+                                               'script-nosemi.js' => [
+                                                       'type' => 'script',
+                                                       'content' => 'mw.log.warn(' .
+                                                               '"This page is using the deprecated ResourceLoader module \"test-deprecated\".\\n' .
+                                                               "Deprecation test" .
+                                                               '");' .
+                                                               $nosemiScript
+                                               ]
+                                       ],
+                                       'main' => 'script-nosemi.js'
+                               ]
+                       ],
+                       [
+                               $base + [
+                                       'packageFiles' => [
+                                               'init.js' => [ 'file' => 'script-comment.js', 'main' => true ],
+                                               'nosemi.js' => 'script-nosemi.js'
+                                       ]
+                               ],
+                               [
+                                       'files' => [
+                                               'init.js' => [
+                                                       'type' => 'script',
+                                                       'content' => $commentScript,
+                                               ],
+                                               'nosemi.js' => [
+                                                       'type' => 'script',
+                                                       'content' => $nosemiScript
+                                               ]
+                                       ],
+                                       'main' => 'init.js'
+                               ]
+                       ],
+                       [
+                               $base + [
+                                       'packageFiles' => [
+                                               'foo.json' => [ 'content' => [ 'Hello' => 'world' ] ],
+                                               'sample.json',
+                                               'bar.js' => [ 'content' => "console.log('Hello');" ],
+                                               'data' => [ 'type' => 'data', 'callback' => function ( $context ) {
+                                                       return [ 'langCode' => $context->getLanguage() ];
+                                               } ],
+                                               'config' => [ 'type' => 'data', 'config' => [
+                                                       'Sitename',
+                                                       'wgVersion' => 'Version',
+                                               ] ],
+                                       ]
+                               ],
+                               [
+                                       'files' => [
+                                               'foo.json' => [
+                                                       'type' => 'data',
+                                                       'content' => [ 'Hello' => 'world' ],
+                                               ],
+                                               'sample.json' => [
+                                                       'type' => 'data',
+                                                       'content' => (object)[ 'foo' => 'bar', 'answer' => 42 ],
+                                               ],
+                                               'bar.js' => [
+                                                       'type' => 'script',
+                                                       'content' => "console.log('Hello');",
+                                               ],
+                                               'data' => [
+                                                       'type' => 'data',
+                                                       'content' => [ 'langCode' => 'fy' ]
+                                               ],
+                                               'config' => [
+                                                       'type' => 'data',
+                                                       'content' => [
+                                                               'Sitename' => $config->get( 'Sitename' ),
+                                                               'wgVersion' => $config->get( 'Version' ),
+                                                       ]
+                                               ]
+                                       ],
+                                       'main' => 'bar.js'
+                               ],
+                               [
+                                       'lang' => 'fy'
+                               ]
+                       ],
+                       [
+                               $base + [
+                                       'packageFiles' => [
+                                               [ 'file' => 'script-comment.js' ]
+                                       ]
+                               ],
+                               false
+                       ],
+                       [
+                               $base + [
+                                       'packageFiles' => [
+                                               'foo.json' => [ 'callback' => 'functionThatDoesNotExist142857' ]
+                                       ]
+                               ],
+                               false
+                       ],
+                       [
+                               $base + [
+                                       'packageFiles' => [
+                                               'foo' => [ 'type' => 'script', 'config' => [ 'Sitename' ] ]
+                                       ]
+                               ],
+                               false
+                       ],
+                       [
+                               $base + [
+                                       'packageFiles' => [
+                                               'foo.js' => [ 'config' => 'Sitename' ]
+                                       ]
+                               ],
+                               false
+                       ],
+                       [
+                               $base + [
+                                       'packageFiles' => [
+                                               'foo.js' => [ 'garbage' => 'data' ]
+                                       ]
+                               ],
+                               false
+                       ],
+                       [
+                               $base + [
+                                       'packageFiles' => [
+                                               'filethatdoesnotexist142857.js'
+                                       ]
+                               ],
+                               false
+                       ],
+                       [
+                               $base + [
+                                       'packageFiles' => [
+                                               'script-nosemi.js',
+                                               'foo.json' => [
+                                                       'type' => 'data',
+                                                       'content' => [ 'Hello' => 'world' ],
+                                                       'main' => true
+                                               ]
+                                       ]
+                               ],
+                               false
+                       ]
+               ];
+       }
+
+       /**
+        * @dataProvider providerGetScriptPackageFiles
+        * @covers ResourceLoaderFileModule::getScript
+        * @covers ResourceLoaderFileModule::getPackageFiles
+        * @covers ResourceLoaderFileModule::expandPackageFiles
+        */
+       public function testGetScriptPackageFiles( $moduleDefinition, $expected, $contextOptions = [] ) {
+               $module = new ResourceLoaderFileModule( $moduleDefinition );
+               $context = $this->getResourceLoaderContext( $contextOptions );
+               if ( isset( $moduleDefinition['name'] ) ) {
+                       $module->setName( $moduleDefinition['name'] );
+               }
+               if ( $expected === false ) {
+                       $this->setExpectedException( MWException::class );
+                       $module->getScript( $context );
+               } else {
+                       $this->assertEquals( $expected, $module->getScript( $context ) );
+               }
+       }
 }