Merge "Language: compare on same object in equals()"
[lhc/web/wiklou.git] / tests / phpunit / includes / ActorMigrationTest.php
1 <?php
2
3 use MediaWiki\User\UserIdentity;
4 use Wikimedia\TestingAccessWrapper;
5
6 /**
7 * @group Database
8 * @covers ActorMigration
9 */
10 class ActorMigrationTest extends MediaWikiLangTestCase {
11
12 protected $tablesUsed = [
13 'revision',
14 'revision_actor_temp',
15 'ipblocks',
16 'recentchanges',
17 'actor',
18 ];
19
20 /**
21 * Create an ActorMigration for a particular stage
22 * @param int $stage
23 * @return ActorMigration
24 */
25 protected function makeMigration( $stage ) {
26 return new ActorMigration( $stage );
27 }
28
29 /**
30 * @dataProvider provideGetJoin
31 * @param int $stage
32 * @param string $key
33 * @param array $expect
34 */
35 public function testGetJoin( $stage, $key, $expect ) {
36 $m = $this->makeMigration( $stage );
37 $result = $m->getJoin( $key );
38 $this->assertEquals( $expect, $result );
39 }
40
41 public static function provideGetJoin() {
42 return [
43 'Simple table, old' => [
44 MIGRATION_OLD, 'rc_user', [
45 'tables' => [],
46 'fields' => [
47 'rc_user' => 'rc_user',
48 'rc_user_text' => 'rc_user_text',
49 'rc_actor' => 'NULL',
50 ],
51 'joins' => [],
52 ],
53 ],
54 'Simple table, write-both' => [
55 MIGRATION_WRITE_BOTH, 'rc_user', [
56 'tables' => [ 'actor_rc_user' => 'actor' ],
57 'fields' => [
58 'rc_user' => 'COALESCE( actor_rc_user.actor_user, rc_user )',
59 'rc_user_text' => 'COALESCE( actor_rc_user.actor_name, rc_user_text )',
60 'rc_actor' => 'rc_actor',
61 ],
62 'joins' => [
63 'actor_rc_user' => [ 'LEFT JOIN', 'actor_rc_user.actor_id = rc_actor' ],
64 ],
65 ],
66 ],
67 'Simple table, write-new' => [
68 MIGRATION_WRITE_NEW, 'rc_user', [
69 'tables' => [ 'actor_rc_user' => 'actor' ],
70 'fields' => [
71 'rc_user' => 'COALESCE( actor_rc_user.actor_user, rc_user )',
72 'rc_user_text' => 'COALESCE( actor_rc_user.actor_name, rc_user_text )',
73 'rc_actor' => 'rc_actor',
74 ],
75 'joins' => [
76 'actor_rc_user' => [ 'LEFT JOIN', 'actor_rc_user.actor_id = rc_actor' ],
77 ],
78 ],
79 ],
80 'Simple table, new' => [
81 MIGRATION_NEW, 'rc_user', [
82 'tables' => [ 'actor_rc_user' => 'actor' ],
83 'fields' => [
84 'rc_user' => 'actor_rc_user.actor_user',
85 'rc_user_text' => 'actor_rc_user.actor_name',
86 'rc_actor' => 'rc_actor',
87 ],
88 'joins' => [
89 'actor_rc_user' => [ 'JOIN', 'actor_rc_user.actor_id = rc_actor' ],
90 ],
91 ],
92 ],
93
94 'ipblocks, old' => [
95 MIGRATION_OLD, 'ipb_by', [
96 'tables' => [],
97 'fields' => [
98 'ipb_by' => 'ipb_by',
99 'ipb_by_text' => 'ipb_by_text',
100 'ipb_by_actor' => 'NULL',
101 ],
102 'joins' => [],
103 ],
104 ],
105 'ipblocks, write-both' => [
106 MIGRATION_WRITE_BOTH, 'ipb_by', [
107 'tables' => [ 'actor_ipb_by' => 'actor' ],
108 'fields' => [
109 'ipb_by' => 'COALESCE( actor_ipb_by.actor_user, ipb_by )',
110 'ipb_by_text' => 'COALESCE( actor_ipb_by.actor_name, ipb_by_text )',
111 'ipb_by_actor' => 'ipb_by_actor',
112 ],
113 'joins' => [
114 'actor_ipb_by' => [ 'LEFT JOIN', 'actor_ipb_by.actor_id = ipb_by_actor' ],
115 ],
116 ],
117 ],
118 'ipblocks, write-new' => [
119 MIGRATION_WRITE_NEW, 'ipb_by', [
120 'tables' => [ 'actor_ipb_by' => 'actor' ],
121 'fields' => [
122 'ipb_by' => 'COALESCE( actor_ipb_by.actor_user, ipb_by )',
123 'ipb_by_text' => 'COALESCE( actor_ipb_by.actor_name, ipb_by_text )',
124 'ipb_by_actor' => 'ipb_by_actor',
125 ],
126 'joins' => [
127 'actor_ipb_by' => [ 'LEFT JOIN', 'actor_ipb_by.actor_id = ipb_by_actor' ],
128 ],
129 ],
130 ],
131 'ipblocks, new' => [
132 MIGRATION_NEW, 'ipb_by', [
133 'tables' => [ 'actor_ipb_by' => 'actor' ],
134 'fields' => [
135 'ipb_by' => 'actor_ipb_by.actor_user',
136 'ipb_by_text' => 'actor_ipb_by.actor_name',
137 'ipb_by_actor' => 'ipb_by_actor',
138 ],
139 'joins' => [
140 'actor_ipb_by' => [ 'JOIN', 'actor_ipb_by.actor_id = ipb_by_actor' ],
141 ],
142 ],
143 ],
144
145 'Revision, old' => [
146 MIGRATION_OLD, 'rev_user', [
147 'tables' => [],
148 'fields' => [
149 'rev_user' => 'rev_user',
150 'rev_user_text' => 'rev_user_text',
151 'rev_actor' => 'NULL',
152 ],
153 'joins' => [],
154 ],
155 ],
156 'Revision, write-both' => [
157 MIGRATION_WRITE_BOTH, 'rev_user', [
158 'tables' => [
159 'temp_rev_user' => 'revision_actor_temp',
160 'actor_rev_user' => 'actor',
161 ],
162 'fields' => [
163 'rev_user' => 'COALESCE( actor_rev_user.actor_user, rev_user )',
164 'rev_user_text' => 'COALESCE( actor_rev_user.actor_name, rev_user_text )',
165 'rev_actor' => 'temp_rev_user.revactor_actor',
166 ],
167 'joins' => [
168 'temp_rev_user' => [ 'LEFT JOIN', 'temp_rev_user.revactor_rev = rev_id' ],
169 'actor_rev_user' => [ 'LEFT JOIN', 'actor_rev_user.actor_id = temp_rev_user.revactor_actor' ],
170 ],
171 ],
172 ],
173 'Revision, write-new' => [
174 MIGRATION_WRITE_NEW, 'rev_user', [
175 'tables' => [
176 'temp_rev_user' => 'revision_actor_temp',
177 'actor_rev_user' => 'actor',
178 ],
179 'fields' => [
180 'rev_user' => 'COALESCE( actor_rev_user.actor_user, rev_user )',
181 'rev_user_text' => 'COALESCE( actor_rev_user.actor_name, rev_user_text )',
182 'rev_actor' => 'temp_rev_user.revactor_actor',
183 ],
184 'joins' => [
185 'temp_rev_user' => [ 'LEFT JOIN', 'temp_rev_user.revactor_rev = rev_id' ],
186 'actor_rev_user' => [ 'LEFT JOIN', 'actor_rev_user.actor_id = temp_rev_user.revactor_actor' ],
187 ],
188 ],
189 ],
190 'Revision, new' => [
191 MIGRATION_NEW, 'rev_user', [
192 'tables' => [
193 'temp_rev_user' => 'revision_actor_temp',
194 'actor_rev_user' => 'actor',
195 ],
196 'fields' => [
197 'rev_user' => 'actor_rev_user.actor_user',
198 'rev_user_text' => 'actor_rev_user.actor_name',
199 'rev_actor' => 'temp_rev_user.revactor_actor',
200 ],
201 'joins' => [
202 'temp_rev_user' => [ 'JOIN', 'temp_rev_user.revactor_rev = rev_id' ],
203 'actor_rev_user' => [ 'JOIN', 'actor_rev_user.actor_id = temp_rev_user.revactor_actor' ],
204 ],
205 ],
206 ],
207 ];
208 }
209
210 /**
211 * @dataProvider provideGetWhere
212 * @param int $stage
213 * @param string $key
214 * @param UserIdentity[] $users
215 * @param bool $useId
216 * @param array $expect
217 */
218 public function testGetWhere( $stage, $key, $users, $useId, $expect ) {
219 $expect['conds'] = '(' . implode( ') OR (', $expect['orconds'] ) . ')';
220
221 if ( count( $users ) === 1 ) {
222 $users = reset( $users );
223 }
224
225 $m = $this->makeMigration( $stage );
226 $result = $m->getWhere( $this->db, $key, $users, $useId );
227 $this->assertEquals( $expect, $result );
228 }
229
230 public function provideGetWhere() {
231 $makeUserIdentity = function ( $id, $name, $actor ) {
232 $u = $this->getMock( UserIdentity::class );
233 $u->method( 'getId' )->willReturn( $id );
234 $u->method( 'getName' )->willReturn( $name );
235 $u->method( 'getActorId' )->willReturn( $actor );
236 return $u;
237 };
238
239 $genericUser = [ $makeUserIdentity( 1, 'User1', 11 ) ];
240 $complicatedUsers = [
241 $makeUserIdentity( 1, 'User1', 11 ),
242 $makeUserIdentity( 2, 'User2', 12 ),
243 $makeUserIdentity( 3, 'User3', 0 ),
244 $makeUserIdentity( 0, '192.168.12.34', 34 ),
245 $makeUserIdentity( 0, '192.168.12.35', 0 ),
246 ];
247
248 return [
249 'Simple table, old' => [
250 MIGRATION_OLD, 'rc_user', $genericUser, true, [
251 'tables' => [],
252 'orconds' => [ 'userid' => "rc_user = '1'" ],
253 'joins' => [],
254 ],
255 ],
256 'Simple table, write-both' => [
257 MIGRATION_WRITE_BOTH, 'rc_user', $genericUser, true, [
258 'tables' => [],
259 'orconds' => [
260 'actor' => "rc_actor = '11'",
261 'userid' => "rc_actor = '0' AND rc_user = '1'"
262 ],
263 'joins' => [],
264 ],
265 ],
266 'Simple table, write-new' => [
267 MIGRATION_WRITE_NEW, 'rc_user', $genericUser, true, [
268 'tables' => [],
269 'orconds' => [
270 'actor' => "rc_actor = '11'",
271 'userid' => "rc_actor = '0' AND rc_user = '1'"
272 ],
273 'joins' => [],
274 ],
275 ],
276 'Simple table, new' => [
277 MIGRATION_NEW, 'rc_user', $genericUser, true, [
278 'tables' => [],
279 'orconds' => [ 'actor' => "rc_actor = '11'" ],
280 'joins' => [],
281 ],
282 ],
283
284 'ipblocks, old' => [
285 MIGRATION_OLD, 'ipb_by', $genericUser, true, [
286 'tables' => [],
287 'orconds' => [ 'userid' => "ipb_by = '1'" ],
288 'joins' => [],
289 ],
290 ],
291 'ipblocks, write-both' => [
292 MIGRATION_WRITE_BOTH, 'ipb_by', $genericUser, true, [
293 'tables' => [],
294 'orconds' => [
295 'actor' => "ipb_by_actor = '11'",
296 'userid' => "ipb_by_actor = '0' AND ipb_by = '1'"
297 ],
298 'joins' => [],
299 ],
300 ],
301 'ipblocks, write-new' => [
302 MIGRATION_WRITE_NEW, 'ipb_by', $genericUser, true, [
303 'tables' => [],
304 'orconds' => [
305 'actor' => "ipb_by_actor = '11'",
306 'userid' => "ipb_by_actor = '0' AND ipb_by = '1'"
307 ],
308 'joins' => [],
309 ],
310 ],
311 'ipblocks, new' => [
312 MIGRATION_NEW, 'ipb_by', $genericUser, true, [
313 'tables' => [],
314 'orconds' => [ 'actor' => "ipb_by_actor = '11'" ],
315 'joins' => [],
316 ],
317 ],
318
319 'Revision, old' => [
320 MIGRATION_OLD, 'rev_user', $genericUser, true, [
321 'tables' => [],
322 'orconds' => [ 'userid' => "rev_user = '1'" ],
323 'joins' => [],
324 ],
325 ],
326 'Revision, write-both' => [
327 MIGRATION_WRITE_BOTH, 'rev_user', $genericUser, true, [
328 'tables' => [
329 'temp_rev_user' => 'revision_actor_temp',
330 ],
331 'orconds' => [
332 'actor' =>
333 "(temp_rev_user.revactor_actor IS NOT NULL) AND temp_rev_user.revactor_actor = '11'",
334 'userid' => "temp_rev_user.revactor_actor IS NULL AND rev_user = '1'"
335 ],
336 'joins' => [
337 'temp_rev_user' => [ 'LEFT JOIN', 'temp_rev_user.revactor_rev = rev_id' ],
338 ],
339 ],
340 ],
341 'Revision, write-new' => [
342 MIGRATION_WRITE_NEW, 'rev_user', $genericUser, true, [
343 'tables' => [
344 'temp_rev_user' => 'revision_actor_temp',
345 ],
346 'orconds' => [
347 'actor' =>
348 "(temp_rev_user.revactor_actor IS NOT NULL) AND temp_rev_user.revactor_actor = '11'",
349 'userid' => "temp_rev_user.revactor_actor IS NULL AND rev_user = '1'"
350 ],
351 'joins' => [
352 'temp_rev_user' => [ 'LEFT JOIN', 'temp_rev_user.revactor_rev = rev_id' ],
353 ],
354 ],
355 ],
356 'Revision, new' => [
357 MIGRATION_NEW, 'rev_user', $genericUser, true, [
358 'tables' => [
359 'temp_rev_user' => 'revision_actor_temp',
360 ],
361 'orconds' => [ 'actor' => "temp_rev_user.revactor_actor = '11'" ],
362 'joins' => [
363 'temp_rev_user' => [ 'JOIN', 'temp_rev_user.revactor_rev = rev_id' ],
364 ],
365 ],
366 ],
367
368 'Multiple users, old' => [
369 MIGRATION_OLD, 'rc_user', $complicatedUsers, true, [
370 'tables' => [],
371 'orconds' => [
372 'userid' => "rc_user IN ('1','2','3') ",
373 'username' => "rc_user_text IN ('192.168.12.34','192.168.12.35') "
374 ],
375 'joins' => [],
376 ],
377 ],
378 'Multiple users, write-both' => [
379 MIGRATION_WRITE_BOTH, 'rc_user', $complicatedUsers, true, [
380 'tables' => [],
381 'orconds' => [
382 'actor' => "rc_actor IN ('11','12','34') ",
383 'userid' => "rc_actor = '0' AND rc_user IN ('1','2','3') ",
384 'username' => "rc_actor = '0' AND rc_user_text IN ('192.168.12.34','192.168.12.35') "
385 ],
386 'joins' => [],
387 ],
388 ],
389 'Multiple users, write-new' => [
390 MIGRATION_WRITE_NEW, 'rc_user', $complicatedUsers, true, [
391 'tables' => [],
392 'orconds' => [
393 'actor' => "rc_actor IN ('11','12','34') ",
394 'userid' => "rc_actor = '0' AND rc_user IN ('1','2','3') ",
395 'username' => "rc_actor = '0' AND rc_user_text IN ('192.168.12.34','192.168.12.35') "
396 ],
397 'joins' => [],
398 ],
399 ],
400 'Multiple users, new' => [
401 MIGRATION_NEW, 'rc_user', $complicatedUsers, true, [
402 'tables' => [],
403 'orconds' => [ 'actor' => "rc_actor IN ('11','12','34') " ],
404 'joins' => [],
405 ],
406 ],
407
408 'Multiple users, no use ID, old' => [
409 MIGRATION_OLD, 'rc_user', $complicatedUsers, false, [
410 'tables' => [],
411 'orconds' => [
412 'username' => "rc_user_text IN ('User1','User2','User3','192.168.12.34','192.168.12.35') "
413 ],
414 'joins' => [],
415 ],
416 ],
417 'Multiple users, write-both' => [
418 MIGRATION_WRITE_BOTH, 'rc_user', $complicatedUsers, false, [
419 'tables' => [],
420 'orconds' => [
421 'actor' => "rc_actor IN ('11','12','34') ",
422 'username' => "rc_actor = '0' AND "
423 . "rc_user_text IN ('User1','User2','User3','192.168.12.34','192.168.12.35') "
424 ],
425 'joins' => [],
426 ],
427 ],
428 'Multiple users, write-new' => [
429 MIGRATION_WRITE_NEW, 'rc_user', $complicatedUsers, false, [
430 'tables' => [],
431 'orconds' => [
432 'actor' => "rc_actor IN ('11','12','34') ",
433 'username' => "rc_actor = '0' AND "
434 . "rc_user_text IN ('User1','User2','User3','192.168.12.34','192.168.12.35') "
435 ],
436 'joins' => [],
437 ],
438 ],
439 'Multiple users, new' => [
440 MIGRATION_NEW, 'rc_user', $complicatedUsers, false, [
441 'tables' => [],
442 'orconds' => [ 'actor' => "rc_actor IN ('11','12','34') " ],
443 'joins' => [],
444 ],
445 ],
446 ];
447 }
448
449 /**
450 * @dataProvider provideInsertRoundTrip
451 * @param string $table
452 * @param string $key
453 * @param string $pk
454 * @param array $extraFields
455 */
456 public function testInsertRoundTrip( $table, $key, $pk, $extraFields ) {
457 $u = $this->getTestUser()->getUser();
458 $user = $this->getMock( UserIdentity::class );
459 $user->method( 'getId' )->willReturn( $u->getId() );
460 $user->method( 'getName' )->willReturn( $u->getName() );
461 if ( $u->getActorId( $this->db ) ) {
462 $user->method( 'getActorId' )->willReturn( $u->getActorId() );
463 } else {
464 $this->db->insert(
465 'actor',
466 [ 'actor_user' => $u->getId(), 'actor_name' => $u->getName() ],
467 __METHOD__
468 );
469 $user->method( 'getActorId' )->willReturn( $this->db->insertId() );
470 }
471
472 $stages = [
473 MIGRATION_OLD => [ MIGRATION_OLD, MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW ],
474 MIGRATION_WRITE_BOTH => [ MIGRATION_OLD, MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW,
475 MIGRATION_NEW ],
476 MIGRATION_WRITE_NEW => [ MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW, MIGRATION_NEW ],
477 MIGRATION_NEW => [ MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW, MIGRATION_NEW ],
478 ];
479
480 $nameKey = $key . '_text';
481 $actorKey = $key === 'ipb_by' ? 'ipb_by_actor' : substr( $key, 0, -5 ) . '_actor';
482
483 foreach ( $stages as $writeStage => $possibleReadStages ) {
484 if ( $key === 'ipb_by' ) {
485 $extraFields['ipb_address'] = __CLASS__ . "#$writeStage";
486 }
487
488 $w = $this->makeMigration( $writeStage );
489 $usesTemp = $key === 'rev_user';
490
491 if ( $usesTemp ) {
492 list( $fields, $callback ) = $w->getInsertValuesWithTempTable( $this->db, $key, $user );
493 } else {
494 $fields = $w->getInsertValues( $this->db, $key, $user );
495 }
496
497 if ( $writeStage <= MIGRATION_WRITE_BOTH ) {
498 $this->assertSame( $user->getId(), $fields[$key], "old field, stage=$writeStage" );
499 $this->assertSame( $user->getName(), $fields[$nameKey], "old field, stage=$writeStage" );
500 } else {
501 $this->assertArrayNotHasKey( $key, $fields, "old field, stage=$writeStage" );
502 $this->assertArrayNotHasKey( $nameKey, $fields, "old field, stage=$writeStage" );
503 }
504 if ( $writeStage >= MIGRATION_WRITE_BOTH && !$usesTemp ) {
505 $this->assertSame( $user->getActorId(), $fields[$actorKey], "new field, stage=$writeStage" );
506 } else {
507 $this->assertArrayNotHasKey( $actorKey, $fields, "new field, stage=$writeStage" );
508 }
509
510 $this->db->insert( $table, $extraFields + $fields, __METHOD__ );
511 $id = $this->db->insertId();
512 if ( $usesTemp ) {
513 $callback( $id, $extraFields );
514 }
515
516 foreach ( $possibleReadStages as $readStage ) {
517 $r = $this->makeMigration( $readStage );
518
519 $queryInfo = $r->getJoin( $key );
520 $row = $this->db->selectRow(
521 [ $table ] + $queryInfo['tables'],
522 $queryInfo['fields'],
523 [ $pk => $id ],
524 __METHOD__,
525 [],
526 $queryInfo['joins']
527 );
528
529 $this->assertSame( $user->getId(), (int)$row->$key, "w=$writeStage, r=$readStage, id" );
530 $this->assertSame( $user->getName(), $row->$nameKey, "w=$writeStage, r=$readStage, name" );
531 $this->assertSame(
532 $readStage === MIGRATION_OLD || $writeStage === MIGRATION_OLD ? 0 : $user->getActorId(),
533 (int)$row->$actorKey,
534 "w=$writeStage, r=$readStage, actor"
535 );
536 }
537 }
538 }
539
540 public static function provideInsertRoundTrip() {
541 $db = wfGetDB( DB_REPLICA ); // for timestamps
542
543 $ipbfields = [
544 ];
545 $revfields = [
546 ];
547
548 return [
549 'recentchanges' => [ 'recentchanges', 'rc_user', 'rc_id', [
550 'rc_timestamp' => $db->timestamp(),
551 'rc_namespace' => 0,
552 'rc_title' => 'Test',
553 'rc_this_oldid' => 42,
554 'rc_last_oldid' => 41,
555 'rc_source' => 'test',
556 ] ],
557 'ipblocks' => [ 'ipblocks', 'ipb_by', 'ipb_id', [
558 'ipb_range_start' => '',
559 'ipb_range_end' => '',
560 'ipb_timestamp' => $db->timestamp(),
561 'ipb_expiry' => $db->getInfinity(),
562 ] ],
563 'revision' => [ 'revision', 'rev_user', 'rev_id', [
564 'rev_page' => 42,
565 'rev_text_id' => 42,
566 'rev_len' => 0,
567 'rev_timestamp' => $db->timestamp(),
568 ] ],
569 ];
570 }
571
572 public static function provideStages() {
573 return [
574 'MIGRATION_OLD' => [ MIGRATION_OLD ],
575 'MIGRATION_WRITE_BOTH' => [ MIGRATION_WRITE_BOTH ],
576 'MIGRATION_WRITE_NEW' => [ MIGRATION_WRITE_NEW ],
577 'MIGRATION_NEW' => [ MIGRATION_NEW ],
578 ];
579 }
580
581 /**
582 * @dataProvider provideStages
583 * @param int $stage
584 * @expectedException InvalidArgumentException
585 * @expectedExceptionMessage Must use getInsertValuesWithTempTable() for rev_user
586 */
587 public function testInsertWrong( $stage ) {
588 $m = $this->makeMigration( $stage );
589 $m->getInsertValues( $this->db, 'rev_user', $this->getTestUser()->getUser() );
590 }
591
592 /**
593 * @dataProvider provideStages
594 * @param int $stage
595 * @expectedException InvalidArgumentException
596 * @expectedExceptionMessage Must use getInsertValues() for rc_user
597 */
598 public function testInsertWithTempTableWrong( $stage ) {
599 $m = $this->makeMigration( $stage );
600 $m->getInsertValuesWithTempTable( $this->db, 'rc_user', $this->getTestUser()->getUser() );
601 }
602
603 /**
604 * @dataProvider provideStages
605 * @param int $stage
606 */
607 public function testInsertWithTempTableDeprecated( $stage ) {
608 $wrap = TestingAccessWrapper::newFromClass( ActorMigration::class );
609 $wrap->formerTempTables += [ 'rc_user' => '1.30' ];
610
611 $this->hideDeprecated( 'ActorMigration::getInsertValuesWithTempTable for rc_user' );
612 $m = $this->makeMigration( $stage );
613 list( $fields, $callback )
614 = $m->getInsertValuesWithTempTable( $this->db, 'rc_user', $this->getTestUser()->getUser() );
615 $this->assertTrue( is_callable( $callback ) );
616 }
617
618 /**
619 * @dataProvider provideStages
620 * @param int $stage
621 * @expectedException InvalidArgumentException
622 * @expectedExceptionMessage $extra[rev_timestamp] is not provided
623 */
624 public function testInsertWithTempTableCallbackMissingFields( $stage ) {
625 $m = $this->makeMigration( $stage );
626 list( $fields, $callback )
627 = $m->getInsertValuesWithTempTable( $this->db, 'rev_user', $this->getTestUser()->getUser() );
628 $callback( 1, [] );
629 }
630
631 public function testInsertUserIdentity() {
632 $user = $this->getTestUser()->getUser();
633 $userIdentity = $this->getMock( UserIdentity::class );
634 $userIdentity->method( 'getId' )->willReturn( $user->getId() );
635 $userIdentity->method( 'getName' )->willReturn( $user->getName() );
636 $userIdentity->method( 'getActorId' )->willReturn( 0 );
637
638 list( $cFields, $cCallback ) = CommentStore::newKey( 'rev_comment' )
639 ->insertWithTempTable( $this->db, '' );
640 $m = $this->makeMigration( MIGRATION_WRITE_BOTH );
641 list( $fields, $callback ) =
642 $m->getInsertValuesWithTempTable( $this->db, 'rev_user', $userIdentity );
643 $extraFields = [
644 'rev_page' => 42,
645 'rev_text_id' => 42,
646 'rev_len' => 0,
647 'rev_timestamp' => $this->db->timestamp(),
648 ] + $cFields;
649 $this->db->insert( 'revision', $extraFields + $fields, __METHOD__ );
650 $id = $this->db->insertId();
651 $callback( $id, $extraFields );
652 $cCallback( $id );
653
654 $qi = Revision::getQueryInfo();
655 $row = $this->db->selectRow(
656 $qi['tables'], $qi['fields'], [ 'rev_id' => $id ], __METHOD__, [], $qi['joins']
657 );
658 $this->assertSame( $user->getId(), (int)$row->rev_user );
659 $this->assertSame( $user->getName(), $row->rev_user_text );
660 $this->assertSame( $user->getActorId(), (int)$row->rev_actor );
661
662 $m = $this->makeMigration( MIGRATION_WRITE_BOTH );
663 $fields = $m->getInsertValues( $this->db, 'dummy_user', $userIdentity );
664 $this->assertSame( $user->getId(), $fields['dummy_user'] );
665 $this->assertSame( $user->getName(), $fields['dummy_user_text'] );
666 $this->assertSame( $user->getActorId(), $fields['dummy_actor'] );
667 }
668
669 public function testConstructor() {
670 $m = ActorMigration::newMigration();
671 $this->assertInstanceOf( ActorMigration::class, $m );
672 $this->assertSame( $m, ActorMigration::newMigration() );
673 }
674
675 /**
676 * @dataProvider provideIsAnon
677 * @param int $stage
678 * @param string $isAnon
679 * @param string $isNotAnon
680 */
681 public function testIsAnon( $stage, $isAnon, $isNotAnon ) {
682 $m = $this->makeMigration( $stage );
683 $this->assertSame( $isAnon, $m->isAnon( 'foo' ) );
684 $this->assertSame( $isNotAnon, $m->isNotAnon( 'foo' ) );
685 }
686
687 public static function provideIsAnon() {
688 return [
689 'MIGRATION_OLD' => [ MIGRATION_OLD, 'foo = 0', 'foo != 0' ],
690 'MIGRATION_WRITE_BOTH' => [ MIGRATION_WRITE_BOTH, 'foo = 0', 'foo != 0' ],
691 'MIGRATION_WRITE_NEW' => [ MIGRATION_WRITE_NEW, 'foo = 0', 'foo != 0' ],
692 'MIGRATION_NEW' => [ MIGRATION_NEW, 'foo IS NULL', 'foo IS NOT NULL' ],
693 ];
694 }
695
696 }