From 1a67f2dd5fc7aae19234680451c577e499044c75 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Fri, 7 Mar 2014 19:29:23 +0100 Subject: [PATCH] tests: Add unit tests for ResourceLoaderStartupModule Change-Id: I7671813e1d7b4ea75265608c22d8efe8805560e4 --- tests/phpunit/ResourceLoaderTestCase.php | 26 +- .../ResourceLoaderStartupModuleTest.php | 278 ++++++++++++++++++ 2 files changed, 303 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/includes/resourceloader/ResourceLoaderStartupModuleTest.php diff --git a/tests/phpunit/ResourceLoaderTestCase.php b/tests/phpunit/ResourceLoaderTestCase.php index b37aa1aa5d..a03e9ccc86 100644 --- a/tests/phpunit/ResourceLoaderTestCase.php +++ b/tests/phpunit/ResourceLoaderTestCase.php @@ -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 index 0000000000..5b51ef8937 --- /dev/null +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderStartupModuleTest.php @@ -0,0 +1,278 @@ + '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'] + ); + } + +} -- 2.20.1