diff options
Diffstat (limited to 'tests/phpunit/includes/resourceloader/ResourceLoaderTest.php')
-rw-r--r-- | tests/phpunit/includes/resourceloader/ResourceLoaderTest.php | 185 |
1 files changed, 127 insertions, 58 deletions
diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php index f19f6886..ca7307ec 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php @@ -2,13 +2,9 @@ class ResourceLoaderTest extends ResourceLoaderTestCase { - protected static $resourceLoaderRegisterModulesHook; - protected function setUp() { parent::setUp(); - // $wgResourceLoaderLESSFunctions, $wgResourceLoaderLESSImportPaths; $wgResourceLoaderLESSVars; - $this->setMwGlobals( array( 'wgResourceLoaderLESSFunctions' => array( 'test-sum' => function ( $frame, $less ) { @@ -30,35 +26,33 @@ class ResourceLoaderTest extends ResourceLoaderTestCase { ) ); } - /* Hook Methods */ - - /** - * ResourceLoaderRegisterModules hook - */ - public static function resourceLoaderRegisterModules( &$resourceLoader ) { - self::$resourceLoaderRegisterModulesHook = true; - - return true; - } - - /* Provider Methods */ public static function provideValidModules() { return array( array( 'TEST.validModule1', new ResourceLoaderTestModule() ), ); } - /* Test Methods */ - /** * Ensures that the ResourceLoaderRegisterModules hook is called when a new * ResourceLoader object is constructed. * @covers ResourceLoader::__construct */ public function testCreatingNewResourceLoaderCallsRegistrationHook() { - self::$resourceLoaderRegisterModulesHook = false; + $resourceLoaderRegisterModulesHook = false; + + $this->setMwGlobals( 'wgHooks', array( + 'ResourceLoaderRegisterModules' => array( + function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) { + $resourceLoaderRegisterModulesHook = true; + } + ) + ) ); + $resourceLoader = new ResourceLoader(); - $this->assertTrue( self::$resourceLoaderRegisterModulesHook ); + $this->assertTrue( + $resourceLoaderRegisterModulesHook, + 'Hook ResourceLoaderRegisterModules called' + ); return $resourceLoader; } @@ -80,7 +74,7 @@ class ResourceLoaderTest extends ResourceLoaderTestCase { * @covers ResourceLoaderFileModule::compileLessFile */ public function testLessFileCompilation() { - $context = self::getResourceLoaderContext(); + $context = $this->getResourceLoaderContext(); $basePath = __DIR__ . '/../../data/less/module'; $module = new ResourceLoaderFileModule( array( 'localBasePath' => $basePath, @@ -96,43 +90,11 @@ class ResourceLoaderTest extends ResourceLoaderTestCase { * @param string $css * @return string */ - private function stripNoflip( $css ) { + private static function stripNoflip( $css ) { return str_replace( '/*@noflip*/ ', '', $css ); } /** - * What happens when you mix @embed and @noflip? - * This really is an integration test, but oh well. - */ - public function testMixedCssAnnotations( ) { - $basePath = __DIR__ . '/../../data/css'; - $testModule = new ResourceLoaderFileModule( array( - 'localBasePath' => $basePath, - 'styles' => array( 'test.css' ), - ) ); - $expectedModule = new ResourceLoaderFileModule( array( - 'localBasePath' => $basePath, - 'styles' => array( 'expected.css' ), - ) ); - - $contextLtr = self::getResourceLoaderContext( 'en' ); - $contextRtl = self::getResourceLoaderContext( 'he' ); - - // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and - // the @noflip annotations are always preserved, we need to strip them first. - $this->assertEquals( - $expectedModule->getStyles( $contextLtr ), - $this->stripNoflip( $testModule->getStyles( $contextLtr ) ), - "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode" - ); - $this->assertEquals( - $expectedModule->getStyles( $contextLtr ), - $this->stripNoflip( $testModule->getStyles( $contextRtl ) ), - "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode" - ); - } - - /** * @dataProvider providePackedModules * @covers ResourceLoader::makePackedModulesString */ @@ -193,6 +155,7 @@ class ResourceLoaderTest extends ResourceLoaderTestCase { /** * @dataProvider provideAddSource * @covers ResourceLoader::addSource + * @covers ResourceLoader::getSources */ public function testAddSource( $name, $info, $expected ) { $rl = new ResourceLoader; @@ -223,6 +186,106 @@ class ResourceLoaderTest extends ResourceLoaderTestCase { ); } + public static function provideLoaderImplement() { + return array( + array( array( + 'title' => 'Implement scripts, styles and messages', + + 'name' => 'test.example', + 'scripts' => 'mw.example();', + 'styles' => array( 'css' => array( '.mw-example {}' ) ), + 'messages' => array( 'example' => '' ), + 'templates' => array(), + + 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) { +mw.example(); +}, { + "css": [ + ".mw-example {}" + ] +}, { + "example": "" +} );', + ) ), + array( array( + 'title' => 'Implement scripts', + + 'name' => 'test.example', + 'scripts' => 'mw.example();', + 'styles' => array(), + 'messages' => new XmlJsCode( '{}' ), + 'templates' => array(), + 'title' => 'scripts, styles and messags', + + 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) { +mw.example(); +} );', + ) ), + array( array( + 'title' => 'Implement styles', + + 'name' => 'test.example', + 'scripts' => array(), + 'styles' => array( 'css' => array( '.mw-example {}' ) ), + 'messages' => new XmlJsCode( '{}' ), + 'templates' => array(), + + 'expected' => 'mw.loader.implement( "test.example", [], { + "css": [ + ".mw-example {}" + ] +} );', + ) ), + array( array( + 'title' => 'Implement scripts and messages', + + 'name' => 'test.example', + 'scripts' => 'mw.example();', + 'styles' => array(), + 'messages' => array( 'example' => '' ), + 'templates' => array(), + + 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) { +mw.example(); +}, {}, { + "example": "" +} );', + ) ), + array( array( + 'title' => 'Implement scripts and templates', + + 'name' => 'test.example', + 'scripts' => 'mw.example();', + 'styles' => array(), + 'messages' => new XmlJsCode( '{}' ), + 'templates' => array( 'example.html' => '' ), + + 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) { +mw.example(); +}, {}, {}, { + "example.html": "" +} );', + ) ), + ); + } + + /** + * @dataProvider provideLoaderImplement + * @covers ResourceLoader::makeLoaderImplementScript + */ + public function testMakeLoaderImplementScript( $case ) { + $this->assertEquals( + $case['expected'], + ResourceLoader::makeLoaderImplementScript( + $case['name'], + $case['scripts'], + $case['styles'], + $case['messages'], + $case['templates'] + ) + ); + } + /** * @covers ResourceLoader::getLoadScript */ @@ -242,8 +305,14 @@ class ResourceLoaderTest extends ResourceLoaderTestCase { $this->assertTrue( true ); } } -} -/* Hooks */ -global $wgHooks; -$wgHooks['ResourceLoaderRegisterModules'][] = 'ResourceLoaderTest::resourceLoaderRegisterModules'; + /** + * @covers ResourceLoader::isModuleRegistered + */ + public function testIsModuleRegistered() { + $rl = new ResourceLoader(); + $rl->register( 'test.module', new ResourceLoaderTestModule() ); + $this->assertTrue( $rl->isModuleRegistered( 'test.module' ) ); + $this->assertFalse( $rl->isModuleRegistered( 'test.modulenotregistered' ) ); + } +} |