3 namespace MediaWiki\Tests\Revision
;
5 use CommentStoreComment
;
6 use InvalidArgumentException
;
7 use MediaWiki\Revision\MutableRevisionRecord
;
8 use MediaWiki\Revision\MutableRevisionSlots
;
9 use MediaWiki\Revision\RevisionAccessException
;
10 use MediaWiki\Revision\RevisionRecord
;
11 use MediaWiki\Revision\SlotRecord
;
12 use MediaWiki\Storage\RevisionSlotsUpdate
;
13 use MediaWiki\User\UserIdentityValue
;
14 use MediaWikiTestCase
;
21 * @covers \MediaWiki\Revision\MutableRevisionRecord
22 * @covers \MediaWiki\Revision\RevisionRecord
24 class MutableRevisionRecordTest
extends MediaWikiTestCase
{
26 use RevisionRecordTests
;
34 * @param array $rowOverrides
36 * @return MutableRevisionRecord
38 protected function newRevision( array $rowOverrides = [] ) {
39 $title = Title
::newFromText( 'Dummy' );
40 $title->resetArticleID( 17 );
42 $user = new UserIdentityValue( 11, 'Tester', 0 );
43 $comment = CommentStoreComment
::newUnsavedComment( 'Hello World' );
45 $record = new MutableRevisionRecord( $title );
47 if ( isset( $rowOverrides['rev_deleted'] ) ) {
48 $record->setVisibility( $rowOverrides['rev_deleted'] );
51 if ( isset( $rowOverrides['rev_id'] ) ) {
52 $record->setId( $rowOverrides['rev_id'] );
55 if ( isset( $rowOverrides['rev_page'] ) ) {
56 $record->setPageId( $rowOverrides['rev_page'] );
59 $record->setContent( SlotRecord
::MAIN
, new TextContent( 'Lorem Ipsum' ) );
60 $record->setComment( $comment );
61 $record->setUser( $user );
62 $record->setTimestamp( '20101010000000' );
67 public function provideConstructor() {
68 $title = Title
::newFromText( 'Dummy' );
69 $title->resetArticleID( 17 );
78 * @dataProvider provideConstructor
83 public function testConstructorAndGetters(
87 $rec = new MutableRevisionRecord( $title, $wikiId );
89 $this->assertSame( $title, $rec->getPageAsLinkTarget(), 'getPageAsLinkTarget' );
90 $this->assertSame( $wikiId, $rec->getWikiId(), 'getWikiId' );
93 public function provideConstructorFailure() {
94 $title = Title
::newFromText( 'Dummy' );
95 $title->resetArticleID( 17 );
97 yield
'not a wiki id' => [
104 * @dataProvider provideConstructorFailure
106 * @param Title $title
107 * @param bool $wikiId
109 public function testConstructorFailure(
113 $this->setExpectedException( InvalidArgumentException
::class );
114 new MutableRevisionRecord( $title, $wikiId );
117 public function testSetGetId() {
118 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
119 $this->assertNull( $record->getId() );
120 $record->setId( 888 );
121 $this->assertSame( 888, $record->getId() );
124 public function testSetGetUser() {
125 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
126 $user = $this->getTestSysop()->getUser();
127 $this->assertNull( $record->getUser() );
128 $record->setUser( $user );
129 $this->assertSame( $user, $record->getUser() );
132 public function testSetGetPageId() {
133 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
134 $this->assertSame( 0, $record->getPageId() );
135 $record->setPageId( 999 );
136 $this->assertSame( 999, $record->getPageId() );
139 public function testSetGetParentId() {
140 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
141 $this->assertNull( $record->getParentId() );
142 $record->setParentId( 100 );
143 $this->assertSame( 100, $record->getParentId() );
146 public function testGetMainContentWhenEmpty() {
147 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
148 $this->setExpectedException( RevisionAccessException
::class );
149 $this->assertNull( $record->getContent( SlotRecord
::MAIN
) );
152 public function testSetGetMainContent() {
153 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
154 $content = new WikitextContent( 'Badger' );
155 $record->setContent( SlotRecord
::MAIN
, $content );
156 $this->assertSame( $content, $record->getContent( SlotRecord
::MAIN
) );
159 public function testGetSlotWhenEmpty() {
160 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
161 $this->assertFalse( $record->hasSlot( SlotRecord
::MAIN
) );
163 $this->setExpectedException( RevisionAccessException
::class );
164 $record->getSlot( SlotRecord
::MAIN
);
167 public function testSetGetSlot() {
168 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
169 $slot = SlotRecord
::newUnsaved(
171 new WikitextContent( 'x' )
173 $record->setSlot( $slot );
174 $this->assertTrue( $record->hasSlot( SlotRecord
::MAIN
) );
175 $this->assertSame( $slot, $record->getSlot( SlotRecord
::MAIN
) );
178 public function testSetGetMinor() {
179 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
180 $this->assertFalse( $record->isMinor() );
181 $record->setMinorEdit( true );
182 $this->assertSame( true, $record->isMinor() );
185 public function testSetGetTimestamp() {
186 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
187 $this->assertNull( $record->getTimestamp() );
188 $record->setTimestamp( '20180101010101' );
189 $this->assertSame( '20180101010101', $record->getTimestamp() );
192 public function testSetGetVisibility() {
193 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
194 $this->assertSame( 0, $record->getVisibility() );
195 $record->setVisibility( RevisionRecord
::DELETED_USER
);
196 $this->assertSame( RevisionRecord
::DELETED_USER
, $record->getVisibility() );
199 public function testSetGetSha1() {
200 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
201 $this->assertSame( 'phoiac9h4m842xq45sp7s6u21eteeq1', $record->getSha1() );
202 $record->setSha1( 'someHash' );
203 $this->assertSame( 'someHash', $record->getSha1() );
206 public function testGetSlots() {
207 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
208 $this->assertInstanceOf( MutableRevisionSlots
::class, $record->getSlots() );
211 public function testSetGetSize() {
212 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
213 $this->assertSame( 0, $record->getSize() );
214 $record->setSize( 775 );
215 $this->assertSame( 775, $record->getSize() );
218 public function testSetGetComment() {
219 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
220 $comment = new CommentStoreComment( 1, 'foo' );
221 $this->assertNull( $record->getComment() );
222 $record->setComment( $comment );
223 $this->assertSame( $comment, $record->getComment() );
226 public function testSimpleGetOriginalAndInheritedSlots() {
227 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
228 $mainSlot = new SlotRecord(
231 'slot_revision_id' => null, // unsaved
232 'slot_content_id' => 1,
233 'content_address' => null, // touched
235 'role_name' => 'main',
236 'slot_origin' => null // touched
238 new WikitextContent( 'main' )
240 $auxSlot = new SlotRecord(
243 'slot_revision_id' => null, // unsaved
244 'slot_content_id' => 1,
245 'content_address' => 'foo', // inherited
247 'role_name' => 'aux',
248 'slot_origin' => 1 // inherited
250 new WikitextContent( 'aux' )
253 $record->setSlot( $mainSlot );
254 $record->setSlot( $auxSlot );
256 $this->assertSame( [ 'main' ], $record->getOriginalSlots()->getSlotRoles() );
257 $this->assertSame( $mainSlot, $record->getOriginalSlots()->getSlot( SlotRecord
::MAIN
) );
259 $this->assertSame( [ 'aux' ], $record->getInheritedSlots()->getSlotRoles() );
260 $this->assertSame( $auxSlot, $record->getInheritedSlots()->getSlot( 'aux' ) );
263 public function testSimpleremoveSlot() {
264 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
266 $a = new WikitextContent( 'a' );
267 $b = new WikitextContent( 'b' );
269 $record->inheritSlot( SlotRecord
::newSaved( 7, 3, 'a', SlotRecord
::newUnsaved( 'a', $a ) ) );
270 $record->inheritSlot( SlotRecord
::newSaved( 7, 4, 'b', SlotRecord
::newUnsaved( 'b', $b ) ) );
272 $record->removeSlot( 'b' );
274 $this->assertTrue( $record->hasSlot( 'a' ) );
275 $this->assertFalse( $record->hasSlot( 'b' ) );
278 public function testApplyUpdate() {
279 $update = new RevisionSlotsUpdate();
281 $a = new WikitextContent( 'a' );
282 $b = new WikitextContent( 'b' );
283 $c = new WikitextContent( 'c' );
284 $x = new WikitextContent( 'x' );
286 $update->modifyContent( 'b', $x );
287 $update->modifyContent( 'c', $x );
288 $update->removeSlot( 'c' );
289 $update->removeSlot( 'd' );
291 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
292 $record->inheritSlot( SlotRecord
::newSaved( 7, 3, 'a', SlotRecord
::newUnsaved( 'a', $a ) ) );
293 $record->inheritSlot( SlotRecord
::newSaved( 7, 4, 'b', SlotRecord
::newUnsaved( 'b', $b ) ) );
294 $record->inheritSlot( SlotRecord
::newSaved( 7, 5, 'c', SlotRecord
::newUnsaved( 'c', $c ) ) );
296 $record->applyUpdate( $update );
298 $this->assertEquals( [ 'b' ], array_keys( $record->getOriginalSlots()->getSlots() ) );
299 $this->assertEquals( $a, $record->getSlot( 'a' )->getContent() );
300 $this->assertEquals( $x, $record->getSlot( 'b' )->getContent() );
301 $this->assertFalse( $record->hasSlot( 'c' ) );
304 public function provideNotReadyForInsertion() {
305 /** @var Title $title */
306 $title = $this->getMock( Title
::class );
308 /** @var User $user */
309 $user = $this->getMock( User
::class );
311 /** @var CommentStoreComment $comment */
312 $comment = $this->getMockBuilder( CommentStoreComment
::class )
313 ->disableOriginalConstructor()
316 $content = new TextContent( 'Test' );
318 $rev = new MutableRevisionRecord( $title );
319 yield
'empty' => [ $rev ];
321 $rev = new MutableRevisionRecord( $title );
322 $rev->setContent( SlotRecord
::MAIN
, $content );
323 $rev->setUser( $user );
324 $rev->setComment( $comment );
325 yield
'no timestamp' => [ $rev ];
327 $rev = new MutableRevisionRecord( $title );
328 $rev->setUser( $user );
329 $rev->setComment( $comment );
330 $rev->setTimestamp( '20101010000000' );
331 yield
'no content' => [ $rev ];
333 $rev = new MutableRevisionRecord( $title );
334 $rev->setContent( SlotRecord
::MAIN
, $content );
335 $rev->setComment( $comment );
336 $rev->setTimestamp( '20101010000000' );
337 yield
'no user' => [ $rev ];
339 $rev = new MutableRevisionRecord( $title );
340 $rev->setUser( $user );
341 $rev->setContent( SlotRecord
::MAIN
, $content );
342 $rev->setTimestamp( '20101010000000' );
343 yield
'no comment' => [ $rev ];
347 * @dataProvider provideNotReadyForInsertion
349 public function testNotReadyForInsertion( $rev ) {
350 $this->assertFalse( $rev->isReadyForInsertion() );