Debian Codegraph @

tests/phpunit/includes/Revision/RevisionStoreRecordTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Revision;
4
5 use InvalidArgumentException;
6 use MediaWiki\CommentStore\CommentStoreComment;
7 use MediaWiki\Content\TextContent;
8 use MediaWiki\Page\PageIdentity;
9 use MediaWiki\Page\PageIdentityValue;
10 use MediaWiki\Revision\RevisionRecord;
11 use MediaWiki\Revision\RevisionSlots;
12 use MediaWiki\Revision\RevisionStoreRecord;
13 use MediaWiki\Revision\SlotRecord;
14 use MediaWiki\Title\Title;
15 use MediaWiki\Title\TitleValue;
16 use MediaWiki\User\UserIdentity;
17 use MediaWiki\User\UserIdentityValue;
18 use MediaWikiIntegrationTestCase;
19 use stdClass;
20 use Wikimedia\Assert\PreconditionException;
21 use Wikimedia\Timestamp\TimestampException;
22
23 /**
24 * @covers \MediaWiki\Revision\RevisionStoreRecord
25 * @covers \MediaWiki\Revision\RevisionRecord
26 */
27 class RevisionStoreRecordTest extends MediaWikiIntegrationTestCase {
28
29 public static function provideConstructor() {
30 $user = new UserIdentityValue( 11, 'Tester' );
31 $comment = CommentStoreComment::newUnsavedComment( 'Hello World' );
32
33 $main = SlotRecord::newUnsaved( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) );
34 $aux = SlotRecord::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) );
35 $slots = new RevisionSlots( [ $main, $aux ] );
36
37 $protoRow = [
38 'rev_id' => '7',
39 'rev_page' => '17',
40 'rev_timestamp' => '20200101000000',
41 'rev_deleted' => 0,
42 'rev_minor_edit' => 0,
43 'rev_parent_id' => '5',
44 'rev_len' => $slots->computeSize(),
45 'rev_sha1' => $slots->computeSha1(),
46 'page_latest' => '18',
47 ];
48
49 $row = $protoRow;
50 yield 'all info' => [
51 new PageIdentityValue( 17, NS_MAIN, 'Dummy', 'acmewiki' ),
52 $user,
53 $comment,
54 (object)$row,
55 $slots,
56 'acmewiki',
57 PreconditionException::class
58 ];
59
60 yield 'all info, local' => [
61 new PageIdentityValue( 17, NS_MAIN, 'Dummy', PageIdentity::LOCAL ),
62 $user,
63 $comment,
64 (object)$row,
65 $slots,
66 ];
67
68 $title = Title::makeTitle( NS_MAIN, 'Dummy' );
69 $title->resetArticleID( 17 );
70
71 yield 'all info, local with Title' => [
72 $title,
73 $user,
74 $comment,
75 (object)$row,
76 $slots,
77 ];
78
79 $row = $protoRow;
80 $row['rev_minor_edit'] = '1';
81 $row['rev_deleted'] = strval( RevisionRecord::DELETED_USER );
82
83 yield 'minor deleted' => [
84 $title,
85 $user,
86 $comment,
87 (object)$row,
88 $slots
89 ];
90
91 $row = $protoRow;
92 $row['page_latest'] = $row['rev_id'];
93
94 yield 'latest' => [
95 $title,
96 $user,
97 $comment,
98 (object)$row,
99 $slots
100 ];
101
102 $row = $protoRow;
103 unset( $row['rev_parent'] );
104
105 yield 'no parent' => [
106 $title,
107 $user,
108 $comment,
109 (object)$row,
110 $slots
111 ];
112
113 $row = $protoRow;
114 $row['rev_len'] = null;
115 $row['rev_sha1'] = '';
116
117 yield 'rev_len is null, rev_sha1 is ""' => [
118 $title,
119 $user,
120 $comment,
121 (object)$row,
122 $slots
123 ];
124
125 $row = $protoRow;
126 $nonExistingTitle = Title::makeTitle( NS_MAIN, 'DummyDoesNotExist' );
127 $nonExistingTitle->resetArticleID( 0 );
128 yield 'no length, no hash' => [
129 $nonExistingTitle,
130 $user,
131 $comment,
132 (object)$row,
133 $slots
134 ];
135 }
136
137 /**
138 * @dataProvider provideConstructor
139 *
140 * @param PageIdentity $page
141 * @param UserIdentity $user
142 * @param CommentStoreComment $comment
143 * @param stdClass $row
144 * @param RevisionSlots $slots
145 * @param string|false $wikiId
146 * @param string|null $expectedException
147 */
148 public function testConstructorAndGetters(
149 PageIdentity $page,
150 UserIdentity $user,
151 CommentStoreComment $comment,
152 $row,
153 RevisionSlots $slots,
154 $wikiId = RevisionRecord::LOCAL,
155 ?string $expectedException = null
156 ) {
157 $rec = new RevisionStoreRecord( $page, $user, $comment, $row, $slots, $wikiId );
158
159 $this->assertTrue( $page->isSamePageAs( $rec->getPage() ), 'getPage' );
160 $this->assertSame( $user, $rec->getUser( RevisionRecord::RAW ), 'getUser' );
161 $this->assertSame( $comment, $rec->getComment(), 'getComment' );
162
163 $this->assertSame( $slots, $rec->getSlots(), 'getSlots' );
164 $this->assertSame( $slots->getSlotRoles(), $rec->getSlotRoles(), 'getSlotRoles' );
165 $this->assertSame( $slots->getSlots(), $rec->getSlots()->getSlots(), 'getSlots' );
166 $this->assertSame( $wikiId, $rec->getWikiId(), 'getWikiId' );
167
168 $this->assertSame( (int)$row->rev_id, $rec->getId( $wikiId ), 'getId' );
169 $this->assertSame( (int)$row->rev_page, $rec->getPageId( $wikiId ), 'getId' );
170 $this->assertSame( $row->rev_timestamp, $rec->getTimestamp(), 'getTimestamp' );
171 $this->assertSame( (int)$row->rev_deleted, $rec->getVisibility(), 'getVisibility' );
172 $this->assertSame( (bool)$row->rev_minor_edit, $rec->isMinor(), 'getIsMinor' );
173
174 if ( isset( $row->rev_parent_id ) ) {
175 $this->assertSame( (int)$row->rev_parent_id, $rec->getParentId( $wikiId ), 'getParentId' );
176 } else {
177 $this->assertSame( 0, $rec->getParentId( $wikiId ), 'getParentId' );
178 }
179
180 if ( isset( $row->rev_len ) ) {
181 $this->assertSame( (int)$row->rev_len, $rec->getSize(), 'getSize' );
182 } else {
183 $this->assertSame( $slots->computeSize(), $rec->getSize(), 'getSize' );
184 }
185
186 if ( !empty( $row->rev_sha1 ) ) {
187 $this->assertSame( $row->rev_sha1, $rec->getSha1(), 'getSha1' );
188 } else {
189 $this->assertSame( $slots->computeSha1(), $rec->getSha1(), 'getSha1' );
190 }
191
192 if ( isset( $row->page_latest ) ) {
193 $this->assertSame(
194 (int)$row->rev_id === (int)$row->page_latest,
195 $rec->isCurrent(),
196 'isCurrent'
197 );
198 } else {
199 $this->assertSame(
200 false,
201 $rec->isCurrent(),
202 'isCurrent'
203 );
204 }
205
206 if ( $expectedException ) {
207 $this->expectException( $expectedException );
208 $rec->getPageAsLinkTarget();
209 } else {
210 $this->assertTrue(
211 TitleValue::newFromPage( $page )->isSameLinkAs( $rec->getPageAsLinkTarget() ),
212 'getPageAsLinkTarget'
213 );
214 }
215 }
216
217 public static function provideConstructorFailure() {
218 $title = Title::makeTitle( NS_MAIN, 'Dummy' );
219 $title->resetArticleID( 17 );
220
221 $user = new UserIdentityValue( 11, 'Tester' );
222
223 $comment = CommentStoreComment::newUnsavedComment( 'Hello World' );
224
225 $main = SlotRecord::newUnsaved( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) );
226 $aux = SlotRecord::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) );
227 $slots = new RevisionSlots( [ $main, $aux ] );
228
229 $protoRow = [
230 'rev_id' => '7',
231 'rev_page' => strval( $title->getArticleID() ),
232 'rev_timestamp' => '20200101000000',
233 'rev_deleted' => 0,
234 'rev_minor_edit' => 0,
235 'rev_parent_id' => '5',
236 'rev_len' => $slots->computeSize(),
237 'rev_sha1' => $slots->computeSha1(),
238 'page_latest' => '18',
239 ];
240
241 $row = $protoRow;
242 $row['rev_page'] = 99;
243
244 yield 'page ID mismatch' => [
245 $title,
246 $user,
247 $comment,
248 (object)$row,
249 $slots
250 ];
251
252 $row = $protoRow;
253
254 yield 'bad wiki' => [
255 $title,
256 $user,
257 $comment,
258 (object)$row,
259 $slots,
260 12345
261 ];
262 }
263
264 /**
265 * @dataProvider provideConstructorFailure
266 *
267 * @param PageIdentity $page
268 * @param UserIdentity $user
269 * @param CommentStoreComment $comment
270 * @param stdClass $row
271 * @param RevisionSlots $slots
272 * @param string|false $wikiId
273 */
274 public function testConstructorFailure(
275 PageIdentity $page,
276 UserIdentity $user,
277 CommentStoreComment $comment,
278 $row,
279 RevisionSlots $slots,
280 $wikiId = false
281 ) {
282 $this->expectException( InvalidArgumentException::class );
283 new RevisionStoreRecord( $page, $user, $comment, $row, $slots, $wikiId );
284 }
285
286 public function testConstructorBadTimestamp() {
287 $row = (object)[
288 'rev_id' => 42,
289 'rev_page' => 'Foobar',
290 'rev_timestamp' => 'kittens',
291 ];
292 $this->expectException( TimestampException::class );
293 new RevisionStoreRecord(
294 $this->createMock( PageIdentity::class ),
295 new UserIdentityValue( 11, __CLASS__ ),
296 $this->createMock( CommentStoreComment::class ),
297 $row,
298 $this->createMock( RevisionSlots::class ),
299 false
300 );
301 }
302 }