Mostly drop old comment schemas
[lhc/web/wiklou.git] / tests / phpunit / includes / CommentStoreTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Wikimedia\ScopedCallback;
5 use Wikimedia\TestingAccessWrapper;
6
7 /**
8 * @group Database
9 * @covers CommentStore
10 * @covers CommentStoreComment
11 */
12 class CommentStoreTest extends MediaWikiLangTestCase {
13
14 protected $tablesUsed = [
15 'revision',
16 'revision_comment_temp',
17 'ipblocks',
18 'comment',
19 ];
20
21 protected function getSchemaOverrides( IMaintainableDatabase $db ) {
22 return [
23 'scripts' => [
24 __DIR__ . '/CommentStoreTest.sql',
25 ],
26 'drop' => [],
27 'create' => [ 'commentstore1', 'commentstore2', 'commentstore2_temp' ],
28 'alter' => [],
29 ];
30 }
31
32 /**
33 * Create a store for a particular stage
34 * @param int $stage
35 * @return CommentStore
36 */
37 protected function makeStore( $stage ) {
38 $store = new CommentStore( MediaWikiServices::getInstance()->getContentLanguage(), $stage );
39
40 TestingAccessWrapper::newFromObject( $store )->tempTables += [ 'cs2_comment' => [
41 'table' => 'commentstore2_temp',
42 'pk' => 'cs2t_id',
43 'field' => 'cs2t_comment_id',
44 'joinPK' => 'cs2_id',
45 'stage' => MIGRATION_OLD,
46 'deprecatedIn' => null,
47 ] ];
48
49 return $store;
50 }
51
52 /**
53 * Create a store for a particular stage and key (for testing deprecated behaviour)
54 * @param int $stage
55 * @param string $key
56 * @return CommentStore
57 */
58 protected function makeStoreWithKey( $stage, $key ) {
59 $this->hideDeprecated( 'CommentStore::newKey' );
60 $store = CommentStore::newKey( $key );
61 TestingAccessWrapper::newFromObject( $store )->stage = $stage;
62
63 TestingAccessWrapper::newFromObject( $store )->tempTables += [ 'cs2_comment' => [
64 'table' => 'commentstore2_temp',
65 'pk' => 'cs2t_id',
66 'field' => 'cs2t_comment_id',
67 'joinPK' => 'cs2_id',
68 'stage' => MIGRATION_OLD,
69 'deprecatedIn' => null,
70 ] ];
71
72 return $store;
73 }
74
75 /**
76 * @dataProvider provideGetFields
77 * @param int $stage
78 * @param string $key
79 * @param array $expect
80 */
81 public function testGetFields_withKeyConstruction( $stage, $key, $expect ) {
82 $store = $this->makeStoreWithKey( $stage, $key );
83 $result = $store->getFields();
84 $this->assertEquals( $expect, $result );
85 }
86
87 /**
88 * @dataProvider provideGetFields
89 * @param int $stage
90 * @param string $key
91 * @param array $expect
92 */
93 public function testGetFields( $stage, $key, $expect ) {
94 $store = $this->makeStore( $stage );
95 $result = $store->getFields( $key );
96 $this->assertEquals( $expect, $result );
97 }
98
99 public static function provideGetFields() {
100 return [
101 'Simple table, old' => [
102 MIGRATION_OLD, 'ipb_reason',
103 [ 'ipb_reason_text' => 'ipb_reason', 'ipb_reason_data' => 'NULL', 'ipb_reason_cid' => 'NULL' ],
104 ],
105 'Simple table, write-both' => [
106 MIGRATION_WRITE_BOTH, 'ipb_reason',
107 [ 'ipb_reason_old' => 'ipb_reason', 'ipb_reason_id' => 'ipb_reason_id' ],
108 ],
109 'Simple table, write-new' => [
110 MIGRATION_WRITE_NEW, 'ipb_reason',
111 [ 'ipb_reason_old' => 'ipb_reason', 'ipb_reason_id' => 'ipb_reason_id' ],
112 ],
113 'Simple table, new' => [
114 MIGRATION_NEW, 'ipb_reason',
115 [ 'ipb_reason_id' => 'ipb_reason_id' ],
116 ],
117
118 'Revision, old' => [
119 MIGRATION_OLD, 'rev_comment',
120 [
121 'rev_comment_text' => 'rev_comment',
122 'rev_comment_data' => 'NULL',
123 'rev_comment_cid' => 'NULL',
124 ],
125 ],
126 'Revision, write-both' => [
127 MIGRATION_WRITE_BOTH, 'rev_comment',
128 [ 'rev_comment_old' => 'rev_comment', 'rev_comment_pk' => 'rev_id' ],
129 ],
130 'Revision, write-new' => [
131 MIGRATION_WRITE_NEW, 'rev_comment',
132 [ 'rev_comment_old' => 'rev_comment', 'rev_comment_pk' => 'rev_id' ],
133 ],
134 'Revision, new' => [
135 MIGRATION_NEW, 'rev_comment',
136 [ 'rev_comment_pk' => 'rev_id' ],
137 ],
138
139 'Image, old' => [
140 MIGRATION_OLD, 'img_description',
141 [
142 'img_description_text' => 'img_description',
143 'img_description_data' => 'NULL',
144 'img_description_cid' => 'NULL',
145 ],
146 ],
147 'Image, write-both' => [
148 MIGRATION_WRITE_BOTH, 'img_description',
149 [
150 'img_description_old' => 'img_description',
151 'img_description_id' => 'img_description_id'
152 ],
153 ],
154 'Image, write-new' => [
155 MIGRATION_WRITE_NEW, 'img_description',
156 [
157 'img_description_old' => 'img_description',
158 'img_description_id' => 'img_description_id'
159 ],
160 ],
161 'Image, new' => [
162 MIGRATION_NEW, 'img_description',
163 [
164 'img_description_id' => 'img_description_id'
165 ],
166 ],
167 ];
168 }
169
170 /**
171 * @dataProvider provideGetJoin
172 * @param int $stage
173 * @param string $key
174 * @param array $expect
175 */
176 public function testGetJoin_withKeyConstruction( $stage, $key, $expect ) {
177 $store = $this->makeStoreWithKey( $stage, $key );
178 $result = $store->getJoin();
179 $this->assertEquals( $expect, $result );
180 }
181
182 /**
183 * @dataProvider provideGetJoin
184 * @param int $stage
185 * @param string $key
186 * @param array $expect
187 */
188 public function testGetJoin( $stage, $key, $expect ) {
189 $store = $this->makeStore( $stage );
190 $result = $store->getJoin( $key );
191 $this->assertEquals( $expect, $result );
192 }
193
194 public static function provideGetJoin() {
195 return [
196 'Simple table, old' => [
197 MIGRATION_OLD, 'ipb_reason', [
198 'tables' => [],
199 'fields' => [
200 'ipb_reason_text' => 'ipb_reason',
201 'ipb_reason_data' => 'NULL',
202 'ipb_reason_cid' => 'NULL',
203 ],
204 'joins' => [],
205 ],
206 ],
207 'Simple table, write-both' => [
208 MIGRATION_WRITE_BOTH, 'ipb_reason', [
209 'tables' => [ 'comment_ipb_reason' => 'comment' ],
210 'fields' => [
211 'ipb_reason_text' => 'COALESCE( comment_ipb_reason.comment_text, ipb_reason )',
212 'ipb_reason_data' => 'comment_ipb_reason.comment_data',
213 'ipb_reason_cid' => 'comment_ipb_reason.comment_id',
214 ],
215 'joins' => [
216 'comment_ipb_reason' => [ 'LEFT JOIN', 'comment_ipb_reason.comment_id = ipb_reason_id' ],
217 ],
218 ],
219 ],
220 'Simple table, write-new' => [
221 MIGRATION_WRITE_NEW, 'ipb_reason', [
222 'tables' => [ 'comment_ipb_reason' => 'comment' ],
223 'fields' => [
224 'ipb_reason_text' => 'COALESCE( comment_ipb_reason.comment_text, ipb_reason )',
225 'ipb_reason_data' => 'comment_ipb_reason.comment_data',
226 'ipb_reason_cid' => 'comment_ipb_reason.comment_id',
227 ],
228 'joins' => [
229 'comment_ipb_reason' => [ 'LEFT JOIN', 'comment_ipb_reason.comment_id = ipb_reason_id' ],
230 ],
231 ],
232 ],
233 'Simple table, new' => [
234 MIGRATION_NEW, 'ipb_reason', [
235 'tables' => [ 'comment_ipb_reason' => 'comment' ],
236 'fields' => [
237 'ipb_reason_text' => 'comment_ipb_reason.comment_text',
238 'ipb_reason_data' => 'comment_ipb_reason.comment_data',
239 'ipb_reason_cid' => 'comment_ipb_reason.comment_id',
240 ],
241 'joins' => [
242 'comment_ipb_reason' => [ 'JOIN', 'comment_ipb_reason.comment_id = ipb_reason_id' ],
243 ],
244 ],
245 ],
246
247 'Revision, old' => [
248 MIGRATION_OLD, 'rev_comment', [
249 'tables' => [],
250 'fields' => [
251 'rev_comment_text' => 'rev_comment',
252 'rev_comment_data' => 'NULL',
253 'rev_comment_cid' => 'NULL',
254 ],
255 'joins' => [],
256 ],
257 ],
258 'Revision, write-both' => [
259 MIGRATION_WRITE_BOTH, 'rev_comment', [
260 'tables' => [
261 'temp_rev_comment' => 'revision_comment_temp',
262 'comment_rev_comment' => 'comment',
263 ],
264 'fields' => [
265 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
266 'rev_comment_data' => 'comment_rev_comment.comment_data',
267 'rev_comment_cid' => 'comment_rev_comment.comment_id',
268 ],
269 'joins' => [
270 'temp_rev_comment' => [ 'LEFT JOIN', 'temp_rev_comment.revcomment_rev = rev_id' ],
271 'comment_rev_comment' => [ 'LEFT JOIN',
272 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id' ],
273 ],
274 ],
275 ],
276 'Revision, write-new' => [
277 MIGRATION_WRITE_NEW, 'rev_comment', [
278 'tables' => [
279 'temp_rev_comment' => 'revision_comment_temp',
280 'comment_rev_comment' => 'comment',
281 ],
282 'fields' => [
283 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
284 'rev_comment_data' => 'comment_rev_comment.comment_data',
285 'rev_comment_cid' => 'comment_rev_comment.comment_id',
286 ],
287 'joins' => [
288 'temp_rev_comment' => [ 'LEFT JOIN', 'temp_rev_comment.revcomment_rev = rev_id' ],
289 'comment_rev_comment' => [ 'LEFT JOIN',
290 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id' ],
291 ],
292 ],
293 ],
294 'Revision, new' => [
295 MIGRATION_NEW, 'rev_comment', [
296 'tables' => [
297 'temp_rev_comment' => 'revision_comment_temp',
298 'comment_rev_comment' => 'comment',
299 ],
300 'fields' => [
301 'rev_comment_text' => 'comment_rev_comment.comment_text',
302 'rev_comment_data' => 'comment_rev_comment.comment_data',
303 'rev_comment_cid' => 'comment_rev_comment.comment_id',
304 ],
305 'joins' => [
306 'temp_rev_comment' => [ 'JOIN', 'temp_rev_comment.revcomment_rev = rev_id' ],
307 'comment_rev_comment' => [ 'JOIN',
308 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id' ],
309 ],
310 ],
311 ],
312
313 'Image, old' => [
314 MIGRATION_OLD, 'img_description', [
315 'tables' => [],
316 'fields' => [
317 'img_description_text' => 'img_description',
318 'img_description_data' => 'NULL',
319 'img_description_cid' => 'NULL',
320 ],
321 'joins' => [],
322 ],
323 ],
324 'Image, write-both' => [
325 MIGRATION_WRITE_BOTH, 'img_description', [
326 'tables' => [
327 'comment_img_description' => 'comment',
328 ],
329 'fields' => [
330 'img_description_text' => 'COALESCE( comment_img_description.comment_text, img_description )',
331 'img_description_data' => 'comment_img_description.comment_data',
332 'img_description_cid' => 'comment_img_description.comment_id',
333 ],
334 'joins' => [
335 'comment_img_description' => [ 'LEFT JOIN',
336 'comment_img_description.comment_id = img_description_id',
337 ],
338 ],
339 ],
340 ],
341 'Image, write-new' => [
342 MIGRATION_WRITE_NEW, 'img_description', [
343 'tables' => [
344 'comment_img_description' => 'comment',
345 ],
346 'fields' => [
347 'img_description_text' => 'COALESCE( comment_img_description.comment_text, img_description )',
348 'img_description_data' => 'comment_img_description.comment_data',
349 'img_description_cid' => 'comment_img_description.comment_id',
350 ],
351 'joins' => [
352 'comment_img_description' => [ 'LEFT JOIN',
353 'comment_img_description.comment_id = img_description_id',
354 ],
355 ],
356 ],
357 ],
358 'Image, new' => [
359 MIGRATION_NEW, 'img_description', [
360 'tables' => [
361 'comment_img_description' => 'comment',
362 ],
363 'fields' => [
364 'img_description_text' => 'comment_img_description.comment_text',
365 'img_description_data' => 'comment_img_description.comment_data',
366 'img_description_cid' => 'comment_img_description.comment_id',
367 ],
368 'joins' => [
369 'comment_img_description' => [ 'JOIN',
370 'comment_img_description.comment_id = img_description_id',
371 ],
372 ],
373 ],
374 ],
375 ];
376 }
377
378 private function assertComment( $expect, $actual, $from ) {
379 $this->assertSame( $expect['text'], $actual->text, "text $from" );
380 $this->assertInstanceOf( get_class( $expect['message'] ), $actual->message,
381 "message class $from" );
382 $this->assertSame( $expect['message']->getKeysToTry(), $actual->message->getKeysToTry(),
383 "message keys $from" );
384 $this->assertEquals( $expect['message']->text(), $actual->message->text(),
385 "message rendering $from" );
386 $this->assertEquals( $expect['data'], $actual->data, "data $from" );
387 }
388
389 /**
390 * @dataProvider provideInsertRoundTrip
391 * @param string $table
392 * @param string $key
393 * @param string $pk
394 * @param string|Message $comment
395 * @param array|null $data
396 * @param array $expect
397 */
398 public function testInsertRoundTrip( $table, $key, $pk, $comment, $data, $expect ) {
399 static $id = 1;
400
401 $expectOld = [
402 'text' => $expect['text'],
403 'message' => new RawMessage( '$1', [ $expect['text'] ] ),
404 'data' => null,
405 ];
406
407 $stages = [
408 MIGRATION_OLD => [ MIGRATION_OLD, MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW ],
409 MIGRATION_WRITE_BOTH => [ MIGRATION_OLD, MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW,
410 MIGRATION_NEW ],
411 MIGRATION_WRITE_NEW => [ MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW, MIGRATION_NEW ],
412 MIGRATION_NEW => [ MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW, MIGRATION_NEW ],
413 ];
414
415 foreach ( $stages as $writeStage => $possibleReadStages ) {
416 $wstore = $this->makeStore( $writeStage );
417 $usesTemp = $key === 'cs2_comment';
418
419 if ( $usesTemp ) {
420 list( $fields, $callback ) = $wstore->insertWithTempTable(
421 $this->db, $key, $comment, $data
422 );
423 } else {
424 $fields = $wstore->insert( $this->db, $key, $comment, $data );
425 }
426
427 if ( $writeStage <= MIGRATION_WRITE_BOTH ) {
428 $this->assertSame( $expect['text'], $fields[$key], "old field, stage=$writeStage" );
429 } else {
430 $this->assertArrayNotHasKey( $key, $fields, "old field, stage=$writeStage" );
431 }
432 if ( $writeStage >= MIGRATION_WRITE_BOTH && !$usesTemp ) {
433 $this->assertArrayHasKey( "{$key}_id", $fields, "new field, stage=$writeStage" );
434 } else {
435 $this->assertArrayNotHasKey( "{$key}_id", $fields, "new field, stage=$writeStage" );
436 }
437
438 $this->db->insert( $table, [ $pk => ++$id ] + $fields, __METHOD__ );
439 if ( $usesTemp ) {
440 $callback( $id );
441 }
442
443 foreach ( $possibleReadStages as $readStage ) {
444 $rstore = $this->makeStore( $readStage );
445
446 $fieldRow = $this->db->selectRow(
447 $table,
448 $rstore->getFields( $key ),
449 [ $pk => $id ],
450 __METHOD__
451 );
452
453 $queryInfo = $rstore->getJoin( $key );
454 $joinRow = $this->db->selectRow(
455 [ $table ] + $queryInfo['tables'],
456 $queryInfo['fields'],
457 [ $pk => $id ],
458 __METHOD__,
459 [],
460 $queryInfo['joins']
461 );
462
463 $this->assertComment(
464 $writeStage === MIGRATION_OLD || $readStage === MIGRATION_OLD ? $expectOld : $expect,
465 $rstore->getCommentLegacy( $this->db, $key, $fieldRow ),
466 "w=$writeStage, r=$readStage, from getFields()"
467 );
468 $this->assertComment(
469 $writeStage === MIGRATION_OLD || $readStage === MIGRATION_OLD ? $expectOld : $expect,
470 $rstore->getComment( $key, $joinRow ),
471 "w=$writeStage, r=$readStage, from getJoin()"
472 );
473 }
474 }
475 }
476
477 /**
478 * @dataProvider provideInsertRoundTrip
479 * @param string $table
480 * @param string $key
481 * @param string $pk
482 * @param string|Message $comment
483 * @param array|null $data
484 * @param array $expect
485 */
486 public function testInsertRoundTrip_withKeyConstruction(
487 $table, $key, $pk, $comment, $data, $expect
488 ) {
489 static $id = 1000;
490
491 $expectOld = [
492 'text' => $expect['text'],
493 'message' => new RawMessage( '$1', [ $expect['text'] ] ),
494 'data' => null,
495 ];
496
497 $stages = [
498 MIGRATION_OLD => [ MIGRATION_OLD, MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW ],
499 MIGRATION_WRITE_BOTH => [ MIGRATION_OLD, MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW,
500 MIGRATION_NEW ],
501 MIGRATION_WRITE_NEW => [ MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW, MIGRATION_NEW ],
502 MIGRATION_NEW => [ MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW, MIGRATION_NEW ],
503 ];
504
505 foreach ( $stages as $writeStage => $possibleReadStages ) {
506 $wstore = $this->makeStoreWithKey( $writeStage, $key );
507 $usesTemp = $key === 'cs2_comment';
508
509 if ( $usesTemp ) {
510 list( $fields, $callback ) = $wstore->insertWithTempTable(
511 $this->db, $comment, $data
512 );
513 } else {
514 $fields = $wstore->insert( $this->db, $comment, $data );
515 }
516
517 if ( $writeStage <= MIGRATION_WRITE_BOTH ) {
518 $this->assertSame( $expect['text'], $fields[$key], "old field, stage=$writeStage" );
519 } else {
520 $this->assertArrayNotHasKey( $key, $fields, "old field, stage=$writeStage" );
521 }
522 if ( $writeStage >= MIGRATION_WRITE_BOTH && !$usesTemp ) {
523 $this->assertArrayHasKey( "{$key}_id", $fields, "new field, stage=$writeStage" );
524 } else {
525 $this->assertArrayNotHasKey( "{$key}_id", $fields, "new field, stage=$writeStage" );
526 }
527
528 $this->db->insert( $table, [ $pk => ++$id ] + $fields, __METHOD__ );
529 if ( $usesTemp ) {
530 $callback( $id );
531 }
532
533 foreach ( $possibleReadStages as $readStage ) {
534 $rstore = $this->makeStoreWithKey( $readStage, $key );
535
536 $fieldRow = $this->db->selectRow(
537 $table,
538 $rstore->getFields(),
539 [ $pk => $id ],
540 __METHOD__
541 );
542
543 $queryInfo = $rstore->getJoin();
544 $joinRow = $this->db->selectRow(
545 [ $table ] + $queryInfo['tables'],
546 $queryInfo['fields'],
547 [ $pk => $id ],
548 __METHOD__,
549 [],
550 $queryInfo['joins']
551 );
552
553 $this->assertComment(
554 $writeStage === MIGRATION_OLD || $readStage === MIGRATION_OLD ? $expectOld : $expect,
555 $rstore->getCommentLegacy( $this->db, $fieldRow ),
556 "w=$writeStage, r=$readStage, from getFields()"
557 );
558 $this->assertComment(
559 $writeStage === MIGRATION_OLD || $readStage === MIGRATION_OLD ? $expectOld : $expect,
560 $rstore->getComment( $joinRow ),
561 "w=$writeStage, r=$readStage, from getJoin()"
562 );
563 }
564 }
565 }
566
567 public static function provideInsertRoundTrip() {
568 $db = wfGetDB( DB_REPLICA ); // for timestamps
569
570 $msgComment = new Message( 'parentheses', [ 'message comment' ] );
571 $textCommentMsg = new RawMessage( '$1', [ 'text comment' ] );
572 $nestedMsgComment = new Message( [ 'parentheses', 'rawmessage' ], [ new Message( 'mainpage' ) ] );
573 $comStoreComment = new CommentStoreComment(
574 null, 'comment store comment', null, [ 'foo' => 'bar' ]
575 );
576
577 return [
578 'Simple table, text comment' => [
579 'commentstore1', 'cs1_comment', 'cs1_id', 'text comment', null, [
580 'text' => 'text comment',
581 'message' => $textCommentMsg,
582 'data' => null,
583 ]
584 ],
585 'Simple table, text comment with data' => [
586 'commentstore1', 'cs1_comment', 'cs1_id', 'text comment', [ 'message' => 42 ], [
587 'text' => 'text comment',
588 'message' => $textCommentMsg,
589 'data' => [ 'message' => 42 ],
590 ]
591 ],
592 'Simple table, message comment' => [
593 'commentstore1', 'cs1_comment', 'cs1_id', $msgComment, null, [
594 'text' => '(message comment)',
595 'message' => $msgComment,
596 'data' => null,
597 ]
598 ],
599 'Simple table, message comment with data' => [
600 'commentstore1', 'cs1_comment', 'cs1_id', $msgComment, [ 'message' => 42 ], [
601 'text' => '(message comment)',
602 'message' => $msgComment,
603 'data' => [ 'message' => 42 ],
604 ]
605 ],
606 'Simple table, nested message comment' => [
607 'commentstore1', 'cs1_comment', 'cs1_id', $nestedMsgComment, null, [
608 'text' => '(Main Page)',
609 'message' => $nestedMsgComment,
610 'data' => null,
611 ]
612 ],
613 'Simple table, CommentStoreComment' => [
614 'commentstore1', 'cs1_comment', 'cs1_id', clone $comStoreComment, [ 'baz' => 'baz' ], [
615 'text' => 'comment store comment',
616 'message' => $comStoreComment->message,
617 'data' => [ 'foo' => 'bar' ],
618 ]
619 ],
620
621 'Revision, text comment' => [
622 'commentstore2', 'cs2_comment', 'cs2_id', 'text comment', null, [
623 'text' => 'text comment',
624 'message' => $textCommentMsg,
625 'data' => null,
626 ]
627 ],
628 'Revision, text comment with data' => [
629 'commentstore2', 'cs2_comment', 'cs2_id', 'text comment', [ 'message' => 42 ], [
630 'text' => 'text comment',
631 'message' => $textCommentMsg,
632 'data' => [ 'message' => 42 ],
633 ]
634 ],
635 'Revision, message comment' => [
636 'commentstore2', 'cs2_comment', 'cs2_id', $msgComment, null, [
637 'text' => '(message comment)',
638 'message' => $msgComment,
639 'data' => null,
640 ]
641 ],
642 'Revision, message comment with data' => [
643 'commentstore2', 'cs2_comment', 'cs2_id', $msgComment, [ 'message' => 42 ], [
644 'text' => '(message comment)',
645 'message' => $msgComment,
646 'data' => [ 'message' => 42 ],
647 ]
648 ],
649 'Revision, nested message comment' => [
650 'commentstore2', 'cs2_comment', 'cs2_id', $nestedMsgComment, null, [
651 'text' => '(Main Page)',
652 'message' => $nestedMsgComment,
653 'data' => null,
654 ]
655 ],
656 'Revision, CommentStoreComment' => [
657 'commentstore2', 'cs2_comment', 'cs2_id', clone $comStoreComment, [ 'baz' => 'baz' ], [
658 'text' => 'comment store comment',
659 'message' => $comStoreComment->message,
660 'data' => [ 'foo' => 'bar' ],
661 ]
662 ],
663 ];
664 }
665
666 public function testGetCommentErrors() {
667 Wikimedia\suppressWarnings();
668 $reset = new ScopedCallback( 'Wikimedia\restoreWarnings' );
669
670 $store = $this->makeStore( MIGRATION_OLD );
671 $res = $store->getComment( 'dummy', [ 'dummy' => 'comment' ] );
672 $this->assertSame( '', $res->text );
673 $res = $store->getComment( 'dummy', [ 'dummy' => 'comment' ], true );
674 $this->assertSame( 'comment', $res->text );
675
676 $store = $this->makeStore( MIGRATION_NEW );
677 try {
678 $store->getComment( 'dummy', [ 'dummy' => 'comment' ] );
679 $this->fail( 'Expected exception not thrown' );
680 } catch ( InvalidArgumentException $ex ) {
681 $this->assertSame( '$row does not contain fields needed for comment dummy', $ex->getMessage() );
682 }
683 $res = $store->getComment( 'dummy', [ 'dummy' => 'comment' ], true );
684 $this->assertSame( 'comment', $res->text );
685 try {
686 $store->getComment( 'dummy', [ 'dummy_id' => 1 ] );
687 $this->fail( 'Expected exception not thrown' );
688 } catch ( InvalidArgumentException $ex ) {
689 $this->assertSame(
690 '$row does not contain fields needed for comment dummy and getComment(), '
691 . 'but does have fields for getCommentLegacy()',
692 $ex->getMessage()
693 );
694 }
695
696 $store = $this->makeStore( MIGRATION_NEW );
697 try {
698 $store->getComment( 'rev_comment', [ 'rev_comment' => 'comment' ] );
699 $this->fail( 'Expected exception not thrown' );
700 } catch ( InvalidArgumentException $ex ) {
701 $this->assertSame(
702 '$row does not contain fields needed for comment rev_comment', $ex->getMessage()
703 );
704 }
705 $res = $store->getComment( 'rev_comment', [ 'rev_comment' => 'comment' ], true );
706 $this->assertSame( 'comment', $res->text );
707 try {
708 $store->getComment( 'rev_comment', [ 'rev_comment_pk' => 1 ] );
709 $this->fail( 'Expected exception not thrown' );
710 } catch ( InvalidArgumentException $ex ) {
711 $this->assertSame(
712 '$row does not contain fields needed for comment rev_comment and getComment(), '
713 . 'but does have fields for getCommentLegacy()',
714 $ex->getMessage()
715 );
716 }
717 }
718
719 public static function provideStages() {
720 return [
721 'MIGRATION_OLD' => [ MIGRATION_OLD ],
722 'MIGRATION_WRITE_BOTH' => [ MIGRATION_WRITE_BOTH ],
723 'MIGRATION_WRITE_NEW' => [ MIGRATION_WRITE_NEW ],
724 'MIGRATION_NEW' => [ MIGRATION_NEW ],
725 ];
726 }
727
728 /**
729 * @dataProvider provideStages
730 * @param int $stage
731 * @expectedException InvalidArgumentException
732 * @expectedExceptionMessage Must use insertWithTempTable() for rev_comment
733 */
734 public function testInsertWrong( $stage ) {
735 $store = $this->makeStore( $stage );
736 $store->insert( $this->db, 'rev_comment', 'foo' );
737 }
738
739 /**
740 * @dataProvider provideStages
741 * @param int $stage
742 * @expectedException InvalidArgumentException
743 * @expectedExceptionMessage Must use insert() for ipb_reason
744 */
745 public function testInsertWithTempTableWrong( $stage ) {
746 $store = $this->makeStore( $stage );
747 $store->insertWithTempTable( $this->db, 'ipb_reason', 'foo' );
748 }
749
750 /**
751 * @dataProvider provideStages
752 * @param int $stage
753 */
754 public function testInsertWithTempTableDeprecated( $stage ) {
755 $store = $this->makeStore( $stage );
756 $wrap = TestingAccessWrapper::newFromObject( $store );
757 $wrap->tempTables += [ 'ipb_reason' => [
758 'stage' => MIGRATION_NEW,
759 'deprecatedIn' => '1.30',
760 ] ];
761
762 $this->hideDeprecated( 'CommentStore::insertWithTempTable for ipb_reason' );
763 list( $fields, $callback ) = $store->insertWithTempTable( $this->db, 'ipb_reason', 'foo' );
764 $this->assertTrue( is_callable( $callback ) );
765 }
766
767 public function testInsertTruncation() {
768 $comment = str_repeat( '💣', 16400 );
769 $truncated1 = str_repeat( '💣', 63 ) . '...';
770 $truncated2 = str_repeat( '💣', CommentStore::COMMENT_CHARACTER_LIMIT - 3 ) . '...';
771
772 $store = $this->makeStore( MIGRATION_WRITE_BOTH );
773 $fields = $store->insert( $this->db, 'ipb_reason', $comment );
774 $this->assertSame( $truncated1, $fields['ipb_reason'] );
775 $stored = $this->db->selectField(
776 'comment', 'comment_text', [ 'comment_id' => $fields['ipb_reason_id'] ], __METHOD__
777 );
778 $this->assertSame( $truncated2, $stored );
779 }
780
781 /**
782 * @expectedException OverflowException
783 * @expectedExceptionMessage Comment data is too long (65611 bytes, maximum is 65535)
784 */
785 public function testInsertTooMuchData() {
786 $store = $this->makeStore( MIGRATION_WRITE_BOTH );
787 $store->insert( $this->db, 'ipb_reason', 'foo', [
788 'long' => str_repeat( '💣', 16400 )
789 ] );
790 }
791
792 public function testGetStore() {
793 $this->assertInstanceOf( CommentStore::class, CommentStore::getStore() );
794 }
795
796 public function testNewKey() {
797 $this->hideDeprecated( 'CommentStore::newKey' );
798 $this->assertInstanceOf( CommentStore::class, CommentStore::newKey( 'dummy' ) );
799 }
800
801 }