MediaWiki  REL1_22
ResourceLoaderTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class ResourceLoaderTest extends MediaWikiTestCase {
00004 
00005     protected static $resourceLoaderRegisterModulesHook;
00006 
00007     protected function setUp() {
00008         parent::setUp();
00009 
00010         // $wgResourceLoaderLESSFunctions, $wgResourceLoaderLESSImportPaths; $wgResourceLoaderLESSVars;
00011 
00012         $this->setMwGlobals( array(
00013             'wgResourceLoaderLESSFunctions' => array(
00014                 'test-sum' => function ( $frame, $less ) {
00015                     $sum = 0;
00016                     foreach ( $frame[2] as $arg ) {
00017                         $sum += (int)$arg[1];
00018                     }
00019                     return $sum;
00020                 },
00021             ),
00022             'wgResourceLoaderLESSImportPaths' => array(
00023                 dirname( __DIR__ ) . '/data/less/common',
00024             ),
00025             'wgResourceLoaderLESSVars' => array(
00026                 'foo'  => '2px',
00027                 'Foo' => '#eeeeee',
00028                 'bar' => 5,
00029             ),
00030         ) );
00031     }
00032 
00033     /* Hook Methods */
00034 
00038     public static function resourceLoaderRegisterModules( &$resourceLoader ) {
00039         self::$resourceLoaderRegisterModulesHook = true;
00040 
00041         return true;
00042     }
00043 
00044     /* Provider Methods */
00045     public static function provideValidModules() {
00046         return array(
00047             array( 'TEST.validModule1', new ResourceLoaderTestModule() ),
00048         );
00049     }
00050 
00051     public static function provideResourceLoaderContext() {
00052         $resourceLoader = new ResourceLoader();
00053         $request = new FauxRequest();
00054         return array(
00055             array( new ResourceLoaderContext( $resourceLoader, $request ) ),
00056         );
00057     }
00058 
00059     /* Test Methods */
00060 
00065     public function testCreatingNewResourceLoaderCallsRegistrationHook() {
00066         self::$resourceLoaderRegisterModulesHook = false;
00067         $resourceLoader = new ResourceLoader();
00068         $this->assertTrue( self::$resourceLoaderRegisterModulesHook );
00069 
00070         return $resourceLoader;
00071     }
00072 
00079     public function testRegisteredValidModulesAreAccessible(
00080         $name, ResourceLoaderModule $module, ResourceLoader $resourceLoader
00081     ) {
00082         $resourceLoader->register( $name, $module );
00083         $this->assertEquals( $module, $resourceLoader->getModule( $name ) );
00084     }
00085 
00090     public function testLessFileCompilation( $context ) {
00091         $basePath = __DIR__ . '/../data/less/module';
00092         $module = new ResourceLoaderFileModule( array(
00093             'localBasePath' => $basePath,
00094             'styles' => array( 'styles.less' ),
00095         ) );
00096         $styles = $module->getStyles( $context );
00097         $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
00098     }
00099 
00104     public function testMakePackedModulesString( $desc, $modules, $packed ) {
00105         $this->assertEquals( $packed, ResourceLoader::makePackedModulesString( $modules ), $desc );
00106     }
00107 
00112     public function testexpandModuleNames( $desc, $modules, $packed ) {
00113         $this->assertEquals( $modules, ResourceLoaderContext::expandModuleNames( $packed ), $desc );
00114     }
00115 
00116     public static function providePackedModules() {
00117         return array(
00118             array(
00119                 'Example from makePackedModulesString doc comment',
00120                 array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ),
00121                 'foo.bar,baz|bar.baz,quux',
00122             ),
00123             array(
00124                 'Example from expandModuleNames doc comment',
00125                 array( 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ),
00126                 'jquery.foo,bar|jquery.ui.baz,quux',
00127             ),
00128             array(
00129                 'Regression fixed in r88706 with dotless names',
00130                 array( 'foo', 'bar', 'baz' ),
00131                 'foo,bar,baz',
00132             ),
00133             array(
00134                 'Prefixless modules after a prefixed module',
00135                 array( 'single.module', 'foobar', 'foobaz' ),
00136                 'single.module|foobar,foobaz',
00137             ),
00138         );
00139     }
00140 }
00141 
00142 /* Stubs */
00143 
00144 class ResourceLoaderTestModule extends ResourceLoaderModule {
00145 }
00146 
00147 /* Hooks */
00148 global $wgHooks;
00149 $wgHooks['ResourceLoaderRegisterModules'][] = 'ResourceLoaderTest::resourceLoaderRegisterModules';