1<?php 2 3use MediaWiki\Edit\PreparedEdit; 4use MediaWiki\MediaWikiServices; 5use MediaWiki\Page\PageIdentity; 6use MediaWiki\Page\PageIdentityValue; 7use MediaWiki\Permissions\Authority; 8use MediaWiki\Revision\MutableRevisionRecord; 9use MediaWiki\Revision\RevisionRecord; 10use MediaWiki\Revision\SlotRecord; 11use MediaWiki\Storage\RevisionSlotsUpdate; 12use MediaWiki\Tests\Unit\DummyServicesTrait; 13use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; 14use PHPUnit\Framework\MockObject\MockObject; 15use Wikimedia\TestingAccessWrapper; 16 17/** 18 * @covers WikiPage 19 * @group Database 20 */ 21class WikiPageDbTest extends MediaWikiLangTestCase { 22 use DummyServicesTrait; 23 use MockAuthorityTrait; 24 25 /** @var WikiPage[] */ 26 private $pagesToDelete; 27 28 protected function setUp(): void { 29 parent::setUp(); 30 31 $this->pagesToDelete = []; 32 $this->tablesUsed = array_merge( 33 $this->tablesUsed, 34 [ 35 'page', 36 'revision', 37 'redirect', 38 'archive', 39 'category', 40 'ip_changes', 41 'text', 42 43 'slots', 44 'content', 45 'slot_roles', 46 'content_models', 47 48 'recentchanges', 49 'logging', 50 51 'page_props', 52 'pagelinks', 53 'categorylinks', 54 'langlinks', 55 'externallinks', 56 'imagelinks', 57 'templatelinks', 58 'iwlinks' 59 ] 60 ); 61 } 62 63 protected function tearDown(): void { 64 $user = $this->getTestSysop()->getUser(); 65 foreach ( $this->pagesToDelete as $p ) { 66 try { 67 if ( $p->canExist() && $p->exists() ) { 68 $p->doDeleteArticleReal( "testing done.", $user ); 69 } 70 } catch ( MWException $ex ) { 71 // fail silently 72 } 73 } 74 parent::tearDown(); 75 } 76 77 /** 78 * @param Title|string $title 79 * @param string|null $model 80 * @return WikiPage 81 */ 82 private function newPage( $title, $model = null ) { 83 if ( is_string( $title ) ) { 84 $ns = $this->getDefaultWikitextNS(); 85 $title = Title::newFromText( $title, $ns ); 86 } 87 88 $p = new WikiPage( $title ); 89 90 $this->pagesToDelete[] = $p; 91 92 return $p; 93 } 94 95 /** 96 * @param string|Title|WikiPage $page 97 * @param string|Content|Content[] $content 98 * @param int|null $model 99 * @param Authority|null $performer 100 * 101 * @return WikiPage 102 */ 103 protected function createPage( $page, $content, $model = null, Authority $performer = null ) { 104 if ( is_string( $page ) || $page instanceof Title ) { 105 $page = $this->newPage( $page, $model ); 106 } 107 108 $performer = $performer ?? $this->getTestUser()->getUser(); 109 110 if ( is_string( $content ) ) { 111 $content = ContentHandler::makeContent( $content, $page->getTitle(), $model ); 112 } 113 114 if ( !is_array( $content ) ) { 115 $content = [ 'main' => $content ]; 116 } 117 118 $updater = $page->newPageUpdater( $performer ); 119 120 foreach ( $content as $role => $cnt ) { 121 $updater->setContent( $role, $cnt ); 122 } 123 124 $updater->saveRevision( CommentStoreComment::newUnsavedComment( "testing" ) ); 125 if ( !$updater->wasSuccessful() ) { 126 $this->fail( $updater->getStatus()->getWikiText() ); 127 } 128 129 return $page; 130 } 131 132 public function testSerialization_fails() { 133 $this->expectException( LogicException::class ); 134 $page = $this->createPage( __METHOD__, __METHOD__ ); 135 serialize( $page ); 136 } 137 138 public function provideTitlesThatCannotExist() { 139 yield 'Special' => [ NS_SPECIAL, 'Recentchanges' ]; // existing special page 140 yield 'Invalid character' => [ NS_MAIN, '#' ]; // bad character 141 } 142 143 /** 144 * @dataProvider provideTitlesThatCannotExist 145 */ 146 public function testConstructionWithPageThatCannotExist( $ns, $text ) { 147 $title = Title::makeTitle( $ns, $text ); 148 $this->expectException( InvalidArgumentException::class ); 149 new WikiPage( $title ); 150 } 151 152 /** 153 * @covers WikiPage::prepareContentForEdit 154 */ 155 public function testPrepareContentForEdit() { 156 $performer = $this->mockUserAuthorityWithPermissions( 157 $this->getTestUser()->getUserIdentity(), 158 [ 'edit' ] 159 ); 160 $sysop = $this->getTestUser( [ 'sysop' ] )->getUserIdentity(); 161 162 $page = $this->createPage( __METHOD__, __METHOD__, null, $performer ); 163 $title = $page->getTitle(); 164 165 $content = ContentHandler::makeContent( 166 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam " 167 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.", 168 $title, 169 CONTENT_MODEL_WIKITEXT 170 ); 171 $content2 = ContentHandler::makeContent( 172 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. " 173 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est. ~~~~", 174 $title, 175 CONTENT_MODEL_WIKITEXT 176 ); 177 178 $edit = $page->prepareContentForEdit( $content, null, $performer->getUser(), null, false ); 179 180 $this->assertInstanceOf( 181 ParserOptions::class, 182 $edit->popts, 183 "pops" 184 ); 185 $this->assertStringContainsString( '</a>', $edit->output->getText(), "output" ); 186 $this->assertStringContainsString( 187 'consetetur sadipscing elitr', 188 $edit->output->getText(), 189 "output" 190 ); 191 192 $this->assertTrue( $content->equals( $edit->newContent ), "newContent field" ); 193 $this->assertTrue( $content->equals( $edit->pstContent ), "pstContent field" ); 194 $this->assertSame( $edit->output, $edit->output, "output field" ); 195 $this->assertSame( $edit->popts, $edit->popts, "popts field" ); 196 $this->assertSame( null, $edit->revid, "revid field" ); 197 198 // Re-using the prepared info if possible 199 $sameEdit = $page->prepareContentForEdit( $content, null, $performer->getUser(), null, false ); 200 $this->assertPreparedEditEquals( $edit, $sameEdit, 'equivalent PreparedEdit' ); 201 $this->assertSame( $edit->pstContent, $sameEdit->pstContent, 're-use output' ); 202 $this->assertSame( $edit->output, $sameEdit->output, 're-use output' ); 203 204 // Not re-using the same PreparedEdit if not possible 205 $edit2 = $page->prepareContentForEdit( $content2, null, $performer->getUser(), null, false ); 206 $this->assertPreparedEditNotEquals( $edit, $edit2 ); 207 $this->assertStringContainsString( 'At vero eos', $edit2->pstContent->serialize(), "content" ); 208 209 // Check pre-safe transform 210 $this->assertStringContainsString( '[[gubergren]]', $edit2->pstContent->serialize() ); 211 $this->assertStringNotContainsString( '~~~~', $edit2->pstContent->serialize() ); 212 213 $edit3 = $page->prepareContentForEdit( $content2, null, $sysop, null, false ); 214 $this->assertPreparedEditNotEquals( $edit2, $edit3 ); 215 216 // TODO: test with passing revision, then same without revision. 217 } 218 219 /** 220 * @covers WikiPage::doEditUpdates 221 */ 222 public function testDoEditUpdates() { 223 $user = $this->getTestUser()->getUserIdentity(); 224 225 // NOTE: if site stats get out of whack and drop below 0, 226 // that causes a DB error during tear-down. So bump the 227 // numbers high enough to not drop below 0. 228 $siteStatsUpdate = SiteStatsUpdate::factory( 229 [ 'edits' => 1000, 'articles' => 1000, 'pages' => 1000 ] 230 ); 231 $siteStatsUpdate->doUpdate(); 232 233 $page = $this->createPage( __METHOD__, __METHOD__ ); 234 235 $comment = CommentStoreComment::newUnsavedComment( __METHOD__ ); 236 237 $contentHandler = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT ); 238 // PST turns [[|foo]] into [[foo]] 239 $content = $contentHandler->unserializeContent( __METHOD__ . ' [[|foo]][[bar]]' ); 240 241 $revRecord = new MutableRevisionRecord( $page->getTitle() ); 242 $revRecord->setContent( SlotRecord::MAIN, $content ); 243 $revRecord->setUser( $user ); 244 $revRecord->setTimestamp( '20170707040404' ); 245 $revRecord->setPageId( $page->getId() ); 246 $revRecord->setId( 9989 ); 247 $revRecord->setMinorEdit( true ); 248 $revRecord->setComment( $comment ); 249 250 $page->doEditUpdates( $revRecord, $user ); 251 252 // TODO: test various options; needs temporary hooks 253 254 $dbr = wfGetDB( DB_REPLICA ); 255 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $page->getId() ] ); 256 $n = $res->numRows(); 257 $res->free(); 258 259 $this->assertSame( 1, $n, 'pagelinks should contain only one link if PST was not applied' ); 260 } 261 262 /** 263 * @covers WikiPage::doEditContent 264 * @covers WikiPage::doUserEditContent 265 */ 266 public function testDoEditContent() { 267 $this->hideDeprecated( 'WikiPage::doEditContent' ); 268 269 // We set $wgUser to a User we create to avoid dealing with StubGlobalUser 270 // deprecation, etc. The entire method is deprecated anyway. 271 $user = $this->getTestSysop()->getUser(); 272 273 // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgUser 274 global $wgUser; 275 $originalUser = $wgUser; 276 $wgUser = $user; 277 278 // NOTE: Test that Editing also works with a fragment title! 279 $page = $this->newPage( __METHOD__ . '#Fragment' ); 280 $title = $page->getTitle(); 281 282 $content = ContentHandler::makeContent( 283 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam " 284 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.", 285 $title, 286 CONTENT_MODEL_WIKITEXT 287 ); 288 289 $status = $page->doEditContent( $content, "[[testing]] 1", EDIT_NEW ); 290 291 $this->assertTrue( $status->isOK(), 'OK' ); 292 $this->assertTrue( $status->value['new'], 'new' ); 293 $this->assertNotNull( $status->value['revision-record'], 'revision-record' ); 294 295 $statusRevRecord = $status->value['revision-record']; 296 $this->assertSame( $statusRevRecord->getId(), $page->getRevisionRecord()->getId() ); 297 $this->assertSame( $statusRevRecord->getSha1(), $page->getRevisionRecord()->getSha1() ); 298 $this->assertTrue( 299 $statusRevRecord->getContent( SlotRecord::MAIN )->equals( $content ), 300 'equals' 301 ); 302 303 $this->assertSame( $statusRevRecord->getId(), $page->getLatest() ); 304 $this->assertSame( $statusRevRecord->getTimestamp(), $page->getTimestamp() ); 305 $this->assertSame( $statusRevRecord->getTimestamp(), $page->getTouched() ); 306 307 $content = ContentHandler::makeContent( 308 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. " 309 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est. ~~~~", 310 $title, 311 CONTENT_MODEL_WIKITEXT 312 ); 313 314 $status = $page->doEditContent( $content, "testing 2", EDIT_UPDATE ); 315 $this->assertTrue( $status->isOK(), 'OK' ); 316 $this->assertFalse( $status->value['new'], 'new' ); 317 $this->assertNotNull( $status->value['revision-record'], 'revision-record' ); 318 319 $statusRevRecord = $status->value['revision-record']; 320 $this->assertSame( $statusRevRecord->getId(), $page->getRevisionRecord()->getId() ); 321 $this->assertSame( $statusRevRecord->getSha1(), $page->getRevisionRecord()->getSha1() ); 322 $this->assertFalse( 323 $statusRevRecord->getContent( SlotRecord::MAIN )->equals( $content ), 324 'not equals (PST must substitute signature)' 325 ); 326 327 $revRecord = $page->getRevisionRecord(); 328 $recentChange = $this->getServiceContainer() 329 ->getRevisionStore() 330 ->getRecentChange( $revRecord ); 331 $this->assertNotNull( $recentChange ); 332 $this->assertSame( $revRecord->getId(), (int)$recentChange->getAttribute( 'rc_this_oldid' ) ); 333 334 # ------------------------ 335 $page = new WikiPage( $title ); 336 337 $retrieved = $page->getContent(); 338 $newText = $retrieved->serialize(); 339 $this->assertStringContainsString( '[[gubergren]]', $newText, 'New text must replace old text.' ); 340 $this->assertStringNotContainsString( '[[Lorem ipsum]]', $newText, 'New text must replace old text.' ); 341 $this->assertStringNotContainsString( '~~~~', $newText, 'PST must substitute signature.' ); 342 $this->assertStringContainsString( $user->getName(), $newText, 343 'Must fall back to $wgUser when no user has been specified.' ); 344 345 // Reset so that other tests would still fail if interacting with $wgUser 346 $wgUser = $originalUser; 347 } 348 349 /** 350 * @covers WikiPage::doUserEditContent 351 * @covers WikiPage::prepareContentForEdit 352 */ 353 public function testDoUserEditContent() { 354 $this->setMwGlobals( 'wgPageCreationLog', true ); 355 356 $page = $this->newPage( __METHOD__ ); 357 $title = $page->getTitle(); 358 359 $user1 = $this->getTestUser()->getUser(); 360 // Use the confirmed group for user2 to make sure the user is different 361 $user2 = $this->getTestUser( [ 'confirmed' ] )->getUser(); 362 363 $content = ContentHandler::makeContent( 364 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam " 365 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.", 366 $title, 367 CONTENT_MODEL_WIKITEXT 368 ); 369 370 $preparedEditBefore = $page->prepareContentForEdit( $content, null, $user1 ); 371 372 $status = $page->doUserEditContent( $content, $user1, "[[testing]] 1", EDIT_NEW ); 373 374 $this->assertTrue( $status->isOK(), 'OK' ); 375 $this->assertTrue( $status->value['new'], 'new' ); 376 $this->assertNotNull( $status->value['revision-record'], 'revision-record' ); 377 378 $statusRevRecord = $status->value['revision-record']; 379 $this->assertSame( $statusRevRecord->getId(), $page->getRevisionRecord()->getId() ); 380 $this->assertSame( $statusRevRecord->getSha1(), $page->getRevisionRecord()->getSha1() ); 381 $this->assertTrue( 382 $statusRevRecord->getContent( SlotRecord::MAIN )->equals( $content ), 383 'equals' 384 ); 385 386 $revRecord = $page->getRevisionRecord(); 387 $recentChange = $this->getServiceContainer() 388 ->getRevisionStore() 389 ->getRecentChange( $revRecord ); 390 $preparedEditAfter = $page->prepareContentForEdit( $content, $revRecord, $user1 ); 391 392 $this->assertNotNull( $recentChange ); 393 $this->assertSame( $revRecord->getId(), (int)$recentChange->getAttribute( 'rc_this_oldid' ) ); 394 395 // make sure that cached ParserOutput gets re-used throughout 396 $this->assertSame( $preparedEditBefore->output, $preparedEditAfter->output ); 397 398 $id = $page->getId(); 399 400 // Test page creation logging 401 $this->assertSelect( 402 'logging', 403 [ 'log_type', 'log_action' ], 404 [ 'log_page' => $id ], 405 [ [ 'create', 'create' ] ] 406 ); 407 408 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" ); 409 $this->assertTrue( $id > 0, "WikiPage should have new page id" ); 410 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" ); 411 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" ); 412 413 # ------------------------ 414 $dbr = wfGetDB( DB_REPLICA ); 415 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] ); 416 $n = $res->numRows(); 417 $res->free(); 418 419 $this->assertSame( 1, $n, 'pagelinks should contain one link from the page' ); 420 421 # ------------------------ 422 $page = new WikiPage( $title ); 423 424 $retrieved = $page->getContent(); 425 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' ); 426 427 # ------------------------ 428 $page = new WikiPage( $title ); 429 430 // try null edit, with a different user 431 $status = $page->doUserEditContent( $content, $user2, 'This changes nothing', EDIT_UPDATE, false ); 432 $this->assertTrue( $status->isOK(), 'OK' ); 433 $this->assertFalse( $status->value['new'], 'new' ); 434 $this->assertNull( $status->value['revision-record'], 'revision-record' ); 435 $this->assertNotNull( $page->getRevisionRecord() ); 436 $this->assertTrue( 437 $page->getRevisionRecord()->getContent( SlotRecord::MAIN )->equals( $content ), 438 'equals' 439 ); 440 441 # ------------------------ 442 $content = ContentHandler::makeContent( 443 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. " 444 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est. ~~~~", 445 $title, 446 CONTENT_MODEL_WIKITEXT 447 ); 448 449 $status = $page->doUserEditContent( $content, $user1, "testing 2", EDIT_UPDATE ); 450 $this->assertTrue( $status->isOK(), 'OK' ); 451 $this->assertFalse( $status->value['new'], 'new' ); 452 $this->assertNotNull( $status->value['revision-record'], 'revision-record' ); 453 $statusRevRecord = $status->value['revision-record']; 454 $this->assertSame( $statusRevRecord->getId(), $page->getRevisionRecord()->getId() ); 455 $this->assertSame( $statusRevRecord->getSha1(), $page->getRevisionRecord()->getSha1() ); 456 $this->assertFalse( 457 $statusRevRecord->getContent( SlotRecord::MAIN )->equals( $content ), 458 'not equals (PST must substitute signature)' 459 ); 460 461 $revRecord = $page->getRevisionRecord(); 462 $recentChange = $this->getServiceContainer() 463 ->getRevisionStore() 464 ->getRecentChange( $revRecord ); 465 $this->assertNotNull( $recentChange ); 466 $this->assertSame( $revRecord->getId(), (int)$recentChange->getAttribute( 'rc_this_oldid' ) ); 467 468 # ------------------------ 469 $page = new WikiPage( $title ); 470 471 $retrieved = $page->getContent(); 472 $newText = $retrieved->serialize(); 473 $this->assertStringContainsString( '[[gubergren]]', $newText, 'New text must replace old text.' ); 474 $this->assertStringNotContainsString( '~~~~', $newText, 'PST must substitute signature.' ); 475 476 # ------------------------ 477 $dbr = wfGetDB( DB_REPLICA ); 478 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] ); 479 $n = $res->numRows(); 480 $res->free(); 481 482 // two in page text and two in signature 483 $this->assertEquals( 4, $n, 'pagelinks should contain four links from the page' ); 484 } 485 486 public function provideNonPageTitle() { 487 yield 'bad case and char' => [ Title::makeTitle( NS_MAIN, 'lower case and bad # char' ) ]; 488 yield 'empty' => [ Title::makeTitle( NS_MAIN, '' ) ]; 489 yield 'special' => [ Title::makeTitle( NS_SPECIAL, 'Dummy' ) ]; 490 yield 'relative' => [ Title::makeTitle( NS_MAIN, '', '#section' ) ]; 491 yield 'interwiki' => [ Title::makeTitle( NS_MAIN, 'Foo', '', 'acme' ) ]; 492 } 493 494 /** 495 * @dataProvider provideNonPageTitle 496 * @covers WikiPage::doUserEditContent 497 */ 498 public function testDoUserEditContent_bad_page( $title ) { 499 $user1 = $this->getTestUser()->getUser(); 500 501 $content = ContentHandler::makeContent( 502 "Yadda yadda", 503 $title, 504 CONTENT_MODEL_WIKITEXT 505 ); 506 507 $this->filterDeprecated( '/WikiPage constructed on a Title that cannot exist as a page/' ); 508 try { 509 $page = $this->newPage( $title ); 510 $page->doUserEditContent( $content, $user1, "[[testing]] 1", EDIT_NEW ); 511 } catch ( Exception $ex ) { 512 // Throwing is an acceptable way to react to an invalid title, 513 // as long as no garbage is written to the database. 514 } 515 516 $row = $this->db->selectRow( 517 'page', 518 '*', 519 [ 520 'page_namespace' => $title->getNamespace(), 521 'page_title' => $title->getDBkey() 522 ] 523 ); 524 525 $this->assertFalse( $row ); 526 } 527 528 /** 529 * @covers WikiPage::doUserEditContent 530 */ 531 public function testDoUserEditContent_twice() { 532 $title = Title::newFromText( __METHOD__ ); 533 $page = WikiPage::factory( $title ); 534 $content = ContentHandler::makeContent( '$1 van $2', $title ); 535 536 $user = $this->getTestUser()->getUser(); 537 538 // Make sure we can do the exact same save twice. 539 // This tests checks that internal caches are reset as appropriate. 540 $status1 = $page->doUserEditContent( $content, $user, __METHOD__ ); 541 $status2 = $page->doUserEditContent( $content, $user, __METHOD__ ); 542 543 $this->assertTrue( $status1->isOK(), 'OK' ); 544 $this->assertTrue( $status2->isOK(), 'OK' ); 545 546 $this->assertTrue( isset( $status1->value['revision-record'] ), 'OK' ); 547 $this->assertFalse( isset( $status2->value['revision-record'] ), 'OK' ); 548 } 549 550 /** 551 * Undeletion is covered in PageArchiveTest::testUndeleteRevisions() 552 * TODO: Revision deletion 553 * 554 * @covers WikiPage::doDeleteArticleReal 555 */ 556 public function testDoDeleteArticleReal() { 557 $page = $this->createPage( 558 __METHOD__, 559 "[[original text]] foo", 560 CONTENT_MODEL_WIKITEXT 561 ); 562 $id = $page->getId(); 563 $user = $this->getTestSysop()->getUser(); 564 565 $reason = "testing deletion"; 566 $status = $page->doDeleteArticleReal( $reason, $user ); 567 568 $this->assertFalse( 569 $page->getTitle()->getArticleID() > 0, 570 "Title object should now have page id 0" 571 ); 572 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" ); 573 $this->assertFalse( 574 $page->exists(), 575 "WikiPage::exists should return false after page was deleted" 576 ); 577 $this->assertNull( 578 $page->getContent(), 579 "WikiPage::getContent should return null after page was deleted" 580 ); 581 582 $t = Title::newFromText( $page->getTitle()->getPrefixedText() ); 583 $this->assertFalse( 584 $t->exists(), 585 "Title::exists should return false after page was deleted" 586 ); 587 588 // Run the job queue 589 $this->runJobs(); 590 591 # ------------------------ 592 $dbr = wfGetDB( DB_REPLICA ); 593 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] ); 594 $n = $res->numRows(); 595 $res->free(); 596 597 $this->assertSame( 0, $n, 'pagelinks should contain no more links from the page' ); 598 599 // Test deletion logging 600 $logId = $status->getValue(); 601 $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'log_comment' ); 602 $this->assertSelect( 603 [ 'logging' ] + $commentQuery['tables'], /* table */ 604 [ 605 'log_type', 606 'log_action', 607 'log_comment' => $commentQuery['fields']['log_comment_text'], 608 'log_actor', 609 'log_namespace', 610 'log_title', 611 ], 612 [ 'log_id' => $logId ], 613 [ [ 614 'delete', 615 'delete', 616 $reason, 617 (string)$user->getActorId(), 618 (string)$page->getTitle()->getNamespace(), 619 $page->getTitle()->getDBkey(), 620 ] ], 621 [], 622 $commentQuery['joins'] 623 ); 624 } 625 626 /** 627 * TODO: Test more stuff about suppression. 628 * 629 * @covers WikiPage::doDeleteArticleReal 630 */ 631 public function testDoDeleteArticleReal_suppress() { 632 $page = $this->createPage( 633 __METHOD__, 634 "[[original text]] foo", 635 CONTENT_MODEL_WIKITEXT 636 ); 637 638 $user = $this->getTestSysop()->getUser(); 639 $status = $page->doDeleteArticleReal( 640 /* reason */ "testing deletion", 641 $user, 642 /* suppress */ true 643 ); 644 645 // Test suppression logging 646 $logId = $status->getValue(); 647 $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'log_comment' ); 648 $this->assertSelect( 649 [ 'logging' ] + $commentQuery['tables'], /* table */ 650 [ 651 'log_type', 652 'log_action', 653 'log_comment' => $commentQuery['fields']['log_comment_text'], 654 'log_actor', 655 'log_namespace', 656 'log_title', 657 ], 658 [ 'log_id' => $logId ], 659 [ [ 660 'suppress', 661 'delete', 662 'testing deletion', 663 (string)$user->getActorId(), 664 (string)$page->getTitle()->getNamespace(), 665 $page->getTitle()->getDBkey(), 666 ] ], 667 [], 668 $commentQuery['joins'] 669 ); 670 671 $archive = new PageArchive( $page->getTitle(), MediaWikiServices::getInstance()->getMainConfig() ); 672 $archivedRevs = $archive->listRevisions(); 673 if ( !$archivedRevs || $archivedRevs->numRows() !== 1 ) { 674 $this->fail( 'Unexpected number of archived revisions' ); 675 } 676 $archivedRev = MediaWikiServices::getInstance()->getRevisionStore() 677 ->newRevisionFromArchiveRow( $archivedRevs->current() ); 678 679 $this->assertNull( 680 $archivedRev->getContent( SlotRecord::MAIN, RevisionRecord::FOR_PUBLIC ), 681 "Archived content should be null after the page was suppressed for general users" 682 ); 683 684 $this->assertNull( 685 $archivedRev->getContent( 686 SlotRecord::MAIN, 687 RevisionRecord::FOR_THIS_USER, 688 $this->getTestUser()->getUser() 689 ), 690 "Archived content should be null after the page was suppressed for individual users" 691 ); 692 693 $this->assertNull( 694 $archivedRev->getContent( SlotRecord::MAIN, RevisionRecord::FOR_THIS_USER, $user ), 695 "Archived content should be null after the page was suppressed even for a sysop" 696 ); 697 } 698 699 /** 700 * @covers WikiPage::doDeleteUpdates 701 */ 702 public function testDoDeleteUpdates() { 703 $this->hideDeprecated( 'WikiPage::doDeleteUpdates' ); 704 $user = $this->getTestUser()->getUserIdentity(); 705 $page = $this->createPage( 706 __METHOD__, 707 "[[original text]] foo", 708 CONTENT_MODEL_WIKITEXT 709 ); 710 $id = $page->getId(); 711 $page->loadPageData(); // make sure the current revision is cached. 712 713 // Similar to MovePage logic 714 wfGetDB( DB_PRIMARY )->delete( 'page', [ 'page_id' => $id ], __METHOD__ ); 715 $page->doDeleteUpdates( 716 $page->getId(), 717 $page->getContent(), 718 $page->getRevisionRecord(), 719 $user 720 ); 721 722 // Run the job queue 723 $this->runJobs(); 724 725 # ------------------------ 726 $dbr = wfGetDB( DB_REPLICA ); 727 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] ); 728 $n = $res->numRows(); 729 $res->free(); 730 731 $this->assertSame( 0, $n, 'pagelinks should contain no more links from the page' ); 732 } 733 734 /** 735 * @param string $name 736 * 737 * @return ContentHandler 738 */ 739 protected function defineMockContentModelForUpdateTesting( $name ) { 740 /** @var ContentHandler|MockObject $handler */ 741 $handler = $this->getMockBuilder( TextContentHandler::class ) 742 ->setConstructorArgs( [ $name ] ) 743 ->onlyMethods( 744 [ 'getSecondaryDataUpdates', 'getDeletionUpdates', 'unserializeContent' ] 745 ) 746 ->getMock(); 747 748 $dataUpdate = new MWCallableUpdate( 'time' ); 749 $dataUpdate->_name = "$name data update"; 750 751 $deletionUpdate = new MWCallableUpdate( 'time' ); 752 $deletionUpdate->_name = "$name deletion update"; 753 754 $handler->method( 'getSecondaryDataUpdates' )->willReturn( [ $dataUpdate ] ); 755 $handler->method( 'getDeletionUpdates' )->willReturn( [ $deletionUpdate ] ); 756 $handler->method( 'unserializeContent' )->willReturnCallback( 757 function ( $text ) use ( $handler ) { 758 return $this->createMockContent( $handler, $text ); 759 } 760 ); 761 762 $this->mergeMwGlobalArrayValue( 763 'wgContentHandlers', [ 764 $name => static function () use ( $handler ){ 765 return $handler; 766 } 767 ] 768 ); 769 770 return $handler; 771 } 772 773 /** 774 * @param ContentHandler $handler 775 * @param string $text 776 * 777 * @return Content 778 */ 779 protected function createMockContent( ContentHandler $handler, $text ) { 780 /** @var Content|MockObject $content */ 781 $content = $this->getMockBuilder( TextContent::class ) 782 ->setConstructorArgs( [ $text ] ) 783 ->onlyMethods( [ 'getModel', 'getContentHandler' ] ) 784 ->getMock(); 785 786 $content->method( 'getModel' )->willReturn( $handler->getModelID() ); 787 $content->method( 'getContentHandler' )->willReturn( $handler ); 788 789 return $content; 790 } 791 792 public function testGetDeletionUpdates() { 793 $this->hideDeprecated( 'WikiPage::getDeletionUpdates' ); 794 $m1 = $this->defineMockContentModelForUpdateTesting( 'M1' ); 795 796 $mainContent1 = $this->createMockContent( $m1, 'main 1' ); 797 798 $page = new WikiPage( Title::newFromText( __METHOD__ ) ); 799 $page = $this->createPage( 800 $page, 801 [ 'main' => $mainContent1 ] 802 ); 803 804 $dataUpdates = $page->getDeletionUpdates( $page->getRevisionRecord() ); 805 $this->assertNotEmpty( $dataUpdates ); 806 807 $updateNames = array_map( static function ( $du ) { 808 return $du->_name ?? get_class( $du ); 809 }, $dataUpdates ); 810 811 $this->assertContains( LinksDeletionUpdate::class, $updateNames ); 812 $this->assertContains( 'M1 deletion update', $updateNames ); 813 } 814 815 /** 816 * @covers WikiPage::getContent 817 */ 818 public function testGetContent() { 819 $page = $this->newPage( __METHOD__ ); 820 821 $content = $page->getContent(); 822 $this->assertNull( $content ); 823 824 # ----------------- 825 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT ); 826 827 $content = $page->getContent(); 828 $this->assertEquals( "some text", $content->getText() ); 829 } 830 831 /** 832 * @covers WikiPage::exists 833 */ 834 public function testExists() { 835 $page = $this->newPage( __METHOD__ ); 836 $this->assertFalse( $page->exists() ); 837 838 # ----------------- 839 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT ); 840 $this->assertTrue( $page->exists() ); 841 842 $page = new WikiPage( $page->getTitle() ); 843 $this->assertTrue( $page->exists() ); 844 845 # ----------------- 846 $page->doDeleteArticleReal( "done testing", $this->getTestSysop()->getUser() ); 847 $this->assertFalse( $page->exists() ); 848 849 $page = new WikiPage( $page->getTitle() ); 850 $this->assertFalse( $page->exists() ); 851 } 852 853 public function provideHasViewableContent() { 854 return [ 855 [ 'WikiPageTest_testHasViewableContent', false, true ], 856 [ 'MediaWiki:WikiPageTest_testHasViewableContent', false ], 857 [ 'MediaWiki:help', true ], 858 ]; 859 } 860 861 /** 862 * @dataProvider provideHasViewableContent 863 * @covers WikiPage::hasViewableContent 864 */ 865 public function testHasViewableContent( $title, $viewable, $create = false ) { 866 $page = $this->newPage( $title ); 867 $this->assertEquals( $viewable, $page->hasViewableContent() ); 868 869 if ( $create ) { 870 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT ); 871 $this->assertTrue( $page->hasViewableContent() ); 872 873 $page = new WikiPage( $page->getTitle() ); 874 $this->assertTrue( $page->hasViewableContent() ); 875 } 876 } 877 878 public function provideGetRedirectTarget() { 879 return [ 880 [ 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT, "hello world", null ], 881 [ 882 'WikiPageTest_testGetRedirectTarget_2', 883 CONTENT_MODEL_WIKITEXT, 884 "#REDIRECT [[hello world]]", 885 "Hello world" 886 ], 887 // The below added to protect against Media namespace 888 // redirects which throw a fatal: (T203942) 889 [ 890 'WikiPageTest_testGetRedirectTarget_3', 891 CONTENT_MODEL_WIKITEXT, 892 "#REDIRECT [[Media:hello_world]]", 893 "File:Hello world" 894 ], 895 // Test fragments longer than 255 bytes (T207876) 896 [ 897 'WikiPageTest_testGetRedirectTarget_4', 898 CONTENT_MODEL_WIKITEXT, 899 // phpcs:ignore Generic.Files.LineLength 900 '#REDIRECT [[Foobar#]]', 901 // phpcs:ignore Generic.Files.LineLength 902 'Foobar#...' 903 ] 904 ]; 905 } 906 907 /** 908 * @dataProvider provideGetRedirectTarget 909 * @covers WikiPage::getRedirectTarget 910 */ 911 public function testGetRedirectTarget( $title, $model, $text, $target ) { 912 $this->setMwGlobals( [ 913 'wgCapitalLinks' => true, 914 ] ); 915 916 $page = $this->createPage( $title, $text, $model ); 917 918 # sanity check, because this test seems to fail for no reason for some people. 919 $c = $page->getContent(); 920 $this->assertEquals( WikitextContent::class, get_class( $c ) ); 921 922 # now, test the actual redirect 923 $t = $page->getRedirectTarget(); 924 $this->assertEquals( $target, $t ? $t->getFullText() : null ); 925 } 926 927 /** 928 * @dataProvider provideGetRedirectTarget 929 * @covers WikiPage::isRedirect 930 */ 931 public function testIsRedirect( $title, $model, $text, $target ) { 932 $page = $this->createPage( $title, $text, $model ); 933 $this->assertEquals( $target !== null, $page->isRedirect() ); 934 } 935 936 public function provideIsCountable() { 937 return [ 938 939 // any 940 [ 'WikiPageTest_testIsCountable', 941 CONTENT_MODEL_WIKITEXT, 942 '', 943 'any', 944 true 945 ], 946 [ 'WikiPageTest_testIsCountable', 947 CONTENT_MODEL_WIKITEXT, 948 'Foo', 949 'any', 950 true 951 ], 952 953 // link 954 [ 'WikiPageTest_testIsCountable', 955 CONTENT_MODEL_WIKITEXT, 956 'Foo', 957 'link', 958 false 959 ], 960 [ 'WikiPageTest_testIsCountable', 961 CONTENT_MODEL_WIKITEXT, 962 'Foo [[bar]]', 963 'link', 964 true 965 ], 966 967 // redirects 968 [ 'WikiPageTest_testIsCountable', 969 CONTENT_MODEL_WIKITEXT, 970 '#REDIRECT [[bar]]', 971 'any', 972 false 973 ], 974 [ 'WikiPageTest_testIsCountable', 975 CONTENT_MODEL_WIKITEXT, 976 '#REDIRECT [[bar]]', 977 'link', 978 false 979 ], 980 981 // not a content namespace 982 [ 'Talk:WikiPageTest_testIsCountable', 983 CONTENT_MODEL_WIKITEXT, 984 'Foo', 985 'any', 986 false 987 ], 988 [ 'Talk:WikiPageTest_testIsCountable', 989 CONTENT_MODEL_WIKITEXT, 990 'Foo [[bar]]', 991 'link', 992 false 993 ], 994 995 // not a content namespace, different model 996 [ 'MediaWiki:WikiPageTest_testIsCountable.js', 997 null, 998 'Foo', 999 'any', 1000 false 1001 ], 1002 [ 'MediaWiki:WikiPageTest_testIsCountable.js', 1003 null, 1004 'Foo [[bar]]', 1005 'link', 1006 false 1007 ], 1008 ]; 1009 } 1010 1011 /** 1012 * @dataProvider provideIsCountable 1013 * @covers WikiPage::isCountable 1014 */ 1015 public function testIsCountable( $title, $model, $text, $mode, $expected ) { 1016 $this->hideDeprecated( 'WikiPage::prepareContentForEdit without a UserIdentity' ); 1017 $this->setMwGlobals( 'wgArticleCountMethod', $mode ); 1018 1019 $title = Title::newFromText( $title ); 1020 1021 $page = $this->createPage( $title, $text, $model ); 1022 1023 $editInfo = $page->prepareContentForEdit( $page->getContent() ); 1024 1025 $v = $page->isCountable(); 1026 $w = $page->isCountable( $editInfo ); 1027 1028 $this->assertEquals( 1029 $expected, 1030 $v, 1031 "isCountable( null ) returned unexpected value " . var_export( $v, true ) 1032 . " instead of " . var_export( $expected, true ) 1033 . " in mode `$mode` for text \"$text\"" 1034 ); 1035 1036 $this->assertEquals( 1037 $expected, 1038 $w, 1039 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true ) 1040 . " instead of " . var_export( $expected, true ) 1041 . " in mode `$mode` for text \"$text\"" 1042 ); 1043 } 1044 1045 public function provideGetParserOutput() { 1046 return [ 1047 [ 1048 CONTENT_MODEL_WIKITEXT, 1049 "hello ''world''\n", 1050 "<div class=\"mw-parser-output\"><p>hello <i>world</i></p></div>" 1051 ], 1052 // @todo more...? 1053 ]; 1054 } 1055 1056 /** 1057 * @dataProvider provideGetParserOutput 1058 * @covers WikiPage::getParserOutput 1059 */ 1060 public function testGetParserOutput( $model, $text, $expectedHtml ) { 1061 $page = $this->createPage( __METHOD__, $text, $model ); 1062 1063 $opt = $page->makeParserOptions( 'canonical' ); 1064 $po = $page->getParserOutput( $opt ); 1065 $text = $po->getText(); 1066 1067 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments 1068 $text = preg_replace( '!\s*(</p>|</div>)!sm', '\1', $text ); # don't let tidy confuse us 1069 1070 $this->assertEquals( $expectedHtml, $text ); 1071 } 1072 1073 /** 1074 * @covers WikiPage::getParserOutput 1075 */ 1076 public function testGetParserOutput_nonexisting() { 1077 $page = new WikiPage( Title::newFromText( __METHOD__ ) ); 1078 1079 $opt = ParserOptions::newFromAnon(); 1080 $po = $page->getParserOutput( $opt ); 1081 1082 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." ); 1083 } 1084 1085 /** 1086 * @covers WikiPage::getParserOutput 1087 */ 1088 public function testGetParserOutput_badrev() { 1089 $page = $this->createPage( __METHOD__, 'dummy', CONTENT_MODEL_WIKITEXT ); 1090 1091 $opt = ParserOptions::newFromAnon(); 1092 $po = $page->getParserOutput( $opt, $page->getLatest() + 1234 ); 1093 1094 // @todo would be neat to also test deleted revision 1095 1096 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." ); 1097 } 1098 1099 public static $sections = 1100 1101 "Intro 1102 1103== stuff == 1104hello world 1105 1106== test == 1107just a test 1108 1109== foo == 1110more stuff 1111"; 1112 1113 public function dataReplaceSection() { 1114 // NOTE: assume the Help namespace to contain wikitext 1115 return [ 1116 [ 'Help:WikiPageTest_testReplaceSection', 1117 CONTENT_MODEL_WIKITEXT, 1118 self::$sections, 1119 "0", 1120 "No more", 1121 null, 1122 trim( preg_replace( '/^Intro/sm', 'No more', self::$sections ) ) 1123 ], 1124 [ 'Help:WikiPageTest_testReplaceSection', 1125 CONTENT_MODEL_WIKITEXT, 1126 self::$sections, 1127 "", 1128 "No more", 1129 null, 1130 "No more" 1131 ], 1132 [ 'Help:WikiPageTest_testReplaceSection', 1133 CONTENT_MODEL_WIKITEXT, 1134 self::$sections, 1135 "2", 1136 "== TEST ==\nmore fun", 1137 null, 1138 trim( preg_replace( '/^== test ==.*== foo ==/sm', 1139 "== TEST ==\nmore fun\n\n== foo ==", 1140 self::$sections ) ) 1141 ], 1142 [ 'Help:WikiPageTest_testReplaceSection', 1143 CONTENT_MODEL_WIKITEXT, 1144 self::$sections, 1145 "8", 1146 "No more", 1147 null, 1148 trim( self::$sections ) 1149 ], 1150 [ 'Help:WikiPageTest_testReplaceSection', 1151 CONTENT_MODEL_WIKITEXT, 1152 self::$sections, 1153 "new", 1154 "No more", 1155 "New", 1156 trim( self::$sections ) . "\n\n== New ==\n\nNo more" 1157 ], 1158 ]; 1159 } 1160 1161 /** 1162 * @dataProvider dataReplaceSection 1163 * @covers WikiPage::replaceSectionContent 1164 */ 1165 public function testReplaceSectionContent( $title, $model, $text, $section, 1166 $with, $sectionTitle, $expected 1167 ) { 1168 $page = $this->createPage( $title, $text, $model ); 1169 1170 $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() ); 1171 /** @var TextContent $c */ 1172 $c = $page->replaceSectionContent( $section, $content, $sectionTitle ); 1173 1174 $this->assertEquals( $expected, $c ? trim( $c->getText() ) : null ); 1175 } 1176 1177 /** 1178 * @dataProvider dataReplaceSection 1179 * @covers WikiPage::replaceSectionAtRev 1180 */ 1181 public function testReplaceSectionAtRev( $title, $model, $text, $section, 1182 $with, $sectionTitle, $expected 1183 ) { 1184 $page = $this->createPage( $title, $text, $model ); 1185 $baseRevId = $page->getLatest(); 1186 1187 $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() ); 1188 /** @var TextContent $c */ 1189 $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId ); 1190 1191 $this->assertEquals( $expected, $c ? trim( $c->getText() ) : null ); 1192 } 1193 1194 public function provideGetAutoDeleteReason() { 1195 return [ 1196 [ 1197 [], 1198 false, 1199 false 1200 ], 1201 1202 [ 1203 [ 1204 [ "first edit", null ], 1205 ], 1206 "/first edit.*only contributor/", 1207 false 1208 ], 1209 1210 [ 1211 [ 1212 [ "first edit", null ], 1213 [ "second edit", null ], 1214 ], 1215 "/second edit.*only contributor/", 1216 true 1217 ], 1218 1219 [ 1220 [ 1221 [ "first edit", "127.0.2.22" ], 1222 [ "second edit", "127.0.3.33" ], 1223 ], 1224 "/second edit/", 1225 true 1226 ], 1227 1228 [ 1229 [ 1230 [ 1231 "first edit: " 1232 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " 1233 . " nonumy eirmod tempor invidunt ut labore et dolore magna " 1234 . "aliquyam erat, sed diam voluptua. At vero eos et accusam " 1235 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, " 1236 . "no sea takimata sanctus est Lorem ipsum dolor sit amet. " 1237 . " this here is some more filler content added to try and " 1238 . "reach the maximum automatic summary length so that this is" 1239 . " truncated ipot sodit colrad ut ad olve amit basul dat" 1240 . "Dorbet romt crobit trop bri. DannyS712 put me here lor pe" 1241 . " ode quob zot bozro see also T22281 for background pol sup" 1242 . "Lorem ipsum dolor sit amet'", 1243 null 1244 ], 1245 ], 1246 '/first edit:.*\.\.\."/', 1247 false 1248 ], 1249 1250 [ 1251 [ 1252 [ "first edit", "127.0.2.22" ], 1253 [ "", "127.0.3.33" ], 1254 ], 1255 "/before blanking.*first edit/", 1256 true 1257 ], 1258 1259 ]; 1260 } 1261 1262 /** 1263 * @dataProvider provideGetAutoDeleteReason 1264 * @covers WikiPage::getAutoDeleteReason 1265 */ 1266 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) { 1267 // NOTE: assume Help namespace to contain wikitext 1268 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" ); 1269 1270 $c = 1; 1271 1272 foreach ( $edits as $edit ) { 1273 $user = new User(); 1274 1275 if ( !empty( $edit[1] ) ) { 1276 $user->setName( $edit[1] ); 1277 } else { 1278 $user = new User; 1279 } 1280 1281 $content = ContentHandler::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() ); 1282 1283 $page->doUserEditContent( $content, $user, "test edit $c", $c < 2 ? EDIT_NEW : 0 ); 1284 1285 $c += 1; 1286 } 1287 1288 $reason = $page->getAutoDeleteReason( $hasHistory ); 1289 1290 if ( is_bool( $expectedResult ) || $expectedResult === null ) { 1291 $this->assertEquals( $expectedResult, $reason ); 1292 } else { 1293 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ), 1294 "Autosummary didn't match expected pattern $expectedResult: $reason" ); 1295 } 1296 1297 $this->assertEquals( $expectedHistory, $hasHistory, 1298 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) ); 1299 } 1300 1301 public function providePreSaveTransform() { 1302 return [ 1303 [ 'hello this is ~~~', 1304 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]", 1305 ], 1306 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>', 1307 'hello \'\'this\'\' is <nowiki>~~~</nowiki>', 1308 ], 1309 ]; 1310 } 1311 1312 /** 1313 * @covers WikiPage::factory 1314 */ 1315 public function testWikiPageFactory() { 1316 $title = Title::makeTitle( NS_FILE, 'Someimage.png' ); 1317 $page = WikiPage::factory( $title ); 1318 $this->assertEquals( WikiFilePage::class, get_class( $page ) ); 1319 1320 $title = Title::makeTitle( NS_CATEGORY, 'SomeCategory' ); 1321 $page = WikiPage::factory( $title ); 1322 $this->assertEquals( WikiCategoryPage::class, get_class( $page ) ); 1323 1324 $title = Title::makeTitle( NS_MAIN, 'SomePage' ); 1325 $page = WikiPage::factory( $title ); 1326 $this->assertEquals( WikiPage::class, get_class( $page ) ); 1327 $this->assertSame( $page, WikiPage::factory( $page ) ); 1328 1329 $title = new PageIdentityValue( 0, NS_MAIN, 'SomePage', PageIdentity::LOCAL ); 1330 $page = WikiPage::factory( $title ); 1331 $this->assertEquals( WikiPage::class, get_class( $page ) ); 1332 } 1333 1334 /** 1335 * @covers WikiPage::loadPageData 1336 * @covers WikiPage::wasLoadedFrom 1337 */ 1338 public function testLoadPageData() { 1339 $title = Title::makeTitle( NS_MAIN, 'SomePage' ); 1340 $page = WikiPage::factory( $title ); 1341 1342 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject::READ_NORMAL ) ); 1343 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject::READ_LATEST ) ); 1344 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject::READ_LOCKING ) ); 1345 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject::READ_EXCLUSIVE ) ); 1346 1347 $page->loadPageData( IDBAccessObject::READ_NORMAL ); 1348 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject::READ_NORMAL ) ); 1349 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject::READ_LATEST ) ); 1350 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject::READ_LOCKING ) ); 1351 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject::READ_EXCLUSIVE ) ); 1352 1353 $page->loadPageData( IDBAccessObject::READ_LATEST ); 1354 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject::READ_NORMAL ) ); 1355 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject::READ_LATEST ) ); 1356 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject::READ_LOCKING ) ); 1357 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject::READ_EXCLUSIVE ) ); 1358 1359 $page->loadPageData( IDBAccessObject::READ_LOCKING ); 1360 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject::READ_NORMAL ) ); 1361 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject::READ_LATEST ) ); 1362 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject::READ_LOCKING ) ); 1363 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject::READ_EXCLUSIVE ) ); 1364 1365 $page->loadPageData( IDBAccessObject::READ_EXCLUSIVE ); 1366 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject::READ_NORMAL ) ); 1367 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject::READ_LATEST ) ); 1368 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject::READ_LOCKING ) ); 1369 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject::READ_EXCLUSIVE ) ); 1370 } 1371 1372 /** 1373 * @covers WikiPage::updateCategoryCounts 1374 */ 1375 public function testUpdateCategoryCounts() { 1376 $page = new WikiPage( Title::newFromText( __METHOD__ ) ); 1377 1378 // Add an initial category 1379 $page->updateCategoryCounts( [ 'A' ], [], 0 ); 1380 1381 $this->assertSame( '1', Category::newFromName( 'A' )->getPageCount() ); 1382 $this->assertSame( 0, Category::newFromName( 'B' )->getPageCount() ); 1383 $this->assertSame( 0, Category::newFromName( 'C' )->getPageCount() ); 1384 1385 // Add a new category 1386 $page->updateCategoryCounts( [ 'B' ], [], 0 ); 1387 1388 $this->assertSame( '1', Category::newFromName( 'A' )->getPageCount() ); 1389 $this->assertSame( '1', Category::newFromName( 'B' )->getPageCount() ); 1390 $this->assertSame( 0, Category::newFromName( 'C' )->getPageCount() ); 1391 1392 // Add and remove a category 1393 $page->updateCategoryCounts( [ 'C' ], [ 'A' ], 0 ); 1394 1395 $this->assertSame( 0, Category::newFromName( 'A' )->getPageCount() ); 1396 $this->assertSame( '1', Category::newFromName( 'B' )->getPageCount() ); 1397 $this->assertSame( '1', Category::newFromName( 'C' )->getPageCount() ); 1398 } 1399 1400 public function provideUpdateRedirectOn() { 1401 yield [ '#REDIRECT [[Foo]]', true, null, true, true, 0 ]; 1402 yield [ '#REDIRECT [[Foo]]', true, 'Foo', true, true, 1 ]; 1403 yield [ 'SomeText', false, null, false, true, 0 ]; 1404 yield [ 'SomeText', false, 'Foo', false, true, 1 ]; 1405 } 1406 1407 /** 1408 * @dataProvider provideUpdateRedirectOn 1409 * @covers WikiPage::updateRedirectOn 1410 * 1411 * @param string $initialText 1412 * @param bool $initialRedirectState 1413 * @param string|null $redirectTitle 1414 * @param bool|null $lastRevIsRedirect 1415 * @param bool $expectedSuccess 1416 * @param int $expectedRowCount 1417 */ 1418 public function testUpdateRedirectOn( 1419 $initialText, 1420 $initialRedirectState, 1421 $redirectTitle, 1422 $lastRevIsRedirect, 1423 $expectedSuccess, 1424 $expectedRowCount 1425 ) { 1426 // FIXME: fails under sqlite and postgres 1427 $this->markTestSkippedIfDbType( 'sqlite' ); 1428 $this->markTestSkippedIfDbType( 'postgres' ); 1429 static $pageCounter = 0; 1430 $pageCounter++; 1431 1432 $page = $this->createPage( Title::newFromText( __METHOD__ . $pageCounter ), $initialText ); 1433 $this->assertSame( $initialRedirectState, $page->isRedirect() ); 1434 1435 $redirectTitle = is_string( $redirectTitle ) 1436 ? Title::newFromText( $redirectTitle ) 1437 : $redirectTitle; 1438 1439 $success = $page->updateRedirectOn( $this->db, $redirectTitle, $lastRevIsRedirect ); 1440 $this->assertSame( $expectedSuccess, $success, 'Success assertion' ); 1441 /** 1442 * updateRedirectOn explicitly updates the redirect table (and not the page table). 1443 * Most of core checks the page table for redirect status, so we have to be ugly and 1444 * assert a select from the table here. 1445 */ 1446 $this->assertRedirectTableCountForPageId( $page->getId(), $expectedRowCount ); 1447 } 1448 1449 private function assertRedirectTableCountForPageId( $pageId, $expected ) { 1450 $this->assertSelect( 1451 'redirect', 1452 'COUNT(*)', 1453 [ 'rd_from' => $pageId ], 1454 [ [ strval( $expected ) ] ] 1455 ); 1456 } 1457 1458 /** 1459 * @covers WikiPage::insertRedirectEntry 1460 */ 1461 public function testInsertRedirectEntry_insertsRedirectEntry() { 1462 $page = $this->createPage( Title::newFromText( __METHOD__ ), 'A' ); 1463 $this->assertRedirectTableCountForPageId( $page->getId(), 0 ); 1464 1465 $targetTitle = Title::newFromText( 'SomeTarget#Frag' ); 1466 $targetTitle->mInterwiki = 'eninter'; 1467 $page->insertRedirectEntry( $targetTitle, null ); 1468 1469 $this->assertSelect( 1470 'redirect', 1471 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ], 1472 [ 'rd_from' => $page->getId() ], 1473 [ [ 1474 strval( $page->getId() ), 1475 strval( $targetTitle->getNamespace() ), 1476 strval( $targetTitle->getDBkey() ), 1477 strval( $targetTitle->getFragment() ), 1478 strval( $targetTitle->getInterwiki() ), 1479 ] ] 1480 ); 1481 } 1482 1483 /** 1484 * @covers WikiPage::insertRedirectEntry 1485 */ 1486 public function testInsertRedirectEntry_insertsRedirectEntryWithPageLatest() { 1487 $page = $this->createPage( Title::newFromText( __METHOD__ ), 'A' ); 1488 $this->assertRedirectTableCountForPageId( $page->getId(), 0 ); 1489 1490 $targetTitle = Title::newFromText( 'SomeTarget#Frag' ); 1491 $targetTitle->mInterwiki = 'eninter'; 1492 $page->insertRedirectEntry( $targetTitle, $page->getLatest() ); 1493 1494 $this->assertSelect( 1495 'redirect', 1496 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ], 1497 [ 'rd_from' => $page->getId() ], 1498 [ [ 1499 strval( $page->getId() ), 1500 strval( $targetTitle->getNamespace() ), 1501 strval( $targetTitle->getDBkey() ), 1502 strval( $targetTitle->getFragment() ), 1503 strval( $targetTitle->getInterwiki() ), 1504 ] ] 1505 ); 1506 } 1507 1508 /** 1509 * @covers WikiPage::insertRedirectEntry 1510 */ 1511 public function testInsertRedirectEntry_doesNotInsertIfPageLatestIncorrect() { 1512 $page = $this->createPage( Title::newFromText( __METHOD__ ), 'A' ); 1513 $this->assertRedirectTableCountForPageId( $page->getId(), 0 ); 1514 1515 $targetTitle = Title::newFromText( 'SomeTarget#Frag' ); 1516 $targetTitle->mInterwiki = 'eninter'; 1517 $page->insertRedirectEntry( $targetTitle, 215251 ); 1518 1519 $this->assertRedirectTableCountForPageId( $page->getId(), 0 ); 1520 } 1521 1522 /** 1523 * @covers WikiPage::insertRedirectEntry 1524 */ 1525 public function testInsertRedirectEntry_T278367() { 1526 $page = $this->createPage( Title::newFromText( __METHOD__ ), 'A' ); 1527 $this->assertRedirectTableCountForPageId( $page->getId(), 0 ); 1528 1529 $targetTitle = Title::newFromText( '#Frag' ); 1530 $ok = $page->insertRedirectEntry( $targetTitle ); 1531 1532 $this->assertFalse( $ok ); 1533 $this->assertRedirectTableCountForPageId( $page->getId(), 0 ); 1534 } 1535 1536 private function getRow( array $overrides = [] ) { 1537 $row = [ 1538 'page_id' => '44', 1539 'page_len' => '76', 1540 'page_is_redirect' => '1', 1541 'page_is_new' => '1', 1542 'page_latest' => '99', 1543 'page_namespace' => '3', 1544 'page_title' => 'JaJaTitle', 1545 'page_restrictions' => 'edit=autoconfirmed,sysop:move=sysop', 1546 'page_touched' => '20120101020202', 1547 'page_links_updated' => '20140101020202', 1548 'page_lang' => 'it', 1549 'page_content_model' => CONTENT_MODEL_WIKITEXT, 1550 ]; 1551 foreach ( $overrides as $key => $value ) { 1552 $row[$key] = $value; 1553 } 1554 return (object)$row; 1555 } 1556 1557 public function provideNewFromRowSuccess() { 1558 yield 'basic row' => [ 1559 $this->getRow(), 1560 static function ( WikiPage $wikiPage, self $test ) { 1561 $test->assertSame( 44, $wikiPage->getId() ); 1562 $test->assertSame( 76, $wikiPage->getTitle()->getLength() ); 1563 $test->assertTrue( $wikiPage->getPageIsRedirectField() ); 1564 $test->assertSame( 99, $wikiPage->getLatest() ); 1565 $test->assertSame( true, $wikiPage->isNew() ); 1566 $test->assertSame( 'it', $wikiPage->getLanguage() ); 1567 $test->assertSame( 3, $wikiPage->getTitle()->getNamespace() ); 1568 $test->assertSame( 'JaJaTitle', $wikiPage->getTitle()->getDBkey() ); 1569 $test->assertSame( 1570 [ 1571 'edit' => [ 'autoconfirmed', 'sysop' ], 1572 'move' => [ 'sysop' ], 1573 ], 1574 $wikiPage->getTitle()->getAllRestrictions() 1575 ); 1576 $test->assertSame( '20120101020202', $wikiPage->getTouched() ); 1577 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() ); 1578 } 1579 ]; 1580 yield 'different timestamp formats' => [ 1581 $this->getRow( [ 1582 'page_touched' => '2012-01-01 02:02:02', 1583 'page_links_updated' => '2014-01-01 02:02:02', 1584 ] ), 1585 static function ( WikiPage $wikiPage, self $test ) { 1586 $test->assertSame( '20120101020202', $wikiPage->getTouched() ); 1587 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() ); 1588 } 1589 ]; 1590 yield 'no restrictions' => [ 1591 $this->getRow( [ 1592 'page_restrictions' => '', 1593 ] ), 1594 static function ( WikiPage $wikiPage, self $test ) { 1595 $test->assertSame( 1596 [ 1597 'edit' => [], 1598 'move' => [], 1599 ], 1600 $wikiPage->getTitle()->getAllRestrictions() 1601 ); 1602 } 1603 ]; 1604 yield 'no language' => [ 1605 $this->getRow( [ 1606 'page_lang' => null, 1607 ] ), 1608 static function ( WikiPage $wikiPage, self $test ) { 1609 $test->assertNull( 1610 $wikiPage->getLanguage() 1611 ); 1612 } 1613 ]; 1614 yield 'not redirect' => [ 1615 $this->getRow( [ 1616 'page_is_redirect' => '0', 1617 ] ), 1618 static function ( WikiPage $wikiPage, self $test ) { 1619 $test->assertFalse( $wikiPage->isRedirect() ); 1620 } 1621 ]; 1622 yield 'not new' => [ 1623 $this->getRow( [ 1624 'page_is_new' => '0', 1625 ] ), 1626 static function ( WikiPage $wikiPage, self $test ) { 1627 $test->assertFalse( $wikiPage->isNew() ); 1628 } 1629 ]; 1630 } 1631 1632 /** 1633 * @covers WikiPage::newFromRow 1634 * @covers WikiPage::loadFromRow 1635 * @dataProvider provideNewFromRowSuccess 1636 * 1637 * @param stdClass $row 1638 * @param callable $assertions 1639 */ 1640 public function testNewFromRow( $row, $assertions ) { 1641 $page = WikiPage::newFromRow( $row, 'fromdb' ); 1642 $assertions( $page, $this ); 1643 } 1644 1645 public function provideTestNewFromId_returnsNullOnBadPageId() { 1646 yield [ 0 ]; 1647 yield [ -11 ]; 1648 } 1649 1650 /** 1651 * @covers WikiPage::newFromID 1652 * @dataProvider provideTestNewFromId_returnsNullOnBadPageId 1653 */ 1654 public function testNewFromId_returnsNullOnBadPageId( $pageId ) { 1655 $this->assertNull( WikiPage::newFromID( $pageId ) ); 1656 } 1657 1658 /** 1659 * @covers WikiPage::newFromID 1660 */ 1661 public function testNewFromId_appearsToFetchCorrectRow() { 1662 $createdPage = $this->createPage( __METHOD__, 'Xsfaij09' ); 1663 $fetchedPage = WikiPage::newFromID( $createdPage->getId() ); 1664 $this->assertSame( $createdPage->getId(), $fetchedPage->getId() ); 1665 $this->assertEquals( 1666 $createdPage->getContent()->getText(), 1667 $fetchedPage->getContent()->getText() 1668 ); 1669 } 1670 1671 /** 1672 * @covers WikiPage::newFromID 1673 */ 1674 public function testNewFromId_returnsNullOnNonExistingId() { 1675 $this->assertNull( WikiPage::newFromID( 2147483647 ) ); 1676 } 1677 1678 /** 1679 * @covers WikiPage::updateRevisionOn 1680 */ 1681 public function testUpdateRevisionOn_existingPage() { 1682 $user = $this->getTestSysop()->getUser(); 1683 $page = $this->createPage( __METHOD__, 'StartText' ); 1684 1685 $revisionRecord = new MutableRevisionRecord( $page ); 1686 $revisionRecord->setContent( 1687 SlotRecord::MAIN, 1688 new WikitextContent( __METHOD__ . '-text' ) 1689 ); 1690 $revisionRecord->setUser( $user ); 1691 $revisionRecord->setTimestamp( '20170707040404' ); 1692 $revisionRecord->setPageId( $page->getId() ); 1693 $revisionRecord->setId( 9989 ); 1694 $revisionRecord->setSize( strlen( __METHOD__ . '-text' ) ); 1695 $revisionRecord->setMinorEdit( true ); 1696 $revisionRecord->setComment( CommentStoreComment::newUnsavedComment( __METHOD__ ) ); 1697 1698 $result = $page->updateRevisionOn( $this->db, $revisionRecord ); 1699 $this->assertTrue( $result ); 1700 $this->assertSame( 9989, $page->getLatest() ); 1701 $this->assertEquals( $revisionRecord, $page->getRevisionRecord() ); 1702 } 1703 1704 /** 1705 * @covers WikiPage::updateRevisionOn 1706 */ 1707 public function testUpdateRevisionOn_NonExistingPage() { 1708 $user = $this->getTestSysop()->getUser(); 1709 $page = $this->createPage( __METHOD__, 'StartText' ); 1710 $page->doDeleteArticleReal( 'reason', $user ); 1711 1712 $revisionRecord = new MutableRevisionRecord( $page ); 1713 $revisionRecord->setContent( 1714 SlotRecord::MAIN, 1715 new WikitextContent( __METHOD__ . '-text' ) 1716 ); 1717 $revisionRecord->setUser( $user ); 1718 $revisionRecord->setTimestamp( '20170707040404' ); 1719 $revisionRecord->setPageId( $page->getId() ); 1720 $revisionRecord->setId( 9989 ); 1721 $revisionRecord->setSize( strlen( __METHOD__ . '-text' ) ); 1722 $revisionRecord->setMinorEdit( true ); 1723 $revisionRecord->setComment( CommentStoreComment::newUnsavedComment( __METHOD__ ) ); 1724 1725 $result = $page->updateRevisionOn( $this->db, $revisionRecord ); 1726 $this->assertFalse( $result ); 1727 } 1728 1729 /** 1730 * @covers WikiPage::insertOn 1731 */ 1732 public function testInsertOn() { 1733 $title = Title::newFromText( __METHOD__ ); 1734 $page = new WikiPage( $title ); 1735 1736 $startTimeStamp = wfTimestampNow(); 1737 $result = $page->insertOn( $this->db ); 1738 $endTimeStamp = wfTimestampNow(); 1739 1740 $this->assertIsInt( $result ); 1741 $this->assertTrue( $result > 0 ); 1742 1743 $condition = [ 'page_id' => $result ]; 1744 1745 // Check the default fields have been filled 1746 $this->assertSelect( 1747 'page', 1748 [ 1749 'page_namespace', 1750 'page_title', 1751 'page_restrictions', 1752 'page_is_redirect', 1753 'page_is_new', 1754 'page_latest', 1755 'page_len', 1756 ], 1757 $condition, 1758 [ [ 1759 '0', 1760 __METHOD__, 1761 '', 1762 '0', 1763 '1', 1764 '0', 1765 '0', 1766 ] ] 1767 ); 1768 1769 // Check the page_random field has been filled 1770 $pageRandom = $this->db->selectField( 'page', 'page_random', $condition ); 1771 $this->assertTrue( (float)$pageRandom < 1 && (float)$pageRandom > 0 ); 1772 1773 // Assert the touched timestamp in the DB is roughly when we inserted the page 1774 $pageTouched = $this->db->selectField( 'page', 'page_touched', $condition ); 1775 $this->assertTrue( 1776 wfTimestamp( TS_UNIX, $startTimeStamp ) 1777 <= wfTimestamp( TS_UNIX, $pageTouched ) 1778 ); 1779 $this->assertTrue( 1780 wfTimestamp( TS_UNIX, $endTimeStamp ) 1781 >= wfTimestamp( TS_UNIX, $pageTouched ) 1782 ); 1783 1784 // Try inserting the same page again and checking the result is false (no change) 1785 $result = $page->insertOn( $this->db ); 1786 $this->assertFalse( $result ); 1787 } 1788 1789 /** 1790 * @covers WikiPage::insertOn 1791 */ 1792 public function testInsertOn_idSpecified() { 1793 $title = Title::newFromText( __METHOD__ ); 1794 $page = new WikiPage( $title ); 1795 $id = 1478952189; 1796 1797 $result = $page->insertOn( $this->db, $id ); 1798 1799 $this->assertSame( $id, $result ); 1800 1801 $condition = [ 'page_id' => $result ]; 1802 1803 // Check there is actually a row in the db 1804 $this->assertSelect( 1805 'page', 1806 [ 'page_title' ], 1807 $condition, 1808 [ [ __METHOD__ ] ] 1809 ); 1810 } 1811 1812 public function provideTestDoUpdateRestrictions_setBasicRestrictions() { 1813 // Note: Once the current dates passes the date in these tests they will fail. 1814 yield 'move something' => [ 1815 true, 1816 [ 'move' => 'something' ], 1817 [], 1818 [ 'edit' => [], 'move' => [ 'something' ] ], 1819 [], 1820 ]; 1821 yield 'move something, edit blank' => [ 1822 true, 1823 [ 'move' => 'something', 'edit' => '' ], 1824 [], 1825 [ 'edit' => [], 'move' => [ 'something' ] ], 1826 [], 1827 ]; 1828 yield 'edit sysop, with expiry' => [ 1829 true, 1830 [ 'edit' => 'sysop' ], 1831 [ 'edit' => '21330101020202' ], 1832 [ 'edit' => [ 'sysop' ], 'move' => [] ], 1833 [ 'edit' => '21330101020202' ], 1834 ]; 1835 yield 'move and edit, move with expiry' => [ 1836 true, 1837 [ 'move' => 'something', 'edit' => 'another' ], 1838 [ 'move' => '22220202010101' ], 1839 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ], 1840 [ 'move' => '22220202010101' ], 1841 ]; 1842 yield 'move and edit, edit with infinity expiry' => [ 1843 true, 1844 [ 'move' => 'something', 'edit' => 'another' ], 1845 [ 'edit' => 'infinity' ], 1846 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ], 1847 [ 'edit' => 'infinity' ], 1848 ]; 1849 yield 'non existing, create something' => [ 1850 false, 1851 [ 'create' => 'something' ], 1852 [], 1853 [ 'create' => [ 'something' ] ], 1854 [], 1855 ]; 1856 yield 'non existing, create something with expiry' => [ 1857 false, 1858 [ 'create' => 'something' ], 1859 [ 'create' => '23451212112233' ], 1860 [ 'create' => [ 'something' ] ], 1861 [ 'create' => '23451212112233' ], 1862 ]; 1863 } 1864 1865 /** 1866 * @dataProvider provideTestDoUpdateRestrictions_setBasicRestrictions 1867 * @covers WikiPage::doUpdateRestrictions 1868 */ 1869 public function testDoUpdateRestrictions_setBasicRestrictions( 1870 $pageExists, 1871 array $limit, 1872 array $expiry, 1873 array $expectedRestrictions, 1874 array $expectedRestrictionExpiries 1875 ) { 1876 if ( $pageExists ) { 1877 $page = $this->createPage( __METHOD__, 'ABC' ); 1878 } else { 1879 $page = new WikiPage( Title::newFromText( __METHOD__ . '-nonexist' ) ); 1880 } 1881 $user = $this->getTestSysop()->getUser(); 1882 $userIdentity = $this->getTestSysop()->getUserIdentity(); 1883 1884 $cascade = false; 1885 1886 $status = $page->doUpdateRestrictions( $limit, $expiry, $cascade, 'aReason', $userIdentity, [] ); 1887 1888 $logId = $status->getValue(); 1889 $allRestrictions = $page->getTitle()->getAllRestrictions(); 1890 1891 $this->assertTrue( $status->isGood() ); 1892 $this->assertIsInt( $logId ); 1893 $this->assertSame( $expectedRestrictions, $allRestrictions ); 1894 foreach ( $expectedRestrictionExpiries as $key => $value ) { 1895 $this->assertSame( $value, $page->getTitle()->getRestrictionExpiry( $key ) ); 1896 } 1897 1898 // Make sure the log entry looks good 1899 // log_params is not checked here 1900 $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'log_comment' ); 1901 $this->assertSelect( 1902 [ 'logging' ] + $commentQuery['tables'], 1903 [ 1904 'log_comment' => $commentQuery['fields']['log_comment_text'], 1905 'log_actor', 1906 'log_namespace', 1907 'log_title', 1908 ], 1909 [ 'log_id' => $logId ], 1910 [ [ 1911 'aReason', 1912 (string)$user->getActorId(), 1913 (string)$page->getTitle()->getNamespace(), 1914 $page->getTitle()->getDBkey(), 1915 ] ], 1916 [], 1917 $commentQuery['joins'] 1918 ); 1919 } 1920 1921 /** 1922 * @covers WikiPage::doUpdateRestrictions 1923 */ 1924 public function testDoUpdateRestrictions_failsOnReadOnly() { 1925 $page = $this->createPage( __METHOD__, 'ABC' ); 1926 $user = $this->getTestSysop()->getUser(); 1927 $cascade = false; 1928 1929 // Set read only 1930 $readOnly = $this->getDummyReadOnlyMode( true ); 1931 $this->setService( 'ReadOnlyMode', $readOnly ); 1932 1933 $status = $page->doUpdateRestrictions( [], [], $cascade, 'aReason', $user, [] ); 1934 $this->assertFalse( $status->isOK() ); 1935 $this->assertSame( 'readonlytext', $status->getMessage()->getKey() ); 1936 } 1937 1938 /** 1939 * @covers WikiPage::doUpdateRestrictions 1940 */ 1941 public function testDoUpdateRestrictions_returnsGoodIfNothingChanged() { 1942 $page = $this->createPage( __METHOD__, 'ABC' ); 1943 $user = $this->getTestSysop()->getUser(); 1944 $cascade = false; 1945 $limit = [ 'edit' => 'sysop' ]; 1946 1947 $status = $page->doUpdateRestrictions( 1948 $limit, 1949 [], 1950 $cascade, 1951 'aReason', 1952 $user, 1953 [] 1954 ); 1955 1956 // The first entry should have a logId as it did something 1957 $this->assertTrue( $status->isGood() ); 1958 $this->assertIsInt( $status->getValue() ); 1959 1960 $status = $page->doUpdateRestrictions( 1961 $limit, 1962 [], 1963 $cascade, 1964 'aReason', 1965 $user, 1966 [] 1967 ); 1968 1969 // The second entry should not have a logId as nothing changed 1970 $this->assertTrue( $status->isGood() ); 1971 $this->assertNull( $status->getValue() ); 1972 } 1973 1974 /** 1975 * @covers WikiPage::doUpdateRestrictions 1976 */ 1977 public function testDoUpdateRestrictions_logEntryTypeAndAction() { 1978 $page = $this->createPage( __METHOD__, 'ABC' ); 1979 $user = $this->getTestSysop()->getUser(); 1980 $cascade = false; 1981 1982 // Protect the page 1983 $status = $page->doUpdateRestrictions( 1984 [ 'edit' => 'sysop' ], 1985 [], 1986 $cascade, 1987 'aReason', 1988 $user, 1989 [] 1990 ); 1991 $this->assertTrue( $status->isGood() ); 1992 $this->assertIsInt( $status->getValue() ); 1993 $this->assertSelect( 1994 'logging', 1995 [ 'log_type', 'log_action' ], 1996 [ 'log_id' => $status->getValue() ], 1997 [ [ 'protect', 'protect' ] ] 1998 ); 1999 2000 // Modify the protection 2001 $status = $page->doUpdateRestrictions( 2002 [ 'edit' => 'somethingElse' ], 2003 [], 2004 $cascade, 2005 'aReason', 2006 $user, 2007 [] 2008 ); 2009 $this->assertTrue( $status->isGood() ); 2010 $this->assertIsInt( $status->getValue() ); 2011 $this->assertSelect( 2012 'logging', 2013 [ 'log_type', 'log_action' ], 2014 [ 'log_id' => $status->getValue() ], 2015 [ [ 'protect', 'modify' ] ] 2016 ); 2017 2018 // Remove the protection 2019 $status = $page->doUpdateRestrictions( 2020 [], 2021 [], 2022 $cascade, 2023 'aReason', 2024 $user, 2025 [] 2026 ); 2027 $this->assertTrue( $status->isGood() ); 2028 $this->assertIsInt( $status->getValue() ); 2029 $this->assertSelect( 2030 'logging', 2031 [ 'log_type', 'log_action' ], 2032 [ 'log_id' => $status->getValue() ], 2033 [ [ 'protect', 'unprotect' ] ] 2034 ); 2035 } 2036 2037 /** 2038 * @covers WikiPage::newPageUpdater 2039 * @covers WikiPage::getDerivedDataUpdater 2040 */ 2041 public function testNewPageUpdater() { 2042 $user = $this->getTestUser()->getUser(); 2043 $page = $this->newPage( __METHOD__, __METHOD__ ); 2044 2045 /** @var Content $content */ 2046 $content = $this->getMockBuilder( WikitextContent::class ) 2047 ->setConstructorArgs( [ 'Hello World' ] ) 2048 ->onlyMethods( [ 'getParserOutput' ] ) 2049 ->getMock(); 2050 $content->expects( $this->once() ) 2051 ->method( 'getParserOutput' ) 2052 ->willReturn( new ParserOutput( 'HTML' ) ); 2053 2054 $preparedEditBefore = $page->prepareContentForEdit( $content, null, $user ); 2055 2056 // provide context, so the cache can be kept in place 2057 $slotsUpdate = new revisionSlotsUpdate(); 2058 $slotsUpdate->modifyContent( SlotRecord::MAIN, $content ); 2059 2060 $updater = $page->newPageUpdater( $user, $slotsUpdate ); 2061 $updater->setContent( SlotRecord::MAIN, $content ); 2062 $revision = $updater->saveRevision( 2063 CommentStoreComment::newUnsavedComment( 'test' ), 2064 EDIT_NEW 2065 ); 2066 2067 $preparedEditAfter = $page->prepareContentForEdit( $content, $revision, $user ); 2068 2069 $this->assertSame( $revision->getId(), $page->getLatest() ); 2070 2071 // Parsed output must remain cached throughout. 2072 $this->assertSame( $preparedEditBefore->output, $preparedEditAfter->output ); 2073 } 2074 2075 /** 2076 * @covers WikiPage::newPageUpdater 2077 * @covers WikiPage::getDerivedDataUpdater 2078 */ 2079 public function testGetDerivedDataUpdater() { 2080 $admin = $this->getTestSysop()->getUser(); 2081 2082 /** @var object $page */ 2083 $page = $this->createPage( __METHOD__, __METHOD__ ); 2084 $page = TestingAccessWrapper::newFromObject( $page ); 2085 2086 $revision = $page->getRevisionRecord(); 2087 $user = $revision->getUser(); 2088 2089 $slotsUpdate = new RevisionSlotsUpdate(); 2090 $slotsUpdate->modifyContent( SlotRecord::MAIN, new WikitextContent( 'Hello World' ) ); 2091 2092 // get a virgin updater 2093 $updater1 = $page->getDerivedDataUpdater( $user ); 2094 $this->assertFalse( $updater1->isUpdatePrepared() ); 2095 2096 $updater1->prepareUpdate( $revision ); 2097 2098 // Re-use updater with same revision or content, even if base changed 2099 $this->assertSame( $updater1, $page->getDerivedDataUpdater( $user, $revision ) ); 2100 2101 $slotsUpdate = RevisionSlotsUpdate::newFromContent( 2102 [ SlotRecord::MAIN => $revision->getContent( SlotRecord::MAIN ) ] 2103 ); 2104 $this->assertSame( $updater1, $page->getDerivedDataUpdater( $user, null, $slotsUpdate ) ); 2105 2106 // Don't re-use for edit if base revision ID changed 2107 $this->assertNotSame( 2108 $updater1, 2109 $page->getDerivedDataUpdater( $user, null, $slotsUpdate, true ) 2110 ); 2111 2112 // Don't re-use with different user 2113 $updater2a = $page->getDerivedDataUpdater( $admin, null, $slotsUpdate ); 2114 $updater2a->prepareContent( $admin, $slotsUpdate, false ); 2115 2116 $updater2b = $page->getDerivedDataUpdater( $user, null, $slotsUpdate ); 2117 $updater2b->prepareContent( $user, $slotsUpdate, false ); 2118 $this->assertNotSame( $updater2a, $updater2b ); 2119 2120 // Don't re-use with different content 2121 $updater3 = $page->getDerivedDataUpdater( $admin, null, $slotsUpdate ); 2122 $updater3->prepareUpdate( $revision ); 2123 $this->assertNotSame( $updater2b, $updater3 ); 2124 2125 // Don't re-use if no context given 2126 $updater4 = $page->getDerivedDataUpdater( $admin ); 2127 $updater4->prepareUpdate( $revision ); 2128 $this->assertNotSame( $updater3, $updater4 ); 2129 2130 // Don't re-use if AGAIN no context given 2131 $updater5 = $page->getDerivedDataUpdater( $admin ); 2132 $this->assertNotSame( $updater4, $updater5 ); 2133 2134 // Don't re-use cached "virgin" unprepared updater 2135 $updater6 = $page->getDerivedDataUpdater( $admin, $revision ); 2136 $this->assertNotSame( $updater5, $updater6 ); 2137 } 2138 2139 protected function assertPreparedEditEquals( 2140 PreparedEdit $edit, PreparedEdit $edit2, $message = '' 2141 ) { 2142 // suppress differences caused by a clock tick between generating the two PreparedEdits 2143 if ( abs( $edit->timestamp - $edit2->timestamp ) < 3 ) { 2144 $edit2 = clone $edit2; 2145 $edit2->timestamp = $edit->timestamp; 2146 } 2147 $this->assertEquals( $edit, $edit2, $message ); 2148 } 2149 2150 protected function assertPreparedEditNotEquals( 2151 PreparedEdit $edit, PreparedEdit $edit2, $message = '' 2152 ) { 2153 if ( abs( $edit->timestamp - $edit2->timestamp ) < 3 ) { 2154 $edit2 = clone $edit2; 2155 $edit2->timestamp = $edit->timestamp; 2156 } 2157 $this->assertNotEquals( $edit, $edit2, $message ); 2158 } 2159 2160 /** 2161 * @covers WikiPage::factory 2162 * 2163 * @throws MWException 2164 */ 2165 public function testWikiPageFactoryHookValid() { 2166 $isCalled = false; 2167 $expectedWikiPage = $this->createMock( WikiPage::class ); 2168 2169 $this->setTemporaryHook( 2170 'WikiPageFactory', 2171 static function ( $title, &$page ) use ( &$isCalled, $expectedWikiPage ) { 2172 $page = $expectedWikiPage; 2173 $isCalled = true; 2174 2175 return false; 2176 } 2177 ); 2178 2179 $title = Title::makeTitle( NS_CATEGORY, 'SomeCategory' ); 2180 $wikiPage = WikiPage::factory( $title ); 2181 2182 $this->assertTrue( $isCalled ); 2183 $this->assertSame( $expectedWikiPage, $wikiPage ); 2184 } 2185 2186 /** 2187 * This is just to confirm that WikiPage::updateRevisionOn() updates the 2188 * Title and LinkCache with the correct redirect value. Failing to do so 2189 * causes subtle test failures in extensions, such as Cognate (T283654) 2190 * and Echo (no task, but see code review of I12542fc899). 2191 * 2192 * @covers WikiPage 2193 */ 2194 public function testUpdateSetsTitleRedirectCache() { 2195 // Get a title object without using the title cache 2196 $title = Title::makeTitleSafe( NS_MAIN, 'A new redirect' ); 2197 $this->assertFalse( $title->isRedirect() ); 2198 2199 $dbw = wfGetDB( DB_PRIMARY ); 2200 $store = MediaWikiServices::getInstance()->getRevisionStore(); 2201 $page = $this->newPage( $title ); 2202 $page->insertOn( $dbw ); 2203 2204 $revision = new MutableRevisionRecord( $page ); 2205 $revision->setContent( 2206 SlotRecord::MAIN, 2207 new WikitextContent( '#REDIRECT [[Target]]' ) 2208 ); 2209 $revision->setTimestamp( wfTimestampNow() ); 2210 $revision->setComment( CommentStoreComment::newUnsavedComment( '' ) ); 2211 $revision->setUser( $this->getTestUser()->getUser() ); 2212 2213 $revision = $store->insertRevisionOn( $revision, $dbw ); 2214 2215 $page->updateRevisionOn( $dbw, $revision ); 2216 // check the title cache 2217 $this->assertTrue( $title->isRedirect() ); 2218 // check the link cache with a fresh title 2219 $title = Title::makeTitleSafe( NS_MAIN, 'A new redirect' ); 2220 $this->assertTrue( $title->isRedirect() ); 2221 } 2222 2223 /** 2224 * @covers WikiPage::getTitle 2225 * @covers WikiPage::getId 2226 * @covers WikiPage::getNamespace 2227 * @covers WikiPage::getDBkey 2228 * @covers WikiPage::getWikiId 2229 * @covers WikiPage::canExist 2230 */ 2231 public function testGetTitle() { 2232 $page = $this->createPage( __METHOD__, 'whatever' ); 2233 2234 $title = $page->getTitle(); 2235 $this->assertSame( __METHOD__, $title->getText() ); 2236 2237 $this->assertSame( $page->getId(), $title->getId() ); 2238 $this->assertSame( $page->getNamespace(), $title->getNamespace() ); 2239 $this->assertSame( $page->getDBkey(), $title->getDBkey() ); 2240 $this->assertSame( $page->getWikiId(), $title->getWikiId() ); 2241 $this->assertSame( $page->canExist(), $title->canExist() ); 2242 } 2243 2244 /** 2245 * @covers WikiPage::toPageRecord 2246 * @covers WikiPage::getLatest 2247 * @covers WikiPage::getTouched 2248 * @covers WikiPage::isNew 2249 * @covers WikiPage::isRedirect 2250 */ 2251 public function testToPageRecord() { 2252 $page = $this->createPage( __METHOD__, 'whatever' ); 2253 $record = $page->toPageRecord(); 2254 2255 $this->assertSame( $page->getId(), $record->getId() ); 2256 $this->assertSame( $page->getNamespace(), $record->getNamespace() ); 2257 $this->assertSame( $page->getDBkey(), $record->getDBkey() ); 2258 $this->assertSame( $page->getWikiId(), $record->getWikiId() ); 2259 $this->assertSame( $page->canExist(), $record->canExist() ); 2260 2261 $this->assertSame( $page->getLatest(), $record->getLatest() ); 2262 $this->assertSame( $page->getTouched(), $record->getTouched() ); 2263 $this->assertSame( $page->isNew(), $record->isNew() ); 2264 $this->assertSame( $page->isRedirect(), $record->isRedirect() ); 2265 } 2266 2267 /** 2268 * @covers WikiPage::setLastEdit 2269 * @covers WikiPage::getTouched 2270 */ 2271 public function testGetTouched() { 2272 $page = $this->createPage( __METHOD__, 'whatever' ); 2273 2274 $touched = $this->db->selectField( 'page', 'page_touched', [ 'page_id' => $page->getId() ] ); 2275 $touched = MWTimestamp::convert( TS_MW, $touched ); 2276 2277 // Internal cache of the touched time was set after the page was created 2278 $this->assertSame( $touched, $page->getTouched() ); 2279 2280 $touched = MWTimestamp::convert( TS_MW, MWTimestamp::convert( TS_UNIX, $touched ) + 100 ); 2281 $page->getTitle()->invalidateCache( $touched ); 2282 2283 // Re-load touched time 2284 $page = $this->newPage( $page->getTitle() ); 2285 $this->assertSame( $touched, $page->getTouched() ); 2286 2287 // Cause the latest revision to be loaded 2288 $page->getRevisionRecord(); 2289 2290 // Make sure the internal cache of the touched time was not overwritten 2291 $this->assertSame( $touched, $page->getTouched() ); 2292 } 2293 2294} 2295