This moves the implementation of ParserOutput::addTrackingCategory() to the TrackingCategories class as a non-static method. This makes invocation from ParserOutput awkward, but when invoking as Parser::addTrackingCategory() all the necessary services are available. As a result, we've also soft-deprecated ParserOutput::addTrackingCategory(); new users should use the TrackingCategories::addTrackingCategory() method, or else Parser::addTrackingCategory() if the parser object is available. The Parser class is already kind of bloated as it is (alas), but there aren't too many callsites which invoke ParserOutput::addTrackingCategory() and don't have the corresponding Parser object handy; see: https://codesearch.wmcloud.org/search/?q=%5BOo%5Dutput%28%5C%28%5C%29%29%3F-%3EaddTrackingCategory%5C%28&i=nope&files=&excludeFiles=&repos= Change-Id: I697ce188a912e445a6a748121575548e79aabac6
138 lines
4.1 KiB
PHP
138 lines
4.1 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Http\HttpRequestFactory;
|
|
use MediaWiki\Page\PageReference;
|
|
use MediaWiki\Page\PageReferenceValue;
|
|
|
|
/**
|
|
* @covers Parser::__construct
|
|
*/
|
|
class ParserTest extends MediaWikiIntegrationTestCase {
|
|
/**
|
|
* Helper method to create mocks
|
|
* @return array
|
|
*/
|
|
private function createConstructorArguments() {
|
|
// Stub out a MagicWordFactory so the Parser can initialize its
|
|
// function hooks when it is created.
|
|
$mwFactory = $this->getMockBuilder( MagicWordFactory::class )
|
|
->disableOriginalConstructor()
|
|
->onlyMethods( [ 'get', 'getVariableIDs' ] )
|
|
->getMock();
|
|
$mwFactory
|
|
->method( 'get' )->will( $this->returnCallback( function ( $arg ) {
|
|
$mw = $this->getMockBuilder( MagicWord::class )
|
|
->disableOriginalConstructor()
|
|
->onlyMethods( [ 'getSynonyms' ] )
|
|
->getMock();
|
|
$mw->method( 'getSynonyms' )->willReturn( [] );
|
|
return $mw;
|
|
} ) );
|
|
$mwFactory
|
|
->method( 'getVariableIDs' )->willReturn( [] );
|
|
|
|
return [
|
|
$this->createMock( MediaWiki\Config\ServiceOptions::class ),
|
|
$mwFactory,
|
|
$this->createMock( Language::class ),
|
|
$this->createMock( ParserFactory::class ),
|
|
'a snail can sleep for three years',
|
|
$this->createMock( MediaWiki\Special\SpecialPageFactory::class ),
|
|
$this->createMock( MediaWiki\Linker\LinkRendererFactory::class ),
|
|
$this->createMock( NamespaceInfo::class ),
|
|
new Psr\Log\NullLogger(),
|
|
$this->createMock( MediaWiki\BadFileLookup::class ),
|
|
$this->createMock( MediaWiki\Languages\LanguageConverterFactory::class ),
|
|
$this->createMock( MediaWiki\HookContainer\HookContainer::class ),
|
|
$this->createMock( MediaWiki\Tidy\TidyDriverBase::class ),
|
|
$this->createMock( WANObjectCache::class ),
|
|
$this->createMock( MediaWiki\User\UserOptionsLookup::class ),
|
|
$this->createMock( MediaWiki\User\UserFactory::class ),
|
|
$this->createMock( TitleFormatter::class ),
|
|
$this->createMock( HttpRequestFactory::class ),
|
|
$this->createMock( TrackingCategories::class )
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @covers Parser::__construct
|
|
*/
|
|
public function testConstructorArguments() {
|
|
$args = $this->createConstructorArguments();
|
|
|
|
// Fool Parser into thinking we are constructing via a ParserFactory
|
|
ParserFactory::$inParserFactory += 1;
|
|
try {
|
|
$parser = new Parser( ...$args );
|
|
} finally {
|
|
ParserFactory::$inParserFactory -= 1;
|
|
}
|
|
|
|
$refObject = new ReflectionObject( $parser );
|
|
foreach ( $refObject->getProperties() as $prop ) {
|
|
$prop->setAccessible( true );
|
|
foreach ( $args as $idx => $mockTest ) {
|
|
if ( $prop->getValue( $parser ) === $mockTest ) {
|
|
unset( $args[$idx] );
|
|
}
|
|
}
|
|
}
|
|
// The WANObjectCache gets set on the Preprocessor, not the
|
|
// Parser.
|
|
$preproc = $parser->getPreprocessor();
|
|
$refObject = new ReflectionObject( $preproc );
|
|
foreach ( $refObject->getProperties() as $prop ) {
|
|
$prop->setAccessible( true );
|
|
foreach ( $args as $idx => $mockTest ) {
|
|
if ( $prop->getValue( $preproc ) === $mockTest ) {
|
|
unset( $args[$idx] );
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->assertSame( [], $args, 'Not all arguments to the Parser constructor were ' .
|
|
'found on the Parser object' );
|
|
}
|
|
|
|
/**
|
|
* @return Parser
|
|
*/
|
|
private function newParser() {
|
|
$args = $this->createConstructorArguments();
|
|
ParserFactory::$inParserFactory += 1;
|
|
try {
|
|
return new Parser( ...$args );
|
|
} finally {
|
|
ParserFactory::$inParserFactory -= 1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @covers Parser::setPage
|
|
* @covers Parser::getPage
|
|
* @covers Parser::getTitle
|
|
*/
|
|
public function testSetPage() {
|
|
$parser = $this->newParser();
|
|
|
|
$page = new PageReferenceValue( NS_SPECIAL, 'Dummy', PageReference::LOCAL );
|
|
$parser->setPage( $page );
|
|
$this->assertTrue( $page->isSamePageAs( $parser->getPage() ) );
|
|
$this->assertTrue( $page->isSamePageAs( $parser->getTitle() ) );
|
|
$this->assertInstanceOf( Title::class, $parser->getTitle() );
|
|
}
|
|
|
|
/**
|
|
* @covers Parser::setPage
|
|
* @covers Parser::getPage
|
|
* @covers Parser::getTitle
|
|
*/
|
|
public function testSetTitle() {
|
|
$parser = $this->newParser();
|
|
|
|
$title = Title::makeTitle( NS_SPECIAL, 'Dummy' );
|
|
$parser->setTitle( $title );
|
|
$this->assertSame( $title, $parser->getTitle() );
|
|
$this->assertTrue( $title->isSamePageAs( $parser->getPage() ) );
|
|
}
|
|
}
|