2 use MediaWiki\MediaWikiServices
;
3 use MediaWiki\Revision\RevisionRecord
;
6 * Base class for tests of PageArchive against different database schemas.
8 abstract class PageArchiveTestBase
extends MediaWikiTestCase
{
16 * @var PageArchive $archivedPage
18 protected $archivedPage;
21 * A logged out user who edited the page before it was archived.
22 * @var string $ipEditor
27 * Revision of the first (initial) edit
33 * Revision of the IP edit (the second edit)
38 function __construct( $name = null, array $data = [], $dataName = '' ) {
39 parent
::__construct( $name, $data, $dataName );
41 $this->tablesUsed
= array_merge(
46 'revision_comment_temp',
58 protected function addCoreDBData() {
59 // Blank out to avoid failures when schema overrides imposed by subclasses
60 // affect revision storage.
66 abstract protected function getMcrMigrationStage();
71 abstract protected function getMcrTablesToReset();
76 protected function getContentHandlerUseDB() {
80 protected function setUp() {
83 $this->tablesUsed +
= $this->getMcrTablesToReset();
85 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_NEW
);
86 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
88 'wgMultiContentRevisionSchemaMigrationStage',
89 $this->getMcrMigrationStage()
91 $this->overrideMwServices();
93 // First create our dummy page
94 $page = Title
::newFromText( 'PageArchiveTest_thePage' );
95 $page = new WikiPage( $page );
96 $content = ContentHandler
::makeContent(
99 CONTENT_MODEL_WIKITEXT
102 $user = $this->getTestUser()->getUser();
103 $page->doEditContent( $content, 'testing', EDIT_NEW
, false, $user );
105 $this->pageId
= $page->getId();
106 $this->firstRev
= $page->getRevision()->getRevisionRecord();
108 // Insert IP revision
109 $this->ipEditor
= '2001:db8::1';
111 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
113 $ipTimestamp = wfTimestamp(
115 wfTimestamp( TS_UNIX
, $this->firstRev
->getTimestamp() ) +
1
118 $rev = $revisionStore->newMutableRevisionFromArray( [
119 'text' => 'Lorem Ipsum',
120 'comment' => 'just a test',
121 'page' => $page->getId(),
122 'user_text' => $this->ipEditor
,
123 'timestamp' => $ipTimestamp,
126 $dbw = wfGetDB( DB_MASTER
);
127 $this->ipRev
= $revisionStore->insertRevisionOn( $rev, $dbw );
130 $page->doDeleteArticleReal( 'Just a test deletion' );
132 $this->archivedPage
= new PageArchive( $page->getTitle() );
136 * @covers PageArchive::undelete
137 * @covers PageArchive::undeleteRevisions
139 public function testUndeleteRevisions() {
140 // TODO: MCR: Test undeletion with multiple slots. Check that slots remain untouched.
142 // First make sure old revisions are archived
143 $dbr = wfGetDB( DB_REPLICA
);
144 $arQuery = Revision
::getArchiveQueryInfo();
145 $row = $dbr->selectRow(
148 [ 'ar_rev_id' => $this->ipRev
->getId() ],
153 $this->assertEquals( $this->ipEditor
, $row->ar_user_text
);
155 // Should not be in revision
156 $row = $dbr->selectRow( 'revision', '1', [ 'rev_id' => $this->ipRev
->getId() ] );
157 $this->assertFalse( $row );
159 // Should not be in ip_changes
160 $row = $dbr->selectRow( 'ip_changes', '1', [ 'ipc_rev_id' => $this->ipRev
->getId() ] );
161 $this->assertFalse( $row );
164 $this->archivedPage
->undelete( [] );
166 // Should be back in revision
167 $revQuery = Revision
::getQueryInfo();
168 $row = $dbr->selectRow(
171 [ 'rev_id' => $this->ipRev
->getId() ],
176 $this->assertNotFalse( $row, 'row exists in revision table' );
177 $this->assertEquals( $this->ipEditor
, $row->rev_user_text
);
179 // Should be back in ip_changes
180 $row = $dbr->selectRow( 'ip_changes', [ 'ipc_hex' ], [ 'ipc_rev_id' => $this->ipRev
->getId() ] );
181 $this->assertNotFalse( $row, 'row exists in ip_changes table' );
182 $this->assertEquals( IP
::toHex( $this->ipEditor
), $row->ipc_hex
);
185 abstract protected function getExpectedArchiveRows();
188 * @covers PageArchive::listRevisions
190 public function testListRevisions() {
191 $revisions = $this->archivedPage
->listRevisions();
192 $this->assertEquals( 2, $revisions->numRows() );
194 // Get the rows as arrays
195 $row0 = (array)$revisions->current();
196 $row1 = (array)$revisions->next();
198 $expectedRows = $this->getExpectedArchiveRows();
211 * @covers PageArchive::listPagesBySearch
213 public function testListPagesBySearch() {
214 $pages = PageArchive
::listPagesBySearch( 'PageArchiveTest_thePage' );
215 $this->assertSame( 1, $pages->numRows() );
217 $page = (array)$pages->current();
221 'ar_namespace' => '0',
222 'ar_title' => 'PageArchiveTest_thePage',
230 * @covers PageArchive::listPagesBySearch
232 public function testListPagesByPrefix() {
233 $pages = PageArchive
::listPagesByPrefix( 'PageArchiveTest' );
234 $this->assertSame( 1, $pages->numRows() );
236 $page = (array)$pages->current();
240 'ar_namespace' => '0',
241 'ar_title' => 'PageArchiveTest_thePage',
248 public function provideGetTextFromRowThrowsInvalidArgumentException() {
249 yield
'missing ar_text_id field' => [ [] ];
250 yield
'ar_text_id is null' => [ [ 'ar_text_id' => null ] ];
251 yield
'ar_text_id is zero' => [ [ 'ar_text_id' => 0 ] ];
252 yield
'ar_text_id is "0"' => [ [ 'ar_text_id' => '0' ] ];
256 * @covers PageArchive::getLastRevisionId
258 public function testGetLastRevisionId() {
259 $id = $this->archivedPage
->getLastRevisionId();
260 $this->assertSame( $this->ipRev
->getId(), $id );
264 * @covers PageArchive::isDeleted
266 public function testIsDeleted() {
267 $this->assertTrue( $this->archivedPage
->isDeleted() );
271 * @covers PageArchive::getRevision
273 public function testGetRevision() {
274 $rev = $this->archivedPage
->getRevision( $this->ipRev
->getTimestamp() );
275 $this->assertNotNull( $rev );
276 $this->assertSame( $this->pageId
, $rev->getPage() );
278 $rev = $this->archivedPage
->getRevision( '22991212115555' );
279 $this->assertNull( $rev );
283 * @covers PageArchive::getRevision
285 public function testGetArchivedRevision() {
286 $rev = $this->archivedPage
->getArchivedRevision( $this->ipRev
->getId() );
287 $this->assertNotNull( $rev );
288 $this->assertSame( $this->ipRev
->getTimestamp(), $rev->getTimestamp() );
289 $this->assertSame( $this->pageId
, $rev->getPage() );
291 $rev = $this->archivedPage
->getArchivedRevision( 632546 );
292 $this->assertNull( $rev );
296 * @covers PageArchive::getPreviousRevision
298 public function testGetPreviousRevision() {
299 $rev = $this->archivedPage
->getPreviousRevision( $this->ipRev
->getTimestamp() );
300 $this->assertNotNull( $rev );
301 $this->assertSame( $this->firstRev
->getId(), $rev->getId() );
303 $rev = $this->archivedPage
->getPreviousRevision( $this->firstRev
->getTimestamp() );
304 $this->assertNull( $rev );
306 // Re-create our dummy page
307 $title = Title
::newFromText( 'PageArchiveTest_thePage' );
308 $page = new WikiPage( $title );
309 $content = ContentHandler
::makeContent(
312 CONTENT_MODEL_WIKITEXT
315 $user = $this->getTestUser()->getUser();
316 $status = $page->doEditContent( $content, 'testing', EDIT_NEW
, false, $user );
318 /** @var Revision $newRev */
319 $newRev = $status->value
['revision'];
321 // force the revision timestamp
322 $newTimestamp = wfTimestamp(
324 wfTimestamp( TS_UNIX
, $this->ipRev
->getTimestamp() ) +
1
329 [ 'rev_timestamp' => $this->db
->timestamp( $newTimestamp ) ],
330 [ 'rev_id' => $newRev->getId() ]
333 // check that we don't get the existing revision too soon.
334 $rev = $this->archivedPage
->getPreviousRevision( $newTimestamp );
335 $this->assertNotNull( $rev );
336 $this->assertSame( $this->ipRev
->getId(), $rev->getId() );
338 // check that we do get the existing revision when appropriate.
339 $afterNewTimestamp = wfTimestamp(
341 wfTimestamp( TS_UNIX
, $newTimestamp ) +
1
344 $rev = $this->archivedPage
->getPreviousRevision( $afterNewTimestamp );
345 $this->assertNotNull( $rev );
346 $this->assertSame( $newRev->getId(), $rev->getId() );