3 namespace MediaWiki\Tests\Revision
;
5 use MediaWiki\CommentStore\CommentStoreComment
;
6 use MediaWiki\Content\ContentHandler
;
7 use MediaWiki\Page\PageIdentityValue
;
8 use MediaWiki\Revision\MutableRevisionRecord
;
9 use MediaWiki\Revision\RevisionRecord
;
10 use MediaWiki\Revision\SlotRecord
;
11 use MediaWiki\Utils\MWTimestamp
;
12 use MediaWikiIntegrationTestCase
;
16 * @coversDefaultClass \MediaWiki\Revision\ArchivedRevisionLookup
17 * @covers ::__construct
19 class ArchivedRevisionLookupTest
extends MediaWikiIntegrationTestCase
{
27 * @var PageIdentityValue
29 protected $archivedPage;
32 * @var PageIdentityValue
34 protected $neverExistingPage;
37 * Revision of the first (initial) edit
43 * Revision of the second edit
48 protected function setUp(): void
{
51 $timestamp = 1635000000;
52 MWTimestamp
::setFakeTime( $timestamp );
54 $this->neverExistingPage
= PageIdentityValue
::localIdentity(
55 0, 0, 'ArchivedRevisionLookupTest_theNeverexistingPage' );
57 // First create our dummy page
58 $this->archivedPage
= PageIdentityValue
::localIdentity( 0, 0, 'ArchivedRevisionLookupTest_thePage' );
59 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $this->archivedPage
);
60 $content = ContentHandler
::makeContent(
63 CONTENT_MODEL_WIKITEXT
66 $user = $this->getTestUser()->getUser();
67 $page->doUserEditContent( $content, $user, 'testing', EDIT_NEW | EDIT_SUPPRESS_RC
);
69 $this->pageId
= $page->getId();
70 $this->firstRev
= $page->getRevisionRecord();
74 $revisionStore = $this->getServiceContainer()->getRevisionStore();
76 $newContent = ContentHandler
::makeContent(
79 CONTENT_MODEL_WIKITEXT
82 $rev = new MutableRevisionRecord( $page );
83 $rev->setUser( $user );
84 $rev->setTimestamp( $timestamp );
85 $rev->setContent( SlotRecord
::MAIN
, $newContent );
86 $rev->setComment( CommentStoreComment
::newUnsavedComment( 'just a test' ) );
88 $this->secondRev
= $revisionStore->insertRevisionOn( $rev, $this->getDb() );
92 MWTimestamp
::setFakeTime( $timestamp );
93 $this->deletePage( $page, '', $user );
96 protected function getExpectedArchiveRows() {
99 'ar_minor_edit' => '0',
100 'ar_user' => (string)$this->getTestUser()->getUser()->getId(),
101 'ar_user_text' => $this->getTestUser()->getUser()->getName(),
102 'ar_actor' => (string)$this->getTestUser()->getUser()->getActorId(),
105 'ar_rev_id' => strval( $this->secondRev
->getId() ),
106 'ar_timestamp' => $this->getDb()->timestamp( $this->secondRev
->getTimestamp() ),
107 'ar_sha1' => '0qdrpxl537ivfnx4gcpnzz0285yxryy',
108 'ar_page_id' => strval( $this->secondRev
->getPageId() ),
109 'ar_comment_text' => 'just a test',
110 'ar_comment_data' => null,
111 'ar_comment_cid' => strval( $this->secondRev
->getComment()->id
),
114 'ar_namespace' => '0',
115 'ar_title' => 'ArchivedRevisionLookupTest_thePage',
116 'ar_parent_id' => strval( $this->secondRev
->getParentId() ),
119 'ar_minor_edit' => '0',
120 'ar_user' => (string)$this->getTestUser()->getUser()->getId(),
121 'ar_user_text' => $this->getTestUser()->getUser()->getName(),
122 'ar_actor' => (string)$this->getTestUser()->getUser()->getActorId(),
125 'ar_rev_id' => strval( $this->firstRev
->getId() ),
126 'ar_timestamp' => $this->getDb()->timestamp( $this->firstRev
->getTimestamp() ),
127 'ar_sha1' => 'pr0s8e18148pxhgjfa0gjrvpy8fiyxc',
128 'ar_page_id' => strval( $this->firstRev
->getPageId() ),
129 'ar_comment_text' => 'testing',
130 'ar_comment_data' => null,
131 'ar_comment_cid' => strval( $this->firstRev
->getComment()->id
),
134 'ar_namespace' => '0',
135 'ar_title' => 'ArchivedRevisionLookupTest_thePage',
136 'ar_parent_id' => '0',
142 * @covers ::listRevisions
144 public function testListRevisions() {
145 $lookup = $this->getServiceContainer()->getArchivedRevisionLookup();
146 $revisions = $lookup->listRevisions( $this->archivedPage
);
147 $this->assertEquals( 2, $revisions->numRows() );
148 // Get the rows as arrays
149 $row0 = (array)$revisions->current();
150 $row1 = (array)$revisions->fetchObject();
152 $expectedRows = $this->getExpectedArchiveRows();
165 * @covers ::listRevisions
167 public function testListRevisions_slots() {
168 $lookup = $this->getServiceContainer()->getArchivedRevisionLookup();
169 $revisions = $lookup->listRevisions( $this->archivedPage
);
171 $revisionStore = $this->getServiceContainer()->getRevisionStore();
172 $slotsQuery = $revisionStore->getSlotsQueryInfo( [ 'content' ] );
174 foreach ( $revisions as $row ) {
175 $this->newSelectQueryBuilder()
176 ->select( 'count(*)' )
178 'tables' => $slotsQuery['tables'],
179 'joins' => $slotsQuery['joins']
181 ->where( [ 'slot_revision_id' => $row->ar_rev_id
] )
182 ->assertFieldValue( 1 );
187 * @covers ::listRevisions
189 public function testListRevisionsOffsetAndLimit() {
190 $lookup = $this->getServiceContainer()->getArchivedRevisionLookup();
191 $db = $this->getDb();
192 $revisions = $lookup->listRevisions(
194 [ $db->expr( 'ar_timestamp', '<', $db->timestamp( $this->secondRev
->getTimestamp() ) ) ],
196 $this->assertSame( 1, $revisions->numRows() );
197 // Get the rows as arrays
198 $row0 = (array)$revisions->fetchObject();
200 $expectedRows = $this->getExpectedArchiveRows();
209 * @covers ::getLastRevisionId
211 public function testGetLastRevisionId() {
212 $lookup = $this->getServiceContainer()->getArchivedRevisionLookup();
213 $id = $lookup->getLastRevisionId( $this->archivedPage
);
214 $this->assertSame( $this->secondRev
->getId(), $id );
215 $this->assertFalse( $lookup->getLastRevisionId( $this->neverExistingPage
) );
219 * @covers ::hasArchivedRevisions
221 public function testHasArchivedRevisions() {
222 $lookup = $this->getServiceContainer()->getArchivedRevisionLookup();
223 $this->assertTrue( $lookup->hasArchivedRevisions( $this->archivedPage
) );
224 $this->assertFalse( $lookup->hasArchivedRevisions( $this->neverExistingPage
) );
228 * @covers ::getRevisionRecordByTimestamp
229 * @covers ::getRevisionByConditions
231 public function testGetRevisionRecordByTimestamp() {
232 $lookup = $this->getServiceContainer()->getArchivedRevisionLookup();
233 $revRecord = $lookup->getRevisionRecordByTimestamp(
235 $this->secondRev
->getTimestamp()
237 $this->assertNotNull( $revRecord );
238 $this->assertSame( $this->secondRev
->getId(), $revRecord->getId() );
240 $revRecord = $lookup->getRevisionRecordByTimestamp(
244 $this->assertNull( $revRecord );
248 * @covers ::getArchivedRevisionRecord
249 * @covers ::getRevisionByConditions
251 public function testGetArchivedRevisionRecord() {
252 $lookup = $this->getServiceContainer()->getArchivedRevisionLookup();
253 $revRecord = $lookup->getArchivedRevisionRecord(
255 $this->secondRev
->getId()
257 $this->assertNotNull( $revRecord );
258 $this->assertSame( $this->pageId
, $revRecord->getPageId() );
260 $revRecord = $lookup->getArchivedRevisionRecord(
262 $this->secondRev
->getId() +
42
264 $this->assertNull( $revRecord );
268 * @covers ::getPreviousRevisionRecord
269 * @covers ::getRevisionByConditions
271 public function testGetPreviousRevisionRecord() {
272 $lookup = $this->getServiceContainer()->getArchivedRevisionLookup();
274 $timestamp = wfTimestamp( TS_UNIX
, $this->secondRev
->getTimestamp() ) +
1;
275 $prevRec = $lookup->getPreviousRevisionRecord(
277 wfTimestamp( TS_MW
, $timestamp )
279 $this->assertNotNull( $prevRec );
280 $this->assertEquals( $this->secondRev
->getId(), $prevRec->getId() );
282 $prevRec = $lookup->getPreviousRevisionRecord(
284 wfTimestamp( TS_MW
, $this->secondRev
->getTimestamp() )
286 $this->assertNotNull( $prevRec );
287 $this->assertEquals( $this->firstRev
->getId(), $prevRec->getId() );
289 $prevRec = $lookup->getPreviousRevisionRecord(
290 $this->neverExistingPage
,
291 wfTimestamp( TS_MW
, $this->secondRev
->getTimestamp() )
293 $this->assertNull( $prevRec );
297 * @covers ::getPreviousRevisionRecord
298 * @covers ::getRevisionByConditions
300 public function testGetPreviousRevisionRecord_recreatedPage() {
301 // recreate the archived page
302 $timestamp = wfTimestamp( TS_UNIX
, $this->secondRev
->getTimestamp() ) +
10;
303 MWTimestamp
::setFakeTime( $timestamp );
305 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $this->archivedPage
);
307 $content = ContentHandler
::makeContent(
310 CONTENT_MODEL_WIKITEXT
312 $page->doUserEditContent(
314 $this->getTestUser()->getUser(),
316 EDIT_NEW | EDIT_SUPPRESS_RC
318 $newRev = $page->getRevisionRecord();
320 $lookup = $this->getServiceContainer()->getArchivedRevisionLookup();
321 $prevRec = $lookup->getPreviousRevisionRecord(
323 wfTimestamp( TS_MW
, $timestamp +
1 )
325 $this->assertEquals( $newRev->getId(), $prevRec->getId() );
327 $prevRec = $lookup->getPreviousRevisionRecord(
329 wfTimestamp( TS_MW
, $timestamp - 1 )
331 $this->assertEquals( $this->secondRev
->getId(), $prevRec->getId() );