MediaWiki  REL1_31
ParserTestRunner Class Reference
Collaboration diagram for ParserTestRunner:

Public Member Functions

 __construct (TestRecorder $recorder, $options=[])
 
 addArticles ( $articles)
 Add articles to the test DB. More...
 
 getParser ( $preprocessor=null)
 Get a Parser object. More...
 
 getRecorder ()
 
 isSetupDone ( $funcName)
 Determine whether a particular setup function has been run. More...
 
 meetsRequirements ( $requirements)
 Determine whether the current parser has the hooks registered in it that are required by a file read by TestFileReader. More...
 
 perTestSetup ( $test, $nextTeardown=null)
 Do any required setup which is dependent on test options. More...
 
 requireFunctionHook ( $name)
 Check if a function hook is installed. More...
 
 requireHook ( $name)
 Check if a hook is installed. More...
 
 requireTransparentHook ( $name)
 Check if a transparent tag hook is installed. More...
 
 runTest ( $test)
 Run a given wikitext input through a freshly-constructed wiki parser, and compare the output against the expected results. More...
 
 runTests ( $testFileInfo)
 Run the tests from a single file. More...
 
 runTestsFromFiles ( $filenames)
 Run a series of tests listed in the given text files. More...
 
 setDatabase (IDatabase $db)
 
 setupDatabase ( $nextTeardown=null)
 Set up temporary DB tables. More...
 
 setupUploads ( $nextTeardown=null)
 Add data about uploads to the new test DB, and set up the upload directory. More...
 
 staticSetup ( $nextTeardown=null)
 Do any setup which can be done once for all tests, independent of test options, except for database setup. More...
 

Static Public Member Functions

static chomp ( $s)
 Remove last character if it is a newline. More...
 
static getParserTestFiles ()
 Get list of filenames to extension and core parser tests. More...
 

Protected Member Functions

 checkSetupDone ( $funcName, $funcName2=null)
 Ensure a given setup stage has been done, throw an exception if it has not. More...
 
 createRepoGroup ()
 Create a RepoGroup object appropriate for the current configuration. More...
 
 createTeardownObject ( $teardown, $nextTeardown=null)
 Take a setup array in the same format as the one given to executeSetupSnippets(), and return a ScopedCallback which, when consumed, executes the snippets in the setup array in reverse order. More...
 
 executeSetupSnippets ( $setup)
 Execute an array in which elements with integer keys are taken to be callable objects, and other elements are taken to be global variable set operations, with the key giving the variable name and the value giving the new global variable value. More...
 
 markSetupDone ( $funcName)
 Set a setupDone flag to indicate that setup has been done, and return the teardown closure. More...
 

Private Member Functions

 addArticle ( $name, $text, $file, $line)
 Insert a temporary test article. More...
 
 appendNamespaceSetup (&$setup, &$teardown)
 
 cleanupOption ( $opt)
 
 deleteFiles ( $files)
 Delete the specified files and their parent directories. More...
 
 getFakeTimestamp ()
 Fake constant timestamp to make sure time-related parser functions give a persistent value. More...
 
 listTables ()
 List of temporary tables to create, without prefix. More...
 
 parseOptions ( $instring)
 Given the options string, return an associative array of options. More...
 
 resetTitleServices ()
 Reset the Title-related services that need resetting for each test. More...
 
 setupInterwikis ()
 Insert hardcoded interwiki in the lookup table. More...
 
 setupUploadBackend ()
 Upload test files to the backend created by createRepoGroup(). More...
 
 teardownDatabase ()
 Helper for database teardown, called from the teardown closure. More...
 
 teardownUploadBackend ()
 Remove the dummy uploads directory. More...
 

Static Private Member Functions

static getOptionValue ( $key, $opts, $default)
 Use a regex to find out the value of an option. More...
 

Private Attributes

Database $db
 Our connection to the database. More...
 
CloneDatabase $dbClone
 Database clone helper. More...
 
string null $fileBackendName
 The name of the file backend to use, or null to use MockFileBackend. More...
 
