From d23af35764bc7a5f223852d25baf554f1ae8f944 Mon Sep 17 00:00:00 2001 From: Amir Sarabadani Date: Mon, 8 Jul 2019 15:25:31 +0200 Subject: [PATCH] Unset all globals unneeded for unit tests, assert correct directory * Unset globals to avoid tests that look like unit tests but actually rely on globals * move some tests out of unit directory so that the test suite will pass. * Assert that tests which extend MediaWikiUnitTestCase are in a directory with "/unit/" in its path name Depends-On: I67b37b1bde94eaa3d4298d9bd98ac57995ce93b9 Depends-On: I90921679518ee95fe393f8b1bbd9134daf0ba032 Bug: T87781 Change-Id: I16691fc8ac063705ba0c2bc63b96c4534ca8660b --- tests/phpunit/MediaWikiUnitTestCase.php | 22 ++++++++++++++++ .../includes/MediaWikiVersionFetcherTest.php | 2 +- .../Revision/RevisionStoreFactoryTest.php | 2 +- .../includes/config/ConfigFactoryTest.php | 2 +- .../media/SVGMetadataExtractorTest.php | 6 ++--- .../phpunit/includes/parser/SanitizerTest.php | 25 +++++++++++++++++++ .../includes/site/CachingSiteStoreTest.php | 2 +- .../includes/site/HashSiteStoreTest.php | 2 +- .../includes/parser/SanitizerUnitTest.php | 24 ------------------ .../languages/SpecialPageAliasTest.php | 0 10 files changed, 55 insertions(+), 32 deletions(-) rename tests/phpunit/{unit => }/includes/MediaWikiVersionFetcherTest.php (87%) rename tests/phpunit/{unit => }/includes/Revision/RevisionStoreFactoryTest.php (98%) rename tests/phpunit/{unit => }/includes/config/ConfigFactoryTest.php (98%) rename tests/phpunit/{unit => }/includes/media/SVGMetadataExtractorTest.php (96%) rename tests/phpunit/{unit => }/includes/site/CachingSiteStoreTest.php (98%) rename tests/phpunit/{unit => }/includes/site/HashSiteStoreTest.php (97%) rename tests/phpunit/{ => unit}/languages/SpecialPageAliasTest.php (100%) diff --git a/tests/phpunit/MediaWikiUnitTestCase.php b/tests/phpunit/MediaWikiUnitTestCase.php index 1065c2f26a..3f0fc7a55d 100644 --- a/tests/phpunit/MediaWikiUnitTestCase.php +++ b/tests/phpunit/MediaWikiUnitTestCase.php @@ -31,4 +31,26 @@ abstract class MediaWikiUnitTestCase extends TestCase { use PHPUnit4And6Compat; use MediaWikiCoversValidator; + private $unitGlobals = []; + + protected function setUp() { + parent::setUp(); + $reflection = new ReflectionClass( $this ); + if ( strpos( $reflection->getFilename(), '/unit/' ) === false ) { + $this->fail( 'This unit test needs to be in "tests/phpunit/unit" !' ); + } + $this->unitGlobals = $GLOBALS; + unset( $GLOBALS ); + $GLOBALS = []; + // Add back the minimal set of globals needed for unit tests to run for core + + // extensions/skins. + foreach ( [ 'wgAutoloadClasses', 'wgAutoloadLocalClasses', 'IP' ] as $requiredGlobal ) { + $GLOBALS[$requiredGlobal] = $this->unitGlobals[ $requiredGlobal ]; + } + } + + protected function tearDown() { + $GLOBALS = $this->unitGlobals; + parent::tearDown(); + } } diff --git a/tests/phpunit/unit/includes/MediaWikiVersionFetcherTest.php b/tests/phpunit/includes/MediaWikiVersionFetcherTest.php similarity index 87% rename from tests/phpunit/unit/includes/MediaWikiVersionFetcherTest.php rename to tests/phpunit/includes/MediaWikiVersionFetcherTest.php index dfdbfa7344..8f0a44dc05 100644 --- a/tests/phpunit/unit/includes/MediaWikiVersionFetcherTest.php +++ b/tests/phpunit/includes/MediaWikiVersionFetcherTest.php @@ -10,7 +10,7 @@ * * @author Jeroen De Dauw < jeroendedauw@gmail.com > */ -class MediaWikiVersionFetcherTest extends \MediaWikiUnitTestCase { +class MediaWikiVersionFetcherTest extends \MediaWikiIntegrationTestCase { public function testReturnsResult() { global $wgVersion; diff --git a/tests/phpunit/unit/includes/Revision/RevisionStoreFactoryTest.php b/tests/phpunit/includes/Revision/RevisionStoreFactoryTest.php similarity index 98% rename from tests/phpunit/unit/includes/Revision/RevisionStoreFactoryTest.php rename to tests/phpunit/includes/Revision/RevisionStoreFactoryTest.php index 8e8fbd760f..f4d324dde7 100644 --- a/tests/phpunit/unit/includes/Revision/RevisionStoreFactoryTest.php +++ b/tests/phpunit/includes/Revision/RevisionStoreFactoryTest.php @@ -20,7 +20,7 @@ use Wikimedia\Rdbms\ILBFactory; use Wikimedia\Rdbms\ILoadBalancer; use Wikimedia\TestingAccessWrapper; -class RevisionStoreFactoryTest extends \MediaWikiUnitTestCase { +class RevisionStoreFactoryTest extends \MediaWikiIntegrationTestCase { /** * @covers \MediaWiki\Revision\RevisionStoreFactory::__construct diff --git a/tests/phpunit/unit/includes/config/ConfigFactoryTest.php b/tests/phpunit/includes/config/ConfigFactoryTest.php similarity index 98% rename from tests/phpunit/unit/includes/config/ConfigFactoryTest.php rename to tests/phpunit/includes/config/ConfigFactoryTest.php index a136018c96..f1cc8573b2 100644 --- a/tests/phpunit/unit/includes/config/ConfigFactoryTest.php +++ b/tests/phpunit/includes/config/ConfigFactoryTest.php @@ -2,7 +2,7 @@ use MediaWiki\MediaWikiServices; -class ConfigFactoryTest extends \MediaWikiUnitTestCase { +class ConfigFactoryTest extends \MediaWikiIntegrationTestCase { /** * @covers ConfigFactory::register diff --git a/tests/phpunit/unit/includes/media/SVGMetadataExtractorTest.php b/tests/phpunit/includes/media/SVGMetadataExtractorTest.php similarity index 96% rename from tests/phpunit/unit/includes/media/SVGMetadataExtractorTest.php rename to tests/phpunit/includes/media/SVGMetadataExtractorTest.php index 30d10083f4..c84efa1640 100644 --- a/tests/phpunit/unit/includes/media/SVGMetadataExtractorTest.php +++ b/tests/phpunit/includes/media/SVGMetadataExtractorTest.php @@ -4,7 +4,7 @@ * @group Media * @covers SVGMetadataExtractor */ -class SVGMetadataExtractorTest extends \MediaWikiUnitTestCase { +class SVGMetadataExtractorTest extends \MediaWikiIntegrationTestCase { /** * @dataProvider provideSvgFiles @@ -46,7 +46,7 @@ class SVGMetadataExtractorTest extends \MediaWikiUnitTestCase { } public static function provideSvgFiles() { - $base = __DIR__ . '/../../../data/media'; + $base = __DIR__ . '/../../data/media'; return [ [ @@ -143,7 +143,7 @@ class SVGMetadataExtractorTest extends \MediaWikiUnitTestCase { } public static function provideSvgFilesWithXMLMetadata() { - $base = __DIR__ . '/../../../data/media'; + $base = __DIR__ . '/../../data/media'; // phpcs:disable Generic.Files.LineLength $metadata = ' diff --git a/tests/phpunit/includes/parser/SanitizerTest.php b/tests/phpunit/includes/parser/SanitizerTest.php index e665d672f8..99e8fb7ebd 100644 --- a/tests/phpunit/includes/parser/SanitizerTest.php +++ b/tests/phpunit/includes/parser/SanitizerTest.php @@ -271,4 +271,29 @@ class SanitizerTest extends MediaWikiTestCase { $this->setMwGlobals( 'wgFragmentMode', [ 666 => 'html5' ] ); Sanitizer::escapeIdForLink( 'This should throw' ); } + + /** + * Test escapeIdReferenceList for consistency with escapeIdForAttribute + * + * @dataProvider provideEscapeIdReferenceList + * @covers Sanitizer::escapeIdReferenceList + */ + public function testEscapeIdReferenceList( $referenceList, $id1, $id2 ) { + $this->assertEquals( + Sanitizer::escapeIdReferenceList( $referenceList ), + Sanitizer::escapeIdForAttribute( $id1 ) + . ' ' + . Sanitizer::escapeIdForAttribute( $id2 ) + ); + } + + public static function provideEscapeIdReferenceList() { + /** [ , , ] */ + return [ + [ 'foo bar', 'foo', 'bar' ], + [ '#1 #2', '#1', '#2' ], + [ '+1 +2', '+1', '+2' ], + ]; + } + } diff --git a/tests/phpunit/unit/includes/site/CachingSiteStoreTest.php b/tests/phpunit/includes/site/CachingSiteStoreTest.php similarity index 98% rename from tests/phpunit/unit/includes/site/CachingSiteStoreTest.php rename to tests/phpunit/includes/site/CachingSiteStoreTest.php index 92ed1f53e8..df12eba765 100644 --- a/tests/phpunit/unit/includes/site/CachingSiteStoreTest.php +++ b/tests/phpunit/includes/site/CachingSiteStoreTest.php @@ -27,7 +27,7 @@ * * @author Jeroen De Dauw < jeroendedauw@gmail.com > */ -class CachingSiteStoreTest extends \MediaWikiUnitTestCase { +class CachingSiteStoreTest extends \MediaWikiIntegrationTestCase { /** * @covers CachingSiteStore::getSites diff --git a/tests/phpunit/unit/includes/site/HashSiteStoreTest.php b/tests/phpunit/includes/site/HashSiteStoreTest.php similarity index 97% rename from tests/phpunit/unit/includes/site/HashSiteStoreTest.php rename to tests/phpunit/includes/site/HashSiteStoreTest.php index 8b0d4e080b..3912504038 100644 --- a/tests/phpunit/unit/includes/site/HashSiteStoreTest.php +++ b/tests/phpunit/includes/site/HashSiteStoreTest.php @@ -24,7 +24,7 @@ * * @author Katie Filbert < aude.wiki@gmail.com > */ -class HashSiteStoreTest extends \MediaWikiUnitTestCase { +class HashSiteStoreTest extends \MediaWikiIntegrationTestCase { /** * @covers HashSiteStore::getSites diff --git a/tests/phpunit/unit/includes/parser/SanitizerUnitTest.php b/tests/phpunit/unit/includes/parser/SanitizerUnitTest.php index 71e4fffb4d..0436e19205 100644 --- a/tests/phpunit/unit/includes/parser/SanitizerUnitTest.php +++ b/tests/phpunit/unit/includes/parser/SanitizerUnitTest.php @@ -274,30 +274,6 @@ class SanitizerUnitTest extends MediaWikiUnitTestCase { ]; } - /** - * Test escapeIdReferenceList for consistency with escapeIdForAttribute - * - * @dataProvider provideEscapeIdReferenceList - * @covers Sanitizer::escapeIdReferenceList - */ - public function testEscapeIdReferenceList( $referenceList, $id1, $id2 ) { - $this->assertEquals( - Sanitizer::escapeIdReferenceList( $referenceList ), - Sanitizer::escapeIdForAttribute( $id1 ) - . ' ' - . Sanitizer::escapeIdForAttribute( $id2 ) - ); - } - - public static function provideEscapeIdReferenceList() { - /** [ , , ] */ - return [ - [ 'foo bar', 'foo', 'bar' ], - [ '#1 #2', '#1', '#2' ], - [ '+1 +2', '+1', '+2' ], - ]; - } - /** * @dataProvider provideIsReservedDataAttribute * @covers Sanitizer::isReservedDataAttribute diff --git a/tests/phpunit/languages/SpecialPageAliasTest.php b/tests/phpunit/unit/languages/SpecialPageAliasTest.php similarity index 100% rename from tests/phpunit/languages/SpecialPageAliasTest.php rename to tests/phpunit/unit/languages/SpecialPageAliasTest.php -- 2.20.1