| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Revision; |
| 4 |
|
| 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; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class ArchivedRevisionLookupTest extends MediaWikiIntegrationTestCase { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
protected $pageId; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
protected $archivedPage; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
protected $neverExistingPage; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
protected $firstRev; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
protected $secondRev; |
| 47 |
|
| 48 |
protected function setUp(): void { |
| 49 |
parent::setUp(); |
| 50 |
|
| 51 |
$timestamp = 1635000000; |
| 52 |
MWTimestamp::setFakeTime( $timestamp ); |
| 53 |
|
| 54 |
$this->neverExistingPage = PageIdentityValue::localIdentity( |
| 55 |
0, 0, 'ArchivedRevisionLookupTest_theNeverexistingPage' ); |
| 56 |
|
| 57 |
|
| 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 |
| 64 |
); |
| 65 |
|
| 66 |
$user = $this->getTestUser()->getUser(); |
| 67 |
$page->doUserEditContent( $content, $user, 'testing', EDIT_NEW | EDIT_SUPPRESS_RC ); |
| 68 |
|
| 69 |
$this->pageId = $page->getId(); |
| 70 |
$this->firstRev = $page->getRevisionRecord(); |
| 71 |
|
| 72 |
$timestamp += 10; |
| 73 |
|
| 74 |
$revisionStore = $this->getServiceContainer()->getRevisionStore(); |
| 75 |
|
| 76 |
$newContent = ContentHandler::makeContent( |
| 77 |
'Lorem Ipsum', |
| 78 |
$page->getTitle(), |
| 79 |
CONTENT_MODEL_WIKITEXT |
| 80 |
); |
| 81 |
|
| 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' ) ); |
| 87 |
|
| 88 |
$this->secondRev = $revisionStore->insertRevisionOn( $rev, $this->getDb() ); |
| 89 |
|
| 90 |
|
| 91 |
$timestamp += 10; |
| 92 |
MWTimestamp::setFakeTime( $timestamp ); |
| 93 |
$this->deletePage( $page, '', $user ); |
| 94 |
} |
| 95 |
|
| 96 |
protected function getExpectedArchiveRows() { |
| 97 |
return [ |
| 98 |
[ |
| 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() ), |
| 117 |
], |
| 118 |
[ |
| 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', |
| 137 |
], |
| 138 |
]; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function testListRevisions() { |
| 145 |
$lookup = $this->getServiceContainer()->getArchivedRevisionLookup(); |
| 146 |
$revisions = $lookup->listRevisions( $this->archivedPage ); |
| 147 |
$this->assertEquals( 2, $revisions->numRows() ); |
| 148 |
|
| 149 |
$row0 = (array)$revisions->current(); |
| 150 |
$row1 = (array)$revisions->fetchObject(); |
| 151 |
|
| 152 |
$expectedRows = $this->getExpectedArchiveRows(); |
| 153 |
|
| 154 |
$this->assertEquals( |
| 155 |
$expectedRows[0], |
| 156 |
$row0 |
| 157 |
); |
| 158 |
$this->assertEquals( |
| 159 |
$expectedRows[1], |
| 160 |
$row1 |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public function testListRevisions_slots() { |
| 168 |
$lookup = $this->getServiceContainer()->getArchivedRevisionLookup(); |
| 169 |
$revisions = $lookup->listRevisions( $this->archivedPage ); |
| 170 |
|
| 171 |
$revisionStore = $this->getServiceContainer()->getRevisionStore(); |
| 172 |
$slotsQuery = $revisionStore->getSlotsQueryInfo( [ 'content' ] ); |
| 173 |
|
| 174 |
foreach ( $revisions as $row ) { |
| 175 |
$this->newSelectQueryBuilder() |
| 176 |
->select( 'count(*)' ) |
| 177 |
->queryInfo( [ |
| 178 |
'tables' => $slotsQuery['tables'], |
| 179 |
'joins' => $slotsQuery['joins'] |
| 180 |
] ) |
| 181 |
->where( [ 'slot_revision_id' => $row->ar_rev_id ] ) |
| 182 |
->assertFieldValue( 1 ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 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 |
|
| 198 |
$row0 = (array)$revisions->fetchObject(); |
| 199 |
|
| 200 |
$expectedRows = $this->getExpectedArchiveRows(); |
| 201 |
|
| 202 |
$this->assertEquals( |
| 203 |
$expectedRows[1], |
| 204 |
$row0 |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 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 ) ); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public function testHasArchivedRevisions() { |
| 222 |
$lookup = $this->getServiceContainer()->getArchivedRevisionLookup(); |
| 223 |
$this->assertTrue( $lookup->hasArchivedRevisions( $this->archivedPage ) ); |
| 224 |
$this->assertFalse( $lookup->hasArchivedRevisions( $this->neverExistingPage ) ); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public function testGetRevisionRecordByTimestamp() { |
| 232 |
$lookup = $this->getServiceContainer()->getArchivedRevisionLookup(); |
| 233 |
$revRecord = $lookup->getRevisionRecordByTimestamp( |
| 234 |
$this->archivedPage, |
| 235 |
$this->secondRev->getTimestamp() |
| 236 |
); |
| 237 |
$this->assertNotNull( $revRecord ); |
| 238 |
$this->assertSame( $this->secondRev->getId(), $revRecord->getId() ); |
| 239 |
|
| 240 |
$revRecord = $lookup->getRevisionRecordByTimestamp( |
| 241 |
$this->archivedPage, |
| 242 |
'22991212115555' |
| 243 |
); |
| 244 |
$this->assertNull( $revRecord ); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public function testGetArchivedRevisionRecord() { |
| 252 |
$lookup = $this->getServiceContainer()->getArchivedRevisionLookup(); |
| 253 |
$revRecord = $lookup->getArchivedRevisionRecord( |
| 254 |
$this->archivedPage, |
| 255 |
$this->secondRev->getId() |
| 256 |
); |
| 257 |
$this->assertNotNull( $revRecord ); |
| 258 |
$this->assertSame( $this->pageId, $revRecord->getPageId() ); |
| 259 |
|
| 260 |
$revRecord = $lookup->getArchivedRevisionRecord( |
| 261 |
$this->archivedPage, |
| 262 |
$this->secondRev->getId() + 42 |
| 263 |
); |
| 264 |
$this->assertNull( $revRecord ); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function testGetPreviousRevisionRecord() { |
| 272 |
$lookup = $this->getServiceContainer()->getArchivedRevisionLookup(); |
| 273 |
|
| 274 |
$timestamp = wfTimestamp( TS_UNIX, $this->secondRev->getTimestamp() ) + 1; |
| 275 |
$prevRec = $lookup->getPreviousRevisionRecord( |
| 276 |
$this->archivedPage, |
| 277 |
wfTimestamp( TS_MW, $timestamp ) |
| 278 |
); |
| 279 |
$this->assertNotNull( $prevRec ); |
| 280 |
$this->assertEquals( $this->secondRev->getId(), $prevRec->getId() ); |
| 281 |
|
| 282 |
$prevRec = $lookup->getPreviousRevisionRecord( |
| 283 |
$this->archivedPage, |
| 284 |
wfTimestamp( TS_MW, $this->secondRev->getTimestamp() ) |
| 285 |
); |
| 286 |
$this->assertNotNull( $prevRec ); |
| 287 |
$this->assertEquals( $this->firstRev->getId(), $prevRec->getId() ); |
| 288 |
|
| 289 |
$prevRec = $lookup->getPreviousRevisionRecord( |
| 290 |
$this->neverExistingPage, |
| 291 |
wfTimestamp( TS_MW, $this->secondRev->getTimestamp() ) |
| 292 |
); |
| 293 |
$this->assertNull( $prevRec ); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public function testGetPreviousRevisionRecord_recreatedPage() { |
| 301 |
|
| 302 |
$timestamp = wfTimestamp( TS_UNIX, $this->secondRev->getTimestamp() ) + 10; |
| 303 |
MWTimestamp::setFakeTime( $timestamp ); |
| 304 |
|
| 305 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $this->archivedPage ); |
| 306 |
|
| 307 |
$content = ContentHandler::makeContent( |
| 308 |
'recreated page', |
| 309 |
$page->getTitle(), |
| 310 |
CONTENT_MODEL_WIKITEXT |
| 311 |
); |
| 312 |
$page->doUserEditContent( |
| 313 |
$content, |
| 314 |
$this->getTestUser()->getUser(), |
| 315 |
'testing', |
| 316 |
EDIT_NEW | EDIT_SUPPRESS_RC |
| 317 |
); |
| 318 |
$newRev = $page->getRevisionRecord(); |
| 319 |
|
| 320 |
$lookup = $this->getServiceContainer()->getArchivedRevisionLookup(); |
| 321 |
$prevRec = $lookup->getPreviousRevisionRecord( |
| 322 |
$this->archivedPage, |
| 323 |
wfTimestamp( TS_MW, $timestamp + 1 ) |
| 324 |
); |
| 325 |
$this->assertEquals( $newRev->getId(), $prevRec->getId() ); |
| 326 |
|
| 327 |
$prevRec = $lookup->getPreviousRevisionRecord( |
| 328 |
$this->archivedPage, |
| 329 |
wfTimestamp( TS_MW, $timestamp - 1 ) |
| 330 |
); |
| 331 |
$this->assertEquals( $this->secondRev->getId(), $prevRec->getId() ); |
| 332 |
} |
| 333 |
|
| 334 |
} |