array $normalizationFunctions = []
 A list of normalization functions to apply to the expected and actual output. More...
 
TestRecorder $recorder
 
string $regex
 A complete regex for filtering tests. More...
 
 $setupDone
 
TidyDriverBase $tidyDriver = null
 
TidySupport $tidySupport
 
string null $uploadDir = null
 The upload directory, or null to not set up an upload directory. More...
 
 $useTemporaryTables = true
 

Static Private Attributes

static array $coreTestFiles
 MediaWiki core parser test files, paths will be prefixed with DIR . More...
 

Detailed Description

Definition at line 36 of file ParserTestRunner.php.

Constructor & Destructor Documentation

◆ __construct()

ParserTestRunner::__construct ( TestRecorder  $recorder,
  $options = [] 
)
Parameters
TestRecorder$recorder
array$options

Definition at line 122 of file ParserTestRunner.php.

References $options, $recorder, as, and TestRecorder\warning().

Member Function Documentation

◆ addArticle()

ParserTestRunner::addArticle (   $name,
  $text,
  $file,
  $line 
)
private

Insert a temporary test article.

Parameters
string$nameThe title, including any prefix
string$textThe article text
string$fileThe input file name
int | string$lineThe input line number, for reporting errors
Exceptions
Exception
MWException

Definition at line 1614 of file ParserTestRunner.php.

References $name, $title, chomp(), EDIT_INTERNAL, EDIT_NEW, executeSetupSnippets(), WikiPage\factory(), ContentHandler\makeContent(), Title\newFromText(), NS_FILE, Revision\RAW, MessageCache\singleton(), RepoGroup\singleton(), and wfDebug().

Referenced by addArticles().

◆ addArticles()

ParserTestRunner::addArticles (   $articles)

Add articles to the test DB.

Parameters
array$articlesArticle info array from TestFileReader

Definition at line 1574 of file ParserTestRunner.php.

References $wgContLang, addArticle(), appendNamespaceSetup(), as, executeSetupSnippets(), Language\factory(), ObjectCache\getMainWANInstance(), and global.

Referenced by runTests().

◆ appendNamespaceSetup()

ParserTestRunner::appendNamespaceSetup ( $setup,
$teardown 
)
private

Definition at line 377 of file ParserTestRunner.php.

References $GLOBALS, and MWNamespace\clearCaches().

Referenced by addArticles(), and staticSetup().

◆ checkSetupDone()

ParserTestRunner::checkSetupDone (   $funcName,
  $funcName2 = null 
)
protected

Ensure a given setup stage has been done, throw an exception if it has not.

Parameters
string$funcName
string | null$funcName2

Definition at line 524 of file ParserTestRunner.php.

References wfGetCaller().

Referenced by perTestSetup(), runTests(), setupUploads(), and teardownDatabase().

◆ chomp()

static ParserTestRunner::chomp (   $s)
static

Remove last character if it is a newline.

Parameters
string$s
Returns
string

Definition at line 646 of file ParserTestRunner.php.

References $s.

Referenced by addArticle(), and TestFileReader\execute().

◆ cleanupOption()

ParserTestRunner::cleanupOption (   $opt)
private

Definition at line 1020 of file ParserTestRunner.php.

References $opt, and FormatJson\decode().

◆ createRepoGroup()

ParserTestRunner::createRepoGroup ( )
protected

Create a RepoGroup object appropriate for the current configuration.

Returns
RepoGroup

Definition at line 398 of file ParserTestRunner.php.

References $fileBackendName, $name, $wgFileBackends, as, class, global, wfTempDir(), and wfWikiID().

Referenced by staticSetup().

◆ createTeardownObject()

ParserTestRunner::createTeardownObject (   $teardown,
  $nextTeardown = null 
)
protected

Take a setup array in the same format as the one given to executeSetupSnippets(), and return a ScopedCallback which, when consumed, executes the snippets in the setup array in reverse order.

This is used to create "teardown objects" for the public API.

See also
staticSetup
Parameters
array$teardownThe snippet array
ScopedCallback | null$nextTeardownA ScopedCallback to consume
Returns
ScopedCallback

