| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Unit\Revision; |
| 4 |
|
| 5 |
use DummyContentForTesting; |
| 6 |
use InvalidArgumentException; |
| 7 |
use MediaWiki\CommentStore\CommentStoreComment; |
| 8 |
use MediaWiki\Page\PageIdentity; |
| 9 |
use MediaWiki\Page\PageIdentityValue; |
| 10 |
use MediaWiki\Revision\BadRevisionException; |
| 11 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 12 |
use MediaWiki\Revision\MutableRevisionSlots; |
| 13 |
use MediaWiki\Revision\RevisionAccessException; |
| 14 |
use MediaWiki\Revision\RevisionRecord; |
| 15 |
use MediaWiki\Revision\SlotRecord; |
| 16 |
use MediaWiki\Storage\RevisionSlotsUpdate; |
| 17 |
use MediaWiki\User\UserIdentityValue; |
| 18 |
use MediaWikiUnitTestCase; |
| 19 |
use Wikimedia\Assert\PreconditionException; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class MutableRevisionRecordTest extends MediaWikiUnitTestCase { |
| 26 |
use RevisionRecordTests; |
| 27 |
|
| 28 |
protected function expectedDefaultFieldVisibility( $field ): bool { |
| 29 |
return true; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
protected function newRevision( array $rowOverrides = [] ) { |
| 37 |
$user = new UserIdentityValue( 11, 'Tester' ); |
| 38 |
$comment = CommentStoreComment::newUnsavedComment( 'Hello World' ); |
| 39 |
$wikiId = $rowOverrides['wikiId'] ?? RevisionRecord::LOCAL; |
| 40 |
|
| 41 |
$record = new MutableRevisionRecord( |
| 42 |
new PageIdentityValue( 17, NS_MAIN, 'Dummy', $wikiId ), |
| 43 |
$wikiId |
| 44 |
); |
| 45 |
|
| 46 |
if ( isset( $rowOverrides['rev_deleted'] ) ) { |
| 47 |
$record->setVisibility( $rowOverrides['rev_deleted'] ); |
| 48 |
} |
| 49 |
|
| 50 |
if ( isset( $rowOverrides['rev_id'] ) ) { |
| 51 |
$record->setId( $rowOverrides['rev_id'] ); |
| 52 |
} |
| 53 |
|
| 54 |
if ( isset( $rowOverrides['rev_page_id'] ) ) { |
| 55 |
$record->setPageId( $rowOverrides['rev_page_id'] ); |
| 56 |
} |
| 57 |
|
| 58 |
if ( isset( $rowOverrides['rev_parent_id'] ) ) { |
| 59 |
$record->setParentId( $rowOverrides['rev_parent_id'] ); |
| 60 |
} |
| 61 |
|
| 62 |
$record->setContent( SlotRecord::MAIN, new DummyContentForTesting( 'Lorem Ipsum' ) ); |
| 63 |
$record->setComment( $comment ); |
| 64 |
$record->setUser( $user ); |
| 65 |
$record->setTimestamp( '20101010000000' ); |
| 66 |
|
| 67 |
return $record; |
| 68 |
} |
| 69 |
|
| 70 |
public static function provideConstructorFailure() { |
| 71 |
yield 'not a wiki id' => [ |
| 72 |
new PageIdentityValue( 17, NS_MAIN, 'Dummy', PageIdentity::LOCAL ), |
| 73 |
InvalidArgumentException::class, |
| 74 |
null, |
| 75 |
]; |
| 76 |
yield 'wiki id mismatch' => [ |
| 77 |
new PageIdentityValue( 17, NS_MAIN, 'Dummy', 'acme' ), |
| 78 |
PreconditionException::class, |
| 79 |
'blawiki', |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function testConstructorFailure( |
| 87 |
PageIdentity $page, |
| 88 |
string $expectedException, |
| 89 |
$wikiId = false |
| 90 |
) { |
| 91 |
$this->expectException( $expectedException ); |
| 92 |
new MutableRevisionRecord( $page, $wikiId ); |
| 93 |
} |
| 94 |
|
| 95 |
public function testSetGetId() { |
| 96 |
$record = new MutableRevisionRecord( |
| 97 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 98 |
); |
| 99 |
$this->assertNull( $record->getId() ); |
| 100 |
$record->setId( 888 ); |
| 101 |
$this->assertSame( 888, $record->getId() ); |
| 102 |
} |
| 103 |
|
| 104 |
public function testSetGetUser() { |
| 105 |
$record = new MutableRevisionRecord( |
| 106 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 107 |
); |
| 108 |
$user = new UserIdentityValue( 0, 'Bla' ); |
| 109 |
$this->assertNull( $record->getUser() ); |
| 110 |
$record->setUser( $user ); |
| 111 |
$this->assertSame( $user, $record->getUser() ); |
| 112 |
} |
| 113 |
|
| 114 |
public function testSetGetPageId() { |
| 115 |
$record = new MutableRevisionRecord( |
| 116 |
new PageIdentityValue( 0, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 117 |
); |
| 118 |
$this->assertSame( 0, $record->getPageId() ); |
| 119 |
$record->setPageId( 999 ); |
| 120 |
$this->assertSame( 999, $record->getPageId() ); |
| 121 |
} |
| 122 |
|
| 123 |
public function testSetGetParentId() { |
| 124 |
$record = new MutableRevisionRecord( |
| 125 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 126 |
); |
| 127 |
$this->assertNull( $record->getParentId() ); |
| 128 |
$record->setParentId( 100 ); |
| 129 |
$this->assertSame( 100, $record->getParentId() ); |
| 130 |
} |
| 131 |
|
| 132 |
public function testGetMainContentWhenEmpty() { |
| 133 |
$record = new MutableRevisionRecord( |
| 134 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 135 |
); |
| 136 |
$this->expectException( RevisionAccessException::class ); |
| 137 |
$this->assertNull( $record->getContent( SlotRecord::MAIN ) ); |
| 138 |
} |
| 139 |
|
| 140 |
public function testSetGetMainContent() { |
| 141 |
$record = new MutableRevisionRecord( |
| 142 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 143 |
); |
| 144 |
$content = new DummyContentForTesting( 'Badger' ); |
| 145 |
$record->setContent( SlotRecord::MAIN, $content ); |
| 146 |
$this->assertSame( $content, $record->getContent( SlotRecord::MAIN ) ); |
| 147 |
} |
| 148 |
|
| 149 |
public function testGetSlotWhenEmpty() { |
| 150 |
$record = new MutableRevisionRecord( |
| 151 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 152 |
); |
| 153 |
$this->assertFalse( $record->hasSlot( SlotRecord::MAIN ) ); |
| 154 |
|
| 155 |
$this->expectException( RevisionAccessException::class ); |
| 156 |
$record->getSlot( SlotRecord::MAIN ); |
| 157 |
} |
| 158 |
|
| 159 |
public function testSetGetSlot() { |
| 160 |
$record = new MutableRevisionRecord( |
| 161 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 162 |
); |
| 163 |
$slot = SlotRecord::newUnsaved( |
| 164 |
SlotRecord::MAIN, |
| 165 |
new DummyContentForTesting( 'x' ) |
| 166 |
); |
| 167 |
$record->setSlot( $slot ); |
| 168 |
$this->assertTrue( $record->hasSlot( SlotRecord::MAIN ) ); |
| 169 |
$this->assertSame( $slot, $record->getSlot( SlotRecord::MAIN ) ); |
| 170 |
} |
| 171 |
|
| 172 |
public function testSetGetMinor() { |
| 173 |
$record = new MutableRevisionRecord( |
| 174 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 175 |
); |
| 176 |
$this->assertFalse( $record->isMinor() ); |
| 177 |
$record->setMinorEdit( true ); |
| 178 |
$this->assertSame( true, $record->isMinor() ); |
| 179 |
} |
| 180 |
|
| 181 |
public function testSetGetTimestamp() { |
| 182 |
$record = new MutableRevisionRecord( |
| 183 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 184 |
); |
| 185 |
$this->assertNull( $record->getTimestamp() ); |
| 186 |
$record->setTimestamp( '20180101010101' ); |
| 187 |
$this->assertSame( '20180101010101', $record->getTimestamp() ); |
| 188 |
} |
| 189 |
|
| 190 |
public function testSetGetVisibility() { |
| 191 |
$record = new MutableRevisionRecord( |
| 192 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 193 |
); |
| 194 |
$this->assertSame( 0, $record->getVisibility() ); |
| 195 |
$record->setVisibility( RevisionRecord::DELETED_USER ); |
| 196 |
$this->assertSame( RevisionRecord::DELETED_USER, $record->getVisibility() ); |
| 197 |
} |
| 198 |
|
| 199 |
public function testSetGetSha1() { |
| 200 |
$record = new MutableRevisionRecord( |
| 201 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 202 |
); |
| 203 |
$this->assertSame( 'phoiac9h4m842xq45sp7s6u21eteeq1', $record->getSha1() ); |
| 204 |
$record->setSha1( 'someHash' ); |
| 205 |
$this->assertSame( 'someHash', $record->getSha1() ); |
| 206 |
} |
| 207 |
|
| 208 |
public function testResetSha1() { |
| 209 |
$record = new MutableRevisionRecord( |
| 210 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 211 |
); |
| 212 |
|
| 213 |
$record->setContent( SlotRecord::MAIN, new DummyContentForTesting( 'foo' ) ); |
| 214 |
$fooHash = $record->getSha1(); |
| 215 |
|
| 216 |
|
| 217 |
$record->setContent( SlotRecord::MAIN, new DummyContentForTesting( 'barx' ) ); |
| 218 |
$barxHash = $record->getSha1(); |
| 219 |
$this->assertNotSame( $fooHash, $barxHash ); |
| 220 |
|
| 221 |
|
| 222 |
$record->getSlots()->setContent( 'aux', new DummyContentForTesting( 'frump' ) ); |
| 223 |
$frumpHash = $record->getSha1(); |
| 224 |
$this->assertNotSame( $barxHash, $frumpHash ); |
| 225 |
} |
| 226 |
|
| 227 |
public function testGetSlots() { |
| 228 |
$record = new MutableRevisionRecord( |
| 229 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 230 |
); |
| 231 |
$this->assertInstanceOf( MutableRevisionSlots::class, $record->getSlots() ); |
| 232 |
} |
| 233 |
|
| 234 |
public function testSetGetSize() { |
| 235 |
$record = new MutableRevisionRecord( |
| 236 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 237 |
); |
| 238 |
$this->assertSame( 0, $record->getSize() ); |
| 239 |
$record->setSize( 775 ); |
| 240 |
$this->assertSame( 775, $record->getSize() ); |
| 241 |
} |
| 242 |
|
| 243 |
public function testResetSize() { |
| 244 |
$record = new MutableRevisionRecord( |
| 245 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 246 |
); |
| 247 |
|
| 248 |
$record->setContent( SlotRecord::MAIN, new DummyContentForTesting( 'foo' ) ); |
| 249 |
$fooSize = $record->getSize(); |
| 250 |
|
| 251 |
|
| 252 |
$record->setContent( SlotRecord::MAIN, new DummyContentForTesting( 'barx' ) ); |
| 253 |
$barxSize = $record->getSize(); |
| 254 |
$this->assertNotSame( $fooSize, $barxSize ); |
| 255 |
|
| 256 |
|
| 257 |
$record->getSlots()->setContent( 'aux', new DummyContentForTesting( 'frump' ) ); |
| 258 |
$frumpSize = $record->getSize(); |
| 259 |
$this->assertNotSame( $barxSize, $frumpSize ); |
| 260 |
} |
| 261 |
|
| 262 |
public function testSetGetComment() { |
| 263 |
$record = new MutableRevisionRecord( |
| 264 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 265 |
); |
| 266 |
$comment = new CommentStoreComment( 1, 'foo' ); |
| 267 |
$this->assertNull( $record->getComment() ); |
| 268 |
$record->setComment( $comment ); |
| 269 |
$this->assertSame( $comment, $record->getComment() ); |
| 270 |
} |
| 271 |
|
| 272 |
public function testSimpleGetOriginalAndInheritedSlots() { |
| 273 |
$record = new MutableRevisionRecord( |
| 274 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 275 |
); |
| 276 |
$mainSlot = new SlotRecord( |
| 277 |
(object)[ |
| 278 |
'slot_id' => 1, |
| 279 |
'slot_revision_id' => null, |
| 280 |
'slot_content_id' => 1, |
| 281 |
'content_address' => null, |
| 282 |
'model_name' => 'x', |
| 283 |
'role_name' => SlotRecord::MAIN, |
| 284 |
'slot_origin' => null |
| 285 |
], |
| 286 |
new DummyContentForTesting( SlotRecord::MAIN ) |
| 287 |
); |
| 288 |
$auxSlot = new SlotRecord( |
| 289 |
(object)[ |
| 290 |
'slot_id' => 2, |
| 291 |
'slot_revision_id' => null, |
| 292 |
'slot_content_id' => 1, |
| 293 |
'content_address' => 'foo', |
| 294 |
'model_name' => 'x', |
| 295 |
'role_name' => 'aux', |
| 296 |
'slot_origin' => 1 |
| 297 |
], |
| 298 |
new DummyContentForTesting( 'aux' ) |
| 299 |
); |
| 300 |
|
| 301 |
$record->setSlot( $mainSlot ); |
| 302 |
$record->setSlot( $auxSlot ); |
| 303 |
|
| 304 |
$this->assertSame( [ SlotRecord::MAIN ], $record->getOriginalSlots()->getSlotRoles() ); |
| 305 |
$this->assertSame( $mainSlot, $record->getOriginalSlots()->getSlot( SlotRecord::MAIN ) ); |
| 306 |
|
| 307 |
$this->assertSame( [ 'aux' ], $record->getInheritedSlots()->getSlotRoles() ); |
| 308 |
$this->assertSame( $auxSlot, $record->getInheritedSlots()->getSlot( 'aux' ) ); |
| 309 |
} |
| 310 |
|
| 311 |
public function testSimpleremoveSlot() { |
| 312 |
$record = new MutableRevisionRecord( |
| 313 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 314 |
); |
| 315 |
|
| 316 |
$a = new DummyContentForTesting( 'a' ); |
| 317 |
$b = new DummyContentForTesting( 'b' ); |
| 318 |
|
| 319 |
$record->inheritSlot( SlotRecord::newSaved( 7, 3, 'a', SlotRecord::newUnsaved( 'a', $a ) ) ); |
| 320 |
$record->inheritSlot( SlotRecord::newSaved( 7, 4, 'b', SlotRecord::newUnsaved( 'b', $b ) ) ); |
| 321 |
|
| 322 |
$record->removeSlot( 'b' ); |
| 323 |
|
| 324 |
$this->assertTrue( $record->hasSlot( 'a' ) ); |
| 325 |
$this->assertFalse( $record->hasSlot( 'b' ) ); |
| 326 |
} |
| 327 |
|
| 328 |
public function testApplyUpdate() { |
| 329 |
$update = new RevisionSlotsUpdate(); |
| 330 |
|
| 331 |
$a = new DummyContentForTesting( 'a' ); |
| 332 |
$b = new DummyContentForTesting( 'b' ); |
| 333 |
$c = new DummyContentForTesting( 'c' ); |
| 334 |
$x = new DummyContentForTesting( 'x' ); |
| 335 |
|
| 336 |
$update->modifyContent( 'b', $x ); |
| 337 |
$update->modifyContent( 'c', $x ); |
| 338 |
$update->removeSlot( 'c' ); |
| 339 |
$update->removeSlot( 'd' ); |
| 340 |
|
| 341 |
$record = new MutableRevisionRecord( |
| 342 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 343 |
); |
| 344 |
$record->inheritSlot( SlotRecord::newSaved( 7, 3, 'a', SlotRecord::newUnsaved( 'a', $a ) ) ); |
| 345 |
$record->inheritSlot( SlotRecord::newSaved( 7, 4, 'b', SlotRecord::newUnsaved( 'b', $b ) ) ); |
| 346 |
$record->inheritSlot( SlotRecord::newSaved( 7, 5, 'c', SlotRecord::newUnsaved( 'c', $c ) ) ); |
| 347 |
|
| 348 |
$record->applyUpdate( $update ); |
| 349 |
|
| 350 |
$this->assertEquals( [ 'b' ], array_keys( $record->getOriginalSlots()->getSlots() ) ); |
| 351 |
$this->assertEquals( $a, $record->getSlot( 'a' )->getContent() ); |
| 352 |
$this->assertEquals( $x, $record->getSlot( 'b' )->getContent() ); |
| 353 |
$this->assertFalse( $record->hasSlot( 'c' ) ); |
| 354 |
} |
| 355 |
|
| 356 |
public function provideNotReadyForInsertion() { |
| 357 |
$title = $this->makeMockTitle( 'Dummy' ); |
| 358 |
$user = new UserIdentityValue( 42, 'Test' ); |
| 359 |
|
| 360 |
|
| 361 |
$comment = $this->createMock( CommentStoreComment::class ); |
| 362 |
|
| 363 |
$content = new DummyContentForTesting( 'Test' ); |
| 364 |
|
| 365 |
$rev = new MutableRevisionRecord( $title ); |
| 366 |
yield 'empty' => [ $rev ]; |
| 367 |
|
| 368 |
$rev = new MutableRevisionRecord( $title ); |
| 369 |
$rev->setContent( SlotRecord::MAIN, $content ); |
| 370 |
$rev->setUser( $user ); |
| 371 |
$rev->setComment( $comment ); |
| 372 |
yield 'no timestamp' => [ $rev ]; |
| 373 |
|
| 374 |
$rev = new MutableRevisionRecord( $title ); |
| 375 |
$rev->setUser( $user ); |
| 376 |
$rev->setComment( $comment ); |
| 377 |
$rev->setTimestamp( '20101010000000' ); |
| 378 |
yield 'no content' => [ $rev ]; |
| 379 |
|
| 380 |
$rev = new MutableRevisionRecord( $title ); |
| 381 |
$rev->setContent( SlotRecord::MAIN, $content ); |
| 382 |
$rev->setComment( $comment ); |
| 383 |
$rev->setTimestamp( '20101010000000' ); |
| 384 |
yield 'no user' => [ $rev ]; |
| 385 |
|
| 386 |
$rev = new MutableRevisionRecord( $title ); |
| 387 |
$rev->setUser( $user ); |
| 388 |
$rev->setContent( SlotRecord::MAIN, $content ); |
| 389 |
$rev->setTimestamp( '20101010000000' ); |
| 390 |
yield 'no comment' => [ $rev ]; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
public function testNotReadyForInsertion( $rev ) { |
| 397 |
$this->assertFalse( $rev->isReadyForInsertion() ); |
| 398 |
} |
| 399 |
|
| 400 |
public function testIsCurrent() { |
| 401 |
$record = new MutableRevisionRecord( |
| 402 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 403 |
); |
| 404 |
$this->assertFalse( $record->isCurrent(), |
| 405 |
MutableRevisionRecord::class . ' cannot be stored current revision' ); |
| 406 |
} |
| 407 |
|
| 408 |
public function testHasSameContent() { |
| 409 |
$rev1 = new MutableRevisionRecord( |
| 410 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 411 |
); |
| 412 |
$this->assertTrue( $rev1->hasSameContent( $rev1 ) ); |
| 413 |
|
| 414 |
$rev2 = new MutableRevisionRecord( |
| 415 |
new PageIdentityValue( 2, NS_MAIN, 'Bar', PageIdentity::LOCAL ) |
| 416 |
); |
| 417 |
$rev1->setSize( 1 ); |
| 418 |
$rev2->setSize( 2 ); |
| 419 |
$this->assertFalse( $rev1->hasSameContent( $rev2 ) ); |
| 420 |
} |
| 421 |
|
| 422 |
public function testAudienceCan() { |
| 423 |
$record = new MutableRevisionRecord( |
| 424 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 425 |
); |
| 426 |
$this->expectException( InvalidArgumentException::class ); |
| 427 |
$this->expectExceptionMessage( |
| 428 |
'An Authority object must be given when checking FOR_THIS_USER audience.' |
| 429 |
); |
| 430 |
$record->audienceCan( RevisionRecord::DELETED_TEXT, RevisionRecord::FOR_THIS_USER ); |
| 431 |
} |
| 432 |
|
| 433 |
public function testGetContent_bad() { |
| 434 |
$record = new MutableRevisionRecord( |
| 435 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL ) |
| 436 |
); |
| 437 |
$slot = new SlotRecord( |
| 438 |
(object)[ |
| 439 |
'slot_id' => 1, |
| 440 |
'slot_revision_id' => null, |
| 441 |
'slot_content_id' => 1, |
| 442 |
'content_address' => null, |
| 443 |
'model_name' => 'x', |
| 444 |
'role_name' => SlotRecord::MAIN, |
| 445 |
'slot_origin' => null |
| 446 |
], |
| 447 |
static function () { |
| 448 |
throw new BadRevisionException( 'bad' ); |
| 449 |
} |
| 450 |
); |
| 451 |
$record->setSlot( $slot ); |
| 452 |
|
| 453 |
$exception = null; |
| 454 |
try { |
| 455 |
$record->getContentOrThrow( SlotRecord::MAIN ); |
| 456 |
} catch ( BadRevisionException $exception ) { |
| 457 |
} |
| 458 |
$this->assertNotNull( $exception ); |
| 459 |
$this->assertNull( $record->getContent( SlotRecord::MAIN ) ); |
| 460 |
} |
| 461 |
} |