3 use MediaWiki\MediaWikiServices
;
4 use MediaWiki\Revision\RevisionRecord
;
7 * Base class for tests of PageArchive against different database schemas.
9 abstract class PageArchiveTestBase
extends MediaWikiTestCase
{
17 * @var PageArchive $archivedPage
19 protected $archivedPage;
22 * A logged out user who edited the page before it was archived.
23 * @var string $ipEditor
28 * Revision of the first (initial) edit
34 * Revision of the IP edit (the second edit)
39 function __construct( $name = null, array $data = [], $dataName = '' ) {
40 parent
::__construct( $name, $data, $dataName );
42 $this->tablesUsed
= array_merge(
47 'revision_comment_temp',
59 protected function addCoreDBData() {
60 // Blank out to avoid failures when schema overrides imposed by subclasses
61 // affect revision storage.
67 abstract protected function getMcrMigrationStage();
72 abstract protected function getMcrTablesToReset();
77 protected function getContentHandlerUseDB() {
81 protected function setUp() {
84 $this->tablesUsed +
= $this->getMcrTablesToReset();
86 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_NEW
);
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 * @covers PageArchive::getLastRevisionId
259 public function testGetLastRevisionId() {
260 $id = $this->archivedPage
->getLastRevisionId();
261 $this->assertSame( $this->ipRev
->getId(), $id );
265 * @covers PageArchive::isDeleted
267 public function testIsDeleted() {
268 $this->assertTrue( $this->archivedPage
->isDeleted() );
272 * @covers PageArchive::getRevision
274 public function testGetRevision() {
275 $rev = $this->archivedPage
->getRevision( $this->ipRev
->getTimestamp() );
276 $this->assertNotNull( $rev );
277 $this->assertSame( $this->pageId
, $rev->getPage() );
279 $rev = $this->archivedPage
->getRevision( '22991212115555' );
280 $this->assertNull( $rev );
284 * @covers PageArchive::getRevision
286 public function testGetArchivedRevision() {
287 $rev = $this->archivedPage
->getArchivedRevision( $this->ipRev
->getId() );
288 $this->assertNotNull( $rev );
289 $this->assertSame( $this->ipRev
->getTimestamp(), $rev->getTimestamp() );
290 $this->assertSame( $this->pageId
, $rev->getPage() );
292 $rev = $this->archivedPage
->getArchivedRevision( 632546 );
293 $this->assertNull( $rev );
297 * @covers PageArchive::getPreviousRevision
299 public function testGetPreviousRevision() {
300 $rev = $this->archivedPage
->getPreviousRevision( $this->ipRev
->getTimestamp() );
301 $this->assertNotNull( $rev );
302 $this->assertSame( $this->firstRev
->getId(), $rev->getId() );
304 $rev = $this->archivedPage
->getPreviousRevision( $this->firstRev
->getTimestamp() );
305 $this->assertNull( $rev );
307 // Re-create our dummy page
308 $title = Title
::newFromText( 'PageArchiveTest_thePage' );
309 $page = new WikiPage( $title );
310 $content = ContentHandler
::makeContent(
313 CONTENT_MODEL_WIKITEXT
316 $user = $this->getTestUser()->getUser();
317 $status = $page->doEditContent( $content, 'testing', EDIT_NEW
, false, $user );
319 /** @var Revision $newRev */
320 $newRev = $status->value
['revision'];
322 // force the revision timestamp
323 $newTimestamp = wfTimestamp(
325 wfTimestamp( TS_UNIX
, $this->ipRev
->getTimestamp() ) +
1
330 [ 'rev_timestamp' => $this->db
->timestamp( $newTimestamp ) ],
331 [ 'rev_id' => $newRev->getId() ]
334 // check that we don't get the existing revision too soon.
335 $rev = $this->archivedPage
->getPreviousRevision( $newTimestamp );
336 $this->assertNotNull( $rev );
337 $this->assertSame( $this->ipRev
->getId(), $rev->getId() );
339 // check that we do get the existing revision when appropriate.
340 $afterNewTimestamp = wfTimestamp(
342 wfTimestamp( TS_UNIX
, $newTimestamp ) +
1
345 $rev = $this->archivedPage
->getPreviousRevision( $afterNewTimestamp );
346 $this->assertNotNull( $rev );
347 $this->assertSame( $newRev->getId(), $rev->getId() );