Definition at line 489 of file ParserTestRunner.php.

References executeSetupSnippets(), and use.

Referenced by perTestSetup(), setupDatabase(), setupUploads(), and staticSetup().

◆ deleteFiles()

ParserTestRunner::deleteFiles (   $files)
private

Delete the specified files and their parent directories.

Parameters
array$filesFile backend URIs mwstore://...

Definition at line 1550 of file ParserTestRunner.php.

References as, FileBackend\parentStoragePath(), and RepoGroup\singleton().

Referenced by teardownUploadBackend().

◆ executeSetupSnippets()

ParserTestRunner::executeSetupSnippets (   $setup)
protected

Execute an array in which elements with integer keys are taken to be callable objects, and other elements are taken to be global variable set operations, with the key giving the variable name and the value giving the new global variable value.

A closure is returned which, when executed, sets the global variables back to the values they had before this function was called.

See also
staticSetup
Parameters
array$setup
Returns
closure

Definition at line 462 of file ParserTestRunner.php.

References $GLOBALS, $name, $value, as, and use.

Referenced by addArticle(), addArticles(), createTeardownObject(), perTestSetup(), and staticSetup().

◆ getFakeTimestamp()

ParserTestRunner::getFakeTimestamp ( )
private

Fake constant timestamp to make sure time-related parser functions give a persistent value.

Definition at line 1733 of file ParserTestRunner.php.

Referenced by runTest(), and staticSetup().

◆ getOptionValue()

static ParserTestRunner::getOptionValue (   $key,
  $opts,
  $default 
)
staticprivate

Use a regex to find out the value of an option.

Parameters
string$keyName of option val to retrieve
array$optsOptions array to look in
mixed$defaultDefault value returned if not found
Returns
mixed

Definition at line 931 of file ParserTestRunner.php.

Referenced by perTestSetup().

◆ getParser()

ParserTestRunner::getParser (   $preprocessor = null)

Get a Parser object.

Parameters
string$preprocessor
Returns
Parser

Definition at line 780 of file ParserTestRunner.php.

References $parser, $wgParserConf, global, and ParserTestParserHook\setup().

Referenced by runTest().

◆ getParserTestFiles()

static ParserTestRunner::getParserTestFiles ( )
static

Get list of filenames to extension and core parser tests.

Returns
array

Definition at line 167 of file ParserTestRunner.php.

References $coreTestFiles, $name, $wgParserTestFiles, as, ExtensionRegistry\getInstance(), and global.

Referenced by ParserTestTopLevelSuite\__construct(), ParserTestsMaintenance\execute(), and ParserEditTests\setupFileData().

◆ getRecorder()

ParserTestRunner::getRecorder ( )

Definition at line 204 of file ParserTestRunner.php.

References $recorder.

◆ isSetupDone()

ParserTestRunner::isSetupDone (   $funcName)

Determine whether a particular setup function has been run.

Parameters
string$funcName
Returns
bool

Definition at line 539 of file ParserTestRunner.php.

◆ listTables()

ParserTestRunner::listTables ( )
private

List of temporary tables to create, without prefix.

Some of these probably aren't necessary.

Returns
array

Definition at line 1167 of file ParserTestRunner.php.

References $tables, $wgActorTableSchemaMigrationStage, $wgCommentTableSchemaMigrationStage, global, MIGRATION_WRITE_BOTH, and Hooks\run().

Referenced by setupDatabase(), and teardownDatabase().

◆ markSetupDone()

ParserTestRunner::markSetupDone (   $funcName)
protected

Set a setupDone flag to indicate that setup has been done, and return the teardown closure.

If the flag was already set, throw an exception.

Parameters
string$funcNameThe setup function name
Returns
closure

Definition at line 508 of file ParserTestRunner.php.

References use.

Referenced by perTestSetup(), setupDatabase(), setupUploads(), and staticSetup().

◆ meetsRequirements()

ParserTestRunner::meetsRequirements (   $requirements)

Determine whether the current parser has the hooks registered in it that are required by a file read by TestFileReader.

