2 use MediaWiki\MediaWikiServices
;
3 use MediaWiki\Storage\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(
56 protected function addCoreDBData() {
57 // Blank out to avoid failures when schema overrides imposed by subclasses
58 // affect revision storage.
64 abstract protected function getMcrMigrationStage();
69 abstract protected function getMcrTablesToReset();
74 protected function getContentHandlerUseDB() {
78 protected function setUp() {
81 $this->tablesUsed +
= $this->getMcrTablesToReset();
83 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
84 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
85 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
87 'wgMultiContentRevisionSchemaMigrationStage',
88 $this->getMcrMigrationStage()
90 $this->overrideMwServices();
92 // First create our dummy page
93 $page = Title
::newFromText( 'PageArchiveTest_thePage' );
94 $page = new WikiPage( $page );
95 $content = ContentHandler
::makeContent(
98 CONTENT_MODEL_WIKITEXT
101 $user = $this->getTestUser()->getUser();
102 $page->doEditContent( $content, 'testing', EDIT_NEW
, false, $user );
104 $this->pageId
= $page->getId();
105 $this->firstRev
= $page->getRevision()->getRevisionRecord();
107 // Insert IP revision
108 $this->ipEditor
= '2001:db8::1';
110 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
112 $ipTimestamp = wfTimestamp(
114 wfTimestamp( TS_UNIX
, $this->firstRev
->getTimestamp() ) +
1
117 $rev = $revisionStore->newMutableRevisionFromArray( [
118 'text' => 'Lorem Ipsum',
119 'comment' => 'just a test',
120 'page' => $page->getId(),
121 'user_text' => $this->ipEditor
,
122 'timestamp' => $ipTimestamp,
125 $dbw = wfGetDB( DB_MASTER
);
126 $this->ipRev
= $revisionStore->insertRevisionOn( $rev, $dbw );
129 $page->doDeleteArticleReal( 'Just a test deletion' );
131 $this->archivedPage
= new PageArchive( $page->getTitle() );
135 * @covers PageArchive::undelete
136 * @covers PageArchive::undeleteRevisions
138 public function testUndeleteRevisions() {
139 // TODO: MCR: Test undeletion with multiple slots. Check that slots remain untouched.
141 // First make sure old revisions are archived
142 $dbr = wfGetDB( DB_REPLICA
);
143 $arQuery = Revision
::getArchiveQueryInfo();
144 $row = $dbr->selectRow(
147 [ 'ar_rev_id' => $this->ipRev
->getId() ],
152 $this->assertEquals( $this->ipEditor
, $row->ar_user_text
);
154 // Should not be in revision
155 $row = $dbr->selectRow( 'revision', '1', [ 'rev_id' => $this->ipRev
->getId() ] );
156 $this->assertFalse( $row );
158 // Should not be in ip_changes
159 $row = $dbr->selectRow( 'ip_changes', '1', [ 'ipc_rev_id' => $this->ipRev
->getId() ] );
160 $this->assertFalse( $row );
163 $this->archivedPage
->undelete( [] );
165 // Should be back in revision
166 $revQuery = Revision
::getQueryInfo();
167 $row = $dbr->selectRow(
170 [ 'rev_id' => $this->ipRev
->getId() ],
175 $this->assertNotFalse( $row, 'row exists in revision table' );
176 $this->assertEquals( $this->ipEditor
, $row->rev_user_text
);
178 // Should be back in ip_changes
179 $row = $dbr->selectRow( 'ip_changes', [ 'ipc_hex' ], [ 'ipc_rev_id' => $this->ipRev
->getId() ] );
180 $this->assertNotFalse( $row, 'row exists in ip_changes table' );
181 $this->assertEquals( IP
::toHex( $this->ipEditor
), $row->ipc_hex
);
184 abstract protected function getExpectedArchiveRows();
187 * @covers PageArchive::listRevisions
189 public function testListRevisions() {
190 // FIXME: fails under postgres
191 $this->markTestSkippedIfDbType( 'postgres' );
193 $revisions = $this->archivedPage
->listRevisions();
194 $this->assertEquals( 2, $revisions->numRows() );
196 // Get the rows as arrays
197 $row0 = (array)$revisions->current();
198 $row1 = (array)$revisions->next();
200 $expectedRows = $this->getExpectedArchiveRows();
213 * @covers PageArchive::listPagesBySearch
215 public function testListPagesBySearch() {
216 $pages = PageArchive
::listPagesBySearch( 'PageArchiveTest_thePage' );
217 $this->assertSame( 1, $pages->numRows() );
219 $page = (array)$pages->current();
223 'ar_namespace' => '0',
224 'ar_title' => 'PageArchiveTest_thePage',
232 * @covers PageArchive::listPagesBySearch
234 public function testListPagesByPrefix() {
235 $pages = PageArchive
::listPagesByPrefix( 'PageArchiveTest' );
236 $this->assertSame( 1, $pages->numRows() );
238 $page = (array)$pages->current();
242 'ar_namespace' => '0',
243 'ar_title' => 'PageArchiveTest_thePage',
250 public function provideGetTextFromRowThrowsInvalidArgumentException() {
251 yield
'missing ar_text_id field' => [ [] ];
252 yield
'ar_text_id is null' => [ [ 'ar_text_id' => null ] ];
253 yield
'ar_text_id is zero' => [ [ 'ar_text_id' => 0 ] ];
254 yield
'ar_text_id is "0"' => [ [ 'ar_text_id' => '0' ] ];
258 * @dataProvider provideGetTextFromRowThrowsInvalidArgumentException
259 * @covers PageArchive::getTextFromRow
261 public function testGetTextFromRowThrowsInvalidArgumentException( array $row ) {
262 $this->hideDeprecated( PageArchive
::class . '::getTextFromRow' );
263 $this->setExpectedException( InvalidArgumentException
::class );
265 $this->archivedPage
->getTextFromRow( (object)$row );
269 * @covers PageArchive::getLastRevisionText
271 public function testGetLastRevisionText() {
272 $this->hideDeprecated( PageArchive
::class . '::getLastRevisionText' );
274 $text = $this->archivedPage
->getLastRevisionText();
275 $this->assertSame( 'Lorem Ipsum', $text );
279 * @covers PageArchive::getLastRevisionId
281 public function testGetLastRevisionId() {
282 $id = $this->archivedPage
->getLastRevisionId();
283 $this->assertSame( $this->ipRev
->getId(), $id );
287 * @covers PageArchive::isDeleted
289 public function testIsDeleted() {
290 $this->assertTrue( $this->archivedPage
->isDeleted() );
294 * @covers PageArchive::getRevision
296 public function testGetRevision() {
297 $rev = $this->archivedPage
->getRevision( $this->ipRev
->getTimestamp() );
298 $this->assertNotNull( $rev );
299 $this->assertSame( $this->pageId
, $rev->getPage() );
301 $rev = $this->archivedPage
->getRevision( '22991212115555' );
302 $this->assertNull( $rev );
306 * @covers PageArchive::getRevision
308 public function testGetArchivedRevision() {
309 $rev = $this->archivedPage
->getArchivedRevision( $this->ipRev
->getId() );
310 $this->assertNotNull( $rev );
311 $this->assertSame( $this->ipRev
->getTimestamp(), $rev->getTimestamp() );
312 $this->assertSame( $this->pageId
, $rev->getPage() );
314 $rev = $this->archivedPage
->getArchivedRevision( 632546 );
315 $this->assertNull( $rev );
319 * @covers PageArchive::getPreviousRevision
321 public function testGetPreviousRevision() {
322 $rev = $this->archivedPage
->getPreviousRevision( $this->ipRev
->getTimestamp() );
323 $this->assertNotNull( $rev );
324 $this->assertSame( $this->firstRev
->getId(), $rev->getId() );
326 $rev = $this->archivedPage
->getPreviousRevision( $this->firstRev
->getTimestamp() );
327 $this->assertNull( $rev );
329 // Re-create our dummy page
330 $title = Title
::newFromText( 'PageArchiveTest_thePage' );
331 $page = new WikiPage( $title );
332 $content = ContentHandler
::makeContent(
335 CONTENT_MODEL_WIKITEXT
338 $user = $this->getTestUser()->getUser();
339 $status = $page->doEditContent( $content, 'testing', EDIT_NEW
, false, $user );
341 /** @var Revision $newRev */
342 $newRev = $status->value
['revision'];
344 // force the revision timestamp
345 $newTimestamp = wfTimestamp(
347 wfTimestamp( TS_UNIX
, $this->ipRev
->getTimestamp() ) +
1
352 [ 'rev_timestamp' => $this->db
->timestamp( $newTimestamp ) ],
353 [ 'rev_id' => $newRev->getId() ]
356 // check that we don't get the existing revision too soon.
357 $rev = $this->archivedPage
->getPreviousRevision( $newTimestamp );
358 $this->assertNotNull( $rev );
359 $this->assertSame( $this->ipRev
->getId(), $rev->getId() );
361 // check that we do get the existing revision when appropriate.
362 $afterNewTimestamp = wfTimestamp(
364 wfTimestamp( TS_UNIX
, $newTimestamp ) +
1
367 $rev = $this->archivedPage
->getPreviousRevision( $afterNewTimestamp );
368 $this->assertNotNull( $rev );
369 $this->assertSame( $newRev->getId(), $rev->getId() );