Merge "Fix 'Tags' padding to keep it farther from the edge and document the source...
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / MutableRevisionRecordTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use CommentStoreComment;
6 use InvalidArgumentException;
7 use MediaWiki\Storage\MutableRevisionRecord;
8 use MediaWiki\Storage\RevisionAccessException;
9 use MediaWiki\Storage\RevisionRecord;
10 use MediaWiki\Storage\RevisionSlotsUpdate;
11 use MediaWiki\Storage\SlotRecord;
12 use MediaWiki\User\UserIdentityValue;
13 use MediaWikiTestCase;
14 use TextContent;
15 use Title;
16 use WikitextContent;
17
18 /**
19 * @covers \MediaWiki\Storage\MutableRevisionRecord
20 * @covers \MediaWiki\Storage\RevisionRecord
21 */
22 class MutableRevisionRecordTest extends MediaWikiTestCase {
23
24 use RevisionRecordTests;
25
26 /**
27 * @param array $rowOverrides
28 *
29 * @return MutableRevisionRecord
30 */
31 protected function newRevision( array $rowOverrides = [] ) {
32 $title = Title::newFromText( 'Dummy' );
33 $title->resetArticleID( 17 );
34
35 $user = new UserIdentityValue( 11, 'Tester', 0 );
36 $comment = CommentStoreComment::newUnsavedComment( 'Hello World' );
37
38 $record = new MutableRevisionRecord( $title );
39
40 if ( isset( $rowOverrides['rev_deleted'] ) ) {
41 $record->setVisibility( $rowOverrides['rev_deleted'] );
42 }
43
44 if ( isset( $rowOverrides['rev_id'] ) ) {
45 $record->setId( $rowOverrides['rev_id'] );
46 }
47
48 if ( isset( $rowOverrides['rev_page'] ) ) {
49 $record->setPageId( $rowOverrides['rev_page'] );
50 }
51
52 $record->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
53 $record->setComment( $comment );
54 $record->setUser( $user );
55
56 return $record;
57 }
58
59 public function provideConstructor() {
60 $title = Title::newFromText( 'Dummy' );
61 $title->resetArticleID( 17 );
62
63 yield [
64 $title,
65 'acmewiki'
66 ];
67 }
68
69 /**
70 * @dataProvider provideConstructor
71 *
72 * @param Title $title
73 * @param bool $wikiId
74 */
75 public function testConstructorAndGetters(
76 Title $title,
77 $wikiId = false
78 ) {
79 $rec = new MutableRevisionRecord( $title, $wikiId );
80
81 $this->assertSame( $title, $rec->getPageAsLinkTarget(), 'getPageAsLinkTarget' );
82 $this->assertSame( $wikiId, $rec->getWikiId(), 'getWikiId' );
83 }
84
85 public function provideConstructorFailure() {
86 $title = Title::newFromText( 'Dummy' );
87 $title->resetArticleID( 17 );
88
89 yield 'not a wiki id' => [
90 $title,
91 null
92 ];
93 }
94
95 /**
96 * @dataProvider provideConstructorFailure
97 *
98 * @param Title $title
99 * @param bool $wikiId
100 */
101 public function testConstructorFailure(
102 Title $title,
103 $wikiId = false
104 ) {
105 $this->setExpectedException( InvalidArgumentException::class );
106 new MutableRevisionRecord( $title, $wikiId );
107 }
108
109 public function testSetGetId() {
110 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
111 $this->assertNull( $record->getId() );
112 $record->setId( 888 );
113 $this->assertSame( 888, $record->getId() );
114 }
115
116 public function testSetGetUser() {
117 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
118 $user = $this->getTestSysop()->getUser();
119 $this->assertNull( $record->getUser() );
120 $record->setUser( $user );
121 $this->assertSame( $user, $record->getUser() );
122 }
123
124 public function testSetGetPageId() {
125 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
126 $this->assertSame( 0, $record->getPageId() );
127 $record->setPageId( 999 );
128 $this->assertSame( 999, $record->getPageId() );
129 }
130
131 public function testSetGetParentId() {
132 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
133 $this->assertNull( $record->getParentId() );
134 $record->setParentId( 100 );
135 $this->assertSame( 100, $record->getParentId() );
136 }
137
138 public function testGetMainContentWhenEmpty() {
139 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
140 $this->setExpectedException( RevisionAccessException::class );
141 $this->assertNull( $record->getContent( 'main' ) );
142 }
143
144 public function testSetGetMainContent() {
145 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
146 $content = new WikitextContent( 'Badger' );
147 $record->setContent( 'main', $content );
148 $this->assertSame( $content, $record->getContent( 'main' ) );
149 }
150
151 public function testGetSlotWhenEmpty() {
152 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
153 $this->assertFalse( $record->hasSlot( 'main' ) );
154
155 $this->setExpectedException( RevisionAccessException::class );
156 $record->getSlot( 'main' );
157 }
158
159 public function testSetGetSlot() {
160 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
161 $slot = SlotRecord::newUnsaved(
162 'main',
163 new WikitextContent( 'x' )
164 );
165 $record->setSlot( $slot );
166 $this->assertTrue( $record->hasSlot( 'main' ) );
167 $this->assertSame( $slot, $record->getSlot( 'main' ) );
168 }
169
170 public function testSetGetMinor() {
171 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
172 $this->assertFalse( $record->isMinor() );
173 $record->setMinorEdit( true );
174 $this->assertSame( true, $record->isMinor() );
175 }
176
177 public function testSetGetTimestamp() {
178 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
179 $this->assertNull( $record->getTimestamp() );
180 $record->setTimestamp( '20180101010101' );
181 $this->assertSame( '20180101010101', $record->getTimestamp() );
182 }
183
184 public function testSetGetVisibility() {
185 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
186 $this->assertSame( 0, $record->getVisibility() );
187 $record->setVisibility( RevisionRecord::DELETED_USER );
188 $this->assertSame( RevisionRecord::DELETED_USER, $record->getVisibility() );
189 }
190
191 public function testSetGetSha1() {
192 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
193 $this->assertSame( 'phoiac9h4m842xq45sp7s6u21eteeq1', $record->getSha1() );
194 $record->setSha1( 'someHash' );
195 $this->assertSame( 'someHash', $record->getSha1() );
196 }
197
198 public function testSetGetSize() {
199 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
200 $this->assertSame( 0, $record->getSize() );
201 $record->setSize( 775 );
202 $this->assertSame( 775, $record->getSize() );
203 }
204
205 public function testSetGetComment() {
206 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
207 $comment = new CommentStoreComment( 1, 'foo' );
208 $this->assertNull( $record->getComment() );
209 $record->setComment( $comment );
210 $this->assertSame( $comment, $record->getComment() );
211 }
212
213 public function testSimpleGetOriginalAndInheritedSlots() {
214 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
215 $mainSlot = new SlotRecord(
216 (object)[
217 'slot_id' => 1,
218 'slot_revision_id' => null, // unsaved
219 'slot_content_id' => 1,
220 'content_address' => null, // touched
221 'model_name' => 'x',
222 'role_name' => 'main',
223 'slot_origin' => null // touched
224 ],
225 new WikitextContent( 'main' )
226 );
227 $auxSlot = new SlotRecord(
228 (object)[
229 'slot_id' => 2,
230 'slot_revision_id' => null, // unsaved
231 'slot_content_id' => 1,
232 'content_address' => 'foo', // inherited
233 'model_name' => 'x',
234 'role_name' => 'aux',
235 'slot_origin' => 1 // inherited
236 ],
237 new WikitextContent( 'aux' )
238 );
239
240 $record->setSlot( $mainSlot );
241 $record->setSlot( $auxSlot );
242
243 $this->assertSame( [ 'main' ], $record->getOriginalSlots()->getSlotRoles() );
244 $this->assertSame( $mainSlot, $record->getOriginalSlots()->getSlot( 'main' ) );
245
246 $this->assertSame( [ 'aux' ], $record->getInheritedSlots()->getSlotRoles() );
247 $this->assertSame( $auxSlot, $record->getInheritedSlots()->getSlot( 'aux' ) );
248 }
249
250 public function testSimpleremoveSlot() {
251 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
252
253 $a = new WikitextContent( 'a' );
254 $b = new WikitextContent( 'b' );
255
256 $record->inheritSlot( SlotRecord::newSaved( 7, 3, 'a', SlotRecord::newUnsaved( 'a', $a ) ) );
257 $record->inheritSlot( SlotRecord::newSaved( 7, 4, 'b', SlotRecord::newUnsaved( 'b', $b ) ) );
258
259 $record->removeSlot( 'b' );
260
261 $this->assertTrue( $record->hasSlot( 'a' ) );
262 $this->assertFalse( $record->hasSlot( 'b' ) );
263 }
264
265 public function testApplyUpdate() {
266 $update = new RevisionSlotsUpdate();
267
268 $a = new WikitextContent( 'a' );
269 $b = new WikitextContent( 'b' );
270 $c = new WikitextContent( 'c' );
271 $x = new WikitextContent( 'x' );
272
273 $update->modifyContent( 'b', $x );
274 $update->modifyContent( 'c', $x );
275 $update->removeSlot( 'c' );
276 $update->removeSlot( 'd' );
277
278 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
279 $record->inheritSlot( SlotRecord::newSaved( 7, 3, 'a', SlotRecord::newUnsaved( 'a', $a ) ) );
280 $record->inheritSlot( SlotRecord::newSaved( 7, 4, 'b', SlotRecord::newUnsaved( 'b', $b ) ) );
281 $record->inheritSlot( SlotRecord::newSaved( 7, 5, 'c', SlotRecord::newUnsaved( 'c', $c ) ) );
282
283 $record->applyUpdate( $update );
284
285 $this->assertEquals( [ 'b' ], array_keys( $record->getOriginalSlots()->getSlots() ) );
286 $this->assertEquals( $a, $record->getSlot( 'a' )->getContent() );
287 $this->assertEquals( $x, $record->getSlot( 'b' )->getContent() );
288 $this->assertFalse( $record->hasSlot( 'c' ) );
289 }
290
291 }