resourceloader: Add explicit tests for isValidModuleName()
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderTest.php
index 7239afc..19113ad 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 use Wikimedia\TestingAccessWrapper;
+use MediaWiki\MediaWikiServices;
 
 class ResourceLoaderTest extends ResourceLoaderTestCase {
 
@@ -14,25 +15,59 @@ class ResourceLoaderTest extends ResourceLoaderTestCase {
 
        /**
         * Ensure the ResourceLoaderRegisterModules hook is called.
-        *
-        * @covers ResourceLoader::__construct
+        * @coversNothing
         */
-       public function testConstructRegistrationHook() {
-               $resourceLoaderRegisterModulesHook = false;
+       public function testServiceWiring() {
+               $this->overrideMwServices();
 
+               $ranHook = 0;
                $this->setMwGlobals( 'wgHooks', [
                        'ResourceLoaderRegisterModules' => [
-                               function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
-                                       $resourceLoaderRegisterModulesHook = true;
+                               function ( &$resourceLoader ) use ( &$ranHook ) {
+                                       $ranHook++;
                                }
                        ]
                ] );
 
-               $unused = new ResourceLoader();
-               $this->assertTrue(
-                       $resourceLoaderRegisterModulesHook,
-                       'Hook ResourceLoaderRegisterModules called'
-               );
+               MediaWikiServices::getInstance()->getResourceLoader();
+
+               $this->assertSame( 1, $ranHook, 'Hook was called' );
+       }
+
+       public static function provideInvalidModuleName() {
+               return [
+                       'name with 300 chars' => [ str_repeat( 'x', 300 ) ],
+                       'name with bang' => [ 'this!that' ],
+                       'name with comma' => [ 'this,that' ],
+                       'name with pipe' => [ 'this|that' ],
+               ];
+       }
+
+       public static function provideValidModuleName() {
+               return [
+                       'empty string' => [ '' ],
+                       'simple name' => [ 'this.and-that2' ],
+                       'name with 100 chars' => [ str_repeat( 'x', 100 ) ],
+                       'name with hash' => [ 'this#that' ],
+                       'name with slash' => [ 'this/that' ],
+                       'name with at' => [ 'this@that' ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideInvalidModuleName
+        * @covers ResourceLoader
+        */
+       public function testIsValidModuleName_invalid( $name ) {
+               $this->assertFalse( ResourceLoader::isValidModuleName( $name ) );
+       }
+
+       /**
+        * @dataProvider provideValidModuleName
+        * @covers ResourceLoader
+        */
+       public function testIsValidModuleName_valid( $name ) {
+               $this->assertTrue( ResourceLoader::isValidModuleName( $name ) );
        }
 
        /**
@@ -61,6 +96,7 @@ class ResourceLoaderTest extends ResourceLoaderTestCase {
 
        /**
         * @covers ResourceLoader::register
+        * @group medium
         */
        public function testRegisterEmptyString() {
                $module = new ResourceLoaderTestModule();
@@ -71,6 +107,7 @@ class ResourceLoaderTest extends ResourceLoaderTestCase {
 
        /**
         * @covers ResourceLoader::register
+        * @group medium
         */
        public function testRegisterInvalidName() {
                $resourceLoader = new EmptyResourceLoader();