diff options
Diffstat (limited to 'www/wiki/tests/phpunit/suites')
7 files changed, 393 insertions, 0 deletions
diff --git a/www/wiki/tests/phpunit/suites/CoreParserTestSuite.php b/www/wiki/tests/phpunit/suites/CoreParserTestSuite.php new file mode 100644 index 00000000..7a33a222 --- /dev/null +++ b/www/wiki/tests/phpunit/suites/CoreParserTestSuite.php @@ -0,0 +1,9 @@ +<?php + +class CoreParserTestSuite extends PHPUnit_Framework_TestSuite { + + public static function suite() { + return ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::CORE_ONLY ); + } + +} diff --git a/www/wiki/tests/phpunit/suites/ExtensionsParserTestSuite.php b/www/wiki/tests/phpunit/suites/ExtensionsParserTestSuite.php new file mode 100644 index 00000000..8d6ee079 --- /dev/null +++ b/www/wiki/tests/phpunit/suites/ExtensionsParserTestSuite.php @@ -0,0 +1,8 @@ +<?php +class ExtensionsParserTestSuite extends PHPUnit_Framework_TestSuite { + + public static function suite() { + return ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::NO_CORE ); + } + +} diff --git a/www/wiki/tests/phpunit/suites/ExtensionsTestSuite.php b/www/wiki/tests/phpunit/suites/ExtensionsTestSuite.php new file mode 100644 index 00000000..02934fa7 --- /dev/null +++ b/www/wiki/tests/phpunit/suites/ExtensionsTestSuite.php @@ -0,0 +1,51 @@ +<?php +/** + * This test suite runs unit tests registered by extensions. + * See https://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList for details of + * how to register your tests. + */ + +class ExtensionsTestSuite extends PHPUnit_Framework_TestSuite { + public function __construct() { + parent::__construct(); + + $paths = []; + // Autodiscover extension unit tests + $registry = ExtensionRegistry::getInstance(); + foreach ( $registry->getAllThings() as $info ) { + $paths[] = dirname( $info['path'] ) . '/tests/phpunit'; + } + // Extensions can return a list of files or directories + Hooks::run( 'UnitTestsList', [ &$paths ] ); + foreach ( array_unique( $paths ) as $path ) { + if ( is_dir( $path ) ) { + // If the path is a directory, search for test cases. + // @since 1.24 + $suffixes = [ 'Test.php' ]; + $fileIterator = new File_Iterator_Facade(); + $matchingFiles = $fileIterator->getFilesAsArray( $path, $suffixes ); + $this->addTestFiles( $matchingFiles ); + } elseif ( file_exists( $path ) ) { + // Add a single test case or suite class + $this->addTestFile( $path ); + } + } + if ( !$paths ) { + $this->addTest( new DummyExtensionsTest( 'testNothing' ) ); + } + } + + public static function suite() { + return new self; + } +} + +/** + * Needed to avoid warnings like 'No tests found in class "ExtensionsTestSuite".' + * when no extensions with tests are used. + */ +class DummyExtensionsTest extends MediaWikiTestCase { + public function testNothing() { + $this->assertTrue( true ); + } +} diff --git a/www/wiki/tests/phpunit/suites/LessTestSuite.php b/www/wiki/tests/phpunit/suites/LessTestSuite.php new file mode 100644 index 00000000..26a784ad --- /dev/null +++ b/www/wiki/tests/phpunit/suites/LessTestSuite.php @@ -0,0 +1,34 @@ +<?php + +/** + * @author Sam Smith <samsmith@wikimedia.org> + */ +class LessTestSuite extends PHPUnit_Framework_TestSuite { + public function __construct() { + parent::__construct(); + + $resourceLoader = new ResourceLoader(); + + foreach ( $resourceLoader->getModuleNames() as $name ) { + $module = $resourceLoader->getModule( $name ); + if ( !$module || !$module instanceof ResourceLoaderFileModule ) { + continue; + } + + foreach ( $module->getAllStyleFiles() as $styleFile ) { + // TODO (phuedx, 2014-03-19) The + // ResourceLoaderFileModule class shouldn't + // know how to get a file's extension. + if ( $module->getStyleSheetLang( $styleFile ) !== 'less' ) { + continue; + } + + $this->addTest( new LessFileCompilationTest( $styleFile, $module ) ); + } + } + } + + public static function suite() { + return new static; + } +} diff --git a/www/wiki/tests/phpunit/suites/ParserTestFileSuite.php b/www/wiki/tests/phpunit/suites/ParserTestFileSuite.php new file mode 100644 index 00000000..b72d8b84 --- /dev/null +++ b/www/wiki/tests/phpunit/suites/ParserTestFileSuite.php @@ -0,0 +1,32 @@ +<?php + +/** + * This is the suite class for running tests within a single .txt source file. + * It is not invoked directly. Use --filter to select files, or + * use parserTests.php. + */ +class ParserTestFileSuite extends PHPUnit_Framework_TestSuite { + private $ptRunner; + private $ptFileName; + private $ptFileInfo; + + function __construct( $runner, $name, $fileName ) { + parent::__construct( $name ); + $this->ptRunner = $runner; + $this->ptFileName = $fileName; + $this->ptFileInfo = TestFileReader::read( $this->ptFileName ); + + foreach ( $this->ptFileInfo['tests'] as $test ) { + $this->addTest( new ParserIntegrationTest( $runner, $fileName, $test ), + [ 'Database', 'Parser', 'ParserTests' ] ); + } + } + + function setUp() { + if ( !$this->ptRunner->meetsRequirements( $this->ptFileInfo['requirements'] ) ) { + $this->markTestSuiteSkipped( 'required extension not enabled' ); + } else { + $this->ptRunner->addArticles( $this->ptFileInfo[ 'articles'] ); + } + } +} diff --git a/www/wiki/tests/phpunit/suites/ParserTestTopLevelSuite.php b/www/wiki/tests/phpunit/suites/ParserTestTopLevelSuite.php new file mode 100644 index 00000000..07b18f59 --- /dev/null +++ b/www/wiki/tests/phpunit/suites/ParserTestTopLevelSuite.php @@ -0,0 +1,160 @@ +<?php +use Wikimedia\ScopedCallback; + +/** + * The UnitTest must be either a class that inherits from MediaWikiTestCase + * or a class that provides a public static suite() method which returns + * an PHPUnit_Framework_Test object + * + * @group Parser + * @group ParserTests + * @group Database + */ +class ParserTestTopLevelSuite extends PHPUnit_Framework_TestSuite { + /** @var ParserTestRunner */ + private $ptRunner; + + /** @var ScopedCallback */ + private $ptTeardownScope; + + /** + * @defgroup filtering_constants Filtering constants + * + * Limit inclusion of parser tests files coming from MediaWiki core + * @{ + */ + + /** Include files shipped with MediaWiki core */ + const CORE_ONLY = 1; + /** Include non core files as set in $wgParserTestFiles */ + const NO_CORE = 2; + /** Include anything set via $wgParserTestFiles */ + const WITH_ALL = 3; # CORE_ONLY | NO_CORE + + /** @} */ + + /** + * Get a PHPUnit test suite of parser tests. Optionally filtered with + * $flags. + * + * @par Examples: + * Get a suite of parser tests shipped by MediaWiki core: + * @code + * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::CORE_ONLY ); + * @endcode + * Get a suite of various parser tests, like extensions: + * @code + * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::NO_CORE ); + * @endcode + * Get any test defined via $wgParserTestFiles: + * @code + * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::WITH_ALL ); + * @endcode + * + * @param int $flags Bitwise flag to filter out the $wgParserTestFiles that + * will be included. Default: ParserTestTopLevelSuite::CORE_ONLY + * + * @return PHPUnit_Framework_TestSuite + */ + public static function suite( $flags = self::CORE_ONLY ) { + return new self( $flags ); + } + + function __construct( $flags ) { + parent::__construct(); + + $this->ptRecorder = new PhpunitTestRecorder; + $this->ptRunner = new ParserTestRunner( $this->ptRecorder ); + + if ( is_string( $flags ) ) { + $flags = self::CORE_ONLY; + } + global $IP; + + $mwTestDir = $IP . '/tests/'; + + # Human friendly helpers + $wantsCore = ( $flags & self::CORE_ONLY ); + $wantsRest = ( $flags & self::NO_CORE ); + + # Will hold the .txt parser test files we will include + $filesToTest = []; + + # Filter out .txt files + $files = ParserTestRunner::getParserTestFiles(); + foreach ( $files as $extName => $parserTestFile ) { + $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) ); + + if ( $isCore && $wantsCore ) { + self::debug( "included core parser tests: $parserTestFile" ); + $filesToTest[$extName] = $parserTestFile; + } elseif ( !$isCore && $wantsRest ) { + self::debug( "included non core parser tests: $parserTestFile" ); + $filesToTest[$extName] = $parserTestFile; + } else { + self::debug( "skipped parser tests: $parserTestFile" ); + } + } + self::debug( 'parser tests files: ' + . implode( ' ', $filesToTest ) ); + + $testList = []; + $counter = 0; + foreach ( $filesToTest as $extensionName => $fileName ) { + if ( is_int( $extensionName ) ) { + // If there's no extension name because this is coming + // from the legacy global, then assume the next level directory + // is the extension name (e.g. extensions/FooBar/parserTests.txt). + $extensionName = basename( dirname( $fileName ) ); + } + $testsName = $extensionName . '__' . basename( $fileName, '.txt' ); + $parserTestClassName = ucfirst( $testsName ); + + // Official spec for class names: https://secure.php.net/manual/en/language.oop5.basic.php + // Prepend 'ParserTest_' to be paranoid about it not starting with a number + $parserTestClassName = 'ParserTest_' . + preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName ); + + if ( isset( $testList[$parserTestClassName] ) ) { + // If there is a conflict, append a number. + $counter++; + $parserTestClassName .= $counter; + } + $testList[$parserTestClassName] = true; + + // Previously we actually created a class here, with eval(). We now + // just override the name. + + self::debug( "Adding test class $parserTestClassName" ); + $this->addTest( new ParserTestFileSuite( + $this->ptRunner, $parserTestClassName, $fileName ) ); + } + } + + public function setUp() { + wfDebug( __METHOD__ ); + $db = wfGetDB( DB_MASTER ); + $type = $db->getType(); + $prefix = $type === 'oracle' ? + MediaWikiTestCase::ORA_DB_PREFIX : MediaWikiTestCase::DB_PREFIX; + MediaWikiTestCase::setupTestDB( $db, $prefix ); + $teardown = $this->ptRunner->setDatabase( $db ); + $teardown = $this->ptRunner->setupUploads( $teardown ); + $this->ptTeardownScope = $teardown; + } + + public function tearDown() { + wfDebug( __METHOD__ ); + if ( $this->ptTeardownScope ) { + ScopedCallback::consume( $this->ptTeardownScope ); + } + } + + /** + * Write $msg under log group 'tests-parser' + * @param string $msg Message to log + */ + protected static function debug( $msg ) { + wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg ); + } +} diff --git a/www/wiki/tests/phpunit/suites/UploadFromUrlTestSuite.php b/www/wiki/tests/phpunit/suites/UploadFromUrlTestSuite.php new file mode 100644 index 00000000..556c7541 --- /dev/null +++ b/www/wiki/tests/phpunit/suites/UploadFromUrlTestSuite.php @@ -0,0 +1,99 @@ +<?php + +require_once dirname( __DIR__ ) . '/includes/upload/UploadFromUrlTest.php'; + +class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite { + public $savedGlobals = []; + + public static function addTables( &$tables ) { + $tables[] = 'user_properties'; + $tables[] = 'filearchive'; + $tables[] = 'logging'; + $tables[] = 'updatelog'; + $tables[] = 'iwlinks'; + + return true; + } + + protected function setUp() { + global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgUser, + $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, + $wgParserCacheType, $wgNamespaceAliases, $wgNamespaceProtection; + + $tmpDir = $this->getNewTempDirectory(); + $tmpGlobals = []; + + $tmpGlobals['wgScript'] = '/index.php'; + $tmpGlobals['wgScriptPath'] = '/'; + $tmpGlobals['wgArticlePath'] = '/wiki/$1'; + $tmpGlobals['wgStylePath'] = '/skins'; + $tmpGlobals['wgThumbnailScriptPath'] = false; + $tmpGlobals['wgLocalFileRepo'] = [ + 'class' => LocalRepo::class, + 'name' => 'local', + 'url' => 'http://example.com/images', + 'hashLevels' => 2, + 'transformVia404' => false, + 'backend' => new FSFileBackend( [ + 'name' => 'local-backend', + 'wikiId' => wfWikiID(), + 'containerPaths' => [ + 'local-public' => "{$tmpDir}/test-repo/public", + 'local-thumb' => "{$tmpDir}/test-repo/thumb", + 'local-temp' => "{$tmpDir}/test-repo/temp", + 'local-deleted' => "{$tmpDir}/test-repo/delete", + ] + ] ), + ]; + foreach ( $tmpGlobals as $var => $val ) { + if ( array_key_exists( $var, $GLOBALS ) ) { + $this->savedGlobals[$var] = $GLOBALS[$var]; + } + $GLOBALS[$var] = $val; + } + + $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface'; + $wgNamespaceAliases['Image'] = NS_FILE; + $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK; + + $wgParserCacheType = CACHE_NONE; + DeferredUpdates::clearPendingUpdates(); + $wgMemc = wfGetMainCache(); + $messageMemc = wfGetMessageCacheStorage(); + + RequestContext::resetMain(); + $context = RequestContext::getMain(); + $wgUser = new User; + $wgLang = $context->getLanguage(); + $wgOut = $context->getOutput(); + $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], [ $wgParserConf ] ); + $wgRequest = $context->getRequest(); + + if ( $wgStyleDirectory === false ) { + $wgStyleDirectory = "$IP/skins"; + } + + RepoGroup::destroySingleton(); + FileBackendGroup::destroySingleton(); + } + + protected function tearDown() { + foreach ( $this->savedGlobals as $var => $val ) { + $GLOBALS[$var] = $val; + } + // Restore backends + RepoGroup::destroySingleton(); + FileBackendGroup::destroySingleton(); + + parent::tearDown(); + } + + public static function suite() { + // Hack to invoke the autoloader required to get phpunit to recognize + // the UploadFromUrlTest class + class_exists( 'UploadFromUrlTest' ); + $suite = new UploadFromUrlTestSuite( 'UploadFromUrlTest' ); + + return $suite; + } +} |