Merge "resourceloader: Add explicit tests for isValidModuleName()"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Mon, 10 Jun 2019 23:44:07 +0000 (23:44 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 10 Jun 2019 23:44:07 +0000 (23:44 +0000)
1  2 
tests/phpunit/includes/resourceloader/ResourceLoaderTest.php

@@@ -34,6 -34,42 +34,42 @@@ class ResourceLoaderTest extends Resour
                $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 ) );
+       }
        /**
         * @covers ResourceLoader::register
         * @covers ResourceLoader::getModule
@@@ -60,6 -96,7 +96,7 @@@
  
        /**
         * @covers ResourceLoader::register
+        * @group medium
         */
        public function testRegisterEmptyString() {
                $module = new ResourceLoaderTestModule();
  
        /**
         * @covers ResourceLoader::register
+        * @group medium
         */
        public function testRegisterInvalidName() {
                $resourceLoader = new EmptyResourceLoader();
                $resourceLoader->register( 'test.foo', new ResourceLoaderTestModule() );
                $resourceLoader->register( 'test.bar', new ResourceLoaderTestModule() );
                $this->assertEquals(
 -                      [ 'test.foo', 'test.bar' ],
 +                      [ 'startup', 'test.foo', 'test.bar' ],
                        $resourceLoader->getModuleNames()
                );
        }
         * @covers ResourceLoader::getSources
         */
        public function testAddSource( $name, $info, $expected ) {
 -              $rl = new ResourceLoader;
 +              $rl = new EmptyResourceLoader;
                $rl->addSource( $name, $info );
                if ( is_array( $expected ) ) {
                        foreach ( $expected as $source ) {
         * @covers ResourceLoader::addSource
         */
        public function testAddSourceDupe() {
 -              $rl = new ResourceLoader;
 +              $rl = new EmptyResourceLoader;
                $this->setExpectedException(
                        MWException::class, 'ResourceLoader duplicate source addition error'
                );
         * @covers ResourceLoader::addSource
         */
        public function testAddSourceInvalid() {
 -              $rl = new ResourceLoader;
 +              $rl = new EmptyResourceLoader;
                $this->setExpectedException( MWException::class, 'with no "loadScript" key' );
                $rl->addSource( 'foo',  [ 'x' => 'https://example.org/w/load.php' ] );
        }
@@@ -623,7 -661,7 +661,7 @@@ EN
         * @covers ResourceLoader::getLoadScript
         */
        public function testGetLoadScript() {
 -              $rl = new ResourceLoader();
 +              $rl = new EmptyResourceLoader();
                $sources = self::fakeSources();
                $rl->addSource( $sources );
                foreach ( [ 'examplewiki', 'example2wiki' ] as $name ) {
         * @covers ResourceLoader::makeModuleResponse
         */
        public function testMakeModuleResponseStartupError() {
 -              $rl = new EmptyResourceLoader();
 +              // This is an integration test that uses a lot of MediaWiki state,
 +              // provide the full Config object here.
 +              $rl = new EmptyResourceLoader( MediaWikiServices::getInstance()->getMainConfig() );
                $rl->register( [
                        'foo' => self::getSimpleModuleMock( 'foo();' ),
                        'ferry' => self::getFailFerryMock(),
                        'bar' => self::getSimpleModuleMock( 'bar();' ),
 -                      'startup' => [ 'class' => ResourceLoaderStartUpModule::class ],
                ] );
                $context = $this->getResourceLoaderContext(
                        [
                );
  
                $this->assertEquals(
 -                      [ 'foo', 'ferry', 'bar', 'startup' ],
 +                      [ 'startup', 'foo', 'ferry', 'bar' ],
                        $rl->getModuleNames(),
                        'getModuleNames'
                );