-class ParserIntegrationTest extends MediaWikiTestCase {
- static protected $articles = []; // Array of test articles defined by the tests
- /* The data provider is run on a different instance than the test, so it must be static
- * When running tests from several files, all tests will see all articles.
- */
- static protected $backendToUse;
-
- public $keepUploads = false;
- public $runDisabled = false;
- public $runParsoid = false;
- public $regex = '';
- public $showProgress = true;
- public $savedWeirdGlobals = [];
- public $savedGlobals = [];
- public $hooks = [];
- public $functionHooks = [];
- public $transparentHooks = [];
-
- /**
- * @var DjVuSupport
- */
- private $djVuSupport;
- /**
- * @var TidySupport
- */
- private $tidySupport;
-
- protected $file = false;
-
- public static function setUpBeforeClass() {
- // Inject ParserTestRunner well-known interwikis
- ParserTestRunner::setupInterwikis();
- }
-
- protected function setUp() {
- global $wgNamespaceAliases, $wgContLang;
- global $wgHooks, $IP;
-
- parent::setUp();
-
- // Setup CLI arguments
- if ( $this->getCliArg( 'regex' ) ) {
- $this->regex = $this->getCliArg( 'regex' );
- } else {
- # Matches anything
- $this->regex = '';
- }
-
- $this->keepUploads = $this->getCliArg( 'keep-uploads' );
-
- $tmpGlobals = [];
-
- $tmpGlobals['wgLanguageCode'] = 'en';
- $tmpGlobals['wgContLang'] = Language::factory( 'en' );
- $tmpGlobals['wgSitename'] = 'MediaWiki';
- $tmpGlobals['wgServer'] = 'http://example.org';
- $tmpGlobals['wgServerName'] = 'example.org';
- $tmpGlobals['wgScriptPath'] = '';
- $tmpGlobals['wgScript'] = '/index.php';
- $tmpGlobals['wgResourceBasePath'] = '';
- $tmpGlobals['wgStylePath'] = '/skins';
- $tmpGlobals['wgExtensionAssetsPath'] = '/extensions';
- $tmpGlobals['wgArticlePath'] = '/wiki/$1';
- $tmpGlobals['wgActionPaths'] = [];
- $tmpGlobals['wgVariantArticlePath'] = false;
- $tmpGlobals['wgEnableUploads'] = true;
- $tmpGlobals['wgUploadNavigationUrl'] = false;
- $tmpGlobals['wgThumbnailScriptPath'] = false;
- $tmpGlobals['wgLocalFileRepo'] = [
- 'class' => 'LocalRepo',
- 'name' => 'local',
- 'url' => 'http://example.com/images',
- 'hashLevels' => 2,
- 'transformVia404' => false,
- 'backend' => 'local-backend'
- ];
- $tmpGlobals['wgForeignFileRepos'] = [];
- $tmpGlobals['wgDefaultExternalStore'] = [];
- $tmpGlobals['wgParserCacheType'] = CACHE_NONE;
- $tmpGlobals['wgCapitalLinks'] = true;
- $tmpGlobals['wgNoFollowLinks'] = true;
- $tmpGlobals['wgNoFollowDomainExceptions'] = [ 'no-nofollow.org' ];
- $tmpGlobals['wgExternalLinkTarget'] = false;
- $tmpGlobals['wgThumbnailScriptPath'] = false;
- $tmpGlobals['wgUseImageResize'] = true;
- $tmpGlobals['wgAllowExternalImages'] = true;
- $tmpGlobals['wgRawHtml'] = false;
- $tmpGlobals['wgExperimentalHtmlIds'] = false;
- $tmpGlobals['wgAdaptiveMessageCache'] = true;
- $tmpGlobals['wgUseDatabaseMessages'] = true;
- $tmpGlobals['wgLocaltimezone'] = 'UTC';
- $tmpGlobals['wgGroupPermissions'] = [
- '*' => [
- 'createaccount' => true,
- 'read' => true,
- 'edit' => true,
- 'createpage' => true,
- 'createtalk' => true,
- ] ];
- $tmpGlobals['wgNamespaceProtection'] = [ NS_MEDIAWIKI => 'editinterface' ];
-
- $tmpGlobals['wgParser'] = new StubObject(
- 'wgParser', $GLOBALS['wgParserConf']['class'],
- [ $GLOBALS['wgParserConf'] ] );
-
- $tmpGlobals['wgFileExtensions'][] = 'svg';
- $tmpGlobals['wgSVGConverter'] = 'rsvg';
- $tmpGlobals['wgSVGConverters']['rsvg'] =
- '$path/rsvg-convert -w $width -h $height -o $output $input';
-
- if ( $GLOBALS['wgStyleDirectory'] === false ) {
- $tmpGlobals['wgStyleDirectory'] = "$IP/skins";
- }
-
- $tmpHooks = $wgHooks;
- $tmpHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
- $tmpHooks['ParserGetVariableValueTs'][] = 'ParserTestRunner::getFakeTimestamp';
- $tmpGlobals['wgHooks'] = $tmpHooks;
- # add a namespace shadowing a interwiki link, to test
- # proper precedence when resolving links. (bug 51680)
- $tmpGlobals['wgExtraNamespaces'] = [
- 100 => 'MemoryAlpha',
- 101 => 'MemoryAlpha_talk'
- ];
-
- $tmpGlobals['wgLocalInterwikis'] = [ 'local', 'mi' ];
- # "extra language links"
- # see https://gerrit.wikimedia.org/r/111390
- $tmpGlobals['wgExtraInterlanguageLinkPrefixes'] = [ 'mul' ];
-
- // DjVu support
- $this->djVuSupport = new DjVuSupport();
- // Tidy support
- $this->tidySupport = new TidySupport();
- $tmpGlobals['wgTidyConfig'] = $this->tidySupport->getConfig();
- $tmpGlobals['wgUseTidy'] = false;
-
- $this->setMwGlobals( $tmpGlobals );
-
- $this->savedWeirdGlobals['image_alias'] = $wgNamespaceAliases['Image'];
- $this->savedWeirdGlobals['image_talk_alias'] = $wgNamespaceAliases['Image_talk'];
-
- $wgNamespaceAliases['Image'] = NS_FILE;
- $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
-
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
- $wgContLang->resetNamespaces(); # reset namespace cache
- ParserTestRunner::resetTitleServices();
- MediaWikiServices::getInstance()->disableService( 'MediaHandlerFactory' );
- MediaWikiServices::getInstance()->redefineService(
- 'MediaHandlerFactory',
- function() {
- return new MockMediaHandlerFactory();
- }
- );
- }
-
- protected function tearDown() {
- global $wgNamespaceAliases, $wgContLang;
-
- $wgNamespaceAliases['Image'] = $this->savedWeirdGlobals['image_alias'];
- $wgNamespaceAliases['Image_talk'] = $this->savedWeirdGlobals['image_talk_alias'];
-
- MWTidy::destroySingleton();
-
- // Restore backends
- RepoGroup::destroySingleton();
- FileBackendGroup::destroySingleton();
-
- // Remove temporary pages from the link cache
- LinkCache::singleton()->clear();
-
- // Restore message cache (temporary pages and $wgUseDatabaseMessages)
- MessageCache::destroyInstance();
- MediaWikiServices::getInstance()->resetServiceForTesting( 'MediaHandlerFactory' );
-
- parent::tearDown();
-
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
- $wgContLang->resetNamespaces(); # reset namespace cache
- }
-
- public static function tearDownAfterClass() {
- ParserTestRunner::tearDownInterwikis();
- parent::tearDownAfterClass();
- }
-
- function addDBDataOnce() {
- # disabled for performance
- # $this->tablesUsed[] = 'image';
-
- # Update certain things in site_stats
- $this->db->insert( 'site_stats',
- [ 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ],
- __METHOD__,
- [ 'IGNORE' ]
- );
-
- $user = User::newFromId( 0 );
- LinkCache::singleton()->clear(); # Avoids the odd failure at creating the nullRevision
-
- # Upload DB table entries for files.
- # We will upload the actual files later. Note that if anything causes LocalFile::load()
- # to be triggered before then, it will break via maybeUpgrade() setting the fileExists
- # member to false and storing it in cache.
- # note that the size/width/height/bits/etc of the file
- # are actually set by inspecting the file itself; the arguments
- # to recordUpload2 have no effect. That said, we try to make things
- # match up so it is less confusing to readers of the code & tests.
- $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
- if ( !$this->db->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
- $image->recordUpload2(
- '', // archive name
- 'Upload of some lame file',
- 'Some lame file',
- [
- 'size' => 7881,
- 'width' => 1941,
- 'height' => 220,
- 'bits' => 8,
- 'media_type' => MEDIATYPE_BITMAP,
- 'mime' => 'image/jpeg',
- 'metadata' => serialize( [] ),
- 'sha1' => Wikimedia\base_convert( '1', 16, 36, 31 ),
- 'fileExists' => true ],
- $this->db->timestamp( '20010115123500' ), $user
- );
- }
-
- $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Thumb.png' ) );
- if ( !$this->db->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
- $image->recordUpload2(
- '', // archive name
- 'Upload of some lame thumbnail',
- 'Some lame thumbnail',
- [
- 'size' => 22589,
- 'width' => 135,
- 'height' => 135,
- 'bits' => 8,
- 'media_type' => MEDIATYPE_BITMAP,
- 'mime' => 'image/png',
- 'metadata' => serialize( [] ),
- 'sha1' => Wikimedia\base_convert( '2', 16, 36, 31 ),
- 'fileExists' => true ],
- $this->db->timestamp( '20130225203040' ), $user
- );
- }
-
- # This image will be blacklisted in [[MediaWiki:Bad image list]]
- $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
- if ( !$this->db->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
- $image->recordUpload2(
- '', // archive name
- 'zomgnotcensored',
- 'Borderline image',
- [
- 'size' => 12345,
- 'width' => 320,
- 'height' => 240,
- 'bits' => 24,
- 'media_type' => MEDIATYPE_BITMAP,
- 'mime' => 'image/jpeg',
- 'metadata' => serialize( [] ),
- 'sha1' => Wikimedia\base_convert( '3', 16, 36, 31 ),
- 'fileExists' => true ],
- $this->db->timestamp( '20010115123500' ), $user
- );
- }
- $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.svg' ) );
- if ( !$this->db->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
- $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', [
- 'size' => 12345,
- 'width' => 240,
- 'height' => 180,
- 'bits' => 0,
- 'media_type' => MEDIATYPE_DRAWING,
- 'mime' => 'image/svg+xml',
- 'metadata' => serialize( [] ),
- 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
- 'fileExists' => true
- ], $this->db->timestamp( '20010115123500' ), $user );
- }
-
- $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Video.ogv' ) );
- if ( !$this->db->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
- $image->recordUpload2( '', 'A pretty movie', 'Will it play', [
- 'size' => 12345,
- 'width' => 320,
- 'height' => 240,
- 'bits' => 0,
- 'media_type' => MEDIATYPE_VIDEO,
- 'mime' => 'application/ogg',
- 'metadata' => serialize( [] ),
- 'sha1' => Wikimedia\base_convert( '', 16, 36, 32 ),
- 'fileExists' => true
- ], $this->db->timestamp( '20010115123500' ), $user );
- }
-
- $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Audio.oga' ) );
- if ( !$this->db->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
- $image->recordUpload2( '', 'An awesome hitsong ', 'Will it play', [
- 'size' => 12345,
- 'width' => 0,
- 'height' => 0,
- 'bits' => 0,
- 'media_type' => MEDIATYPE_AUDIO,
- 'mime' => 'application/ogg',
- 'metadata' => serialize( [] ),
- 'sha1' => Wikimedia\base_convert( '', 16, 36, 32 ),
- 'fileExists' => true
- ], $this->db->timestamp( '20010115123500' ), $user );
- }
-
- # A DjVu file
- $image = wfLocalFile( Title::makeTitle( NS_FILE, 'LoremIpsum.djvu' ) );
- if ( !$this->db->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
- $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', [
- 'size' => 3249,
- 'width' => 2480,
- 'height' => 3508,
- 'bits' => 0,
- 'media_type' => MEDIATYPE_BITMAP,
- 'mime' => 'image/vnd.djvu',
- 'metadata' => '<?xml version="1.0" ?>
-<!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
-<DjVuXML>
-<HEAD></HEAD>
-<BODY><OBJECT height="3508" width="2480">
-<PARAM name="DPI" value="300" />
-<PARAM name="GAMMA" value="2.2" />
-</OBJECT>
-<OBJECT height="3508" width="2480">
-<PARAM name="DPI" value="300" />
-<PARAM name="GAMMA" value="2.2" />
-</OBJECT>
-<OBJECT height="3508" width="2480">
-<PARAM name="DPI" value="300" />
-<PARAM name="GAMMA" value="2.2" />
-</OBJECT>
-<OBJECT height="3508" width="2480">
-<PARAM name="DPI" value="300" />
-<PARAM name="GAMMA" value="2.2" />
-</OBJECT>
-<OBJECT height="3508" width="2480">
-<PARAM name="DPI" value="300" />
-<PARAM name="GAMMA" value="2.2" />
-</OBJECT>
-</BODY>
-</DjVuXML>',
- 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
- 'fileExists' => true
- ], $this->db->timestamp( '20140115123600' ), $user );
- }
- }
-
- // ParserTestRunner setup/teardown functions
-
- /**
- * Set up the global variables for a consistent environment for each test.
- * Ideally this should replace the global configuration entirely.
- * @param array $opts
- * @param string $config
- * @return RequestContext
- */
- protected function setupGlobals( $opts = [], $config = '' ) {
- global $wgFileBackends;
- # Find out values for some special options.
- $lang =
- self::getOptionValue( 'language', $opts, 'en' );
- $variant =
- self::getOptionValue( 'variant', $opts, false );
- $maxtoclevel =
- self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
- $linkHolderBatchSize =
- self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
-
- $uploadDir = $this->getUploadDir();
- if ( $this->getCliArg( 'use-filebackend' ) ) {
- if ( self::$backendToUse ) {
- $backend = self::$backendToUse;
- } else {
- $name = $this->getCliArg( 'use-filebackend' );
- $useConfig = [];
- foreach ( $wgFileBackends as $conf ) {
- if ( $conf['name'] == $name ) {
- $useConfig = $conf;
- }
- }
- $useConfig['name'] = 'local-backend'; // swap name
- unset( $useConfig['lockManager'] );
- unset( $useConfig['fileJournal'] );
- $class = $useConfig['class'];
- self::$backendToUse = new $class( $useConfig );
- $backend = self::$backendToUse;
- }
- } else {
- # Replace with a mock. We do not care about generating real
- # files on the filesystem, just need to expose the file
- # informations.
- $backend = new MockFileBackend( [
- 'name' => 'local-backend',
- 'wikiId' => wfWikiID()
- ] );
- }
-
- $settings = [
- 'wgLocalFileRepo' => [
- 'class' => 'LocalRepo',
- 'name' => 'local',
- 'url' => 'http://example.com/images',
- 'hashLevels' => 2,
- 'transformVia404' => false,
- 'backend' => $backend
- ],
- 'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ),
- 'wgLanguageCode' => $lang,
- 'wgDBprefix' => $this->db->getType() != 'oracle' ? 'unittest_' : 'ut_',
- 'wgRawHtml' => self::getOptionValue( 'wgRawHtml', $opts, false ),
- 'wgNamespacesWithSubpages' => [ NS_MAIN => isset( $opts['subpage'] ) ],
- 'wgAllowExternalImages' => self::getOptionValue( 'wgAllowExternalImages', $opts, true ),
- 'wgThumbLimits' => [ self::getOptionValue( 'thumbsize', $opts, 180 ) ],
- 'wgMaxTocLevel' => $maxtoclevel,
- 'wgUseTeX' => isset( $opts['math'] ) || isset( $opts['texvc'] ),
- 'wgMathDirectory' => $uploadDir . '/math',
- 'wgDefaultLanguageVariant' => $variant,
- 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
- 'wgUseTidy' => false,
- 'wgTidyConfig' => isset( $opts['tidy'] ) ? $this->tidySupport->getConfig() : null
- ];
-
- if ( $config ) {
- $configLines = explode( "\n", $config );
-
- foreach ( $configLines as $line ) {
- list( $var, $value ) = explode( '=', $line, 2 );
-
- $settings[$var] = eval( "return $value;" ); // ???
- }
- }
-
- $this->savedGlobals = [];
-
- /** @since 1.20 */
- Hooks::run( 'ParserTestGlobals', [ &$settings ] );
-
- $langObj = Language::factory( $lang );
- $settings['wgContLang'] = $langObj;
- $settings['wgLang'] = $langObj;
-
- $context = new RequestContext();
- $settings['wgOut'] = $context->getOutput();
- $settings['wgUser'] = $context->getUser();
- $settings['wgRequest'] = $context->getRequest();
-
- // We (re)set $wgThumbLimits to a single-element array above.
- $context->getUser()->setOption( 'thumbsize', 0 );
-
- foreach ( $settings as $var => $val ) {
- if ( array_key_exists( $var, $GLOBALS ) ) {
- $this->savedGlobals[$var] = $GLOBALS[$var];
- }
-
- $GLOBALS[$var] = $val;
- }
-
- MWTidy::destroySingleton();
- MagicWord::clearCache();
-
- # The entries saved into RepoGroup cache with previous globals will be wrong.
- RepoGroup::destroySingleton();
- FileBackendGroup::destroySingleton();
-
- # Create dummy files in storage
- $this->setupUploads();
-
- # Publish the articles after we have the final language set
- $this->publishTestArticles();
-
- MessageCache::destroyInstance();
-
- return $context;
- }
-
- /**
- * Get an FS upload directory (only applies to FSFileBackend)
- *
- * @return string The directory
- */
- protected function getUploadDir() {
- if ( $this->keepUploads ) {
- // Don't use getNewTempDirectory() as this is meant to persist
- $dir = wfTempDir() . '/mwParser-images';
-
- if ( is_dir( $dir ) ) {
- return $dir;
- }
- } else {
- $dir = $this->getNewTempDirectory();
- }
-
- if ( file_exists( $dir ) ) {
- wfDebug( "Already exists!\n" );
-
- return $dir;
- }
-
- return $dir;
- }
-
- /**
- * Create a dummy uploads directory which will contain a couple
- * of files in order to pass existence tests.
- *
- * @return string The directory
- */
- protected function setupUploads() {
- global $IP;