MediaWiki  REL1_24
ResourceLoaderStartupModuleTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class ResourceLoaderStartupModuleTest extends ResourceLoaderTestCase {
00004 
00005     public static function provideGetModuleRegistrations() {
00006         return array(
00007             array( array(
00008                 'msg' => 'Empty registry',
00009                 'modules' => array(),
00010                 'out' => '
00011 mw.loader.addSource( {
00012     "local": "/w/load.php"
00013 } );mw.loader.register( [] );'
00014             ) ),
00015             array( array(
00016                 'msg' => 'Basic registry',
00017                 'modules' => array(
00018                     'test.blank' => new ResourceLoaderTestModule(),
00019                 ),
00020                 'out' => '
00021 mw.loader.addSource( {
00022     "local": "/w/load.php"
00023 } );mw.loader.register( [
00024     [
00025         "test.blank",
00026         "1388534400"
00027     ]
00028 ] );',
00029             ) ),
00030             array( array(
00031                 'msg' => 'Group signature',
00032                 'modules' => array(
00033                     'test.blank' => new ResourceLoaderTestModule(),
00034                     'test.group.foo' => new ResourceLoaderTestModule( array( 'group' => 'x-foo' ) ),
00035                     'test.group.bar' => new ResourceLoaderTestModule( array( 'group' => 'x-bar' ) ),
00036                 ),
00037                 'out' => '
00038 mw.loader.addSource( {
00039     "local": "/w/load.php"
00040 } );mw.loader.register( [
00041     [
00042         "test.blank",
00043         "1388534400"
00044     ],
00045     [
00046         "test.group.foo",
00047         "1388534400",
00048         [],
00049         "x-foo"
00050     ],
00051     [
00052         "test.group.bar",
00053         "1388534400",
00054         [],
00055         "x-bar"
00056     ]
00057 ] );'
00058             ) ),
00059             array( array(
00060                 'msg' => 'Different target (non-test should not be registered)',
00061                 'modules' => array(
00062                     'test.blank' => new ResourceLoaderTestModule(),
00063                     'test.target.foo' => new ResourceLoaderTestModule( array( 'targets' => array( 'x-foo' ) ) ),
00064                 ),
00065                 'out' => '
00066 mw.loader.addSource( {
00067     "local": "/w/load.php"
00068 } );mw.loader.register( [
00069     [
00070         "test.blank",
00071         "1388534400"
00072     ]
00073 ] );'
00074             ) ),
00075             array( array(
00076                 'msg' => 'Foreign source',
00077                 'sources' => array(
00078                     'example' => array(
00079                         'loadScript' => 'http://example.org/w/load.php',
00080                         'apiScript' => 'http://example.org/w/api.php',
00081                     ),
00082                 ),
00083                 'modules' => array(
00084                     'test.blank' => new ResourceLoaderTestModule( array( 'source' => 'example' ) ),
00085                 ),
00086                 'out' => '
00087 mw.loader.addSource( {
00088     "local": "/w/load.php",
00089     "example": "http://example.org/w/load.php"
00090 } );mw.loader.register( [
00091     [
00092         "test.blank",
00093         "1388534400",
00094         [],
00095         null,
00096         "example"
00097     ]
00098 ] );'
00099             ) ),
00100             array( array(
00101                 'msg' => 'Conditional dependency function',
00102                 'modules' => array(
00103                     'test.x.core' => new ResourceLoaderTestModule(),
00104                     'test.x.polyfill' => new ResourceLoaderTestModule( array(
00105                         'skipFunction' => 'return true;'
00106                     ) ),
00107                     'test.y.polyfill' => new ResourceLoaderTestModule( array(
00108                         'skipFunction' =>
00109                             'return !!(' .
00110                             '    window.JSON &&' .
00111                             '    JSON.parse &&' .
00112                             '    JSON.stringify' .
00113                             ');'
00114                     ) ),
00115                     'test.z.foo' => new ResourceLoaderTestModule( array(
00116                         'dependencies' => array(
00117                             'test.x.core',
00118                             'test.x.polyfil',
00119                             'test.y.polyfil',
00120                         ),
00121                     ) ),
00122                 ),
00123                 'out' => '
00124 mw.loader.addSource( {
00125     "local": "/w/load.php"
00126 } );mw.loader.register( [
00127     [
00128         "test.x.core",
00129         "1388534400"
00130     ],
00131     [
00132         "test.x.polyfill",
00133         "1388534400",
00134         [],
00135         null,
00136         "local",
00137         "return true;"
00138     ],
00139     [
00140         "test.y.polyfill",
00141         "1388534400",
00142         [],
00143         null,
00144         "local",
00145         "return !!(    window.JSON \u0026\u0026    JSON.parse \u0026\u0026    JSON.stringify);"
00146     ],
00147     [
00148         "test.z.foo",
00149         "1388534400",
00150         [
00151             "test.x.core",
00152             "test.x.polyfil",
00153             "test.y.polyfil"
00154         ]
00155     ]
00156 ] );',
00157             ) ),
00158             array( array(
00159                 // This may seem like an edge case, but a plain MediaWiki core install
00160                 // with a few extensions installed is likely far more complex than this
00161                 // even, not to mention an install like Wikipedia.
00162                 // TODO: Make this even more realistic.
00163                 'msg' => 'Advanced (everything combined)',
00164                 'sources' => array(
00165                     'example' => array(
00166                         'loadScript' => 'http://example.org/w/load.php',
00167                         'apiScript' => 'http://example.org/w/api.php',
00168                     ),
00169                 ),
00170                 'modules' => array(
00171                     'test.blank' => new ResourceLoaderTestModule(),
00172                     'test.x.core' => new ResourceLoaderTestModule(),
00173                     'test.x.util' => new ResourceLoaderTestModule( array(
00174                         'dependencies' => array(
00175                             'test.x.core',
00176                         ),
00177                     ) ),
00178                     'test.x.foo' => new ResourceLoaderTestModule( array(
00179                         'dependencies' => array(
00180                             'test.x.core',
00181                         ),
00182                     ) ),
00183                     'test.x.bar' => new ResourceLoaderTestModule( array(
00184                         'dependencies' => array(
00185                             'test.x.core',
00186                             'test.x.util',
00187                         ),
00188                     ) ),
00189                     'test.x.quux' => new ResourceLoaderTestModule( array(
00190                         'dependencies' => array(
00191                             'test.x.foo',
00192                             'test.x.bar',
00193                             'test.x.util',
00194                             'test.x.unknown',
00195                         ),
00196                     ) ),
00197                     'test.group.foo.1' => new ResourceLoaderTestModule( array(
00198                         'group' => 'x-foo',
00199                     ) ),
00200                     'test.group.foo.2' => new ResourceLoaderTestModule( array(
00201                         'group' => 'x-foo',
00202                     ) ),
00203                     'test.group.bar.1' => new ResourceLoaderTestModule( array(
00204                         'group' => 'x-bar',
00205                     ) ),
00206                     'test.group.bar.2' => new ResourceLoaderTestModule( array(
00207                         'group' => 'x-bar',
00208                         'source' => 'example',
00209                     ) ),
00210                     'test.target.foo' => new ResourceLoaderTestModule( array(
00211                         'targets' => array( 'x-foo' ),
00212                     ) ),
00213                     'test.target.bar' => new ResourceLoaderTestModule( array(
00214                         'source' => 'example',
00215                         'targets' => array( 'x-foo' ),
00216                     ) ),
00217                 ),
00218                 'out' => '
00219 mw.loader.addSource( {
00220     "local": "/w/load.php",
00221     "example": "http://example.org/w/load.php"
00222 } );mw.loader.register( [
00223     [
00224         "test.blank",
00225         "1388534400"
00226     ],
00227     [
00228         "test.x.core",
00229         "1388534400"
00230     ],
00231     [
00232         "test.x.util",
00233         "1388534400",
00234         [
00235             "test.x.core"
00236         ]
00237     ],
00238     [
00239         "test.x.foo",
00240         "1388534400",
00241         [
00242             "test.x.core"
00243         ]
00244     ],
00245     [
00246         "test.x.bar",
00247         "1388534400",
00248         [
00249             "test.x.util"
00250         ]
00251     ],
00252     [
00253         "test.x.quux",
00254         "1388534400",
00255         [
00256             "test.x.foo",
00257             "test.x.bar",
00258             "test.x.unknown"
00259         ]
00260     ],
00261     [
00262         "test.group.foo.1",
00263         "1388534400",
00264         [],
00265         "x-foo"
00266     ],
00267     [
00268         "test.group.foo.2",
00269         "1388534400",
00270         [],
00271         "x-foo"
00272     ],
00273     [
00274         "test.group.bar.1",
00275         "1388534400",
00276         [],
00277         "x-bar"
00278     ],
00279     [
00280         "test.group.bar.2",
00281         "1388534400",
00282         [],
00283         "x-bar",
00284         "example"
00285     ]
00286 ] );'
00287             ) ),
00288         );
00289     }
00290 
00298     public function testGetModuleRegistrations( $case ) {
00299         if ( isset( $case['sources'] ) ) {
00300             $this->setMwGlobals( 'wgResourceLoaderSources', $case['sources'] );
00301         }
00302 
00303         $context = self::getResourceLoaderContext();
00304         $rl = $context->getResourceLoader();
00305 
00306         $rl->register( $case['modules'] );
00307 
00308         $module = new ResourceLoaderStartUpModule();
00309         $this->assertEquals(
00310             ltrim( $case['out'], "\n" ),
00311             $module->getModuleRegistrations( $context ),
00312             $case['msg']
00313         );
00314     }
00315 
00316     public static function provideRegistrations() {
00317         return array(
00318             array( array(
00319                 'test.blank' => new ResourceLoaderTestModule(),
00320                 'test.min' => new ResourceLoaderTestModule( array(
00321                     'skipFunction' =>
00322                         'return !!(' .
00323                         '    window.JSON &&' .
00324                         '    JSON.parse &&' .
00325                         '    JSON.stringify' .
00326                         ');',
00327                     'dependencies' => array(
00328                         'test.blank',
00329                     ),
00330                 ) ),
00331             ) )
00332         );
00333     }
00337     public function testRegistrationsMinified( $modules ) {
00338         $this->setMwGlobals( 'wgResourceLoaderDebug', false );
00339 
00340         $context = self::getResourceLoaderContext();
00341         $rl = $context->getResourceLoader();
00342         $rl->register( $modules );
00343         $module = new ResourceLoaderStartUpModule();
00344         $this->assertEquals(
00345 'mw.loader.addSource({"local":"/w/load.php"});'
00346 . 'mw.loader.register(['
00347 . '["test.blank","1388534400"],'
00348 . '["test.min","1388534400",["test.blank"],null,"local",'
00349 . '"return!!(window.JSON\u0026\u0026JSON.parse\u0026\u0026JSON.stringify);"'
00350 . ']]);',
00351             $module->getModuleRegistrations( $context ),
00352             'Minified output'
00353         );
00354     }
00355 
00359     public function testRegistrationsUnminified( $modules ) {
00360         $context = self::getResourceLoaderContext();
00361         $rl = $context->getResourceLoader();
00362         $rl->register( $modules );
00363         $module = new ResourceLoaderStartUpModule();
00364         $this->assertEquals(
00365 'mw.loader.addSource( {
00366     "local": "/w/load.php"
00367 } );mw.loader.register( [
00368     [
00369         "test.blank",
00370         "1388534400"
00371     ],
00372     [
00373         "test.min",
00374         "1388534400",
00375         [
00376             "test.blank"
00377         ],
00378         null,
00379         "local",
00380         "return !!(    window.JSON \u0026\u0026    JSON.parse \u0026\u0026    JSON.stringify);"
00381     ]
00382 ] );',
00383             $module->getModuleRegistrations( $context ),
00384             'Unminified output'
00385         );
00386     }
00387 
00388 }