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