Parameters
array$requirements
Returns
bool

Definition at line 711 of file ParserTestRunner.php.

References as, requireFunctionHook(), requireHook(), and requireTransparentHook().

Referenced by runTests().

◆ parseOptions()

ParserTestRunner::parseOptions (   $instring)
private

Given the options string, return an associative array of options.

Todo:
Move this to TestFileReader
Parameters
string$instring
Returns
array

Definition at line 948 of file ParserTestRunner.php.

References $matches, $regex, and as.

Referenced by perTestSetup(), and runTest().

◆ perTestSetup()

ParserTestRunner::perTestSetup (   $test,
  $nextTeardown = null 
)

Do any required setup which is dependent on test options.

See also
staticSetup() for more information about setup/teardown
Parameters
array$testTest info supplied by TestFileReader
callable | null$nextTeardown
Returns
ScopedCallback

Definition at line 1044 of file ParserTestRunner.php.

References $context, $GLOBALS, $lang, $line, $skin, $tidyDriver, $user, $value, as, checkSetupDone(), MagicWord\clearCache(), createTeardownObject(), MWTidy\destroySingleton(), executeSetupSnippets(), Language\factory(), MWTidy\factory(), RequestContext\getMain(), getOptionValue(), MWNamespace\getValidNamespaces(), list, markSetupDone(), parseOptions(), resetTitleServices(), Hooks\run(), MWTidy\setInstance(), true, use, and User.

Referenced by runTest().

◆ requireFunctionHook()

ParserTestRunner::requireFunctionHook (   $name)

Check if a function hook is installed.

Parameters
string$name
Returns
bool True if function hook is present

Definition at line 1692 of file ParserTestRunner.php.

References $name, $wgParser, and global.

Referenced by meetsRequirements().

◆ requireHook()

ParserTestRunner::requireHook (   $name)

Check if a hook is installed.

Parameters
string$name
Returns
bool True if tag hook is present

Definition at line 1673 of file ParserTestRunner.php.

References $name, $wgParser, and global.

Referenced by meetsRequirements().

◆ requireTransparentHook()

ParserTestRunner::requireTransparentHook (   $name)

Check if a transparent tag hook is installed.

Parameters
string$name
Returns
bool True if function hook is present

Definition at line 1712 of file ParserTestRunner.php.

References $name, $wgParser, and global.

Referenced by meetsRequirements().

◆ resetTitleServices()

ParserTestRunner::resetTitleServices ( )
private

Reset the Title-related services that need resetting for each test.

Definition at line 632 of file ParserTestRunner.php.

References $services.

Referenced by perTestSetup(), and staticSetup().

◆ runTest()

ParserTestRunner::runTest (   $test)

Run a given wikitext input through a freshly-constructed wiki parser, and compare the output against the expected results.

Prints status and explanatory messages to stdout.

staticSetup() and setupWikiData() must be called before this function is entered.

Parameters
array$testThe test parameters:
  • test: The test name
  • desc: The subtest description
  • input: Wikitext to try rendering
  • options: Array of test options
  • config: Overrides for global variables, one per line
Returns
ParserTestResult or false if skipped

Definition at line 807 of file ParserTestRunner.php.

References $context, $name, $options, $out, $output, $parser, $section, $title, $user, as, Html\element(), Linker\formatComment(), getFakeTimestamp(), RequestContext\getMain(), getParser(), Html\inlineStyle(), Parser\MARKER_PREFIX, ParserOptions\newFromContext(), Title\newFromText(), ParserTestResultNormalizer\normalize(), parseOptions(), perTestSetup(), and wfDebug().

Referenced by runTests().

◆ runTests()

ParserTestRunner::runTests (   $testFileInfo)

Run the tests from a single file.

staticSetup() and setupDatabase() must have been called already.

Parameters
array$testFileInfoParsed file info returned by TestFileReader
Returns
bool True if passed all tests, false if any tests failed.

Definition at line 738 of file ParserTestRunner.php.

References addArticles(), as, checkSetupDone(), meetsRequirements(), and runTest().

