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( 'wgCommentTableSchemaMigrationStage', MIGRATION_NEW
);
86 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_OLD
);
87 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
89 'wgMultiContentRevisionSchemaMigrationStage',
90 $this->getMcrMigrationStage()
92 $this->overrideMwServices();
94 // First create our dummy page
95 $page = Title
::newFromText( 'PageArchiveTest_thePage' );
96 $page = new WikiPage( $page );
97 $content = ContentHandler
::makeContent(
100 CONTENT_MODEL_WIKITEXT
103 $user = $this->getTestUser()->getUser();
104 $page->doEditContent( $content, 'testing', EDIT_NEW
, false, $user );
106 $this->pageId
= $page->getId();
107 $this->firstRev
= $page->getRevision()->getRevisionRecord();
109 // Insert IP revision
110 $this->ipEditor
= '2001:db8::1';
112 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
114 $ipTimestamp = wfTimestamp(
116 wfTimestamp( TS_UNIX
, $this->firstRev
->getTimestamp() ) +
1
119 $rev = $revisionStore->newMutableRevisionFromArray( [
120 'text' => 'Lorem Ipsum',
121 'comment' => 'just a test',
122 'page' => $page->getId(),
123 'user_text' => $this->ipEditor
,
124 'timestamp' => $ipTimestamp,
127 $dbw = wfGetDB( DB_MASTER
);
128 $this->ipRev
= $revisionStore->insertRevisionOn( $rev, $dbw );
131 $page->doDeleteArticleReal( 'Just a test deletion' );
133 $this->archivedPage
= new PageArchive( $page->getTitle() );
137 * @covers PageArchive::undelete
138 * @covers PageArchive::undeleteRevisions
140 public function testUndeleteRevisions() {
141 // TODO: MCR: Test undeletion with multiple slots. Check that slots remain untouched.
143 // First make sure old revisions are archived
144 $dbr = wfGetDB( DB_REPLICA
);
145 $arQuery = Revision
::getArchiveQueryInfo();
146 $row = $dbr->selectRow(
149 [ 'ar_rev_id' => $this->ipRev
->getId() ],
154 $this->assertEquals( $this->ipEditor
, $row->ar_user_text
);
156 // Should not be in revision
157 $row = $dbr->selectRow( 'revision', '1', [ 'rev_id' => $this->ipRev
->getId() ] );
158 $this->assertFalse( $row );
160 // Should not be in ip_changes
161 $row = $dbr->selectRow( 'ip_changes', '1', [ 'ipc_rev_id' => $this->ipRev
->getId() ] );
162 $this->assertFalse( $row );
165 $this->archivedPage
->undelete( [] );
167 // Should be back in revision
168 $revQuery = Revision
::getQueryInfo();
169 $row = $dbr->selectRow(
172 [ 'rev_id' => $this->ipRev
->getId() ],
177 $this->assertNotFalse( $row, 'row exists in revision table' );
178 $this->assertEquals( $this->ipEditor
, $row->rev_user_text
);
180 // Should be back in ip_changes
181 $row = $dbr->selectRow( 'ip_changes', [ 'ipc_hex' ], [ 'ipc_rev_id' => $this->ipRev
->getId() ] );
182 $this->assertNotFalse( $row, 'row exists in ip_changes table' );
183 $this->assertEquals( IP
::toHex( $this->ipEditor
), $row->ipc_hex
);
186 abstract protected function getExpectedArchiveRows();
189 * @covers PageArchive::listRevisions
191 public function testListRevisions() {
192 $revisions = $this->archivedPage
->listRevisions();
193 $this->assertEquals( 2, $revisions->numRows() );
195 // Get the rows as arrays
196 $row0 = (array)$revisions->current();
197 $row1 = (array)$revisions->next();
199 $expectedRows = $this->getExpectedArchiveRows();
212 * @covers PageArchive::listPagesBySearch
214 public function testListPagesBySearch() {
215 $pages = PageArchive
::listPagesBySearch( 'PageArchiveTest_thePage' );
216 $this->assertSame( 1, $pages->numRows() );
218 $page = (array)$pages->current();
222 'ar_namespace' => '0',
223 'ar_title' => 'PageArchiveTest_thePage',
231 * @covers PageArchive::listPagesBySearch
233 public function testListPagesByPrefix() {
234 $pages = PageArchive
::listPagesByPrefix( 'PageArchiveTest' );
235 $this->assertSame( 1, $pages->numRows() );
237 $page = (array)$pages->current();
241 'ar_namespace' => '0',
242 'ar_title' => 'PageArchiveTest_thePage',
249 public function provideGetTextFromRowThrowsInvalidArgumentException() {
250 yield
'missing ar_text_id field' => [ [] ];
251 yield
'ar_text_id is null' => [ [ 'ar_text_id' => null ] ];
252 yield
'ar_text_id is zero' => [ [ 'ar_text_id' => 0 ] ];
253 yield
'ar_text_id is "0"' => [ [ 'ar_text_id' => '0' ] ];
257 * @dataProvider provideGetTextFromRowThrowsInvalidArgumentException
258 * @covers PageArchive::getTextFromRow
260 public function testGetTextFromRowThrowsInvalidArgumentException( array $row ) {
261 $this->hideDeprecated( PageArchive
::class . '::getTextFromRow' );
262 $this->setExpectedException( InvalidArgumentException
::class );
264 $this->archivedPage
->getTextFromRow( (object)$row );
268 * @covers PageArchive::getLastRevisionText
270 public function testGetLastRevisionText() {
271 $this->hideDeprecated( PageArchive
::class . '::getLastRevisionText' );
273 $text = $this->archivedPage
->getLastRevisionText();
274 $this->assertSame( 'Lorem Ipsum', $text );
278 * @covers PageArchive::getLastRevisionId
280 public function testGetLastRevisionId() {
281 $id = $this->archivedPage
->getLastRevisionId();
282 $this->assertSame( $this->ipRev
->getId(), $id );
286 * @covers PageArchive::isDeleted
288 public function testIsDeleted() {
289 $this->assertTrue( $this->archivedPage
->isDeleted() );
293 * @covers PageArchive::getRevision
295 public function testGetRevision() {
296 $rev = $this->archivedPage
->getRevision( $this->ipRev
->getTimestamp() );
297 $this->assertNotNull( $rev );
298 $this->assertSame( $this->pageId
, $rev->getPage() );
300 $rev = $this->archivedPage
->getRevision( '22991212115555' );
301 $this->assertNull( $rev );
305 * @covers PageArchive::getRevision
307 public function testGetArchivedRevision() {
308 $rev = $this->archivedPage
->getArchivedRevision( $this->ipRev
->getId() );
309 $this->assertNotNull( $rev );
310 $this->assertSame( $this->ipRev
->getTimestamp(), $rev->getTimestamp() );
311 $this->assertSame( $this->pageId
, $rev->getPage() );
313 $rev = $this->archivedPage
->getArchivedRevision( 632546 );
314 $this->assertNull( $rev );
318 * @covers PageArchive::getPreviousRevision
320 public function testGetPreviousRevision() {
321 $rev = $this->archivedPage
->getPreviousRevision( $this->ipRev
->getTimestamp() );
322 $this->assertNotNull( $rev );
323 $this->assertSame( $this->firstRev
->getId(), $rev->getId() );
325 $rev = $this->archivedPage
->getPreviousRevision( $this->firstRev
->getTimestamp() );
326 $this->assertNull( $rev );
328 // Re-create our dummy page
329 $title = Title
::newFromText( 'PageArchiveTest_thePage' );
330 $page = new WikiPage( $title );
331 $content = ContentHandler
::makeContent(
334 CONTENT_MODEL_WIKITEXT
337 $user = $this->getTestUser()->getUser();
338 $status = $page->doEditContent( $content, 'testing', EDIT_NEW
, false, $user );
340 /** @var Revision $newRev */
341 $newRev = $status->value
['revision'];
343 // force the revision timestamp
344 $newTimestamp = wfTimestamp(
346 wfTimestamp( TS_UNIX
, $this->ipRev
->getTimestamp() ) +
1
351 [ 'rev_timestamp' => $this->db
->timestamp( $newTimestamp ) ],
352 [ 'rev_id' => $newRev->getId() ]
355 // check that we don't get the existing revision too soon.
356 $rev = $this->archivedPage
->getPreviousRevision( $newTimestamp );
357 $this->assertNotNull( $rev );
358 $this->assertSame( $this->ipRev
->getId(), $rev->getId() );
360 // check that we do get the existing revision when appropriate.
361 $afterNewTimestamp = wfTimestamp(
363 wfTimestamp( TS_UNIX
, $newTimestamp ) +
1
366 $rev = $this->archivedPage
->getPreviousRevision( $afterNewTimestamp );
367 $this->assertNotNull( $rev );
368 $this->assertSame( $newRev->getId(), $rev->getId() );