| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Revision; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use LogicException; |
| 7 |
use MediaWiki\Content\Content; |
| 8 |
use MediaWiki\Content\Renderer\ContentRenderer; |
| 9 |
use MediaWiki\Content\WikitextContent; |
| 10 |
use MediaWiki\Page\PageIdentity; |
| 11 |
use MediaWiki\Page\PageIdentityValue; |
| 12 |
use MediaWiki\Page\PageReference; |
| 13 |
use MediaWiki\Parser\ParserOptions; |
| 14 |
use MediaWiki\Parser\ParserOutput; |
| 15 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 16 |
use MediaWiki\Revision\MutableRevisionSlots; |
| 17 |
use MediaWiki\Revision\RenderedRevision; |
| 18 |
use MediaWiki\Revision\RevisionArchiveRecord; |
| 19 |
use MediaWiki\Revision\RevisionRecord; |
| 20 |
use MediaWiki\Revision\RevisionStore; |
| 21 |
use MediaWiki\Revision\RevisionStoreRecord; |
| 22 |
use MediaWiki\Revision\SlotRecord; |
| 23 |
use MediaWiki\Revision\SuppressedDataException; |
| 24 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 25 |
use MediaWiki\Title\TitleValue; |
| 26 |
use MediaWiki\User\UserIdentityValue; |
| 27 |
use MediaWikiIntegrationTestCase; |
| 28 |
use PHPUnit\Framework\MockObject\MockObject; |
| 29 |
use Wikimedia\TestingAccessWrapper; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class RenderedRevisionTest extends MediaWikiIntegrationTestCase { |
| 36 |
use MockAuthorityTrait; |
| 37 |
|
| 38 |
|
| 39 |
private $combinerCallback; |
| 40 |
|
| 41 |
|
| 42 |
private $contentRenderer; |
| 43 |
|
| 44 |
protected function setUp(): void { |
| 45 |
parent::setUp(); |
| 46 |
|
| 47 |
$this->combinerCallback = function ( RenderedRevision $rr, array $hints = [] ) { |
| 48 |
return $this->combineOutput( $rr, $hints ); |
| 49 |
}; |
| 50 |
|
| 51 |
$this->contentRenderer = $this->getServiceContainer()->getContentRenderer(); |
| 52 |
} |
| 53 |
|
| 54 |
private function combineOutput( RenderedRevision $rrev, array $hints = [] ) { |
| 55 |
|
| 56 |
|
| 57 |
$withHtml = $hints['generate-html'] ?? true; |
| 58 |
|
| 59 |
$revision = $rrev->getRevision(); |
| 60 |
$slots = $revision->getSlots()->getSlots(); |
| 61 |
|
| 62 |
$combinedOutput = new ParserOutput( null ); |
| 63 |
$slotOutput = []; |
| 64 |
foreach ( $slots as $role => $slot ) { |
| 65 |
$out = $rrev->getSlotParserOutput( $role, $hints ); |
| 66 |
$slotOutput[$role] = $out; |
| 67 |
|
| 68 |
$combinedOutput->mergeInternalMetaDataFrom( $out ); |
| 69 |
$combinedOutput->mergeTrackingMetaDataFrom( $out ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( $withHtml ) { |
| 73 |
$html = ''; |
| 74 |
|
| 75 |
foreach ( $slotOutput as $role => $out ) { |
| 76 |
|
| 77 |
if ( $html !== '' ) { |
| 78 |
|
| 79 |
$html .= "(($role))"; |
| 80 |
} |
| 81 |
|
| 82 |
$html .= $out->getRawText(); |
| 83 |
$combinedOutput->mergeHtmlMetaDataFrom( $out ); |
| 84 |
} |
| 85 |
|
| 86 |
$combinedOutput->setRawText( $html ); |
| 87 |
} |
| 88 |
|
| 89 |
return $combinedOutput; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private function getMockRevision( |
| 101 |
$class, |
| 102 |
$page, |
| 103 |
$id = null, |
| 104 |
$visibility = 0, |
| 105 |
?array $content = null |
| 106 |
) { |
| 107 |
$frank = new UserIdentityValue( 9, 'Frank' ); |
| 108 |
|
| 109 |
if ( !$content ) { |
| 110 |
$text = ""; |
| 111 |
$text .= "* page:{{PAGENAME}}!\n"; |
| 112 |
$text .= "* rev:{{REVISIONID}}!\n"; |
| 113 |
$text .= "* user:{{REVISIONUSER}}!\n"; |
| 114 |
$text .= "* time:{{REVISIONTIMESTAMP}}!\n"; |
| 115 |
$text .= "* [[Link It]]\n"; |
| 116 |
|
| 117 |
$content = [ SlotRecord::MAIN => new WikitextContent( $text ) ]; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
$mock = $this->getMockBuilder( $class ) |
| 122 |
->disableOriginalConstructor() |
| 123 |
->onlyMethods( [ |
| 124 |
'getId', |
| 125 |
'getPageId', |
| 126 |
'getPageAsLinkTarget', |
| 127 |
'getPage', |
| 128 |
'getUser', |
| 129 |
'getVisibility', |
| 130 |
'getTimestamp', |
| 131 |
] )->getMock(); |
| 132 |
|
| 133 |
$mock->method( 'getId' )->willReturn( $id ); |
| 134 |
$mock->method( 'getPageId' )->willReturn( $page->getId() ); |
| 135 |
$mock->method( 'getPageAsLinkTarget' )->willReturn( TitleValue::castPageToLinkTarget( $page ) ); |
| 136 |
$mock->method( 'getPage' )->willReturn( $page ); |
| 137 |
$mock->method( 'getUser' )->willReturn( $frank ); |
| 138 |
$mock->method( 'getVisibility' )->willReturn( $visibility ); |
| 139 |
$mock->method( 'getTimestamp' )->willReturn( '20180101000003' ); |
| 140 |
|
| 141 |
|
| 142 |
$mockAccess = TestingAccessWrapper::newFromObject( $mock ); |
| 143 |
$mockAccess->mSlots = new MutableRevisionSlots(); |
| 144 |
|
| 145 |
foreach ( $content as $role => $cnt ) { |
| 146 |
$mockAccess->mSlots->setContent( $role, $cnt ); |
| 147 |
} |
| 148 |
|
| 149 |
return $mock; |
| 150 |
} |
| 151 |
|
| 152 |
public function testConstructorInvalidArguments() { |
| 153 |
$rev = $this->getMockRevision( |
| 154 |
RevisionStoreRecord::class, |
| 155 |
PageIdentityValue::localIdentity( 0, NS_MAIN, __METHOD__ ) |
| 156 |
); |
| 157 |
$options = ParserOptions::newFromAnon(); |
| 158 |
|
| 159 |
$this->expectException( InvalidArgumentException::class ); |
| 160 |
$this->expectExceptionMessage( |
| 161 |
'User must be specified when setting audience to FOR_THIS_USER' |
| 162 |
); |
| 163 |
$rr = new RenderedRevision( |
| 164 |
$rev, |
| 165 |
$options, |
| 166 |
$this->contentRenderer, |
| 167 |
$this->combinerCallback, |
| 168 |
RevisionRecord::FOR_THIS_USER |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
public function testGetRevisionParserOutput_new() { |
| 173 |
$rev = $this->getMockRevision( |
| 174 |
RevisionStoreRecord::class, |
| 175 |
PageIdentityValue::localIdentity( 0, NS_MAIN, 'RenderTestPage' ) |
| 176 |
); |
| 177 |
|
| 178 |
$options = ParserOptions::newFromAnon(); |
| 179 |
$rr = new RenderedRevision( |
| 180 |
$rev, |
| 181 |
$options, |
| 182 |
$this->contentRenderer, |
| 183 |
$this->combinerCallback |
| 184 |
); |
| 185 |
|
| 186 |
$this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' ); |
| 187 |
|
| 188 |
$this->assertSame( $rev, $rr->getRevision() ); |
| 189 |
$this->assertSame( $options, $rr->getOptions() ); |
| 190 |
|
| 191 |
$html = $rr->getRevisionParserOutput()->getRawText(); |
| 192 |
|
| 193 |
$this->assertStringContainsString( 'page:RenderTestPage!', $html ); |
| 194 |
$this->assertStringContainsString( 'user:Frank!', $html ); |
| 195 |
$this->assertStringContainsString( 'time:20180101000003!', $html ); |
| 196 |
} |
| 197 |
|
| 198 |
public function testGetRevisionParserOutput_previewWithSelfTransclusion() { |
| 199 |
$title = PageIdentityValue::localIdentity( 0, NS_MAIN, __METHOD__ ); |
| 200 |
$name = $this->getServiceContainer()->getTitleFormatter()->getPrefixedText( $title ); |
| 201 |
|
| 202 |
$text = "(ONE)<includeonly>(TWO)</includeonly><noinclude>#{{:$name}}#</noinclude>"; |
| 203 |
|
| 204 |
$content = [ |
| 205 |
SlotRecord::MAIN => new WikitextContent( $text ) |
| 206 |
]; |
| 207 |
|
| 208 |
$rev = $this->getMockRevision( RevisionStoreRecord::class, $title, null, 0, $content ); |
| 209 |
|
| 210 |
$options = ParserOptions::newFromAnon(); |
| 211 |
$rr = new RenderedRevision( |
| 212 |
$rev, |
| 213 |
$options, |
| 214 |
$this->contentRenderer, |
| 215 |
$this->combinerCallback |
| 216 |
); |
| 217 |
|
| 218 |
$html = $rr->getRevisionParserOutput()->getRawText(); |
| 219 |
$this->assertStringContainsString( '(ONE)#(ONE)(TWO)#', $html ); |
| 220 |
} |
| 221 |
|
| 222 |
public function testGetRevisionParserOutput_current() { |
| 223 |
$rev = $this->getMockRevision( |
| 224 |
RevisionStoreRecord::class, |
| 225 |
PageIdentityValue::localIdentity( 0, NS_MAIN, 'RenderTestPage' ), |
| 226 |
21 |
| 227 |
); |
| 228 |
|
| 229 |
$options = ParserOptions::newFromAnon(); |
| 230 |
$rr = new RenderedRevision( |
| 231 |
$rev, |
| 232 |
$options, |
| 233 |
$this->contentRenderer, |
| 234 |
$this->combinerCallback |
| 235 |
); |
| 236 |
|
| 237 |
$this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' ); |
| 238 |
|
| 239 |
$this->assertSame( $rev, $rr->getRevision() ); |
| 240 |
$this->assertSame( $options, $rr->getOptions() ); |
| 241 |
|
| 242 |
$html = $rr->getRevisionParserOutput()->getRawText(); |
| 243 |
|
| 244 |
$this->assertStringContainsString( 'page:RenderTestPage!', $html ); |
| 245 |
$this->assertStringContainsString( 'rev:21!', $html ); |
| 246 |
$this->assertStringContainsString( 'user:Frank!', $html ); |
| 247 |
$this->assertStringContainsString( 'time:20180101000003!', $html ); |
| 248 |
|
| 249 |
$this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getRawText() ); |
| 250 |
} |
| 251 |
|
| 252 |
public function testGetRevisionParserOutput_old() { |
| 253 |
$rev = $this->getMockRevision( |
| 254 |
RevisionStoreRecord::class, |
| 255 |
PageIdentityValue::localIdentity( 7, NS_MAIN, 'RenderTestPage' ), |
| 256 |
11 |
| 257 |
); |
| 258 |
|
| 259 |
$options = ParserOptions::newFromAnon(); |
| 260 |
$rr = new RenderedRevision( |
| 261 |
$rev, |
| 262 |
$options, |
| 263 |
$this->contentRenderer, |
| 264 |
$this->combinerCallback |
| 265 |
); |
| 266 |
|
| 267 |
$this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' ); |
| 268 |
|
| 269 |
$this->assertSame( $rev, $rr->getRevision() ); |
| 270 |
$this->assertSame( $options, $rr->getOptions() ); |
| 271 |
|
| 272 |
$html = $rr->getRevisionParserOutput()->getRawText(); |
| 273 |
|
| 274 |
$this->assertStringContainsString( 'page:RenderTestPage!', $html ); |
| 275 |
$this->assertStringContainsString( 'rev:11!', $html ); |
| 276 |
$this->assertStringContainsString( 'user:Frank!', $html ); |
| 277 |
$this->assertStringContainsString( 'time:20180101000003!', $html ); |
| 278 |
|
| 279 |
$this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getRawText() ); |
| 280 |
} |
| 281 |
|
| 282 |
public function testGetRevisionParserOutput_archive() { |
| 283 |
$rev = $this->getMockRevision( |
| 284 |
RevisionArchiveRecord::class, |
| 285 |
PageIdentityValue::localIdentity( 7, NS_MAIN, 'RenderTestPage' ), |
| 286 |
11 |
| 287 |
); |
| 288 |
|
| 289 |
$options = ParserOptions::newFromAnon(); |
| 290 |
$rr = new RenderedRevision( |
| 291 |
$rev, |
| 292 |
$options, |
| 293 |
$this->contentRenderer, |
| 294 |
$this->combinerCallback, |
| 295 |
RevisionRecord::RAW |
| 296 |
); |
| 297 |
|
| 298 |
$this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' ); |
| 299 |
|
| 300 |
$this->assertSame( $rev, $rr->getRevision() ); |
| 301 |
$this->assertSame( $options, $rr->getOptions() ); |
| 302 |
|
| 303 |
$html = $rr->getRevisionParserOutput()->getRawText(); |
| 304 |
|
| 305 |
$this->assertStringContainsString( 'page:RenderTestPage!', $html ); |
| 306 |
$this->assertStringContainsString( 'rev:11!', $html ); |
| 307 |
$this->assertStringContainsString( 'user:Frank!', $html ); |
| 308 |
$this->assertStringContainsString( 'time:20180101000003!', $html ); |
| 309 |
|
| 310 |
$this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getRawText() ); |
| 311 |
} |
| 312 |
|
| 313 |
public function testGetRevisionParserOutput_suppressed() { |
| 314 |
$rev = $this->getMockRevision( |
| 315 |
RevisionStoreRecord::class, |
| 316 |
PageIdentityValue::localIdentity( 7, NS_MAIN, 'RenderTestPage' ), |
| 317 |
11, |
| 318 |
RevisionRecord::DELETED_TEXT |
| 319 |
); |
| 320 |
|
| 321 |
$options = ParserOptions::newFromAnon(); |
| 322 |
$rr = new RenderedRevision( |
| 323 |
$rev, |
| 324 |
$options, |
| 325 |
$this->contentRenderer, |
| 326 |
$this->combinerCallback |
| 327 |
); |
| 328 |
|
| 329 |
$this->expectException( SuppressedDataException::class ); |
| 330 |
$rr->getRevisionParserOutput(); |
| 331 |
} |
| 332 |
|
| 333 |
public function testGetRevisionParserOutput_privileged() { |
| 334 |
$rev = $this->getMockRevision( |
| 335 |
RevisionStoreRecord::class, |
| 336 |
PageIdentityValue::localIdentity( 7, NS_MAIN, 'RenderTestPage' ), |
| 337 |
11, |
| 338 |
RevisionRecord::DELETED_TEXT |
| 339 |
); |
| 340 |
|
| 341 |
$options = ParserOptions::newFromAnon(); |
| 342 |
$rr = new RenderedRevision( |
| 343 |
$rev, |
| 344 |
$options, |
| 345 |
$this->contentRenderer, |
| 346 |
$this->combinerCallback, |
| 347 |
RevisionRecord::FOR_THIS_USER, |
| 348 |
$this->mockRegisteredUltimateAuthority() |
| 349 |
); |
| 350 |
|
| 351 |
$this->assertTrue( $rr->isContentDeleted(), 'isContentDeleted' ); |
| 352 |
|
| 353 |
$this->assertSame( $rev, $rr->getRevision() ); |
| 354 |
$this->assertSame( $options, $rr->getOptions() ); |
| 355 |
|
| 356 |
$html = $rr->getRevisionParserOutput()->getRawText(); |
| 357 |
|
| 358 |
|
| 359 |
$this->assertStringContainsString( 'page:RenderTestPage!', $html ); |
| 360 |
$this->assertStringContainsString( 'rev:11!', $html ); |
| 361 |
$this->assertStringContainsString( 'user:Frank!', $html ); |
| 362 |
$this->assertStringContainsString( 'time:20180101000003!', $html ); |
| 363 |
|
| 364 |
$this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getRawText() ); |
| 365 |
} |
| 366 |
|
| 367 |
public function testGetRevisionParserOutput_raw() { |
| 368 |
$rev = $this->getMockRevision( |
| 369 |
RevisionStoreRecord::class, |
| 370 |
PageIdentityValue::localIdentity( 7, NS_MAIN, 'RenderTestPage' ), |
| 371 |
11, |
| 372 |
RevisionRecord::DELETED_TEXT |
| 373 |
); |
| 374 |
|
| 375 |
$options = ParserOptions::newFromAnon(); |
| 376 |
$rr = new RenderedRevision( |
| 377 |
$rev, |
| 378 |
$options, |
| 379 |
$this->contentRenderer, |
| 380 |
$this->combinerCallback, |
| 381 |
RevisionRecord::RAW |
| 382 |
); |
| 383 |
|
| 384 |
$this->assertTrue( $rr->isContentDeleted(), 'isContentDeleted' ); |
| 385 |
|
| 386 |
$this->assertSame( $rev, $rr->getRevision() ); |
| 387 |
$this->assertSame( $options, $rr->getOptions() ); |
| 388 |
|
| 389 |
$html = $rr->getRevisionParserOutput()->getRawText(); |
| 390 |
|
| 391 |
|
| 392 |
$this->assertStringContainsString( 'page:RenderTestPage!', $html ); |
| 393 |
$this->assertStringContainsString( 'rev:11!', $html ); |
| 394 |
$this->assertStringContainsString( 'user:Frank!', $html ); |
| 395 |
$this->assertStringContainsString( 'time:20180101000003!', $html ); |
| 396 |
|
| 397 |
$this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getRawText() ); |
| 398 |
} |
| 399 |
|
| 400 |
public function testGetRevisionParserOutput_multi() { |
| 401 |
$content = [ |
| 402 |
SlotRecord::MAIN => new WikitextContent( '[[Kittens]]' ), |
| 403 |
'aux' => new WikitextContent( '[[Goats]]' ), |
| 404 |
]; |
| 405 |
|
| 406 |
$rev = $this->getMockRevision( |
| 407 |
RevisionStoreRecord::class, |
| 408 |
PageIdentityValue::localIdentity( 7, NS_MAIN, 'RenderTestPage' ), |
| 409 |
11, |
| 410 |
0, |
| 411 |
$content ); |
| 412 |
|
| 413 |
$options = ParserOptions::newFromAnon(); |
| 414 |
$rr = new RenderedRevision( |
| 415 |
$rev, |
| 416 |
$options, |
| 417 |
$this->contentRenderer, |
| 418 |
$this->combinerCallback |
| 419 |
); |
| 420 |
|
| 421 |
$combinedOutput = $rr->getRevisionParserOutput(); |
| 422 |
$mainOutput = $rr->getSlotParserOutput( SlotRecord::MAIN ); |
| 423 |
$auxOutput = $rr->getSlotParserOutput( 'aux' ); |
| 424 |
|
| 425 |
$combinedHtml = $combinedOutput->getRawText(); |
| 426 |
$mainHtml = $mainOutput->getRawText(); |
| 427 |
$auxHtml = $auxOutput->getRawText(); |
| 428 |
|
| 429 |
$this->assertStringContainsString( 'Kittens', $mainHtml ); |
| 430 |
$this->assertStringContainsString( 'Goats', $auxHtml ); |
| 431 |
$this->assertStringNotContainsString( 'Goats', $mainHtml ); |
| 432 |
$this->assertStringNotContainsString( 'Kittens', $auxHtml ); |
| 433 |
$this->assertStringContainsString( 'Kittens', $combinedHtml ); |
| 434 |
$this->assertStringContainsString( 'Goats', $combinedHtml ); |
| 435 |
$this->assertStringContainsString( 'aux', $combinedHtml, 'slot section header' ); |
| 436 |
|
| 437 |
$combinedLinks = $combinedOutput->getLinks(); |
| 438 |
$mainLinks = $mainOutput->getLinks(); |
| 439 |
$auxLinks = $auxOutput->getLinks(); |
| 440 |
$this->assertTrue( isset( $combinedLinks[NS_MAIN]['Kittens'] ), 'links from main slot' ); |
| 441 |
$this->assertTrue( isset( $combinedLinks[NS_MAIN]['Goats'] ), 'links from aux slot' ); |
| 442 |
$this->assertFalse( isset( $mainLinks[NS_MAIN]['Goats'] ), 'no aux links in main' ); |
| 443 |
$this->assertFalse( isset( $auxLinks[NS_MAIN]['Kittens'] ), 'no main links in aux' ); |
| 444 |
} |
| 445 |
|
| 446 |
public function testGetRevisionParserOutput_incompleteNoId() { |
| 447 |
$rev = new MutableRevisionRecord( |
| 448 |
PageIdentityValue::localIdentity( 7, NS_MAIN, 'RenderTestPage' ) |
| 449 |
); |
| 450 |
|
| 451 |
$text = ""; |
| 452 |
$text .= "* page:{{PAGENAME}}!\n"; |
| 453 |
$text .= "* rev:{{REVISIONID}}!\n"; |
| 454 |
$text .= "* user:{{REVISIONUSER}}!\n"; |
| 455 |
$text .= "* time:{{REVISIONTIMESTAMP}}!\n"; |
| 456 |
|
| 457 |
$rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) ); |
| 458 |
|
| 459 |
$options = ParserOptions::newFromAnon(); |
| 460 |
$rr = new RenderedRevision( |
| 461 |
$rev, |
| 462 |
$options, |
| 463 |
$this->contentRenderer, |
| 464 |
$this->combinerCallback |
| 465 |
); |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
$html = $rr->getRevisionParserOutput()->getRawText(); |
| 470 |
|
| 471 |
$this->assertStringContainsString( 'page:RenderTestPage!', $html ); |
| 472 |
$this->assertStringContainsString( 'rev:!', $html ); |
| 473 |
$this->assertStringContainsString( 'user:!', $html ); |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
$this->assertStringContainsString( 'time:2', $html ); |
| 478 |
} |
| 479 |
|
| 480 |
public function testGetRevisionParserOutput_incompleteWithId() { |
| 481 |
$page = PageIdentityValue::localIdentity( 7, NS_MAIN, 'RenderTestPage' ); |
| 482 |
$rev = new MutableRevisionRecord( $page ); |
| 483 |
$rev->setId( 21 ); |
| 484 |
|
| 485 |
$text = ""; |
| 486 |
$text .= "* page:{{PAGENAME}}!\n"; |
| 487 |
$text .= "* rev:{{REVISIONID}}!\n"; |
| 488 |
$text .= "* user:{{REVISIONUSER}}!\n"; |
| 489 |
$text .= "* time:{{REVISIONTIMESTAMP}}!\n"; |
| 490 |
|
| 491 |
$rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) ); |
| 492 |
|
| 493 |
$actualRevision = $this->getMockRevision( |
| 494 |
RevisionStoreRecord::class, |
| 495 |
$page, |
| 496 |
21, |
| 497 |
RevisionRecord::DELETED_TEXT |
| 498 |
); |
| 499 |
|
| 500 |
$options = ParserOptions::newFromAnon(); |
| 501 |
$rr = new RenderedRevision( |
| 502 |
$rev, |
| 503 |
$options, |
| 504 |
$this->contentRenderer, |
| 505 |
$this->combinerCallback |
| 506 |
); |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
$revisionStore = $this->createMock( RevisionStore::class ); |
| 511 |
|
| 512 |
$revisionStore->expects( $this->once() ) |
| 513 |
->method( 'getKnownCurrentRevision' ) |
| 514 |
->willReturn( $actualRevision ); |
| 515 |
|
| 516 |
$this->setService( 'RevisionStore', $revisionStore ); |
| 517 |
|
| 518 |
$html = $rr->getRevisionParserOutput()->getRawText(); |
| 519 |
|
| 520 |
$this->assertStringContainsString( 'page:RenderTestPage!', $html ); |
| 521 |
$this->assertStringContainsString( 'rev:21!', $html ); |
| 522 |
$this->assertStringContainsString( 'user:Frank!', $html ); |
| 523 |
$this->assertStringContainsString( 'time:20180101000003!', $html ); |
| 524 |
} |
| 525 |
|
| 526 |
public function testSetRevisionParserOutput() { |
| 527 |
$rev = $this->getMockRevision( |
| 528 |
RevisionStoreRecord::class, |
| 529 |
PageIdentityValue::localIdentity( 3, NS_MAIN, 'RenderTestPage' ) |
| 530 |
); |
| 531 |
|
| 532 |
$options = ParserOptions::newFromAnon(); |
| 533 |
$rr = new RenderedRevision( |
| 534 |
$rev, |
| 535 |
$options, |
| 536 |
$this->contentRenderer, |
| 537 |
$this->combinerCallback |
| 538 |
); |
| 539 |
|
| 540 |
$output = new ParserOutput( 'Kittens' ); |
| 541 |
$rr->setRevisionParserOutput( $output ); |
| 542 |
|
| 543 |
$this->assertSame( $output, $rr->getRevisionParserOutput() ); |
| 544 |
$this->assertSame( 'Kittens', $rr->getRevisionParserOutput()->getRawText() ); |
| 545 |
|
| 546 |
$this->assertSame( $output, $rr->getSlotParserOutput( SlotRecord::MAIN ) ); |
| 547 |
$this->assertSame( 'Kittens', $rr->getSlotParserOutput( SlotRecord::MAIN ) |
| 548 |
->getRawText() ); |
| 549 |
} |
| 550 |
|
| 551 |
public function testNoHtml() { |
| 552 |
$content = new WikitextContent( 'whatever' ); |
| 553 |
|
| 554 |
|
| 555 |
$mockContentRenderer = $this->getMockBuilder( ContentRenderer::class ) |
| 556 |
->onlyMethods( [ 'getParserOutput' ] ) |
| 557 |
->disableOriginalConstructor() |
| 558 |
->getMock(); |
| 559 |
$mockContentRenderer->method( 'getParserOutput' ) |
| 560 |
->willReturnCallback( function ( Content $content, PageReference $page, $revId = null, |
| 561 |
?ParserOptions $options = null, $hints = [] |
| 562 |
) { |
| 563 |
if ( is_bool( $hints ) ) { |
| 564 |
$hints = [ 'generate-html' => $hints ]; |
| 565 |
} |
| 566 |
$generateHtml = $hints['generate-html'] ?? true; |
| 567 |
if ( !$generateHtml ) { |
| 568 |
return new ParserOutput( null ); |
| 569 |
} else { |
| 570 |
$this->fail( 'Should not be called with $generateHtml == true' ); |
| 571 |
return null; |
| 572 |
} |
| 573 |
} ); |
| 574 |
|
| 575 |
$rev = new MutableRevisionRecord( |
| 576 |
PageIdentityValue::localIdentity( 7, NS_MAIN, 'RenderTestPage' ) |
| 577 |
); |
| 578 |
$rev->setContent( SlotRecord::MAIN, $content ); |
| 579 |
$rev->setContent( 'aux', $content ); |
| 580 |
|
| 581 |
$options = ParserOptions::newFromAnon(); |
| 582 |
$rr = new RenderedRevision( |
| 583 |
$rev, |
| 584 |
$options, |
| 585 |
$mockContentRenderer, |
| 586 |
$this->combinerCallback |
| 587 |
); |
| 588 |
|
| 589 |
$output = $rr->getSlotParserOutput( SlotRecord::MAIN, [ 'generate-html' => false ] ); |
| 590 |
$this->assertFalse( $output->hasText(), 'hasText' ); |
| 591 |
|
| 592 |
$output = $rr->getRevisionParserOutput( [ 'generate-html' => false ] ); |
| 593 |
$this->assertFalse( $output->hasText(), 'hasText' ); |
| 594 |
} |
| 595 |
|
| 596 |
public function testUpdateRevision() { |
| 597 |
$page = PageIdentityValue::localIdentity( 7, NS_MAIN, 'RenderTestPage' ); |
| 598 |
$rev = new MutableRevisionRecord( $page ); |
| 599 |
|
| 600 |
$text = ""; |
| 601 |
$text .= "* page:{{PAGENAME}}!\n"; |
| 602 |
$text .= "* rev:{{REVISIONID}}!\n"; |
| 603 |
$text .= "* user:{{REVISIONUSER}}!\n"; |
| 604 |
$text .= "* time:{{REVISIONTIMESTAMP}}!\n"; |
| 605 |
|
| 606 |
$rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) ); |
| 607 |
$rev->setContent( 'aux', new WikitextContent( '[[Goats]]' ) ); |
| 608 |
|
| 609 |
$options = ParserOptions::newFromAnon(); |
| 610 |
$rr = new RenderedRevision( |
| 611 |
$rev, |
| 612 |
$options, |
| 613 |
$this->contentRenderer, |
| 614 |
$this->combinerCallback |
| 615 |
); |
| 616 |
|
| 617 |
$firstOutput = $rr->getRevisionParserOutput(); |
| 618 |
$mainOutput = $rr->getSlotParserOutput( SlotRecord::MAIN ); |
| 619 |
$auxOutput = $rr->getSlotParserOutput( 'aux' ); |
| 620 |
|
| 621 |
|
| 622 |
$savedRev = new MutableRevisionRecord( $page ); |
| 623 |
$savedRev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) ); |
| 624 |
$savedRev->setContent( 'aux', new WikitextContent( '[[Goats]]' ) ); |
| 625 |
$savedRev->setId( 23 ); |
| 626 |
$savedRev->setUser( new UserIdentityValue( 9, 'Frank' ) ); |
| 627 |
$savedRev->setTimestamp( '20180101000003' ); |
| 628 |
|
| 629 |
$rr->updateRevision( $savedRev ); |
| 630 |
|
| 631 |
$this->assertNotSame( $mainOutput, $rr->getSlotParserOutput( SlotRecord::MAIN ), 'Reset main' ); |
| 632 |
$this->assertSame( $auxOutput, $rr->getSlotParserOutput( 'aux' ), 'Keep aux' ); |
| 633 |
|
| 634 |
$updatedOutput = $rr->getRevisionParserOutput(); |
| 635 |
$html = $updatedOutput->getRawText(); |
| 636 |
|
| 637 |
$this->assertNotSame( $firstOutput, $updatedOutput, 'Reset merged' ); |
| 638 |
$this->assertStringContainsString( 'page:RenderTestPage!', $html ); |
| 639 |
$this->assertStringContainsString( 'rev:23!', $html ); |
| 640 |
$this->assertStringContainsString( 'user:Frank!', $html ); |
| 641 |
$this->assertStringContainsString( 'time:20180101000003!', $html ); |
| 642 |
$this->assertStringContainsString( 'Goats', $html ); |
| 643 |
|
| 644 |
$rr->updateRevision( $savedRev ); |
| 645 |
$this->assertSame( $updatedOutput, $rr->getRevisionParserOutput(), 'no more reset needed' ); |
| 646 |
} |
| 647 |
|
| 648 |
public function testUpdateRevision_revIdSet() { |
| 649 |
$page = PageIdentityValue::localIdentity( 7, NS_MAIN, 'RenderTestPage' ); |
| 650 |
$rev = new MutableRevisionRecord( $page ); |
| 651 |
$rev->setId( 123 ); |
| 652 |
$rev->setContent( SlotRecord::MAIN, new WikitextContent( 'FooBar' ) ); |
| 653 |
|
| 654 |
$options = ParserOptions::newFromAnon(); |
| 655 |
$rr = new RenderedRevision( |
| 656 |
$rev, |
| 657 |
$options, |
| 658 |
$this->contentRenderer, |
| 659 |
$this->combinerCallback |
| 660 |
); |
| 661 |
|
| 662 |
$newRev = new MutableRevisionRecord( $page ); |
| 663 |
$newRev->setId( 321 ); |
| 664 |
$newRev->setContent( SlotRecord::MAIN, new WikitextContent( 'FooBar' ) ); |
| 665 |
|
| 666 |
$this->expectException( LogicException::class ); |
| 667 |
$this->expectExceptionMessage( |
| 668 |
'RenderedRevision already has a revision with ID 123, ' . |
| 669 |
'can\'t update to revision with ID 321' |
| 670 |
); |
| 671 |
$rr->updateRevision( $newRev ); |
| 672 |
} |
| 673 |
|
| 674 |
} |