3 class ResourceLoaderTest
extends ResourceLoaderTestCase
{
5 protected static $resourceLoaderRegisterModulesHook;
7 protected function setUp() {
10 // $wgResourceLoaderLESSFunctions, $wgResourceLoaderLESSImportPaths; $wgResourceLoaderLESSVars;
12 $this->setMwGlobals( array(
13 'wgResourceLoaderLESSFunctions' => array(
14 'test-sum' => function ( $frame, $less ) {
16 foreach ( $frame[2] as $arg ) {
22 'wgResourceLoaderLESSImportPaths' => array(
23 dirname( dirname( __DIR__
) ) . '/data/less/common',
25 'wgResourceLoaderLESSVars' => array(
36 * ResourceLoaderRegisterModules hook
38 public static function resourceLoaderRegisterModules( &$resourceLoader ) {
39 self
::$resourceLoaderRegisterModulesHook = true;
44 /* Provider Methods */
45 public static function provideValidModules() {
47 array( 'TEST.validModule1', new ResourceLoaderTestModule() ),
54 * Ensures that the ResourceLoaderRegisterModules hook is called when a new
55 * ResourceLoader object is constructed.
56 * @covers ResourceLoader::__construct
58 public function testCreatingNewResourceLoaderCallsRegistrationHook() {
59 self
::$resourceLoaderRegisterModulesHook = false;
60 $resourceLoader = new ResourceLoader();
61 $this->assertTrue( self
::$resourceLoaderRegisterModulesHook );
63 return $resourceLoader;
67 * @dataProvider provideValidModules
68 * @depends testCreatingNewResourceLoaderCallsRegistrationHook
69 * @covers ResourceLoader::register
70 * @covers ResourceLoader::getModule
72 public function testRegisteredValidModulesAreAccessible(
73 $name, ResourceLoaderModule
$module, ResourceLoader
$resourceLoader
75 $resourceLoader->register( $name, $module );
76 $this->assertEquals( $module, $resourceLoader->getModule( $name ) );
80 * @covers ResourceLoaderFileModule::compileLessFile
82 public function testLessFileCompilation() {
83 $context = self
::getResourceLoaderContext();
84 $basePath = __DIR__
. '/../../data/less/module';
85 $module = new ResourceLoaderFileModule( array(
86 'localBasePath' => $basePath,
87 'styles' => array( 'styles.less' ),
89 $styles = $module->getStyles( $context );
90 $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
94 * What happens when you mix @embed and @noflip?
95 * This really is an integration test, but oh well.
97 public function testMixedCssAnnotations( ) {
98 $basePath = __DIR__
. '/../../data/css';
99 $testModule = new ResourceLoaderFileModule( array(
100 'localBasePath' => $basePath,
101 'styles' => array( 'test.css' ),
103 $expectedModule = new ResourceLoaderFileModule( array(
104 'localBasePath' => $basePath,
105 'styles' => array( 'expected.css' ),
108 $contextLtr = self
::getResourceLoaderContext( 'en' );
109 $contextRtl = self
::getResourceLoaderContext( 'he' );
112 $expectedModule->getStyles( $contextLtr ),
113 $testModule->getStyles( $contextLtr ),
114 "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
117 $expectedModule->getStyles( $contextLtr ),
118 $testModule->getStyles( $contextRtl ),
119 "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
124 * @dataProvider providePackedModules
125 * @covers ResourceLoader::makePackedModulesString
127 public function testMakePackedModulesString( $desc, $modules, $packed ) {
128 $this->assertEquals( $packed, ResourceLoader
::makePackedModulesString( $modules ), $desc );
132 * @dataProvider providePackedModules
133 * @covers ResourceLoaderContext::expandModuleNames
135 public function testexpandModuleNames( $desc, $modules, $packed ) {
136 $this->assertEquals( $modules, ResourceLoaderContext
::expandModuleNames( $packed ), $desc );
139 public static function providePackedModules() {
142 'Example from makePackedModulesString doc comment',
143 array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ),
144 'foo.bar,baz|bar.baz,quux',
147 'Example from expandModuleNames doc comment',
148 array( 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ),
149 'jquery.foo,bar|jquery.ui.baz,quux',
152 'Regression fixed in r88706 with dotless names',
153 array( 'foo', 'bar', 'baz' ),
157 'Prefixless modules after a prefixed module',
158 array( 'single.module', 'foobar', 'foobaz' ),
159 'single.module|foobar,foobaz',
164 public static function fakeSources() {
166 'examplewiki' => array(
167 'loadScript' => '//example.org/w/load.php',
168 'apiScript' => '//example.org/w/api.php',
170 'example2wiki' => array(
171 'loadScript' => '//example.com/w/load.php',
172 'apiScript' => '//example.com/w/api.php',
178 * @covers ResourceLoader::getLoadScript
180 public function testGetLoadScript() {
181 $this->setMwGlobals( 'wgResourceLoaderSources', array() );
182 $rl = new ResourceLoader();
183 $sources = self
::fakeSources();
184 $rl->addSource( $sources );
185 foreach ( array( 'examplewiki', 'example2wiki' ) as $name ) {
186 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
190 $rl->getLoadScript( 'thiswasneverreigstered' );
191 $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
192 } catch ( MWException
$e ) {
193 $this->assertTrue( true );
200 $wgHooks['ResourceLoaderRegisterModules'][] = 'ResourceLoaderTest::resourceLoaderRegisterModules';