3 class ResourceLoaderTest
extends ResourceLoaderTestCase
{
5 protected function setUp() {
8 // $wgResourceLoaderLESSFunctions, $wgResourceLoaderLESSImportPaths; $wgResourceLoaderLESSVars;
10 $this->setMwGlobals( array(
11 'wgResourceLoaderLESSFunctions' => array(
12 'test-sum' => function ( $frame, $less ) {
14 foreach ( $frame[2] as $arg ) {
20 'wgResourceLoaderLESSImportPaths' => array(
21 dirname( dirname( __DIR__
) ) . '/data/less/common',
23 'wgResourceLoaderLESSVars' => array(
31 /* Provider Methods */
32 public static function provideValidModules() {
34 array( 'TEST.validModule1', new ResourceLoaderTestModule() ),
41 * Ensures that the ResourceLoaderRegisterModules hook is called when a new
42 * ResourceLoader object is constructed.
43 * @covers ResourceLoader::__construct
45 public function testCreatingNewResourceLoaderCallsRegistrationHook() {
46 $resourceLoaderRegisterModulesHook = false;
48 $this->setMwGlobals( 'wgHooks', array(
49 'ResourceLoaderRegisterModules' => array(
50 function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
51 $resourceLoaderRegisterModulesHook = true;
56 $resourceLoader = new ResourceLoader();
58 $resourceLoaderRegisterModulesHook,
59 'Hook ResourceLoaderRegisterModules called'
62 return $resourceLoader;
66 * @dataProvider provideValidModules
67 * @depends testCreatingNewResourceLoaderCallsRegistrationHook
68 * @covers ResourceLoader::register
69 * @covers ResourceLoader::getModule
71 public function testRegisteredValidModulesAreAccessible(
72 $name, ResourceLoaderModule
$module, ResourceLoader
$resourceLoader
74 $resourceLoader->register( $name, $module );
75 $this->assertEquals( $module, $resourceLoader->getModule( $name ) );
79 * @covers ResourceLoaderFileModule::compileLessFile
81 public function testLessFileCompilation() {
82 $context = $this->getResourceLoaderContext();
83 $basePath = __DIR__
. '/../../data/less/module';
84 $module = new ResourceLoaderFileModule( array(
85 'localBasePath' => $basePath,
86 'styles' => array( 'styles.less' ),
88 $module->setName( 'test.less' );
89 $styles = $module->getStyles( $context );
90 $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
94 * Strip @noflip annotations from CSS code.
98 private function stripNoflip( $css ) {
99 return str_replace( '/*@noflip*/ ', '', $css );
103 * What happens when you mix @embed and @noflip?
104 * This really is an integration test, but oh well.
106 public function testMixedCssAnnotations( ) {
107 $basePath = __DIR__
. '/../../data/css';
108 $testModule = new ResourceLoaderFileModule( array(
109 'localBasePath' => $basePath,
110 'styles' => array( 'test.css' ),
112 $expectedModule = new ResourceLoaderFileModule( array(
113 'localBasePath' => $basePath,
114 'styles' => array( 'expected.css' ),
117 $contextLtr = $this->getResourceLoaderContext( 'en', 'ltr' );
118 $contextRtl = $this->getResourceLoaderContext( 'he', 'rtl' );
120 // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
121 // the @noflip annotations are always preserved, we need to strip them first.
123 $expectedModule->getStyles( $contextLtr ),
124 $this->stripNoflip( $testModule->getStyles( $contextLtr ) ),
125 "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
128 $expectedModule->getStyles( $contextLtr ),
129 $this->stripNoflip( $testModule->getStyles( $contextRtl ) ),
130 "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
135 * @dataProvider providePackedModules
136 * @covers ResourceLoader::makePackedModulesString
138 public function testMakePackedModulesString( $desc, $modules, $packed ) {
139 $this->assertEquals( $packed, ResourceLoader
::makePackedModulesString( $modules ), $desc );
143 * @dataProvider providePackedModules
144 * @covers ResourceLoaderContext::expandModuleNames
146 public function testexpandModuleNames( $desc, $modules, $packed ) {
147 $this->assertEquals( $modules, ResourceLoaderContext
::expandModuleNames( $packed ), $desc );
150 public static function providePackedModules() {
153 'Example from makePackedModulesString doc comment',
154 array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ),
155 'foo.bar,baz|bar.baz,quux',
158 'Example from expandModuleNames doc comment',
159 array( 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ),
160 'jquery.foo,bar|jquery.ui.baz,quux',
163 'Regression fixed in r88706 with dotless names',
164 array( 'foo', 'bar', 'baz' ),
168 'Prefixless modules after a prefixed module',
169 array( 'single.module', 'foobar', 'foobaz' ),
170 'single.module|foobar,foobaz',
175 public static function provideAddSource() {
177 array( 'examplewiki', '//example.org/w/load.php', 'examplewiki' ),
178 array( 'example2wiki', array( 'loadScript' => '//example.com/w/load.php' ), 'example2wiki' ),
180 array( 'foowiki' => '//foo.org/w/load.php', 'bazwiki' => '//baz.org/w/load.php' ),
182 array( 'foowiki', 'bazwiki' )
185 array( 'foowiki' => '//foo.org/w/load.php' ),
193 * @dataProvider provideAddSource
194 * @covers ResourceLoader::addSource
196 public function testAddSource( $name, $info, $expected ) {
197 $rl = new ResourceLoader
;
198 if ( $expected === false ) {
199 $this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
200 $rl->addSource( $name, $info );
202 $rl->addSource( $name, $info );
203 if ( is_array( $expected ) ) {
204 foreach ( $expected as $source ) {
205 $this->assertArrayHasKey( $source, $rl->getSources() );
208 $this->assertArrayHasKey( $expected, $rl->getSources() );
212 public static function fakeSources() {
214 'examplewiki' => array(
215 'loadScript' => '//example.org/w/load.php',
216 'apiScript' => '//example.org/w/api.php',
218 'example2wiki' => array(
219 'loadScript' => '//example.com/w/load.php',
220 'apiScript' => '//example.com/w/api.php',
226 * @covers ResourceLoader::getLoadScript
228 public function testGetLoadScript() {
229 $this->setMwGlobals( 'wgResourceLoaderSources', array() );
230 $rl = new ResourceLoader();
231 $sources = self
::fakeSources();
232 $rl->addSource( $sources );
233 foreach ( array( 'examplewiki', 'example2wiki' ) as $name ) {
234 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
238 $rl->getLoadScript( 'thiswasneverreigstered' );
239 $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
240 } catch ( MWException
$e ) {
241 $this->assertTrue( true );
246 * @covers ResourceLoader::isModuleRegistered
248 public function testIsModuleRegistered() {
249 $rl = new ResourceLoader();
250 $rl->register( 'test.module', new ResourceLoaderTestModule() );
251 $this->assertTrue( $rl->isModuleRegistered( 'test.module' ) );
252 $this->assertFalse( $rl->isModuleRegistered( 'test.modulenotregistered' ) );