Debian Codegraph @

tests/phpunit/includes/page/UndeletePageTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\CommentStore\CommentStoreComment;
4 use MediaWiki\Content\ContentHandler;
5 use MediaWiki\Page\UndeletePage;
6 use MediaWiki\Revision\SlotRecord;
7 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
8 use MediaWiki\Title\Title;
9 use MediaWiki\User\UserIdentityValue;
10 use Wikimedia\IPUtils;
11
12 /**
13 * @group Database
14 * @coversDefaultClass \MediaWiki\Page\UndeletePage
15 */
16 class UndeletePageTest extends MediaWikiIntegrationTestCase {
17
18 use TempUserTestTrait;
19
20 /**
21 * @var array
22 */
23 private $pages = [];
24
25 /**
26 * A logged out user who edited the page before it was archived.
27 * @var string
28 */
29 private $ipEditor;
30
31 protected function setUp(): void {
32 parent::setUp();
33
34 $this->ipEditor = '2001:DB8:0:0:0:0:0:1';
35 $this->setupPage( 'UndeletePageTest_thePage', NS_MAIN, ' ' );
36 $this->setupPage( 'UndeletePageTest_thePage', NS_TALK, ' ' );
37 }
38
39 /**
40 * @param string $titleText
41 * @param int $ns
42 * @param string $content
43 */
44 private function setupPage( string $titleText, int $ns, string $content ): void {
45 $this->disableAutoCreateTempUser();
46 $title = Title::makeTitle( $ns, $titleText );
47 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
48 $performer = static::getTestUser()->getUser();
49 $content = ContentHandler::makeContent( $content, $page->getTitle(), CONTENT_MODEL_WIKITEXT );
50 $updater = $page->newPageUpdater( UserIdentityValue::newAnonymous( $this->ipEditor ) )
51 ->setContent( SlotRecord::MAIN, $content );
52
53 $revisionRecord = $updater->saveRevision( CommentStoreComment::newUnsavedComment( "testing" ) );
54 if ( !$updater->wasSuccessful() ) {
55 $this->fail( $updater->getStatus()->getWikiText() );
56 }
57
58 $this->pages[] = [ 'page' => $page, 'revId' => $revisionRecord->getId() ];
59 $this->deletePage( $page, '', $performer );
60 }
61
62 /**
63 * @covers ::undeleteUnsafe
64 * @covers ::undeleteRevisions
65 * @covers \MediaWiki\Revision\RevisionStoreFactory::getRevisionStoreForUndelete
66 * @covers \MediaWiki\User\ActorStoreFactory::getActorStoreForUndelete
67 */
68 public function testUndeleteRevisions() {
69 // TODO: MCR: Test undeletion with multiple slots. Check that slots remain untouched.
70 $revisionStore = $this->getServiceContainer()->getRevisionStore();
71
72 // First make sure old revisions are archived
73 $dbr = $this->getDb();
74
75 foreach ( [ 0, 1 ] as $key ) {
76 $row = $revisionStore->newArchiveSelectQueryBuilder( $dbr )
77 ->joinComment()
78 ->where( [ 'ar_rev_id' => $this->pages[$key]['revId'] ] )
79 ->caller( __METHOD__ )->fetchRow();
80 $this->assertEquals( $this->ipEditor, $row->ar_user_text );
81
82 // Should not be in revision
83 $row = $dbr->newSelectQueryBuilder()
84 ->select( '1' )
85 ->from( 'revision' )
86 ->where( [ 'rev_id' => $this->pages[$key]['revId'] ] )
87 ->fetchRow();
88 $this->assertFalse( $row );
89
90 // Should not be in ip_changes
91 $row = $dbr->newSelectQueryBuilder()
92 ->select( '1' )
93 ->from( 'ip_changes' )
94 ->where( [ 'ipc_rev_id' => $this->pages[$key]['revId'] ] )
95 ->fetchRow();
96 $this->assertFalse( $row );
97 }
98
99 // Enable autocreation of temporary users to test that undeletion of revisions performed by IP addresses works
100 // when temporary accounts are enabled.
101 $this->enableAutoCreateTempUser();
102 // Restore the page
103 $undeletePage = $this->getServiceContainer()->getUndeletePageFactory()->newUndeletePage(
104 $this->pages[0]['page'],
105 $this->getTestSysop()->getUser()
106 );
107
108 $status = $undeletePage->setUndeleteAssociatedTalk( true )->undeleteUnsafe( '' );
109 $this->assertEquals( 2, $status->value[UndeletePage::REVISIONS_RESTORED] );
110
111 // check subject page and talk page are both back in the revision table
112 foreach ( [ 0, 1 ] as $key ) {
113 $row = $revisionStore->newSelectQueryBuilder( $dbr )
114 ->where( [ 'rev_id' => $this->pages[$key]['revId'] ] )
115 ->caller( __METHOD__ )->fetchRow();
116
117 $this->assertNotFalse( $row, 'row exists in revision table' );
118 $this->assertEquals( $this->ipEditor, $row->rev_user_text );
119
120 // Should be back in ip_changes
121 $row = $dbr->newSelectQueryBuilder()
122 ->select( [ 'ipc_hex' ] )
123 ->from( 'ip_changes' )
124 ->where( [ 'ipc_rev_id' => $this->pages[$key]['revId'] ] )
125 ->fetchRow();
126 $this->assertNotFalse( $row, 'row exists in ip_changes table' );
127 $this->assertEquals( IPUtils::toHex( $this->ipEditor ), $row->ipc_hex );
128 }
129 }
130 }