View source
<?php
namespace Drupal\Tests\migrate\Unit;
use Drupal\sqlite\Driver\Database\sqlite\Connection;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Row;
class MigrateSqlIdMapTest extends MigrateTestCase {
protected $migrationConfiguration = [
'id' => 'sql_idmap_test',
];
protected $sourceIds = [
'source_id_property' => [
'type' => 'string',
],
];
protected $destinationIds = [
'destination_id_property' => [
'type' => 'string',
],
];
protected $database;
protected function setUp() : void {
$this->database = $this
->getDatabase([]);
}
protected function saveMap(array $map) {
$table = 'migrate_map_sql_idmap_test';
$schema = $this->database
->schema();
if ($schema
->tableExists($table)) {
foreach (array_keys($map) as $field) {
if (!$schema
->fieldExists($table, $field)) {
$schema
->addField($table, $field, [
'type' => 'text',
]);
}
}
}
else {
$schema
->createTable($table, $this
->createSchemaFromRow($map));
}
$this->database
->insert($table)
->fields($map)
->execute();
}
protected function getIdMap() {
$migration = $this
->getMigration();
$plugin = $this
->createMock('Drupal\\migrate\\Plugin\\MigrateSourceInterface');
$plugin
->method('getIds')
->willReturn($this->sourceIds);
$migration
->method('getSourcePlugin')
->willReturn($plugin);
$plugin = $this
->createMock('Drupal\\migrate\\Plugin\\MigrateDestinationInterface');
$plugin
->method('getIds')
->willReturn($this->destinationIds);
$migration
->method('getDestinationPlugin')
->willReturn($plugin);
$event_dispatcher = $this
->createMock('Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface');
$id_map = new TestSqlIdMap($this->database, [], 'sql', [], $migration, $event_dispatcher);
$migration
->method('getIdMap')
->willReturn($id_map);
return $id_map;
}
protected function idMapDefaults() {
$defaults = [
'source_row_status' => MigrateIdMapInterface::STATUS_IMPORTED,
'rollback_action' => MigrateIdMapInterface::ROLLBACK_DELETE,
'hash' => '',
];
if ($this->database
->driver() == 'sqlite') {
$defaults['source_row_status'] = (string) $defaults['source_row_status'];
$defaults['rollback_action'] = (string) $defaults['rollback_action'];
}
return $defaults;
}
public function testSaveIdMapping() {
$source = [
'source_id_property' => 'source_value',
];
$row = new Row($source, [
'source_id_property' => [],
]);
$id_map = $this
->getIdMap();
$id_map
->saveIdMapping($row, [
'destination_id_property' => 2,
]);
$expected_result = [
[
'sourceid1' => 'source_value',
'source_ids_hash' => $this
->getIdMap()
->getSourceIdsHash($source),
'destid1' => 2,
] + $this
->idMapDefaults(),
];
$this
->queryResultTest($this
->getIdMapContents(), $expected_result);
$source = [
'source_id_property' => 'source_value_1',
];
$row = new Row($source, [
'source_id_property' => [],
]);
$id_map
->saveIdMapping($row, [
'destination_id_property' => 3,
]);
$expected_result[] = [
'sourceid1' => 'source_value_1',
'source_ids_hash' => $this
->getIdMap()
->getSourceIdsHash($source),
'destid1' => 3,
] + $this
->idMapDefaults();
$this
->queryResultTest($this
->getIdMapContents(), $expected_result);
$id_map
->saveIdMapping($row, [
'destination_id_property' => 4,
]);
$expected_result[1]['destid1'] = 4;
$this
->queryResultTest($this
->getIdMapContents(), $expected_result);
}
public function testSetMessage() {
$message = $this
->createMock('Drupal\\migrate\\MigrateMessageInterface');
$id_map = $this
->getIdMap();
$id_map
->setMessage($message);
$this
->assertEquals($message, $id_map->message);
}
public function testClearMessages() {
$message = 'Hello world.';
$expected_results = [
0,
1,
2,
3,
];
$id_map = $this
->getIdMap();
foreach ($expected_results as $key => $expected_result) {
$id_map
->saveMessage([
'source_id_property' => $key,
], $message);
}
$this
->assertSame($id_map
->messageCount(), 4);
$id_map
->clearMessages();
$count = $id_map
->messageCount();
$this
->assertSame($count, 0);
}
public function testGetRowsNeedingUpdate() {
$id_map = $this
->getIdMap();
$row_statuses = [
MigrateIdMapInterface::STATUS_IMPORTED,
MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
MigrateIdMapInterface::STATUS_IGNORED,
MigrateIdMapInterface::STATUS_FAILED,
];
foreach ($row_statuses as $status) {
$source = [
'source_id_property' => 'source_value_' . $status,
];
$row = new Row($source, [
'source_id_property' => [],
]);
$destination = [
'destination_id_property' => 'destination_value_' . $status,
];
$id_map
->saveIdMapping($row, $destination, $status);
$expected_results[] = [
'sourceid1' => 'source_value_' . $status,
'source_ids_hash' => $this
->getIdMap()
->getSourceIdsHash($source),
'destid1' => 'destination_value_' . $status,
'source_row_status' => $status,
'rollback_action' => MigrateIdMapInterface::ROLLBACK_DELETE,
'hash' => '',
];
if ($status == MigrateIdMapInterface::STATUS_IMPORTED) {
$rows_needing_update = $id_map
->getRowsNeedingUpdate(1);
$this
->assertCount(0, $rows_needing_update);
}
}
$this
->queryResultTest($this
->getIdMapContents(), $expected_results);
$row_needing_update = $id_map
->getRowsNeedingUpdate(1);
$this
->assertCount(1, $row_needing_update);
$source_id = $expected_results[MigrateIdMapInterface::STATUS_NEEDS_UPDATE]['sourceid1'];
$test_row = $id_map
->getRowBySource([
'source_id_property' => $source_id,
]);
$this
->assertSame($test_row, (array) $row_needing_update[0]);
$source = [
'source_id_property' => 'source_value_multiple',
];
$row = new Row($source, [
'source_id_property' => [],
]);
$destination = [
'destination_id_property' => 'destination_value_multiple',
];
$id_map
->saveIdMapping($row, $destination, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
$rows_needing_update = $id_map
->getRowsNeedingUpdate(2);
$this
->assertCount(2, $rows_needing_update);
}
public function testMessageCount() {
$message = 'Hello world.';
$expected_results = [
0,
1,
2,
3,
];
$id_map = $this
->getIdMap();
foreach ($expected_results as $key => $expected_result) {
$count = $id_map
->messageCount();
$this
->assertSame($expected_result, $count);
$id_map
->saveMessage([
'source_id_property' => $key,
], $message);
}
}
public function testMessageSave() {
$message = 'Hello world.';
$original_values = [
1 => [
'message' => $message,
'level' => MigrationInterface::MESSAGE_ERROR,
],
2 => [
'message' => $message,
'level' => MigrationInterface::MESSAGE_WARNING,
],
3 => [
'message' => $message,
'level' => MigrationInterface::MESSAGE_NOTICE,
],
4 => [
'message' => $message,
'level' => MigrationInterface::MESSAGE_INFORMATIONAL,
],
];
$expected_results = [
'7ad742edb7e866caa78ced1e4455d2e9cbd8adb2074e7c323d21b4e67732e755' => [
'message' => $message,
'level' => MigrationInterface::MESSAGE_ERROR,
],
'2d3ec2b0c547e819346e6ae03f881fd9f5c978ff3cbe29dfb807d40735e53703' => [
'message' => $message,
'level' => MigrationInterface::MESSAGE_WARNING,
],
'12a042f72cad9a2a8c7715df0c7695d762975f0687d87f5d480725dae1432a6f' => [
'message' => $message,
'level' => MigrationInterface::MESSAGE_NOTICE,
],
'd9d1fd27a2447ace48f47a2e9ff649673f67b446d9381a7963c949fc083f8791' => [
'message' => $message,
'level' => MigrationInterface::MESSAGE_INFORMATIONAL,
],
];
$id_map = $this
->getIdMap();
foreach ($original_values as $key => $original_value) {
$id_map
->saveMessage([
'source_id_property' => $key,
], $message, $original_value['level']);
}
foreach ($id_map
->getMessages() as $message_row) {
$key = $message_row->source_ids_hash;
$this
->assertEquals($expected_results[$key]['message'], $message_row->message);
$this
->assertEquals($expected_results[$key]['level'], $message_row->level);
}
$message_default = 'Hello world default.';
$id_map
->saveMessage([
'source_id_property' => 5,
], $message_default);
$messages = $id_map
->getMessages([
'source_id_property' => 5,
]);
$count = 0;
foreach ($messages as $key => $message_row) {
$count = 1;
$this
->assertEquals($message_default, $message_row->message);
$this
->assertEquals(MigrationInterface::MESSAGE_ERROR, $message_row->level);
}
$this
->assertEquals(1, $count);
$messages = $id_map
->getMessages([], MigrationInterface::MESSAGE_WARNING);
$count = 0;
foreach ($messages as $key => $message_row) {
$count = 1;
$this
->assertEquals(MigrationInterface::MESSAGE_WARNING, $message_row->level);
}
$this
->assertEquals(1, $count);
}
public function testGetRowBySource() {
$this
->getDatabase([]);
$row = [
'sourceid1' => 'source_id_value_1',
'sourceid2' => 'source_id_value_2',
'source_ids_hash' => $this
->getIdMap()
->getSourceIdsHash([
'source_id_property' => 'source_id_value_1',
]),
'destid1' => 'destination_id_value_1',
] + $this
->idMapDefaults();
$this
->saveMap($row);
$row = [
'sourceid1' => 'source_id_value_3',
'sourceid2' => 'source_id_value_4',
'source_ids_hash' => $this
->getIdMap()
->getSourceIdsHash([
'source_id_property' => 'source_id_value_3',
'sourceid2' => 'source_id_value_4',
]),
'destid1' => 'destination_id_value_2',
] + $this
->idMapDefaults();
$this
->saveMap($row);
$source_id_values = [
'source_id_property' => $row['sourceid1'],
'sourceid2' => $row['sourceid2'],
];
$id_map = $this
->getIdMap();
$result_row = $id_map
->getRowBySource($source_id_values);
$this
->assertSame($row, $result_row);
$source_id_values = [
'source_id_property' => 'missing_value_1',
'sourceid2' => 'missing_value_2',
];
$result_row = $id_map
->getRowBySource($source_id_values);
$this
->assertFalse($result_row);
}
public function lookupDestinationIdMappingDataProvider() {
return [
[
1,
1,
],
[
2,
2,
],
[
1,
2,
],
[
2,
1,
],
];
}
public function testLookupDestinationIdMapping($num_source_fields, $num_destination_fields) {
$this->sourceIds = [];
$this->destinationIds = [];
$source_id_values = [];
$nonexistent_id_values = [];
$row = $this
->idMapDefaults();
for ($i = 1; $i <= $num_source_fields; $i++) {
$row["sourceid{$i}"] = "source_id_value_{$i}";
$source_id_values[] = "source_id_value_{$i}";
$nonexistent_id_values[] = "nonexistent_source_id_value_{$i}";
$this->sourceIds["source_id_property_{$i}"] = [];
}
$expected_result = [];
for ($i = 1; $i <= $num_destination_fields; $i++) {
$row["destid{$i}"] = "destination_id_value_{$i}";
$expected_result[] = "destination_id_value_{$i}";
$this->destinationIds["destination_id_property_{$i}"] = [];
}
$row['source_ids_hash'] = $this
->getIdMap()
->getSourceIdsHash($source_id_values);
$this
->saveMap($row);
$id_map = $this
->getIdMap();
$destination_ids = $id_map
->lookupDestinationIds($source_id_values);
$this
->assertSame([
$expected_result,
], $destination_ids);
$destination_ids = $id_map
->lookupDestinationIds($nonexistent_id_values);
$this
->assertCount(0, $destination_ids);
}
protected function setupRows($source_keys, $dest_keys, $rows) {
$this->database = $this
->getDatabase([]);
$this->sourceIds = array_fill_keys($source_keys, []);
$this->destinationIds = array_fill_keys($dest_keys, []);
$db_keys = [];
foreach (array_keys($source_keys) as $i) {
$db_keys[] = 'sourceid' . ($i + 1);
}
foreach (array_keys($dest_keys) as $i) {
$db_keys[] = 'destid' . ($i + 1);
}
foreach ($rows as $row) {
$values = array_combine($db_keys, $row);
$source_values = array_slice($row, 0, count($source_keys));
$values['source_ids_hash'] = $this
->getIdMap()
->getSourceIdsHash($source_values);
$this
->saveMap($values);
}
return $this
->getIdMap();
}
public function testLookupDestinationIds() {
$id_map = $this
->setupRows([
'nid',
], [
'nid',
], [
[
1,
101,
],
[
2,
102,
],
[
3,
103,
],
]);
$this
->assertEquals([], $id_map
->lookupDestinationIds([]));
$this
->assertEquals([
[
101,
],
], $id_map
->lookupDestinationIds([
1,
]));
$this
->assertEquals([
[
102,
],
], $id_map
->lookupDestinationIds([
2,
]));
$this
->assertEquals([], $id_map
->lookupDestinationIds([
99,
]));
$this
->assertEquals([
[
101,
],
], $id_map
->lookupDestinationIds([
'nid' => 1,
]));
$this
->assertEquals([
[
102,
],
], $id_map
->lookupDestinationIds([
'nid' => 2,
]));
$this
->assertEquals([], $id_map
->lookupDestinationIds([
'nid' => 99,
]));
$id_map = $this
->setupRows([
'nid',
'language',
], [
'nid',
'langcode',
], [
[
1,
'en',
101,
'en',
],
[
1,
'fr',
101,
'fr',
],
[
1,
'de',
101,
'de',
],
[
2,
'en',
102,
'en',
],
]);
$this
->assertEquals([], $id_map
->lookupDestinationIds([]));
$this
->assertEquals([
[
101,
'en',
],
], $id_map
->lookupDestinationIds([
1,
'en',
]));
$this
->assertEquals([
[
101,
'fr',
],
], $id_map
->lookupDestinationIds([
1,
'fr',
]));
$this
->assertEquals([
[
102,
'en',
],
], $id_map
->lookupDestinationIds([
2,
'en',
]));
$this
->assertEquals([], $id_map
->lookupDestinationIds([
2,
'fr',
]));
$this
->assertEquals([], $id_map
->lookupDestinationIds([
99,
'en',
]));
$this
->assertEquals([
[
101,
'en',
],
], $id_map
->lookupDestinationIds([
'nid' => 1,
'language' => 'en',
]));
$this
->assertEquals([
[
101,
'fr',
],
], $id_map
->lookupDestinationIds([
'nid' => 1,
'language' => 'fr',
]));
$this
->assertEquals([
[
102,
'en',
],
], $id_map
->lookupDestinationIds([
'nid' => 2,
'language' => 'en',
]));
$this
->assertEquals([], $id_map
->lookupDestinationIds([
'nid' => 2,
'language' => 'fr',
]));
$this
->assertEquals([], $id_map
->lookupDestinationIds([
'nid' => 99,
'language' => 'en',
]));
$this
->assertEquals([
[
101,
'en',
],
[
101,
'fr',
],
[
101,
'de',
],
], $id_map
->lookupDestinationIds([
1,
]));
$this
->assertEquals([
[
102,
'en',
],
], $id_map
->lookupDestinationIds([
2,
]));
$this
->assertEquals([], $id_map
->lookupDestinationIds([
99,
]));
$this
->assertEquals([
[
101,
'en',
],
[
101,
'fr',
],
[
101,
'de',
],
], $id_map
->lookupDestinationIds([
'nid' => 1,
]));
$this
->assertEquals([
[
102,
'en',
],
], $id_map
->lookupDestinationIds([
'nid' => 2,
]));
$this
->assertEquals([], $id_map
->lookupDestinationIds([
'nid' => 99,
]));
$this
->assertEquals([
[
101,
'en',
],
[
101,
'fr',
],
[
101,
'de',
],
], $id_map
->lookupDestinationIds([
'nid' => 1,
'language' => NULL,
]));
$this
->assertEquals([
[
102,
'en',
],
], $id_map
->lookupDestinationIds([
'nid' => 2,
'language' => NULL,
]));
$this
->assertEquals([
[
101,
'en',
],
[
102,
'en',
],
], $id_map
->lookupDestinationIds([
'language' => 'en',
]));
$this
->assertEquals([
[
101,
'fr',
],
], $id_map
->lookupDestinationIds([
'language' => 'fr',
]));
$this
->assertEquals([], $id_map
->lookupDestinationIds([
'language' => 'zh',
]));
try {
$id_map
->lookupDestinationIds([
1,
2,
3,
]);
$this
->fail('Too many source IDs should throw');
} catch (MigrateException $e) {
$this
->assertEquals("Extra unknown items for map migrate_map_sql_idmap_test in source IDs: array (\n 0 => 3,\n)", $e
->getMessage());
}
try {
$id_map
->lookupDestinationIds([
'nid' => 1,
'aaa' => '2',
]);
$this
->fail('Unknown source ID key should throw');
} catch (MigrateException $e) {
$this
->assertEquals("Extra unknown items for map migrate_map_sql_idmap_test in source IDs: array (\n 'aaa' => '2',\n)", $e
->getMessage());
}
$id_map
->getDatabase()
->update($id_map
->mapTableName())
->condition('sourceid1', 1)
->condition('sourceid2', 'en')
->fields([
TestSqlIdMap::SOURCE_IDS_HASH => uniqid(),
])
->execute();
$this
->assertNotEquals([
[
101,
'en',
],
], $id_map
->lookupDestinationIds([
1,
'en',
]));
}
public function testGetRowByDestination() {
$row = [
'sourceid1' => 'source_id_value_1',
'sourceid2' => 'source_id_value_2',
'source_ids_hash' => $this
->getIdMap()
->getSourceIdsHash([
'source_id_property' => 'source_id_value_1',
]),
'destid1' => 'destination_id_value_1',
] + $this
->idMapDefaults();
$this
->saveMap($row);
$row = [
'sourceid1' => 'source_id_value_3',
'sourceid2' => 'source_id_value_4',
'source_ids_hash' => $this
->getIdMap()
->getSourceIdsHash([
'source_id_property' => 'source_id_value_3',
]),
'destid1' => 'destination_id_value_2',
] + $this
->idMapDefaults();
$this
->saveMap($row);
$dest_id_values = [
'destination_id_property' => $row['destid1'],
];
$id_map = $this
->getIdMap();
$result_row = $id_map
->getRowByDestination($dest_id_values);
$this
->assertSame($row, $result_row);
$missing_result_row = $id_map
->getRowByDestination([
'destination_id_property' => 'invalid_destination_id_property',
]);
$this
->assertEquals([], $missing_result_row);
$invalid_result_row = $id_map
->getRowByDestination([
'invalid_destination_key' => 'invalid_destination_id_property',
]);
$this
->assertEquals([], $invalid_result_row);
}
public function lookupSourceIdMappingDataProvider() {
return [
[
1,
1,
],
[
2,
2,
],
[
1,
2,
],
[
2,
1,
],
];
}
public function testLookupSourceIdMapping($num_source_fields, $num_destination_fields) {
$this->sourceIds = [];
$this->destinationIds = [];
$row = $this
->idMapDefaults();
$source_ids_values = [];
$expected_result = [];
for ($i = 1; $i <= $num_source_fields; $i++) {
$row["sourceid{$i}"] = "source_id_value_{$i}";
$source_ids_values = [
$row["sourceid{$i}"],
];
$expected_result["source_id_property_{$i}"] = "source_id_value_{$i}";
$this->sourceIds["source_id_property_{$i}"] = [];
}
$destination_id_values = [];
$nonexistent_id_values = [];
for ($i = 1; $i <= $num_destination_fields; $i++) {
$row["destid{$i}"] = "destination_id_value_{$i}";
$destination_id_values["destination_id_property_{$i}"] = "destination_id_value_{$i}";
$nonexistent_id_values["destination_id_property_{$i}"] = "nonexistent_destination_id_value_{$i}";
$this->destinationIds["destination_id_property_{$i}"] = [];
}
$row['source_ids_hash'] = $this
->getIdMap()
->getSourceIdsHash($source_ids_values);
$this
->saveMap($row);
$id_map = $this
->getIdMap();
$source_id = $id_map
->lookupSourceId($destination_id_values);
$this
->assertSame($expected_result, $source_id);
$source_id = $id_map
->lookupSourceId($nonexistent_id_values);
$this
->assertCount(0, $source_id);
}
public function testCurrentDestinationAndSource() {
$id_map = $this
->setupRows([
'nid',
], [
'nid',
], [
[
1,
101,
],
[
2,
102,
],
[
3,
103,
],
[
4,
NULL,
],
]);
$id_map
->rewind();
$this
->assertEquals([], $id_map
->currentDestination());
$this
->assertEquals([
'nid' => 4,
], $id_map
->currentSource());
$id_map
->next();
$this
->assertEquals([
'nid' => 101,
], $id_map
->currentDestination());
$this
->assertEquals([
'nid' => 1,
], $id_map
->currentSource());
$id_map
->next();
$this
->assertEquals([
'nid' => 102,
], $id_map
->currentDestination());
$this
->assertEquals([
'nid' => 2,
], $id_map
->currentSource());
$id_map
->next();
$this
->assertEquals([
'nid' => 103,
], $id_map
->currentDestination());
$this
->assertEquals([
'nid' => 3,
], $id_map
->currentSource());
$id_map
->next();
}
public function testImportedCount() {
$id_map = $this
->getIdMap();
$source = [
'source_id_property' => 'source_value_failed',
];
$row = new Row($source, [
'source_id_property' => [],
]);
$destination = [
'destination_id_property' => 'destination_value_failed',
];
$id_map
->saveIdMapping($row, $destination, MigrateIdMapInterface::STATUS_FAILED);
$this
->assertSame(0, $id_map
->importedCount());
$source = [
'source_id_property' => 'source_value_imported',
];
$row = new Row($source, [
'source_id_property' => [],
]);
$destination = [
'destination_id_property' => 'destination_value_imported',
];
$id_map
->saveIdMapping($row, $destination, MigrateIdMapInterface::STATUS_IMPORTED);
$this
->assertSame(1, $id_map
->importedCount());
$source = [
'source_id_property' => 'source_value_update',
];
$row = new Row($source, [
'source_id_property' => [],
]);
$destination = [
'destination_id_property' => 'destination_value_update',
];
$id_map
->saveIdMapping($row, $destination, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
$this
->assertSame(2, $id_map
->importedCount());
}
public function testProcessedCount() {
$id_map = $this
->getIdMap();
$this
->assertSame(0, $id_map
->processedCount());
$row_statuses = [
MigrateIdMapInterface::STATUS_IMPORTED,
MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
MigrateIdMapInterface::STATUS_IGNORED,
MigrateIdMapInterface::STATUS_FAILED,
];
foreach ($row_statuses as $status) {
$source = [
'source_id_property' => 'source_value_' . $status,
];
$row = new Row($source, [
'source_id_property' => [],
]);
$destination = [
'destination_id_property' => 'destination_value_' . $status,
];
$id_map
->saveIdMapping($row, $destination, $status);
if ($status == MigrateIdMapInterface::STATUS_IMPORTED) {
$this
->assertSame(1, $id_map
->processedCount());
}
}
$this
->assertSame(count($row_statuses), $id_map
->processedCount());
}
public function updateCountDataProvider() {
return [
[
0,
],
[
1,
],
[
3,
],
];
}
public function testUpdateCount($num_update_rows) {
for ($i = 0; $i < 5; $i++) {
$row = $this
->idMapDefaults();
$row['sourceid1'] = "source_id_value_{$i}";
$row['source_ids_hash'] = $this
->getIdMap()
->getSourceIdsHash([
'source_id_property' => $row['sourceid1'],
]);
$row['destid1'] = "destination_id_value_{$i}";
$row['source_row_status'] = MigrateIdMapInterface::STATUS_IMPORTED;
$this
->saveMap($row);
}
for (; $i < 5 + $num_update_rows; $i++) {
$row = $this
->idMapDefaults();
$row['sourceid1'] = "source_id_value_{$i}";
$row['source_ids_hash'] = $this
->getIdMap()
->getSourceIdsHash([
'source_id_property' => $row['sourceid1'],
]);
$row['destid1'] = "destination_id_value_{$i}";
$row['source_row_status'] = MigrateIdMapInterface::STATUS_NEEDS_UPDATE;
$this
->saveMap($row);
}
$id_map = $this
->getIdMap();
$this
->assertSame($num_update_rows, $id_map
->updateCount());
}
public function errorCountDataProvider() {
return [
[
0,
],
[
1,
],
[
3,
],
];
}
public function testErrorCount($num_error_rows) {
for ($i = 0; $i < 5; $i++) {
$row = $this
->idMapDefaults();
$row['sourceid1'] = "source_id_value_{$i}";
$row['source_ids_hash'] = $this
->getIdMap()
->getSourceIdsHash([
'source_id_property' => $row['sourceid1'],
]);
$row['destid1'] = "destination_id_value_{$i}";
$row['source_row_status'] = MigrateIdMapInterface::STATUS_IMPORTED;
$this
->saveMap($row);
}
for (; $i < 5 + $num_error_rows; $i++) {
$row = $this
->idMapDefaults();
$row['sourceid1'] = "source_id_value_{$i}";
$row['source_ids_hash'] = $this
->getIdMap()
->getSourceIdsHash([
'source_id_property' => $row['sourceid1'],
]);
$row['destid1'] = "destination_id_value_{$i}";
$row['source_row_status'] = MigrateIdMapInterface::STATUS_FAILED;
$this
->saveMap($row);
}
$this
->assertSame($num_error_rows, $this
->getIdMap()
->errorCount());
}
public function testSetUpdate() {
$id_map = $this
->getIdMap();
$row_statuses = [
MigrateIdMapInterface::STATUS_IMPORTED,
MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
MigrateIdMapInterface::STATUS_IGNORED,
MigrateIdMapInterface::STATUS_FAILED,
];
foreach ($row_statuses as $status) {
$source = [
'source_id_property' => 'source_value_' . $status,
];
$row = new Row($source, [
'source_id_property' => [],
]);
$destination = [
'destination_id_property' => 'destination_value_' . $status,
];
$id_map
->saveIdMapping($row, $destination, $status);
$expected_results[] = [
'sourceid1' => 'source_value_' . $status,
'source_ids_hash' => $this
->getIdMap()
->getSourceIdsHash($source),
'destid1' => 'destination_value_' . $status,
'source_row_status' => $status,
'rollback_action' => MigrateIdMapInterface::ROLLBACK_DELETE,
'hash' => '',
];
}
$this
->queryResultTest($this
->getIdMapContents(), $expected_results);
foreach ($row_statuses as $status) {
$id_map
->setUpdate([
'source_id_property' => 'source_value_' . $status,
]);
}
foreach ($expected_results as $key => $value) {
$expected_results[$key]['source_row_status'] = MigrateIdMapInterface::STATUS_NEEDS_UPDATE;
}
$this
->queryResultTest($this
->getIdMapContents(), $expected_results);
try {
$id_map
->setUpdate([]);
$this
->assertFalse(FALSE, 'MigrateException not thrown, when source identifiers were provided to update.');
} catch (MigrateException $e) {
$this
->assertTrue(TRUE, "MigrateException thrown, when source identifiers were not provided to update.");
}
}
public function testPrepareUpdate() {
$id_map = $this
->getIdMap();
$row_statuses = [
MigrateIdMapInterface::STATUS_IMPORTED,
MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
MigrateIdMapInterface::STATUS_IGNORED,
MigrateIdMapInterface::STATUS_FAILED,
];
foreach ($row_statuses as $status) {
$source = [
'source_id_property' => 'source_value_' . $status,
];
$row = new Row($source, [
'source_id_property' => [],
]);
$destination = [
'destination_id_property' => 'destination_value_' . $status,
];
$id_map
->saveIdMapping($row, $destination, $status);
$expected_results[] = [
'sourceid1' => 'source_value_' . $status,
'destid1' => 'destination_value_' . $status,
'source_row_status' => $status,
'rollback_action' => MigrateIdMapInterface::ROLLBACK_DELETE,
'hash' => '',
];
}
$this
->queryResultTest($this
->getIdMapContents(), $expected_results);
$id_map
->prepareUpdate();
foreach ($expected_results as $key => $value) {
$expected_results[$key]['source_row_status'] = MigrateIdMapInterface::STATUS_NEEDS_UPDATE;
}
$this
->queryResultTest($this
->getIdMapContents(), $expected_results);
}
public function testDestroy() {
$id_map = $this
->getIdMap();
$id_map
->getDatabase();
$map_table_name = $id_map
->mapTableName();
$message_table_name = $id_map
->messageTableName();
$row = new Row([
'source_id_property' => 'source_value',
], [
'source_id_property' => [],
]);
$id_map
->saveIdMapping($row, [
'destination_id_property' => 2,
]);
$id_map
->saveMessage([
'source_id_property' => 'source_value',
], 'A message');
$this
->assertTrue($this->database
->schema()
->tableExists($map_table_name), "{$map_table_name} exists");
$this
->assertTrue($this->database
->schema()
->tableExists($message_table_name), "{$message_table_name} exists");
$id_map
->destroy();
$this
->assertFalse($this->database
->schema()
->tableExists($map_table_name), "{$map_table_name} does not exist");
$this
->assertFalse($this->database
->schema()
->tableExists($message_table_name), "{$message_table_name} does not exist");
}
public function testGetQualifiedMapTablePrefix() {
$connection_options = [
'database' => ':memory:',
'prefix' => 'prefix',
];
$pdo = Connection::open($connection_options);
$this->database = new Connection($pdo, $connection_options);
$qualified_map_table = $this
->getIdMap()
->getQualifiedMapTableName();
$this
->assertEquals('prefix.migrate_map_sql_idmap_test', $qualified_map_table);
}
public function testIterators() {
for ($i = 0; $i < 3; $i++) {
$row = $this
->idMapDefaults();
$row['sourceid1'] = "source_id_value_{$i}";
$row['source_ids_hash'] = $this
->getIdMap()
->getSourceIdsHash([
'source_id_property' => $row['sourceid1'],
]);
$row['destid1'] = "destination_id_value_{$i}";
$row['source_row_status'] = MigrateIdMapInterface::STATUS_IMPORTED;
$expected_results[serialize([
'sourceid1' => $row['sourceid1'],
])] = [
'destid1' => $row['destid1'],
];
$this
->saveMap($row);
}
$this
->assertSame(iterator_to_array($this
->getIdMap()), $expected_results);
}
private function getIdMapContents() {
$result = $this->database
->select('migrate_map_sql_idmap_test', 't')
->fields('t')
->execute();
$contents = [];
foreach ($result as $row) {
$contents[] = (array) $row;
}
return $contents;
}
public function testMapTableCreation() {
$id_map = $this
->getIdMap();
$map_table_name = $id_map
->mapTableName();
$message_table_name = $id_map
->messageTableName();
$this
->assertEquals('migrate_map_sql_idmap_test', $map_table_name);
$this
->assertEquals('migrate_message_sql_idmap_test', $message_table_name);
$this
->assertFalse($this->database
->schema()
->tableExists($map_table_name));
$this
->assertFalse($this->database
->schema()
->tableExists($message_table_name));
$id_map
->getDatabase();
$this
->assertTrue($this->database
->schema()
->tableExists($map_table_name));
$this
->assertTrue($this->database
->schema()
->tableExists($message_table_name));
}
public function testGetHighestId(array $destination_ids, array $rows, $expected) {
$this->database = $this
->getDatabase([]);
$this->sourceIds = $destination_ids;
$this->destinationIds = $destination_ids;
$db_keys = [];
$dest_id_count = count($destination_ids);
for ($i = 1; $i <= $dest_id_count; $i++) {
$db_keys[$i] = "sourceid{$i}";
}
for ($i = 1; $i <= $dest_id_count; $i++) {
$db_keys[] = "destid{$i}";
}
$id_map = $this
->getIdMap();
foreach ($rows as $row) {
$values = array_combine($db_keys, $row);
$source_values = array_slice($row, 0, $dest_id_count);
$values['source_ids_hash'] = $id_map
->getSourceIdsHash($source_values);
$this
->saveMap($values);
}
$actual = $id_map
->getHighestId();
$this
->assertSame($expected, $actual);
}
public function getHighestIdDataProvider() {
return [
'Destination ID type integer' => [
'dest_ids' => [
'nid' => [
'type' => 'integer',
],
],
'rows' => [
[
1,
2,
],
[
2,
1,
],
[
4,
3,
],
[
9,
5,
],
],
'expected' => 5,
],
'Destination ID types integer and string' => [
'dest_ids' => [
'nid' => [
'type' => 'integer',
],
'vid' => [
'type' => 'integer',
],
'language' => [
'type' => 'string',
],
],
'rows' => [
[
1,
1,
'en',
1,
6,
'en',
],
[
1,
4,
'fr',
1,
6,
'fr',
],
[
1,
6,
'de',
1,
6,
'de',
],
[
2,
8,
'en',
2,
8,
'en',
],
],
'expected' => 2,
],
];
}
public function testGetHighestIdInvalid(array $destination_ids) {
$this
->expectException(\LogicException::class);
$this
->expectExceptionMessage('To determine the highest migrated ID the first ID must be an integer');
$this->destinationIds = $destination_ids;
$id_map = $this
->getIdMap();
$id_map
->getHighestId();
}
public function getHighestIdInvalidDataProvider() {
return [
'Destination ID type string' => [
'ids' => [
'language' => [
'type' => 'string',
],
],
],
'Destination ID types int (not integer) and string' => [
'ids' => [
'nid' => [
'type' => 'int',
],
'language' => [
'type' => 'string',
],
],
],
];
}
}