Debian Codegraph @

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