| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Parser; |
| 4 |
|
| 5 |
use MediaWiki\Category\TrackingCategories; |
| 6 |
use MediaWiki\Config\ServiceOptions; |
| 7 |
use MediaWiki\HookContainer\HookContainer; |
| 8 |
use MediaWiki\Http\HttpRequestFactory; |
| 9 |
use MediaWiki\Language\Language; |
| 10 |
use MediaWiki\Languages\LanguageConverterFactory; |
| 11 |
use MediaWiki\Languages\LanguageNameUtils; |
| 12 |
use MediaWiki\Linker\LinkRendererFactory; |
| 13 |
use MediaWiki\Page\File\BadFileLookup; |
| 14 |
use MediaWiki\Page\PageReference; |
| 15 |
use MediaWiki\Page\PageReferenceValue; |
| 16 |
use MediaWiki\Parser\MagicWord; |
| 17 |
use MediaWiki\Parser\MagicWordFactory; |
| 18 |
use MediaWiki\Parser\Parser; |
| 19 |
use MediaWiki\Parser\ParserFactory; |
| 20 |
use MediaWiki\Preferences\SignatureValidatorFactory; |
| 21 |
use MediaWiki\SpecialPage\SpecialPageFactory; |
| 22 |
use MediaWiki\Tidy\TidyDriverBase; |
| 23 |
use MediaWiki\Title\NamespaceInfo; |
| 24 |
use MediaWiki\Title\Title; |
| 25 |
use MediaWiki\Title\TitleFormatter; |
| 26 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 27 |
use MediaWiki\User\UserFactory; |
| 28 |
use MediaWiki\User\UserNameUtils; |
| 29 |
use MediaWiki\Utils\UrlUtils; |
| 30 |
use MediaWikiIntegrationTestCase; |
| 31 |
use Psr\Log\NullLogger; |
| 32 |
use ReflectionObject; |
| 33 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class ParserTest extends MediaWikiIntegrationTestCase { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private function createConstructorArguments() { |
| 44 |
$options = new ServiceOptions( |
| 45 |
Parser::CONSTRUCTOR_OPTIONS, |
| 46 |
array_fill_keys( Parser::CONSTRUCTOR_OPTIONS, null ) |
| 47 |
); |
| 48 |
|
| 49 |
$contLang = $this->createMock( Language::class ); |
| 50 |
$mw = new MagicWord( null, [], true, $contLang ); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
$mwFactory = $this->createNoOpMock( MagicWordFactory::class, |
| 55 |
[ 'get', 'getVariableIDs', 'getSubstArray', 'newArray' ] ); |
| 56 |
$mwFactory->method( 'get' )->willReturn( $mw ); |
| 57 |
$mwFactory->method( 'getVariableIDs' )->willReturn( [] ); |
| 58 |
|
| 59 |
$urlUtils = $this->createNoOpMock( UrlUtils::class, [ 'validProtocols' ] ); |
| 60 |
$urlUtils->method( 'validProtocols' )->willReturn( '' ); |
| 61 |
|
| 62 |
return [ |
| 63 |
$options, |
| 64 |
$mwFactory, |
| 65 |
$contLang, |
| 66 |
$this->createNoOpMock( ParserFactory::class ), |
| 67 |
$urlUtils, |
| 68 |
$this->createNoOpMock( SpecialPageFactory::class ), |
| 69 |
$this->createNoOpMock( LinkRendererFactory::class ), |
| 70 |
$this->createNoOpMock( NamespaceInfo::class ), |
| 71 |
new NullLogger(), |
| 72 |
$this->createNoOpMock( BadFileLookup::class ), |
| 73 |
$this->createNoOpMock( LanguageConverterFactory::class, [ 'isConversionDisabled' ] ), |
| 74 |
$this->createNoOpMock( LanguageNameUtils::class ), |
| 75 |
$this->createNoOpMock( HookContainer::class, [ 'run' ] ), |
| 76 |
$this->createNoOpMock( TidyDriverBase::class ), |
| 77 |
$this->createNoOpMock( WANObjectCache::class ), |
| 78 |
$this->createNoOpMock( UserOptionsLookup::class ), |
| 79 |
$this->createNoOpMock( UserFactory::class ), |
| 80 |
$this->createNoOpMock( TitleFormatter::class ), |
| 81 |
$this->createNoOpMock( HttpRequestFactory::class ), |
| 82 |
$this->createNoOpMock( TrackingCategories::class ), |
| 83 |
$this->createNoOpMock( SignatureValidatorFactory::class ), |
| 84 |
$this->createNoOpMock( UserNameUtils::class ) |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function testConstructorArguments() { |
| 92 |
$args = $this->createConstructorArguments(); |
| 93 |
|
| 94 |
|
| 95 |
ParserFactory::$inParserFactory += 1; |
| 96 |
try { |
| 97 |
$parser = new Parser( ...$args ); |
| 98 |
} finally { |
| 99 |
ParserFactory::$inParserFactory -= 1; |
| 100 |
} |
| 101 |
|
| 102 |
$refObject = new ReflectionObject( $parser ); |
| 103 |
foreach ( $refObject->getProperties() as $prop ) { |
| 104 |
foreach ( $args as $idx => $mockTest ) { |
| 105 |
if ( $prop->isInitialized( $parser ) && $prop->getValue( $parser ) === $mockTest ) { |
| 106 |
unset( $args[$idx] ); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
$preproc = $parser->getPreprocessor(); |
| 113 |
$refObject = new ReflectionObject( $preproc ); |
| 114 |
foreach ( $refObject->getProperties() as $prop ) { |
| 115 |
foreach ( $args as $idx => $mockTest ) { |
| 116 |
if ( $prop->getValue( $preproc ) === $mockTest ) { |
| 117 |
unset( $args[$idx] ); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
$this->assertSame( [], $args, 'Not all arguments to the Parser constructor were ' . |
| 123 |
'found on the Parser object' ); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
private function newParser() { |
| 130 |
$args = $this->createConstructorArguments(); |
| 131 |
ParserFactory::$inParserFactory += 1; |
| 132 |
try { |
| 133 |
return new Parser( ...$args ); |
| 134 |
} finally { |
| 135 |
ParserFactory::$inParserFactory -= 1; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function testSetPage() { |
| 145 |
$parser = $this->newParser(); |
| 146 |
|
| 147 |
$page = new PageReferenceValue( NS_SPECIAL, 'Dummy', PageReference::LOCAL ); |
| 148 |
$parser->setPage( $page ); |
| 149 |
$this->assertTrue( $page->isSamePageAs( $parser->getPage() ) ); |
| 150 |
$this->assertTrue( $page->isSamePageAs( $parser->getTitle() ) ); |
| 151 |
$this->assertInstanceOf( Title::class, $parser->getTitle() ); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function testSetTitle() { |
| 160 |
$parser = $this->newParser(); |
| 161 |
|
| 162 |
$title = Title::makeTitle( NS_SPECIAL, 'Dummy' ); |
| 163 |
$parser->setTitle( $title ); |
| 164 |
$this->assertSame( $title, $parser->getTitle() ); |
| 165 |
$this->assertTrue( $title->isSamePageAs( $parser->getPage() ) ); |
| 166 |
} |
| 167 |
} |