Referenced by runTestsFromFiles().

◆ runTestsFromFiles()

ParserTestRunner::runTestsFromFiles (   $filenames)

Run a series of tests listed in the given text files.

Each test consists of a brief description, wikitext input, and the expected HTML output.

Prints status updates on stdout and counts up the total number and percentage of passed tests.

Handles all setup and teardown.

Parameters
array$filenamesArray of strings
Returns
bool True if passed all tests, false if any tests failed.

Definition at line 667 of file ParserTestRunner.php.

References $e, as, TestFileReader\read(), runTests(), setupDatabase(), setupUploads(), and staticSetup().

◆ setDatabase()

ParserTestRunner::setDatabase ( IDatabase  $db)

Definition at line 1204 of file ParserTestRunner.php.

References $db.

◆ setupDatabase()

ParserTestRunner::setupDatabase (   $nextTeardown = null)

Set up temporary DB tables.

For best performance, call this once only for all tests. However, it can be called at the start of each test if more isolation is desired.

Todo:
: This is basically an unrefactored copy of MediaWikiTestCase::setupAllTestDBs. They should be factored out somehow.

Do not call this function from a MediaWikiTestCase subclass, since MediaWikiTestCase does its own DB setup. Instead use setDatabase().

See also
staticSetup() for more information about setup/teardown
Parameters
ScopedCallback | null$nextTeardownThe next teardown object
Returns
ScopedCallback The teardown object

Definition at line 1226 of file ParserTestRunner.php.

References $wgDBprefix, createTeardownObject(), DB_MASTER, MediaWikiTestCase\DB_PREFIX, global, listTables(), markSetupDone(), MediaWikiTestCase\ORA_DB_PREFIX, LinkCache\singleton(), MessageCache\singleton(), teardownDatabase(), wfGetDB(), and wfGetLB().

Referenced by runTestsFromFiles().

◆ setupInterwikis()

ParserTestRunner::setupInterwikis ( )
private

Insert hardcoded interwiki in the lookup table.

This function insert a set of well known interwikis that are used in the parser tests. They can be considered has fixtures are injected in the interwiki cache by using the 'InterwikiLoadPrefix' hook. Since we are not interested in looking up interwikis in the database, the hook completely replace the existing mechanism (hook returns false).

Returns
closure for teardown

Definition at line 554 of file ParserTestRunner.php.

References Hooks\clear(), and Hooks\register().

Referenced by staticSetup().

◆ setupUploadBackend()

ParserTestRunner::setupUploadBackend ( )
private

Upload test files to the backend created by createRepoGroup().

Returns
callable The teardown callback

Definition at line 1476 of file ParserTestRunner.php.

References $base, $IP, global, RepoGroup\singleton(), teardownUploadBackend(), and use.

Referenced by setupUploads().

◆ setupUploads()

ParserTestRunner::setupUploads (   $nextTeardown = null)

Add data about uploads to the new test DB, and set up the upload directory.

This should be called after either setDatabase() or setupDatabase().

Parameters
ScopedCallback | null$nextTeardownThe next teardown object
Returns
ScopedCallback The teardown object

Definition at line 1291 of file ParserTestRunner.php.

References $image, $user, checkSetupDone(), User\createNew(), createTeardownObject(), Title\makeTitle(), markSetupDone(), MEDIATYPE_AUDIO, MEDIATYPE_BITMAP, MEDIATYPE_DRAWING, MEDIATYPE_VIDEO, NS_FILE, serialize(), setupUploadBackend(), and wfLocalFile().

Referenced by runTestsFromFiles().

◆ staticSetup()

ParserTestRunner::staticSetup (   $nextTeardown = null)

Do any setup which can be done once for all tests, independent of test options, except for database setup.

Public setup functions in this class return a ScopedCallback object. When this object is destroyed by going out of scope, teardown of the corresponding test setup is performed.

