| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\CommentStore\CommentStoreComment; |
| 4 |
use MediaWiki\Content\ContentHandler; |
| 5 |
use MediaWiki\Content\TextContent; |
| 6 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 7 |
use MediaWiki\Revision\RevisionRecord; |
| 8 |
use MediaWiki\Revision\SlotRecord; |
| 9 |
use MediaWiki\Tests\User\TempUser\TempUserTestTrait; |
| 10 |
use MediaWiki\Title\Title; |
| 11 |
use MediaWiki\User\UserIdentityValue; |
| 12 |
use Wikimedia\IPUtils; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class PageArchiveTest extends MediaWikiIntegrationTestCase { |
| 20 |
|
| 21 |
use TempUserTestTrait; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
protected $pageId; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
protected $archivedPage; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
protected $ipEditor; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
protected $firstRev; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
protected $ipRev; |
| 50 |
|
| 51 |
protected function setUp(): void { |
| 52 |
parent::setUp(); |
| 53 |
$this->disableAutoCreateTempUser(); |
| 54 |
|
| 55 |
|
| 56 |
$this->archivedPage = Title::makeTitle( NS_MAIN, 'PageArchiveTest_thePage' ); |
| 57 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $this->archivedPage ); |
| 58 |
$content = ContentHandler::makeContent( |
| 59 |
'testing', |
| 60 |
$page->getTitle(), |
| 61 |
CONTENT_MODEL_WIKITEXT |
| 62 |
); |
| 63 |
|
| 64 |
$user = $this->getTestUser()->getUser(); |
| 65 |
$page->doUserEditContent( $content, $user, 'testing', EDIT_NEW | EDIT_SUPPRESS_RC ); |
| 66 |
|
| 67 |
$this->pageId = $page->getId(); |
| 68 |
$this->firstRev = $page->getRevisionRecord(); |
| 69 |
|
| 70 |
|
| 71 |
$this->ipEditor = '2001:DB8:0:0:0:0:0:1'; |
| 72 |
|
| 73 |
$revisionStore = $this->getServiceContainer()->getRevisionStore(); |
| 74 |
|
| 75 |
$ipTimestamp = wfTimestamp( |
| 76 |
TS_MW, |
| 77 |
wfTimestamp( TS_UNIX, $this->firstRev->getTimestamp() ) + 1 |
| 78 |
); |
| 79 |
$rev = new MutableRevisionRecord( $page ); |
| 80 |
$rev->setUser( UserIdentityValue::newAnonymous( $this->ipEditor ) ); |
| 81 |
$rev->setTimestamp( $ipTimestamp ); |
| 82 |
$rev->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) ); |
| 83 |
$rev->setComment( CommentStoreComment::newUnsavedComment( 'just a test' ) ); |
| 84 |
|
| 85 |
$dbw = $this->getDb(); |
| 86 |
$this->ipRev = $revisionStore->insertRevisionOn( $rev, $dbw ); |
| 87 |
|
| 88 |
$this->deletePage( $page, '', $user ); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function testUndeleteRevisions() { |
| 95 |
$this->hideDeprecated( 'PageArchive::undeleteAsUser' ); |
| 96 |
|
| 97 |
|
| 98 |
$revisionStore = $this->getServiceContainer()->getRevisionStore(); |
| 99 |
|
| 100 |
|
| 101 |
$dbr = $this->getDb(); |
| 102 |
$row = $revisionStore->newArchiveSelectQueryBuilder( $dbr ) |
| 103 |
->joinComment() |
| 104 |
->where( [ 'ar_rev_id' => $this->ipRev->getId() ] ) |
| 105 |
->caller( __METHOD__ )->fetchRow(); |
| 106 |
$this->assertEquals( $this->ipEditor, $row->ar_user_text ); |
| 107 |
|
| 108 |
|
| 109 |
$row = $dbr->newSelectQueryBuilder() |
| 110 |
->select( '1' ) |
| 111 |
->from( 'revision' ) |
| 112 |
->where( [ 'rev_id' => $this->ipRev->getId() ] ) |
| 113 |
->fetchRow(); |
| 114 |
$this->assertFalse( $row ); |
| 115 |
|
| 116 |
|
| 117 |
$row = $dbr->newSelectQueryBuilder() |
| 118 |
->select( '1' ) |
| 119 |
->from( 'ip_changes' ) |
| 120 |
->where( [ 'ipc_rev_id' => $this->ipRev->getId() ] ) |
| 121 |
->fetchRow(); |
| 122 |
$this->assertFalse( $row ); |
| 123 |
|
| 124 |
|
| 125 |
$archive = new PageArchive( $this->archivedPage ); |
| 126 |
$archive->undeleteAsUser( [], $this->getTestSysop()->getUser() ); |
| 127 |
|
| 128 |
|
| 129 |
$row = $revisionStore->newSelectQueryBuilder( $dbr ) |
| 130 |
->where( [ 'rev_id' => $this->ipRev->getId() ] ) |
| 131 |
->caller( __METHOD__ )->fetchRow(); |
| 132 |
$this->assertNotFalse( $row, 'row exists in revision table' ); |
| 133 |
$this->assertEquals( $this->ipEditor, $row->rev_user_text ); |
| 134 |
|
| 135 |
|
| 136 |
$row = $dbr->newSelectQueryBuilder() |
| 137 |
->select( [ 'ipc_hex' ] ) |
| 138 |
->from( 'ip_changes' ) |
| 139 |
->where( [ 'ipc_rev_id' => $this->ipRev->getId() ] ) |
| 140 |
->fetchRow(); |
| 141 |
$this->assertNotFalse( $row, 'row exists in ip_changes table' ); |
| 142 |
$this->assertEquals( IPUtils::toHex( $this->ipEditor ), $row->ipc_hex ); |
| 143 |
} |
| 144 |
|
| 145 |
protected function getExpectedArchiveRows() { |
| 146 |
return [ |
| 147 |
[ |
| 148 |
'ar_minor_edit' => '0', |
| 149 |
'ar_user' => null, |
| 150 |
'ar_user_text' => $this->ipEditor, |
| 151 |
'ar_actor' => (string)$this->getServiceContainer()->getActorNormalization() |
| 152 |
->acquireActorId( new UserIdentityValue( 0, $this->ipEditor ), $this->getDb() ), |
| 153 |
'ar_len' => '11', |
| 154 |
'ar_deleted' => '0', |
| 155 |
'ar_rev_id' => strval( $this->ipRev->getId() ), |
| 156 |
'ar_timestamp' => $this->getDb()->timestamp( $this->ipRev->getTimestamp() ), |
| 157 |
'ar_sha1' => '0qdrpxl537ivfnx4gcpnzz0285yxryy', |
| 158 |
'ar_page_id' => strval( $this->ipRev->getPageId() ), |
| 159 |
'ar_comment_text' => 'just a test', |
| 160 |
'ar_comment_data' => null, |
| 161 |
'ar_comment_cid' => strval( $this->ipRev->getComment()->id ), |
| 162 |
'ts_tags' => null, |
| 163 |
'ar_id' => '2', |
| 164 |
'ar_namespace' => '0', |
| 165 |
'ar_title' => 'PageArchiveTest_thePage', |
| 166 |
'ar_parent_id' => strval( $this->ipRev->getParentId() ), |
| 167 |
], |
| 168 |
[ |
| 169 |
'ar_minor_edit' => '0', |
| 170 |
'ar_user' => (string)$this->getTestUser()->getUser()->getId(), |
| 171 |
'ar_user_text' => $this->getTestUser()->getUser()->getName(), |
| 172 |
'ar_actor' => (string)$this->getTestUser()->getUser()->getActorId(), |
| 173 |
'ar_len' => '7', |
| 174 |
'ar_deleted' => '0', |
| 175 |
'ar_rev_id' => strval( $this->firstRev->getId() ), |
| 176 |
'ar_timestamp' => $this->getDb()->timestamp( $this->firstRev->getTimestamp() ), |
| 177 |
'ar_sha1' => 'pr0s8e18148pxhgjfa0gjrvpy8fiyxc', |
| 178 |
'ar_page_id' => strval( $this->firstRev->getPageId() ), |
| 179 |
'ar_comment_text' => 'testing', |
| 180 |
'ar_comment_data' => null, |
| 181 |
'ar_comment_cid' => strval( $this->firstRev->getComment()->id ), |
| 182 |
'ts_tags' => null, |
| 183 |
'ar_id' => '1', |
| 184 |
'ar_namespace' => '0', |
| 185 |
'ar_title' => 'PageArchiveTest_thePage', |
| 186 |
'ar_parent_id' => '0', |
| 187 |
], |
| 188 |
]; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function testListPagesBySearch() { |
| 197 |
$pages = PageArchive::listPagesBySearch( 'PageArchiveTest_thePage' ); |
| 198 |
$this->assertSame( 1, $pages->numRows() ); |
| 199 |
|
| 200 |
$page = (array)$pages->current(); |
| 201 |
|
| 202 |
$this->assertSame( |
| 203 |
[ |
| 204 |
'ar_namespace' => '0', |
| 205 |
'ar_title' => 'PageArchiveTest_thePage', |
| 206 |
'count' => '2', |
| 207 |
], |
| 208 |
$page |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public function testListPagesByPrefix() { |
| 217 |
$pages = PageArchive::listPagesByPrefix( 'PageArchiveTest' ); |
| 218 |
$this->assertSame( 1, $pages->numRows() ); |
| 219 |
|
| 220 |
$page = (array)$pages->current(); |
| 221 |
|
| 222 |
$this->assertSame( |
| 223 |
[ |
| 224 |
'ar_namespace' => '0', |
| 225 |
'ar_title' => 'PageArchiveTest_thePage', |
| 226 |
'count' => '2', |
| 227 |
], |
| 228 |
$page |
| 229 |
); |
| 230 |
} |
| 231 |
|
| 232 |
} |