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 $styles = $module->getStyles( $context );
89 $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
93 * Strip @noflip annotations from CSS code.
97 private function stripNoflip( $css ) {
98 return str_replace( '/*@noflip*/ ', '', $css );
102 * What happens when you mix @embed and @noflip?
103 * This really is an integration test, but oh well.
105 public function testMixedCssAnnotations( ) {
106 $basePath = __DIR__
. '/../../data/css';
107 $testModule = new ResourceLoaderFileModule( array(
108 'localBasePath' => $basePath,
109 'styles' => array( 'test.css' ),
111 $expectedModule = new ResourceLoaderFileModule( array(
112 'localBasePath' => $basePath,
113 'styles' => array( 'expected.css' ),
116 $contextLtr = $this->getResourceLoaderContext( 'en', 'ltr' );
117 $contextRtl = $this->getResourceLoaderContext( 'he', 'rtl' );
119 // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
120 // the @noflip annotations are always preserved, we need to strip them first.
122 $expectedModule->getStyles( $contextLtr ),
123 $this->stripNoflip( $testModule->getStyles( $contextLtr ) ),
124 "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
127 $expectedModule->getStyles( $contextLtr ),
128 $this->stripNoflip( $testModule->getStyles( $contextRtl ) ),
129 "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
134 * @dataProvider providePackedModules
135 * @covers ResourceLoader::makePackedModulesString
137 public function testMakePackedModulesString( $desc, $modules, $packed ) {
138 $this->assertEquals( $packed, ResourceLoader
::makePackedModulesString( $modules ), $desc );
142 * @dataProvider providePackedModules
143 * @covers ResourceLoaderContext::expandModuleNames
145 public function testexpandModuleNames( $desc, $modules, $packed ) {
146 $this->assertEquals( $modules, ResourceLoaderContext
::expandModuleNames( $packed ), $desc );
149 public static function providePackedModules() {
152 'Example from makePackedModulesString doc comment',
153 array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ),
154 'foo.bar,baz|bar.baz,quux',
157 'Example from expandModuleNames doc comment',
158 array( 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ),
159 'jquery.foo,bar|jquery.ui.baz,quux',
162 'Regression fixed in r88706 with dotless names',
163 array( 'foo', 'bar', 'baz' ),
167 'Prefixless modules after a prefixed module',
168 array( 'single.module', 'foobar', 'foobaz' ),
169 'single.module|foobar,foobaz',
174 public static function provideAddSource() {
176 array( 'examplewiki', '//example.org/w/load.php', 'examplewiki' ),
177 array( 'example2wiki', array( 'loadScript' => '//example.com/w/load.php' ), 'example2wiki' ),
179 array( 'foowiki' => '//foo.org/w/load.php', 'bazwiki' => '//baz.org/w/load.php' ),
181 array( 'foowiki', 'bazwiki' )
184 array( 'foowiki' => '//foo.org/w/load.php' ),
192 * @dataProvider provideAddSource
193 * @covers ResourceLoader::addSource
195 public function testAddSource( $name, $info, $expected ) {
196 $rl = new ResourceLoader
;
197 if ( $expected === false ) {
198 $this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
199 $rl->addSource( $name, $info );
201 $rl->addSource( $name, $info );
202 if ( is_array( $expected ) ) {
203 foreach ( $expected as $source ) {
204 $this->assertArrayHasKey( $source, $rl->getSources() );
207 $this->assertArrayHasKey( $expected, $rl->getSources() );
211 public static function fakeSources() {
213 'examplewiki' => array(
214 'loadScript' => '//example.org/w/load.php',
215 'apiScript' => '//example.org/w/api.php',
217 'example2wiki' => array(
218 'loadScript' => '//example.com/w/load.php',
219 'apiScript' => '//example.com/w/api.php',
225 * @covers ResourceLoader::getLoadScript
227 public function testGetLoadScript() {
228 $this->setMwGlobals( 'wgResourceLoaderSources', array() );
229 $rl = new ResourceLoader();
230 $sources = self
::fakeSources();
231 $rl->addSource( $sources );
232 foreach ( array( 'examplewiki', 'example2wiki' ) as $name ) {
233 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
237 $rl->getLoadScript( 'thiswasneverreigstered' );
238 $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
239 } catch ( MWException
$e ) {
240 $this->assertTrue( true );