MediaWiki  REL1_24
ResourceLoaderTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class ResourceLoaderTest extends ResourceLoaderTestCase {
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( 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     /* Test Methods */
00052 
00058     public function testCreatingNewResourceLoaderCallsRegistrationHook() {
00059         self::$resourceLoaderRegisterModulesHook = false;
00060         $resourceLoader = new ResourceLoader();
00061         $this->assertTrue( self::$resourceLoaderRegisterModulesHook );
00062 
00063         return $resourceLoader;
00064     }
00065 
00072     public function testRegisteredValidModulesAreAccessible(
00073         $name, ResourceLoaderModule $module, ResourceLoader $resourceLoader
00074     ) {
00075         $resourceLoader->register( $name, $module );
00076         $this->assertEquals( $module, $resourceLoader->getModule( $name ) );
00077     }
00078 
00082     public function testLessFileCompilation() {
00083         $context = self::getResourceLoaderContext();
00084         $basePath = __DIR__ . '/../../data/less/module';
00085         $module = new ResourceLoaderFileModule( array(
00086             'localBasePath' => $basePath,
00087             'styles' => array( 'styles.less' ),
00088         ) );
00089         $styles = $module->getStyles( $context );
00090         $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
00091     }
00092 
00098     private function stripNoflip( $css ) {
00099         return str_replace( '/*@noflip*/ ', '', $css );
00100     }
00101 
00106     public function testMixedCssAnnotations(  ) {
00107         $basePath = __DIR__ . '/../../data/css';
00108         $testModule = new ResourceLoaderFileModule( array(
00109             'localBasePath' => $basePath,
00110             'styles' => array( 'test.css' ),
00111         ) );
00112         $expectedModule = new ResourceLoaderFileModule( array(
00113             'localBasePath' => $basePath,
00114             'styles' => array( 'expected.css' ),
00115         ) );
00116 
00117         $contextLtr = self::getResourceLoaderContext( 'en' );
00118         $contextRtl = self::getResourceLoaderContext( 'he' );
00119 
00120         // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
00121         // the @noflip annotations are always preserved, we need to strip them first.
00122         $this->assertEquals(
00123             $expectedModule->getStyles( $contextLtr ),
00124             $this->stripNoflip( $testModule->getStyles( $contextLtr ) ),
00125             "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
00126         );
00127         $this->assertEquals(
00128             $expectedModule->getStyles( $contextLtr ),
00129             $this->stripNoflip( $testModule->getStyles( $contextRtl ) ),
00130             "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
00131         );
00132     }
00133 
00138     public function testMakePackedModulesString( $desc, $modules, $packed ) {
00139         $this->assertEquals( $packed, ResourceLoader::makePackedModulesString( $modules ), $desc );
00140     }
00141 
00146     public function testexpandModuleNames( $desc, $modules, $packed ) {
00147         $this->assertEquals( $modules, ResourceLoaderContext::expandModuleNames( $packed ), $desc );
00148     }
00149 
00150     public static function providePackedModules() {
00151         return array(
00152             array(
00153                 'Example from makePackedModulesString doc comment',
00154                 array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ),
00155                 'foo.bar,baz|bar.baz,quux',
00156             ),
00157             array(
00158                 'Example from expandModuleNames doc comment',
00159                 array( 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ),
00160                 'jquery.foo,bar|jquery.ui.baz,quux',
00161             ),
00162             array(
00163                 'Regression fixed in r88706 with dotless names',
00164                 array( 'foo', 'bar', 'baz' ),
00165                 'foo,bar,baz',
00166             ),
00167             array(
00168                 'Prefixless modules after a prefixed module',
00169                 array( 'single.module', 'foobar', 'foobaz' ),
00170                 'single.module|foobar,foobaz',
00171             ),
00172         );
00173     }
00174 
00175     public static function provideAddSource() {
00176         return array(
00177             array( 'examplewiki', '//example.org/w/load.php', 'examplewiki' ),
00178             array( 'example2wiki', array( 'loadScript' => '//example.com/w/load.php' ), 'example2wiki' ),
00179             array(
00180                 array( 'foowiki' => '//foo.org/w/load.php', 'bazwiki' => '//baz.org/w/load.php' ),
00181                 null,
00182                 array( 'foowiki', 'bazwiki' )
00183             ),
00184             array(
00185                 array( 'foowiki' => '//foo.org/w/load.php' ),
00186                 null,
00187                 false,
00188             ),
00189         );
00190     }
00191 
00196     public function testAddSource( $name, $info, $expected ) {
00197         $rl = new ResourceLoader;
00198         if ( $expected === false ) {
00199             $this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
00200             $rl->addSource( $name, $info );
00201         }
00202         $rl->addSource( $name, $info );
00203         if ( is_array( $expected ) ) {
00204             foreach ( $expected as $source ) {
00205                 $this->assertArrayHasKey( $source, $rl->getSources() );
00206             }
00207         } else {
00208             $this->assertArrayHasKey( $expected, $rl->getSources() );
00209         }
00210     }
00211 
00212     public static function fakeSources() {
00213         return array(
00214             'examplewiki' => array(
00215                 'loadScript' => '//example.org/w/load.php',
00216                 'apiScript' => '//example.org/w/api.php',
00217             ),
00218             'example2wiki' => array(
00219                 'loadScript' => '//example.com/w/load.php',
00220                 'apiScript' => '//example.com/w/api.php',
00221             ),
00222         );
00223     }
00224 
00228     public function testGetLoadScript() {
00229         $this->setMwGlobals( 'wgResourceLoaderSources', array() );
00230         $rl = new ResourceLoader();
00231         $sources = self::fakeSources();
00232         $rl->addSource( $sources );
00233         foreach ( array( 'examplewiki', 'example2wiki' ) as $name ) {
00234             $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
00235         }
00236 
00237         try {
00238             $rl->getLoadScript( 'thiswasneverreigstered' );
00239             $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
00240         } catch ( MWException $e ) {
00241             $this->assertTrue( true );
00242         }
00243     }
00244 }
00245 
00246 /* Hooks */
00247 global $wgHooks;
00248 $wgHooks['ResourceLoaderRegisterModules'][] = 'ResourceLoaderTest::resourceLoaderRegisterModules';