tests: Add unit tests for ResourceLoaderStartupModule
authorTimo Tijhof <krinklemail@gmail.com>
Fri, 7 Mar 2014 18:29:23 +0000 (19:29 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Fri, 7 Mar 2014 19:15:35 +0000 (20:15 +0100)
Change-Id: I7671813e1d7b4ea75265608c22d8efe8805560e4

tests/phpunit/ResourceLoaderTestCase.php
tests/phpunit/includes/resourceloader/ResourceLoaderStartupModuleTest.php [new file with mode: 0644]

index b37aa1a..a03e9cc 100644 (file)
@@ -39,6 +39,30 @@ abstract class ResourceLoaderTestCase extends MediaWikiTestCase {
 
 /* Stubs */
 
-class ResourceLoaderTestModule extends ResourceLoaderModule {}
+class ResourceLoaderTestModule extends ResourceLoaderModule {
+
+       protected $dependencies = array();
+       protected $group = null;
+       protected $source = 'local';
+       protected $targets = array( 'test' );
+
+       public function __construct( $options = array() ) {
+               foreach ( $options as $key => $value ) {
+                       $this->$key = $value;
+               }
+       }
+
+       public function getDependencies() {
+               return $this->dependencies;
+       }
+
+       public function getGroup() {
+               return $this->group;
+       }
+
+       public function getSource() {
+               return $this->source;
+       }
+}
 
 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {}
diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderStartupModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderStartupModuleTest.php
new file mode 100644 (file)
index 0000000..5b51ef8
--- /dev/null
@@ -0,0 +1,278 @@
+<?php
+
+class ResourceLoaderStartupModuleTest extends ResourceLoaderTestCase {
+
+       public static function provideGetModuleRegistrations() {
+               return array(
+                       array( array(
+                               'msg' => 'Empty registry',
+                               'modules' => array(),
+                               'out' => '
+mw.loader.addSource( {
+    "local": {
+        "loadScript": "/w/load.php",
+        "apiScript": "/w/api.php"
+    }
+} );mw.loader.register( [] );'
+                       ) ),
+                       array( array(
+                               'msg' => 'Basic registry',
+                               'modules' => array(
+                                       'test.blank' => new ResourceLoaderTestModule(),
+                               ),
+                               'out' => '
+mw.loader.addSource( {
+    "local": {
+        "loadScript": "/w/load.php",
+        "apiScript": "/w/api.php"
+    }
+} );mw.loader.register( [
+    [
+        "test.blank",
+        "1388534400"
+    ]
+] );',
+                       ) ),
+                       array( array(
+                               'msg' => 'Group signature',
+                               'modules' => array(
+                                       'test.blank' => new ResourceLoaderTestModule(),
+                                       'test.group.foo' => new ResourceLoaderTestModule( array( 'group' => 'x-foo' ) ),
+                                       'test.group.bar' => new ResourceLoaderTestModule( array( 'group' => 'x-bar' ) ),
+                               ),
+                               'out' => '
+mw.loader.addSource( {
+    "local": {
+        "loadScript": "/w/load.php",
+        "apiScript": "/w/api.php"
+    }
+} );mw.loader.register( [
+    [
+        "test.blank",
+        "1388534400"
+    ],
+    [
+        "test.group.foo",
+        "1388534400",
+        [],
+        "x-foo"
+    ],
+    [
+        "test.group.bar",
+        "1388534400",
+        [],
+        "x-bar"
+    ]
+] );'
+                       ) ),
+                       array( array(
+                               'msg' => 'Different target (non-test should not be registered)',
+                               'modules' => array(
+                                       'test.blank' => new ResourceLoaderTestModule(),
+                                       'test.target.foo' => new ResourceLoaderTestModule( array( 'targets' => array( 'x-foo' ) ) ),
+                               ),
+                               'out' => '
+mw.loader.addSource( {
+    "local": {
+        "loadScript": "/w/load.php",
+        "apiScript": "/w/api.php"
+    }
+} );mw.loader.register( [
+    [
+        "test.blank",
+        "1388534400"
+    ]
+] );'
+                       ) ),
+                       array( array(
+                               'msg' => 'Foreign source',
+                               'sources' => array(
+                                       'example' => array(
+                                               'loadScript' => 'http://example.org/w/load.php',
+                                               'apiScript' => 'http://example.org/w/api.php',
+                                       ),
+                               ),
+                               'modules' => array(
+                                       'test.blank' => new ResourceLoaderTestModule( array( 'source' => 'example' ) ),
+                               ),
+                               'out' => '
+mw.loader.addSource( {
+    "local": {
+        "loadScript": "/w/load.php",
+        "apiScript": "/w/api.php"
+    },
+    "example": {
+        "loadScript": "http://example.org/w/load.php",
+        "apiScript": "http://example.org/w/api.php"
+    }
+} );mw.loader.register( [
+    [
+        "test.blank",
+        "1388534400",
+        [],
+        null,
+        "example"
+    ]
+] );'
+                       ) ),
+                       array( array(
+                               // This may seem like an edge case, but a plain MediaWiki core install
+                               // with a few extensions installed is likely far more complex than this
+                               // even, not to mention an install like Wikipedia.
+                               // TODO: Make this even more realistic.
+                               'msg' => 'Advanced (everything combined)',
+                               'sources' => array(
+                                       'example' => array(
+                                               'loadScript' => 'http://example.org/w/load.php',
+                                               'apiScript' => 'http://example.org/w/api.php',
+                                       ),
+                               ),
+                               'modules' => array(
+                                       'test.blank' => new ResourceLoaderTestModule(),
+                                       'test.x.core' => new ResourceLoaderTestModule(),
+                                       'test.x.util' => new ResourceLoaderTestModule( array(
+                                               'dependencies' => array(
+                                                       'test.x.core',
+                                               ),
+                                       ) ),
+                                       'test.x.foo' => new ResourceLoaderTestModule( array(
+                                               'dependencies' => array(
+                                                       'test.x.core',
+                                               ),
+                                       ) ),
+                                       'test.x.bar' => new ResourceLoaderTestModule( array(
+                                               'dependencies' => array(
+                                                       'test.x.core',
+                                                       'test.x.util',
+                                               ),
+                                       ) ),
+                                       'test.x.quux' => new ResourceLoaderTestModule( array(
+                                               'dependencies' => array(
+                                                       'test.x.foo',
+                                                       'test.x.bar',
+                                                       'test.x.util',
+                                               ),
+                                       ) ),
+                                       'test.group.foo.1' => new ResourceLoaderTestModule( array(
+                                               'group' => 'x-foo',
+                                       ) ),
+                                       'test.group.foo.2' => new ResourceLoaderTestModule( array(
+                                               'group' => 'x-foo',
+                                       ) ),
+                                       'test.group.bar.1' => new ResourceLoaderTestModule( array(
+                                               'group' => 'x-bar',
+                                       ) ),
+                                       'test.group.bar.2' => new ResourceLoaderTestModule( array(
+                                               'group' => 'x-bar',
+                                               'source' => 'example',
+                                       ) ),
+                                       'test.target.foo' => new ResourceLoaderTestModule( array(
+                                               'targets' => array( 'x-foo' ),
+                                       ) ),
+                                       'test.target.bar' => new ResourceLoaderTestModule( array(
+                                               'source' => 'example',
+                                               'targets' => array( 'x-foo' ),
+                                       ) ),
+                               ),
+                               'out' => '
+mw.loader.addSource( {
+    "local": {
+        "loadScript": "/w/load.php",
+        "apiScript": "/w/api.php"
+    },
+    "example": {
+        "loadScript": "http://example.org/w/load.php",
+        "apiScript": "http://example.org/w/api.php"
+    }
+} );mw.loader.register( [
+    [
+        "test.blank",
+        "1388534400"
+    ],
+    [
+        "test.x.core",
+        "1388534400"
+    ],
+    [
+        "test.x.util",
+        "1388534400",
+        [
+            "test.x.core"
+        ]
+    ],
+    [
+        "test.x.foo",
+        "1388534400",
+        [
+            "test.x.core"
+        ]
+    ],
+    [
+        "test.x.bar",
+        "1388534400",
+        [
+            "test.x.core",
+            "test.x.util"
+        ]
+    ],
+    [
+        "test.x.quux",
+        "1388534400",
+        [
+            "test.x.foo",
+            "test.x.bar",
+            "test.x.util"
+        ]
+    ],
+    [
+        "test.group.foo.1",
+        "1388534400",
+        [],
+        "x-foo"
+    ],
+    [
+        "test.group.foo.2",
+        "1388534400",
+        [],
+        "x-foo"
+    ],
+    [
+        "test.group.bar.1",
+        "1388534400",
+        [],
+        "x-bar"
+    ],
+    [
+        "test.group.bar.2",
+        "1388534400",
+        [],
+        "x-bar",
+        "example"
+    ]
+] );'
+                       ) ),
+               );
+       }
+
+       /**
+        * @dataProvider provideGetModuleRegistrations
+        * @covers ResourceLoaderStartUpModule::getModuleRegistrations
+        */
+       public function testGetModuleRegistrations( $case ) {
+               if ( isset( $case['sources'] ) ) {
+                       $this->setMwGlobals( 'wgResourceLoaderSources', $case['sources'] );
+               }
+
+               $context = self::getResourceLoaderContext();
+               $rl = $context->getResourceLoader();
+
+               $rl->register( $case['modules'] );
+
+               $this->assertEquals(
+                       ltrim( $case['out'], "\n" ),
+                       ResourceLoaderStartUpModule::getModuleRegistrations( $context ),
+                       $case['msg']
+               );
+       }
+
+}