comparison core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 1fec387a4317
children 129ea1e6d783
comparison
equal deleted inserted replaced
15:e200cb7efeb3 16:c2387f117808
164 $id_map = $this->getIdMap(); 164 $id_map = $this->getIdMap();
165 $id_map->saveIdMapping($row, ['destination_id_property' => 2]); 165 $id_map->saveIdMapping($row, ['destination_id_property' => 2]);
166 $expected_result = [ 166 $expected_result = [
167 [ 167 [
168 'sourceid1' => 'source_value', 168 'sourceid1' => 'source_value',
169 'source_ids_hash' => $this->getIdMap()->getSourceIDsHash($source), 169 'source_ids_hash' => $this->getIdMap()->getSourceIdsHash($source),
170 'destid1' => 2, 170 'destid1' => 2,
171 ] + $this->idMapDefaults(), 171 ] + $this->idMapDefaults(),
172 ]; 172 ];
173 $this->queryResultTest($this->getIdMapContents(), $expected_result); 173 $this->queryResultTest($this->getIdMapContents(), $expected_result);
174 $source = [ 174 $source = [
176 ]; 176 ];
177 $row = new Row($source, ['source_id_property' => []]); 177 $row = new Row($source, ['source_id_property' => []]);
178 $id_map->saveIdMapping($row, ['destination_id_property' => 3]); 178 $id_map->saveIdMapping($row, ['destination_id_property' => 3]);
179 $expected_result[] = [ 179 $expected_result[] = [
180 'sourceid1' => 'source_value_1', 180 'sourceid1' => 'source_value_1',
181 'source_ids_hash' => $this->getIdMap()->getSourceIDsHash($source), 181 'source_ids_hash' => $this->getIdMap()->getSourceIdsHash($source),
182 'destid1' => 3, 182 'destid1' => 3,
183 ] + $this->idMapDefaults(); 183 ] + $this->idMapDefaults();
184 $this->queryResultTest($this->getIdMapContents(), $expected_result); 184 $this->queryResultTest($this->getIdMapContents(), $expected_result);
185 $id_map->saveIdMapping($row, ['destination_id_property' => 4]); 185 $id_map->saveIdMapping($row, ['destination_id_property' => 4]);
186 $expected_result[1]['destid1'] = 4; 186 $expected_result[1]['destid1'] = 4;
234 $row = new Row($source, ['source_id_property' => []]); 234 $row = new Row($source, ['source_id_property' => []]);
235 $destination = ['destination_id_property' => 'destination_value_' . $status]; 235 $destination = ['destination_id_property' => 'destination_value_' . $status];
236 $id_map->saveIdMapping($row, $destination, $status); 236 $id_map->saveIdMapping($row, $destination, $status);
237 $expected_results[] = [ 237 $expected_results[] = [
238 'sourceid1' => 'source_value_' . $status, 238 'sourceid1' => 'source_value_' . $status,
239 'source_ids_hash' => $this->getIdMap()->getSourceIDsHash($source), 239 'source_ids_hash' => $this->getIdMap()->getSourceIdsHash($source),
240 'destid1' => 'destination_value_' . $status, 240 'destid1' => 'destination_value_' . $status,
241 'source_row_status' => $status, 241 'source_row_status' => $status,
242 'rollback_action' => MigrateIdMapInterface::ROLLBACK_DELETE, 242 'rollback_action' => MigrateIdMapInterface::ROLLBACK_DELETE,
243 'hash' => '', 243 'hash' => '',
244 ]; 244 ];
346 public function testGetRowBySource() { 346 public function testGetRowBySource() {
347 $this->getDatabase([]); 347 $this->getDatabase([]);
348 $row = [ 348 $row = [
349 'sourceid1' => 'source_id_value_1', 349 'sourceid1' => 'source_id_value_1',
350 'sourceid2' => 'source_id_value_2', 350 'sourceid2' => 'source_id_value_2',
351 'source_ids_hash' => $this->getIdMap()->getSourceIDsHash(['source_id_property' => 'source_id_value_1']), 351 'source_ids_hash' => $this->getIdMap()->getSourceIdsHash(['source_id_property' => 'source_id_value_1']),
352 'destid1' => 'destination_id_value_1', 352 'destid1' => 'destination_id_value_1',
353 ] + $this->idMapDefaults(); 353 ] + $this->idMapDefaults();
354 $this->saveMap($row); 354 $this->saveMap($row);
355 $row = [ 355 $row = [
356 'sourceid1' => 'source_id_value_3', 356 'sourceid1' => 'source_id_value_3',
357 'sourceid2' => 'source_id_value_4', 357 'sourceid2' => 'source_id_value_4',
358 'source_ids_hash' => $this->getIdMap()->getSourceIDsHash(['source_id_property' => 'source_id_value_3', 'sourceid2' => 'source_id_value_4']), 358 'source_ids_hash' => $this->getIdMap()->getSourceIdsHash(['source_id_property' => 'source_id_value_3', 'sourceid2' => 'source_id_value_4']),
359 'destid1' => 'destination_id_value_2', 359 'destid1' => 'destination_id_value_2',
360 ] + $this->idMapDefaults(); 360 ] + $this->idMapDefaults();
361 $this->saveMap($row); 361 $this->saveMap($row);
362 $source_id_values = ['source_id_property' => $row['sourceid1'], 'sourceid2' => $row['sourceid2']]; 362 $source_id_values = ['source_id_property' => $row['sourceid1'], 'sourceid2' => $row['sourceid2']];
363 $id_map = $this->getIdMap(); 363 $id_map = $this->getIdMap();
417 for ($i = 1; $i <= $num_destination_fields; $i++) { 417 for ($i = 1; $i <= $num_destination_fields; $i++) {
418 $row["destid$i"] = "destination_id_value_$i"; 418 $row["destid$i"] = "destination_id_value_$i";
419 $expected_result[] = "destination_id_value_$i"; 419 $expected_result[] = "destination_id_value_$i";
420 $this->destinationIds["destination_id_property_$i"] = []; 420 $this->destinationIds["destination_id_property_$i"] = [];
421 } 421 }
422 $row['source_ids_hash'] = $this->getIdMap()->getSourceIDsHash($source_id_values); 422 $row['source_ids_hash'] = $this->getIdMap()->getSourceIdsHash($source_id_values);
423 $this->saveMap($row); 423 $this->saveMap($row);
424 $id_map = $this->getIdMap(); 424 $id_map = $this->getIdMap();
425 // Test for a valid hit. 425 // Test for a valid hit.
426 $destination_id = $id_map->lookupDestinationId($source_id_values); 426 $destination_id = $id_map->lookupDestinationId($source_id_values);
427 $this->assertSame($expected_result, $destination_id); 427 $this->assertSame($expected_result, $destination_id);
456 $db_keys[] = 'destid' . ($i + 1); 456 $db_keys[] = 'destid' . ($i + 1);
457 } 457 }
458 foreach ($rows as $row) { 458 foreach ($rows as $row) {
459 $values = array_combine($db_keys, $row); 459 $values = array_combine($db_keys, $row);
460 $source_values = array_slice($row, 0, count($source_keys)); 460 $source_values = array_slice($row, 0, count($source_keys));
461 $values['source_ids_hash'] = $this->getIdMap()->getSourceIDsHash($source_values); 461 $values['source_ids_hash'] = $this->getIdMap()->getSourceIdsHash($source_values);
462 $this->saveMap($values); 462 $this->saveMap($values);
463 } 463 }
464 464
465 return $this->getIdMap(); 465 return $this->getIdMap();
466 } 466 }
515 $this->assertEquals([], $id_map->lookupDestinationIds([99])); 515 $this->assertEquals([], $id_map->lookupDestinationIds([99]));
516 // Lookup by partial associative list. 516 // Lookup by partial associative list.
517 $this->assertEquals([[101, 'en'], [101, 'fr'], [101, 'de']], $id_map->lookupDestinationIds(['nid' => 1])); 517 $this->assertEquals([[101, 'en'], [101, 'fr'], [101, 'de']], $id_map->lookupDestinationIds(['nid' => 1]));
518 $this->assertEquals([[102, 'en']], $id_map->lookupDestinationIds(['nid' => 2])); 518 $this->assertEquals([[102, 'en']], $id_map->lookupDestinationIds(['nid' => 2]));
519 $this->assertEquals([], $id_map->lookupDestinationIds(['nid' => 99])); 519 $this->assertEquals([], $id_map->lookupDestinationIds(['nid' => 99]));
520 $this->assertEquals([[101, 'en'], [101, 'fr'], [101, 'de']], $id_map->lookupDestinationIds(['nid' => 1, 'language' => NULL]));
521 $this->assertEquals([[102, 'en']], $id_map->lookupDestinationIds(['nid' => 2, 'language' => NULL]));
520 // Out-of-order partial associative list. 522 // Out-of-order partial associative list.
521 $this->assertEquals([[101, 'en'], [102, 'en']], $id_map->lookupDestinationIds(['language' => 'en'])); 523 $this->assertEquals([[101, 'en'], [102, 'en']], $id_map->lookupDestinationIds(['language' => 'en']));
522 $this->assertEquals([[101, 'fr']], $id_map->lookupDestinationIds(['language' => 'fr'])); 524 $this->assertEquals([[101, 'fr']], $id_map->lookupDestinationIds(['language' => 'fr']));
523 $this->assertEquals([], $id_map->lookupDestinationIds(['language' => 'zh'])); 525 $this->assertEquals([], $id_map->lookupDestinationIds(['language' => 'zh']));
524 // Error conditions. 526 // Error conditions.
525 try { 527 try {
526 $id_map->lookupDestinationIds([1, 2, 3]); 528 $id_map->lookupDestinationIds([1, 2, 3]);
527 $this->fail('Too many source IDs should throw'); 529 $this->fail('Too many source IDs should throw');
528 } 530 }
529 catch (MigrateException $e) { 531 catch (MigrateException $e) {
530 $this->assertEquals("Extra unknown items in source IDs", $e->getMessage()); 532 $this->assertEquals("Extra unknown items in source IDs: array (\n 0 => 3,\n)", $e->getMessage());
531 } 533 }
532 try { 534 try {
533 $id_map->lookupDestinationIds(['nid' => 1, 'aaa' => '2']); 535 $id_map->lookupDestinationIds(['nid' => 1, 'aaa' => '2']);
534 $this->fail('Unknown source ID key should throw'); 536 $this->fail('Unknown source ID key should throw');
535 } 537 }
536 catch (MigrateException $e) { 538 catch (MigrateException $e) {
537 $this->assertEquals("Extra unknown items in source IDs", $e->getMessage()); 539 $this->assertEquals("Extra unknown items in source IDs: array (\n 'aaa' => '2',\n)", $e->getMessage());
538 } 540 }
539 541
540 // Verify that we are looking up by source_id_hash when all source IDs are 542 // Verify that we are looking up by source_id_hash when all source IDs are
541 // passed in. 543 // passed in.
542 $id_map->getDatabase()->update($id_map->mapTableName()) 544 $id_map->getDatabase()->update($id_map->mapTableName())
552 */ 554 */
553 public function testGetRowByDestination() { 555 public function testGetRowByDestination() {
554 $row = [ 556 $row = [
555 'sourceid1' => 'source_id_value_1', 557 'sourceid1' => 'source_id_value_1',
556 'sourceid2' => 'source_id_value_2', 558 'sourceid2' => 'source_id_value_2',
557 'source_ids_hash' => $this->getIdMap()->getSourceIDsHash(['source_id_property' => 'source_id_value_1']), 559 'source_ids_hash' => $this->getIdMap()->getSourceIdsHash(['source_id_property' => 'source_id_value_1']),
558 'destid1' => 'destination_id_value_1', 560 'destid1' => 'destination_id_value_1',
559 ] + $this->idMapDefaults(); 561 ] + $this->idMapDefaults();
560 $this->saveMap($row); 562 $this->saveMap($row);
561 $row = [ 563 $row = [
562 'sourceid1' => 'source_id_value_3', 564 'sourceid1' => 'source_id_value_3',
563 'sourceid2' => 'source_id_value_4', 565 'sourceid2' => 'source_id_value_4',
564 'source_ids_hash' => $this->getIdMap()->getSourceIDsHash(['source_id_property' => 'source_id_value_3']), 566 'source_ids_hash' => $this->getIdMap()->getSourceIdsHash(['source_id_property' => 'source_id_value_3']),
565 'destid1' => 'destination_id_value_2', 567 'destid1' => 'destination_id_value_2',
566 ] + $this->idMapDefaults(); 568 ] + $this->idMapDefaults();
567 $this->saveMap($row); 569 $this->saveMap($row);
568 $dest_id_values = ['destination_id_property' => $row['destid1']]; 570 $dest_id_values = ['destination_id_property' => $row['destid1']];
569 $id_map = $this->getIdMap(); 571 $id_map = $this->getIdMap();
575 $result_row = $id_map->getRowByDestination($dest_id_values); 577 $result_row = $id_map->getRowByDestination($dest_id_values);
576 $this->assertFalse($result_row); 578 $this->assertFalse($result_row);
577 } 579 }
578 580
579 /** 581 /**
580 * Data provider for testLookupSourceIDMapping(). 582 * Data provider for testLookupSourceIdMapping().
581 * 583 *
582 * Scenarios to test (for both hits and misses) are: 584 * Scenarios to test (for both hits and misses) are:
583 * - Single-value destination ID to single-value source ID. 585 * - Single-value destination ID to single-value source ID.
584 * - Multi-value destination ID to multi-value source ID. 586 * - Multi-value destination ID to multi-value source ID.
585 * - Single-value destination ID to multi-value source ID. 587 * - Single-value destination ID to multi-value source ID.
586 * - Multi-value destination ID to single-value source ID. 588 * - Multi-value destination ID to single-value source ID.
587 * 589 *
588 * @return array 590 * @return array
589 * An array of data values. 591 * An array of data values.
590 */ 592 */
591 public function lookupSourceIDMappingDataProvider() { 593 public function lookupSourceIdMappingDataProvider() {
592 return [ 594 return [
593 [1, 1], 595 [1, 1],
594 [2, 2], 596 [2, 2],
595 [1, 2], 597 [1, 2],
596 [2, 1], 598 [2, 1],
603 * @param int $num_source_fields 605 * @param int $num_source_fields
604 * Number of source fields to test. 606 * Number of source fields to test.
605 * @param int $num_destination_fields 607 * @param int $num_destination_fields
606 * Number of destination fields to test. 608 * Number of destination fields to test.
607 * 609 *
608 * @dataProvider lookupSourceIDMappingDataProvider 610 * @dataProvider lookupSourceIdMappingDataProvider
609 */ 611 */
610 public function testLookupSourceIDMapping($num_source_fields, $num_destination_fields) { 612 public function testLookupSourceIdMapping($num_source_fields, $num_destination_fields) {
611 // Adjust the migration configuration according to the number of source and 613 // Adjust the migration configuration according to the number of source and
612 // destination fields. 614 // destination fields.
613 $this->sourceIds = []; 615 $this->sourceIds = [];
614 $this->destinationIds = []; 616 $this->destinationIds = [];
615 $row = $this->idMapDefaults(); 617 $row = $this->idMapDefaults();
627 $row["destid$i"] = "destination_id_value_$i"; 629 $row["destid$i"] = "destination_id_value_$i";
628 $destination_id_values["destination_id_property_$i"] = "destination_id_value_$i"; 630 $destination_id_values["destination_id_property_$i"] = "destination_id_value_$i";
629 $nonexistent_id_values["destination_id_property_$i"] = "nonexistent_destination_id_value_$i"; 631 $nonexistent_id_values["destination_id_property_$i"] = "nonexistent_destination_id_value_$i";
630 $this->destinationIds["destination_id_property_$i"] = []; 632 $this->destinationIds["destination_id_property_$i"] = [];
631 } 633 }
632 $row['source_ids_hash'] = $this->getIdMap()->getSourceIDsHash($source_ids_values); 634 $row['source_ids_hash'] = $this->getIdMap()->getSourceIdsHash($source_ids_values);
633 $this->saveMap($row); 635 $this->saveMap($row);
634 $id_map = $this->getIdMap(); 636 $id_map = $this->getIdMap();
635 // Test for a valid hit. 637 // Test for a valid hit.
636 $source_id = $id_map->lookupSourceID($destination_id_values); 638 $source_id = $id_map->lookupSourceId($destination_id_values);
637 $this->assertSame($expected_result, $source_id); 639 $this->assertSame($expected_result, $source_id);
638 // Test for a miss. 640 // Test for a miss.
639 $source_id = $id_map->lookupSourceID($nonexistent_id_values); 641 $source_id = $id_map->lookupSourceId($nonexistent_id_values);
640 $this->assertSame(0, count($source_id)); 642 $this->assertSame(0, count($source_id));
641 } 643 }
642 644
643 /** 645 /**
644 * Tests currentDestination() and currentSource(). 646 * Tests currentDestination() and currentSource().
763 */ 765 */
764 public function testUpdateCount($num_update_rows) { 766 public function testUpdateCount($num_update_rows) {
765 for ($i = 0; $i < 5; $i++) { 767 for ($i = 0; $i < 5; $i++) {
766 $row = $this->idMapDefaults(); 768 $row = $this->idMapDefaults();
767 $row['sourceid1'] = "source_id_value_$i"; 769 $row['sourceid1'] = "source_id_value_$i";
768 $row['source_ids_hash'] = $this->getIdMap()->getSourceIDsHash(['source_id_property' => $row['sourceid1']]); 770 $row['source_ids_hash'] = $this->getIdMap()->getSourceIdsHash(['source_id_property' => $row['sourceid1']]);
769 $row['destid1'] = "destination_id_value_$i"; 771 $row['destid1'] = "destination_id_value_$i";
770 $row['source_row_status'] = MigrateIdMapInterface::STATUS_IMPORTED; 772 $row['source_row_status'] = MigrateIdMapInterface::STATUS_IMPORTED;
771 $this->saveMap($row); 773 $this->saveMap($row);
772 } 774 }
773 for (; $i < 5 + $num_update_rows; $i++) { 775 for (; $i < 5 + $num_update_rows; $i++) {
774 $row = $this->idMapDefaults(); 776 $row = $this->idMapDefaults();
775 $row['sourceid1'] = "source_id_value_$i"; 777 $row['sourceid1'] = "source_id_value_$i";
776 $row['source_ids_hash'] = $this->getIdMap()->getSourceIDsHash(['source_id_property' => $row['sourceid1']]); 778 $row['source_ids_hash'] = $this->getIdMap()->getSourceIdsHash(['source_id_property' => $row['sourceid1']]);
777 $row['destid1'] = "destination_id_value_$i"; 779 $row['destid1'] = "destination_id_value_$i";
778 $row['source_row_status'] = MigrateIdMapInterface::STATUS_NEEDS_UPDATE; 780 $row['source_row_status'] = MigrateIdMapInterface::STATUS_NEEDS_UPDATE;
779 $this->saveMap($row); 781 $this->saveMap($row);
780 } 782 }
781 $id_map = $this->getIdMap(); 783 $id_map = $this->getIdMap();
811 */ 813 */
812 public function testErrorCount($num_error_rows) { 814 public function testErrorCount($num_error_rows) {
813 for ($i = 0; $i < 5; $i++) { 815 for ($i = 0; $i < 5; $i++) {
814 $row = $this->idMapDefaults(); 816 $row = $this->idMapDefaults();
815 $row['sourceid1'] = "source_id_value_$i"; 817 $row['sourceid1'] = "source_id_value_$i";
816 $row['source_ids_hash'] = $this->getIdMap()->getSourceIDsHash(['source_id_property' => $row['sourceid1']]); 818 $row['source_ids_hash'] = $this->getIdMap()->getSourceIdsHash(['source_id_property' => $row['sourceid1']]);
817 $row['destid1'] = "destination_id_value_$i"; 819 $row['destid1'] = "destination_id_value_$i";
818 $row['source_row_status'] = MigrateIdMapInterface::STATUS_IMPORTED; 820 $row['source_row_status'] = MigrateIdMapInterface::STATUS_IMPORTED;
819 $this->saveMap($row); 821 $this->saveMap($row);
820 } 822 }
821 for (; $i < 5 + $num_error_rows; $i++) { 823 for (; $i < 5 + $num_error_rows; $i++) {
822 $row = $this->idMapDefaults(); 824 $row = $this->idMapDefaults();
823 $row['sourceid1'] = "source_id_value_$i"; 825 $row['sourceid1'] = "source_id_value_$i";
824 $row['source_ids_hash'] = $this->getIdMap()->getSourceIDsHash(['source_id_property' => $row['sourceid1']]); 826 $row['source_ids_hash'] = $this->getIdMap()->getSourceIdsHash(['source_id_property' => $row['sourceid1']]);
825 $row['destid1'] = "destination_id_value_$i"; 827 $row['destid1'] = "destination_id_value_$i";
826 $row['source_row_status'] = MigrateIdMapInterface::STATUS_FAILED; 828 $row['source_row_status'] = MigrateIdMapInterface::STATUS_FAILED;
827 $this->saveMap($row); 829 $this->saveMap($row);
828 } 830 }
829 831
847 $row = new Row($source, ['source_id_property' => []]); 849 $row = new Row($source, ['source_id_property' => []]);
848 $destination = ['destination_id_property' => 'destination_value_' . $status]; 850 $destination = ['destination_id_property' => 'destination_value_' . $status];
849 $id_map->saveIdMapping($row, $destination, $status); 851 $id_map->saveIdMapping($row, $destination, $status);
850 $expected_results[] = [ 852 $expected_results[] = [
851 'sourceid1' => 'source_value_' . $status, 853 'sourceid1' => 'source_value_' . $status,
852 'source_ids_hash' => $this->getIdMap()->getSourceIDsHash($source), 854 'source_ids_hash' => $this->getIdMap()->getSourceIdsHash($source),
853 'destid1' => 'destination_value_' . $status, 855 'destid1' => 'destination_value_' . $status,
854 'source_row_status' => $status, 856 'source_row_status' => $status,
855 'rollback_action' => MigrateIdMapInterface::ROLLBACK_DELETE, 857 'rollback_action' => MigrateIdMapInterface::ROLLBACK_DELETE,
856 'hash' => '', 858 'hash' => '',
857 ]; 859 ];
976 */ 978 */
977 public function testIterators() { 979 public function testIterators() {
978 for ($i = 0; $i < 3; $i++) { 980 for ($i = 0; $i < 3; $i++) {
979 $row = $this->idMapDefaults(); 981 $row = $this->idMapDefaults();
980 $row['sourceid1'] = "source_id_value_$i"; 982 $row['sourceid1'] = "source_id_value_$i";
981 $row['source_ids_hash'] = $this->getIdMap()->getSourceIDsHash(['source_id_property' => $row['sourceid1']]); 983 $row['source_ids_hash'] = $this->getIdMap()->getSourceIdsHash(['source_id_property' => $row['sourceid1']]);
982 $row['destid1'] = "destination_id_value_$i"; 984 $row['destid1'] = "destination_id_value_$i";
983 $row['source_row_status'] = MigrateIdMapInterface::STATUS_IMPORTED; 985 $row['source_row_status'] = MigrateIdMapInterface::STATUS_IMPORTED;
984 $expected_results[serialize(['sourceid1' => $row['sourceid1']])] = ['destid1' => $row['destid1']]; 986 $expected_results[serialize(['sourceid1' => $row['sourceid1']])] = ['destid1' => $row['destid1']];
985 $this->saveMap($row); 987 $this->saveMap($row);
986 } 988 }