| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\RequestContext; |
| 4 |
use MediaWiki\MainConfigNames; |
| 5 |
use MediaWiki\MainConfigSchema; |
| 6 |
use MediaWiki\Message\Message; |
| 7 |
use MediaWiki\Page\ParserOutputAccess; |
| 8 |
use MediaWiki\Parser\ParserOptions; |
| 9 |
use MediaWiki\Parser\ParserOutput; |
| 10 |
use MediaWiki\Status\Status; |
| 11 |
use MediaWiki\Title\Title; |
| 12 |
use MediaWiki\User\User; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class ArticleTest extends \MediaWikiIntegrationTestCase { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private function newArticle( Title $title, ?User $user = null ): Article { |
| 26 |
if ( !$user ) { |
| 27 |
$user = $this->getTestUser()->getUser(); |
| 28 |
} |
| 29 |
|
| 30 |
$context = new RequestContext(); |
| 31 |
$article = new Article( $title ); |
| 32 |
$context->setUser( $user ); |
| 33 |
$context->setTitle( $title ); |
| 34 |
$article->setContext( $context ); |
| 35 |
|
| 36 |
return $article; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public function testSerialization_fails() { |
| 43 |
$article = new Article( Title::newMainPage() ); |
| 44 |
|
| 45 |
$this->expectException( LogicException::class ); |
| 46 |
serialize( $article ); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function testMissingArticleMessage() { |
| 55 |
|
| 56 |
$title = Title::makeTitle( NS_MEDIAWIKI, 'Uploadedimage' ); |
| 57 |
$article = $this->newArticle( $title ); |
| 58 |
|
| 59 |
$article->showMissingArticle(); |
| 60 |
$output = $article->getContext()->getOutput(); |
| 61 |
$this->assertStringContainsString( |
| 62 |
Message::newFromKey( 'uploadedimage' )->parse(), |
| 63 |
$output->getHTML() |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function testShowPatrolFooter( $group, $title, $editPageText, $isEditedBySameUser, $expectedResult ) { |
| 73 |
$testPage = $this->getNonexistingTestPage( $title ); |
| 74 |
$user1 = $this->getTestUser( $group )->getUser(); |
| 75 |
$user2 = $this->getTestUser()->getUser(); |
| 76 |
if ( $editPageText !== null ) { |
| 77 |
$editedUser = $isEditedBySameUser ? $user1 : $user2; |
| 78 |
$editIsGood = $this->editPage( $testPage, $editPageText, '', NS_MAIN, $editedUser )->isGood(); |
| 79 |
$this->assertTrue( $editIsGood, 'edited a page' ); |
| 80 |
} |
| 81 |
|
| 82 |
$article = $this->newArticle( $title, $user1 ); |
| 83 |
$this->assertSame( $expectedResult, $article->showPatrolFooter() ); |
| 84 |
} |
| 85 |
|
| 86 |
public static function provideShowPatrolFooter() { |
| 87 |
yield 'UserAllowedRevExist' => [ |
| 88 |
'sysop', |
| 89 |
Title::makeTitle( NS_MAIN, 'Page1' ), |
| 90 |
'EditPage1', |
| 91 |
false, |
| 92 |
true |
| 93 |
]; |
| 94 |
|
| 95 |
yield 'UserNotAllowedRevExist' => [ |
| 96 |
null, |
| 97 |
Title::makeTitle( NS_MAIN, 'Page2' ), |
| 98 |
'EditPage2', |
| 99 |
false, |
| 100 |
false |
| 101 |
]; |
| 102 |
|
| 103 |
yield 'UserAllowedNoRev' => [ |
| 104 |
'sysop', |
| 105 |
Title::makeTitle( NS_MAIN, 'Page3' ), |
| 106 |
null, |
| 107 |
false, |
| 108 |
false |
| 109 |
]; |
| 110 |
|
| 111 |
yield 'UserAllowedRevExistBySameUser' => [ |
| 112 |
'sysop', |
| 113 |
Title::makeTitle( NS_MAIN, 'Page4' ), |
| 114 |
'EditPage4', |
| 115 |
true, |
| 116 |
false |
| 117 |
]; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function testShowPatrolFooterMovedPage() { |
| 126 |
$oldTitle = Title::makeTitle( NS_USER, 'NewDraft' ); |
| 127 |
$newTitle = Title::makeTitle( NS_MAIN, 'NewDraft' ); |
| 128 |
$editor = $this->getTestUser()->getUser(); |
| 129 |
|
| 130 |
$editIsGood = $this->editPage( $oldTitle, 'Content', '', NS_USER, $editor )->isGood(); |
| 131 |
$this->assertTrue( $editIsGood, 'edited a page' ); |
| 132 |
|
| 133 |
$status = $this->getServiceContainer() |
| 134 |
->getMovePageFactory() |
| 135 |
->newMovePage( $oldTitle, $newTitle ) |
| 136 |
->move( $this->getTestUser()->getUser() ); |
| 137 |
$this->assertTrue( $status->isOK() ); |
| 138 |
|
| 139 |
$sysop = $this->getTestUser( 'sysop' )->getUser(); |
| 140 |
$article = $this->newArticle( $newTitle, $sysop ); |
| 141 |
|
| 142 |
$this->assertTrue( $article->showPatrolFooter() ); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function testUsesCachedOutput() { |
| 151 |
$title = $this->getExistingTestPage()->getTitle(); |
| 152 |
|
| 153 |
$parserOutputAccess = $this->createNoOpMock( ParserOutputAccess::class, [ 'getCachedParserOutput' ] ); |
| 154 |
$parserOutputAccess->method( 'getCachedParserOutput' ) |
| 155 |
->willReturn( new ParserOutput( 'Kittens' ) ); |
| 156 |
|
| 157 |
$this->setService( 'ParserOutputAccess', $parserOutputAccess ); |
| 158 |
|
| 159 |
$article = $this->newArticle( $title ); |
| 160 |
$article->view(); |
| 161 |
$this->assertStringContainsString( 'Kittens', $article->getContext()->getOutput()->getHTML() ); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function testOutputIsCached() { |
| 170 |
$this->overrideConfigValue( |
| 171 |
MainConfigNames::ParsoidCacheConfig, |
| 172 |
[ 'WarmParsoidParserCache' => true ] |
| 173 |
+ MainConfigSchema::getDefaultValue( MainConfigNames::ParsoidCacheConfig ) |
| 174 |
); |
| 175 |
$title = $this->getExistingTestPage()->getTitle(); |
| 176 |
|
| 177 |
$this->runJobs( [ 'minJobs' => 0 ] ); |
| 178 |
|
| 179 |
$beforePreWarm = true; |
| 180 |
$parserOutputAccess = $this->createNoOpMock( |
| 181 |
ParserOutputAccess::class, |
| 182 |
[ 'getCachedParserOutput', 'getParserOutput', ] |
| 183 |
); |
| 184 |
$parserOutputAccess->method( 'getCachedParserOutput' ) |
| 185 |
->willReturn( null ); |
| 186 |
$parserOutputAccess |
| 187 |
->expects( $this->exactly( 2 ) ) |
| 188 |
->method( 'getParserOutput' ) |
| 189 |
->with( |
| 190 |
$this->anything(), |
| 191 |
$this->callback( function ( ParserOptions $parserOptions ) use ( &$beforePreWarm ) { |
| 192 |
$expectedReason = $beforePreWarm ? 'page-view' : 'view'; |
| 193 |
$this->assertSame( $expectedReason, $parserOptions->getRenderReason() ); |
| 194 |
return true; |
| 195 |
} ), |
| 196 |
$this->anything(), |
| 197 |
$this->callback( function ( $options ) use ( &$beforePreWarm ) { |
| 198 |
if ( $beforePreWarm ) { |
| 199 |
$this->assertTrue( (bool)( $options & ParserOutputAccess::OPT_NO_CHECK_CACHE ), |
| 200 |
"The cache is not checked again" ); |
| 201 |
$this->assertTrue( (bool)( $options & ParserOutputAccess::OPT_LINKS_UPDATE ), |
| 202 |
"WikiPage::triggerOpportunisticLinksUpdate is attempted" ); |
| 203 |
} |
| 204 |
return true; |
| 205 |
} ) |
| 206 |
) |
| 207 |
->willReturnCallback( static function ( $page, $parserOptions, $revision, $options ) use ( &$beforePreWarm ) { |
| 208 |
$content = $beforePreWarm ? 'Old Kittens' : 'New Kittens'; |
| 209 |
return Status::newGood( new ParserOutput( $content ) ); |
| 210 |
} ); |
| 211 |
|
| 212 |
$this->setService( 'ParserOutputAccess', $parserOutputAccess ); |
| 213 |
|
| 214 |
$article = $this->newArticle( $title ); |
| 215 |
$article->view(); |
| 216 |
|
| 217 |
$beforePreWarm = false; |
| 218 |
$this->runJobs( [ 'minJobs' => 1, 'maxJobs' => 1 ], [ 'type' => 'parsoidCachePrewarm' ] ); |
| 219 |
|
| 220 |
|
| 221 |
$this->assertStringContainsString( 'Old Kittens', $article->getContext()->getOutput()->getHTML() ); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public function testShowProtectionIndicator() { |
| 229 |
$this->overrideConfigValue( |
| 230 |
MainConfigNames::EnableProtectionIndicators, |
| 231 |
true |
| 232 |
); |
| 233 |
$title = $this->getExistingTestPage()->getTitle(); |
| 234 |
$article = $this->newArticle( $title ); |
| 235 |
|
| 236 |
$wikiPage = new WikiPage( $title ); |
| 237 |
$cascade = false; |
| 238 |
$wikiPage->doUpdateRestrictions( [ |
| 239 |
'edit' => 'autoconfirmed', |
| 240 |
], |
| 241 |
[ 'edit' => 'infinity' ], |
| 242 |
$cascade, |
| 243 |
'Test reason', |
| 244 |
$this->getTestSysop()->getUser() |
| 245 |
); |
| 246 |
|
| 247 |
$article->showProtectionIndicator(); |
| 248 |
$output = $article->getContext()->getOutput(); |
| 249 |
$this->assertArrayHasKey( 'protection-autoconfirmed', $output->getIndicators(), 'Protection indicators are shown when a page is protected' ); |
| 250 |
|
| 251 |
$templateTitle = Title::newFromText( 'CascadeProtectionTest', NS_TEMPLATE ); |
| 252 |
$this->editPage( $templateTitle, 'Some text here', 'Test', NS_TEMPLATE, $this->getTestSysop()->getUser() ); |
| 253 |
$articleTitle = $this->getExistingTestPage()->getTitle(); |
| 254 |
$this->editPage( $articleTitle, '{{CascadeProtectionTest}}', 'Test', NS_MAIN, $this->getTestSysop()->getUser() ); |
| 255 |
$wikiPage = new WikiPage( $articleTitle ); |
| 256 |
$cascade = true; |
| 257 |
$wikiPage->doUpdateRestrictions( [ |
| 258 |
'edit' => 'sysop', |
| 259 |
], |
| 260 |
[ 'edit' => 'infinity' ], |
| 261 |
$cascade, |
| 262 |
'Test reason', |
| 263 |
$this->getTestSysop()->getUser() |
| 264 |
); |
| 265 |
|
| 266 |
$template = $this->newArticle( $templateTitle ); |
| 267 |
|
| 268 |
$template->showProtectionIndicator(); |
| 269 |
$output = $template->getContext()->getOutput(); |
| 270 |
$this->assertArrayHasKey( |
| 271 |
'protection-sysop-cascade', |
| 272 |
$output->getIndicators(), |
| 273 |
'Protection indicators are shown when a page protected using cascade protection' |
| 274 |
); |
| 275 |
} |
| 276 |
} |