Teardown objects may be chained by passing a ScopedCallback from a previous setup stage as the $nextTeardown parameter. This enforces the convention that teardown actions are taken in reverse order to the corresponding setup actions. When $nextTeardown is specified, a ScopedCallback will be returned which first tears down the current setup stage, and then tears down the previous setup stage which was specified by $nextTeardown.

Parameters
ScopedCallback | null$nextTeardown
Returns
ScopedCallback

Definition at line 227 of file ParserTestRunner.php.

References ObjectCache\$instances, $parser, $services, $wgObjectCaches, appendNamespaceSetup(), CACHE_DB, class, Hooks\clear(), createRepoGroup(), createTeardownObject(), MessageCache\destroyInstance(), RepoGroup\destroySingleton(), LockManagerGroup\destroySingletons(), executeSetupSnippets(), getFakeTimestamp(), global, markSetupDone(), Hooks\register(), resetTitleServices(), RepoGroup\setSingleton(), setupInterwikis(), and use.

Referenced by runTestsFromFiles().

◆ teardownDatabase()

ParserTestRunner::teardownDatabase ( )
private

Helper for database teardown, called from the teardown closure.

Destroy the database clone and fix up some things that CloneDatabase doesn't fix.

Todo:
Move most things here to CloneDatabase

Definition at line 1438 of file ParserTestRunner.php.

References $tables, as, checkSetupDone(), listTables(), and wfDebug().

Referenced by setupDatabase().

◆ teardownUploadBackend()

ParserTestRunner::teardownUploadBackend ( )
private

Remove the dummy uploads directory.

Definition at line 1525 of file ParserTestRunner.php.

References deleteFiles(), and RepoGroup\singleton().

Referenced by setupUploadBackend().

Member Data Documentation

◆ $coreTestFiles

array ParserTestRunner::$coreTestFiles
staticprivate
Initial value:
= [
'parserTests.txt',
'extraParserTests.txt',
]

MediaWiki core parser test files, paths will be prefixed with DIR .

'/'

Definition at line 44 of file ParserTestRunner.php.

Referenced by getParserTestFiles().

◆ $db

Database ParserTestRunner::$db
private

Our connection to the database.

Definition at line 69 of file ParserTestRunner.php.

Referenced by setDatabase().

◆ $dbClone

CloneDatabase ParserTestRunner::$dbClone
private

Database clone helper.

Definition at line 75 of file ParserTestRunner.php.

◆ $fileBackendName

string null ParserTestRunner::$fileBackendName
private

The name of the file backend to use, or null to use MockFileBackend.

Definition at line 103 of file ParserTestRunner.php.

Referenced by createRepoGroup().

◆ $normalizationFunctions

array ParserTestRunner::$normalizationFunctions = []
private

A list of normalization functions to apply to the expected and actual output.

Definition at line 116 of file ParserTestRunner.php.

◆ $recorder

TestRecorder ParserTestRunner::$recorder
private

Definition at line 90 of file ParserTestRunner.php.

Referenced by __construct(), and getRecorder().

◆ $regex

string ParserTestRunner::$regex
private

A complete regex for filtering tests.

Definition at line 109 of file ParserTestRunner.php.

Referenced by parseOptions().

◆ $setupDone

ParserTestRunner::$setupDone
private
Initial value:
= [
'staticSetup' => false,
'perTestSetup' => false,
'setupDatabase' => false,
'setDatabase' => false,
'setupUploads' => false,
]

Definition at line 57 of file ParserTestRunner.php.

◆ $tidyDriver

TidyDriverBase ParserTestRunner::$tidyDriver = null
private

Definition at line 85 of file ParserTestRunner.php.

Referenced by perTestSetup().

◆ $tidySupport

TidySupport ParserTestRunner::$tidySupport
private

Definition at line 80 of file ParserTestRunner.php.

◆ $uploadDir

string null ParserTestRunner::$uploadDir = null
private

The upload directory, or null to not set up an upload directory.

Definition at line 97 of file ParserTestRunner.php.

◆ $useTemporaryTables

ParserTestRunner::$useTemporaryTables = true
private

Definition at line 52 of file ParserTestRunner.php.


The documentation for this class was generated from the following file: