Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / tests / phpunit / includes / Revision / ArchivedRevisionLookupTest.php
blobe28283722afe397a0dd622d33efb3b4798b8bc87
1 <?php
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;
14 /**
15 * @group Database
16 * @coversDefaultClass \MediaWiki\Revision\ArchivedRevisionLookup
17 * @covers ::__construct
19 class ArchivedRevisionLookupTest extends MediaWikiIntegrationTestCase {
21 /**
22 * @var int
24 protected $pageId;
26 /**
27 * @var PageIdentityValue
29 protected $archivedPage;
31 /**
32 * @var PageIdentityValue
34 protected $neverExistingPage;
36 /**
37 * Revision of the first (initial) edit
38 * @var RevisionRecord
40 protected $firstRev;
42 /**
43 * Revision of the second edit
44 * @var RevisionRecord
46 protected $secondRev;
48 protected function setUp(): void {
49 parent::setUp();
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(
61 'testing',
62 $page->getTitle(),
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();
72 $timestamp += 10;
74 $revisionStore = $this->getServiceContainer()->getRevisionStore();
76 $newContent = ContentHandler::makeContent(
77 'Lorem Ipsum',
78 $page->getTitle(),
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() );
90 // Delete the page
91 $timestamp += 10;
92 MWTimestamp::setFakeTime( $timestamp );
93 $this->deletePage( $page, '', $user );
96 protected function getExpectedArchiveRows() {
97 return [
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(),
103 'ar_len' => '11',
104 'ar_deleted' => '0',
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 ),
112 'ts_tags' => null,
113 'ar_id' => '2',
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(),
123 'ar_len' => '7',
124 'ar_deleted' => '0',
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 ),
132 'ts_tags' => null,
133 'ar_id' => '1',
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();
154 $this->assertEquals(
155 $expectedRows[0],
156 $row0
158 $this->assertEquals(
159 $expectedRows[1],
160 $row1
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(*)' )
177 ->queryInfo( [
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(
193 $this->archivedPage,
194 [ $db->expr( 'ar_timestamp', '<', $db->timestamp( $this->secondRev->getTimestamp() ) ) ],
195 1 );
196 $this->assertSame( 1, $revisions->numRows() );
197 // Get the rows as arrays
198 $row0 = (array)$revisions->fetchObject();
200 $expectedRows = $this->getExpectedArchiveRows();
202 $this->assertEquals(
203 $expectedRows[1],
204 $row0
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(
234 $this->archivedPage,
235 $this->secondRev->getTimestamp()
237 $this->assertNotNull( $revRecord );
238 $this->assertSame( $this->secondRev->getId(), $revRecord->getId() );
240 $revRecord = $lookup->getRevisionRecordByTimestamp(
241 $this->archivedPage,
242 '22991212115555'
244 $this->assertNull( $revRecord );
248 * @covers ::getArchivedRevisionRecord
249 * @covers ::getRevisionByConditions
251 public function testGetArchivedRevisionRecord() {
252 $lookup = $this->getServiceContainer()->getArchivedRevisionLookup();
253 $revRecord = $lookup->getArchivedRevisionRecord(
254 $this->archivedPage,
255 $this->secondRev->getId()
257 $this->assertNotNull( $revRecord );
258 $this->assertSame( $this->pageId, $revRecord->getPageId() );
260 $revRecord = $lookup->getArchivedRevisionRecord(
261 $this->archivedPage,
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(
276 $this->archivedPage,
277 wfTimestamp( TS_MW, $timestamp )
279 $this->assertNotNull( $prevRec );
280 $this->assertEquals( $this->secondRev->getId(), $prevRec->getId() );
282 $prevRec = $lookup->getPreviousRevisionRecord(
283 $this->archivedPage,
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(
308 'recreated page',
309 $page->getTitle(),
310 CONTENT_MODEL_WIKITEXT
312 $page->doUserEditContent(
313 $content,
314 $this->getTestUser()->getUser(),
315 'testing',
316 EDIT_NEW | EDIT_SUPPRESS_RC
318 $newRev = $page->getRevisionRecord();
320 $lookup = $this->getServiceContainer()->getArchivedRevisionLookup();
321 $prevRec = $lookup->getPreviousRevisionRecord(
322 $this->archivedPage,
323 wfTimestamp( TS_MW, $timestamp + 1 )
325 $this->assertEquals( $newRev->getId(), $prevRec->getId() );
327 $prevRec = $lookup->getPreviousRevisionRecord(
328 $this->archivedPage,
329 wfTimestamp( TS_MW, $timestamp - 1 )
331 $this->assertEquals( $this->secondRev->getId(), $prevRec->getId() );