resourceloader: Configure eslint to disallow $ and require inside startup
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / McrRevisionStoreDbTest.php
1 <?php
2 namespace MediaWiki\Tests\Storage;
3
4 use CommentStoreComment;
5 use MediaWiki\MediaWikiServices;
6 use MediaWiki\Storage\MutableRevisionRecord;
7 use MediaWiki\Storage\RevisionRecord;
8 use MediaWiki\Storage\SlotRecord;
9 use TextContent;
10 use Title;
11 use WikitextContent;
12
13 /**
14 * Tests RevisionStore against the post-migration MCR DB schema.
15 *
16 * @covers \MediaWiki\Storage\RevisionStore
17 *
18 * @group RevisionStore
19 * @group Storage
20 * @group Database
21 * @group medium
22 */
23 class McrRevisionStoreDbTest extends RevisionStoreDbTestBase {
24
25 use McrSchemaOverride;
26
27 protected function assertRevisionExistsInDatabase( RevisionRecord $rev ) {
28 $numberOfSlots = count( $rev->getSlotRoles() );
29
30 // new schema is written
31 $this->assertSelect(
32 'slots',
33 [ 'count(*)' ],
34 [ 'slot_revision_id' => $rev->getId() ],
35 [ [ (string)$numberOfSlots ] ]
36 );
37
38 $store = MediaWikiServices::getInstance()->getRevisionStore();
39 $revQuery = $store->getSlotsQueryInfo( [ 'content' ] );
40
41 $this->assertSelect(
42 $revQuery['tables'],
43 [ 'count(*)' ],
44 [
45 'slot_revision_id' => $rev->getId(),
46 ],
47 [ [ (string)$numberOfSlots ] ],
48 [],
49 $revQuery['joins']
50 );
51
52 parent::assertRevisionExistsInDatabase( $rev );
53 }
54
55 /**
56 * @param SlotRecord $a
57 * @param SlotRecord $b
58 */
59 protected function assertSameSlotContent( SlotRecord $a, SlotRecord $b ) {
60 parent::assertSameSlotContent( $a, $b );
61
62 // Assert that the same content ID has been used
63 $this->assertSame( $a->getContentId(), $b->getContentId() );
64 }
65
66 public function provideInsertRevisionOn_successes() {
67 foreach ( parent::provideInsertRevisionOn_successes() as $case ) {
68 yield $case;
69 }
70
71 yield 'Multi-slot revision insertion' => [
72 [
73 'content' => [
74 'main' => new WikitextContent( 'Chicken' ),
75 'aux' => new TextContent( 'Egg' ),
76 ],
77 'page' => true,
78 'comment' => $this->getRandomCommentStoreComment(),
79 'timestamp' => '20171117010101',
80 'user' => true,
81 ],
82 ];
83 }
84
85 public function provideNewNullRevision() {
86 foreach ( parent::provideNewNullRevision() as $case ) {
87 yield $case;
88 }
89
90 yield [
91 Title::newFromText( 'UTPage_notAutoCreated' ),
92 [
93 'content' => [
94 'main' => new WikitextContent( 'Chicken' ),
95 'aux' => new WikitextContent( 'Omelet' ),
96 ],
97 ],
98 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment multi' ),
99 ];
100 }
101
102 public function provideNewMutableRevisionFromArray() {
103 foreach ( parent::provideNewMutableRevisionFromArray() as $case ) {
104 yield $case;
105 }
106
107 yield 'Basic array, multiple roles' => [
108 [
109 'id' => 2,
110 'page' => 1,
111 'timestamp' => '20171017114835',
112 'user_text' => '111.0.1.2',
113 'user' => 0,
114 'minor_edit' => false,
115 'deleted' => 0,
116 'len' => 29,
117 'parent_id' => 1,
118 'sha1' => '89qs83keq9c9ccw9olvvm4oc9oq50ii',
119 'comment' => 'Goat Comment!',
120 'content' => [
121 'main' => new WikitextContent( 'Söme Cöntent' ),
122 'aux' => new TextContent( 'Öther Cöntent' ),
123 ]
124 ]
125 ];
126 }
127
128 public function testGetQueryInfo_NoSlotDataJoin() {
129 $store = MediaWikiServices::getInstance()->getRevisionStore();
130 $queryInfo = $store->getQueryInfo();
131
132 // with the new schema enabled, query info should not join the main slot info
133 $this->assertFalse( array_key_exists( 'a_slot_data', $queryInfo['tables'] ) );
134 $this->assertFalse( array_key_exists( 'a_slot_data', $queryInfo['joins'] ) );
135 }
136
137 public function provideGetArchiveQueryInfo() {
138 yield [
139 [
140 'tables' => [
141 'archive',
142 ],
143 'fields' => array_merge(
144 $this->getDefaultArchiveFields( false ),
145 [
146 'ar_comment_text' => 'ar_comment',
147 'ar_comment_data' => 'NULL',
148 'ar_comment_cid' => 'NULL',
149 'ar_user_text' => 'ar_user_text',
150 'ar_user' => 'ar_user',
151 'ar_actor' => 'NULL',
152 ]
153 ),
154 'joins' => [
155 ],
156 ]
157 ];
158 }
159
160 public function provideGetQueryInfo() {
161 // TODO: more option variations
162 yield [
163 [ 'page', 'user' ],
164 [
165 'tables' => [
166 'revision',
167 'page',
168 'user',
169 ],
170 'fields' => array_merge(
171 $this->getDefaultQueryFields( false ),
172 $this->getCommentQueryFields(),
173 $this->getActorQueryFields(),
174 [
175 'page_namespace',
176 'page_title',
177 'page_id',
178 'page_latest',
179 'page_is_redirect',
180 'page_len',
181 'user_name',
182 ]
183 ),
184 'joins' => [
185 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
186 'user' => [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
187 ],
188 ]
189 ];
190 }
191
192 public function provideGetSlotsQueryInfo() {
193 yield 'no options' => [
194 [],
195 [
196 'tables' => [
197 'slots'
198 ],
199 'fields' => [
200 'slot_revision_id',
201 'slot_content_id',
202 'slot_origin',
203 'slot_role_id',
204 ],
205 'joins' => [],
206 ]
207 ];
208 yield 'role option' => [
209 [ 'role' ],
210 [
211 'tables' => [
212 'slots',
213 'slot_roles',
214 ],
215 'fields' => [
216 'slot_revision_id',
217 'slot_content_id',
218 'slot_origin',
219 'slot_role_id',
220 'role_name',
221 ],
222 'joins' => [
223 'slot_roles' => [ 'LEFT JOIN', [ 'slot_role_id = role_id' ] ],
224 ],
225 ]
226 ];
227 yield 'content option' => [
228 [ 'content' ],
229 [
230 'tables' => [
231 'slots',
232 'content',
233 ],
234 'fields' => [
235 'slot_revision_id',
236 'slot_content_id',
237 'slot_origin',
238 'slot_role_id',
239 'content_size',
240 'content_sha1',
241 'content_address',
242 'content_model',
243 ],
244 'joins' => [
245 'content' => [ 'INNER JOIN', [ 'slot_content_id = content_id' ] ],
246 ],
247 ]
248 ];
249 yield 'content and model options' => [
250 [ 'content', 'model' ],
251 [
252 'tables' => [
253 'slots',
254 'content',
255 'content_models',
256 ],
257 'fields' => [
258 'slot_revision_id',
259 'slot_content_id',
260 'slot_origin',
261 'slot_role_id',
262 'content_size',
263 'content_sha1',
264 'content_address',
265 'content_model',
266 'model_name',
267 ],
268 'joins' => [
269 'content' => [ 'INNER JOIN', [ 'slot_content_id = content_id' ] ],
270 'content_models' => [ 'LEFT JOIN', [ 'content_model = model_id' ] ],
271 ],
272 ]
273 ];
274 }
275
276 /**
277 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
278 * @covers \MediaWiki\Storage\RevisionStore::insertSlotRowOn
279 * @covers \MediaWiki\Storage\RevisionStore::insertContentRowOn
280 */
281 public function testInsertRevisionOn_T202032() {
282 // This test only makes sense for MySQL
283 if ( $this->db->getType() !== 'mysql' ) {
284 $this->assertTrue( true );
285 return;
286 }
287
288 // NOTE: must be done before checking MAX(rev_id)
289 $page = $this->getTestPage();
290
291 $maxRevId = $this->db->selectField( 'revision', 'MAX(rev_id)' );
292
293 // Construct a slot row that will conflict with the insertion of the next revision ID,
294 // to emulate the failure mode described in T202032. Nothing will ever read this row,
295 // we just need it to trigger a primary key conflict.
296 $this->db->insert( 'slots', [
297 'slot_revision_id' => $maxRevId + 1,
298 'slot_role_id' => 1,
299 'slot_content_id' => 0,
300 'slot_origin' => 0
301 ], __METHOD__ );
302
303 $rev = new MutableRevisionRecord( $page->getTitle() );
304 $rev->setTimestamp( '20180101000000' );
305 $rev->setComment( CommentStoreComment::newUnsavedComment( 'test' ) );
306 $rev->setUser( $this->getTestUser()->getUser() );
307 $rev->setContent( 'main', new WikitextContent( 'Text' ) );
308 $rev->setPageId( $page->getId() );
309
310 $store = MediaWikiServices::getInstance()->getRevisionStore();
311 $return = $store->insertRevisionOn( $rev, $this->db );
312
313 $this->assertSame( $maxRevId + 2, $return->getId() );
314
315 // is the new revision correct?
316 $this->assertRevisionCompleteness( $return );
317 $this->assertRevisionRecordsEqual( $rev, $return );
318
319 // can we find it directly in the database?
320 $this->assertRevisionExistsInDatabase( $return );
321
322 // can we load it from the store?
323 $loaded = $store->getRevisionById( $return->getId() );
324 $this->assertRevisionCompleteness( $loaded );
325 $this->assertRevisionRecordsEqual( $return, $loaded );
326 }
327
328 }