View source
<?php
namespace Drupal\Tests\migrate_drupal\Traits;
use Drupal\migrate_drupal\NodeMigrateType;
trait NodeMigrateTypeTestTrait {
public $tableName = NULL;
protected function nodeMigrateMapTableCount($version) {
$results = [];
$bases = [
'node',
'node_complete',
];
$tables = \Drupal::database()
->schema()
->findTables('migrate_map_d' . $version . '_node%');
foreach ($bases as $base) {
$base_tables = preg_grep('/^migrate_map_d' . $version . '_' . $base . '_{2}.*$/', $tables);
$results[$base] = count($base_tables);
}
return $results;
}
protected function removeNodeMigrateMapTable($type, $version) {
$name = $this
->getTableName($type, $version);
\Drupal::database()
->schema()
->dropTable($name);
}
protected function getTableName($type, $version) {
if (!$this->tableName) {
$content_type = strtolower($this
->randomMachineName());
$this->tableName = 'migrate_map_d' . $version . '_node_complete__' . $content_type;
if ($type == NodeMigrateType::NODE_MIGRATE_TYPE_CLASSIC) {
$this->tableName = 'migrate_map_d' . $version . '_node__' . $content_type;
}
}
return $this->tableName;
}
protected function makeNodeMigrateMapTable($type, $version) {
$name = $this
->getTableName($type, $version);
$fields = [
'source_ids_hash' => [
'type' => 'varchar',
'not null' => TRUE,
'length' => '64',
],
'sourceid1' => [
'type' => 'int',
'not null' => TRUE,
'size' => 'normal',
],
'sourceid2' => [
'type' => 'int',
'not null' => TRUE,
'size' => 'normal',
],
'sourceid3' => [
'type' => 'varchar',
'not null' => TRUE,
'length' => '255',
],
'destid1' => [
'type' => 'int',
'not null' => FALSE,
'size' => 'normal',
'unsigned' => TRUE,
],
'destid2' => [
'type' => 'int',
'not null' => FALSE,
'size' => 'normal',
'unsigned' => TRUE,
],
'destid3' => [
'type' => 'varchar_ascii',
'not null' => FALSE,
'length' => '12',
],
'source_row_status' => [
'type' => 'int',
'not null' => TRUE,
'size' => 'tiny',
'default' => '0',
'unsigned' => TRUE,
],
'rollback_action' => [
'type' => 'int',
'not null' => TRUE,
'size' => 'tiny',
'default' => '0',
'unsigned' => TRUE,
],
'last_imported' => [
'type' => 'int',
'not null' => TRUE,
'size' => 'normal',
'default' => '0',
'unsigned' => TRUE,
],
'hash' => [
'type' => 'varchar',
'not null' => FALSE,
'length' => '64',
],
];
$values = [
'source_ids_hash' => '123',
'sourceid1' => '4242',
'sourceid2' => '4242',
'sourceid3' => 'en',
'destid1' => '4242',
'destid2' => '4242',
'destid3' => 'en',
'source_row_status' => '1',
'rollback_action' => '1',
'last_imported' => time(),
'hash' => 'abc',
];
$indexes = [
'source' => [
'sourceid1',
'sourceid2',
'sourceid3',
],
];
if ($type == NodeMigrateType::NODE_MIGRATE_TYPE_CLASSIC) {
$keys = [
'sourceid2',
'sourceid3',
'destid2',
'destid3',
];
foreach ($keys as $key) {
unset($fields[$key]);
unset($values[$key]);
if (strstr($key, 'sourceid')) {
$index_key = substr($key, -1) - 1;
unset($indexes['source'][$index_key]);
}
}
}
$connection = \Drupal::database();
$connection
->schema()
->createTable($name, [
'fields' => $fields,
'primary key' => [
'source_ids_hash',
],
'indexes' => $indexes,
'mysql_character_set' => 'utf8mb4',
]);
$field_names = array_keys($fields);
$connection
->insert($name)
->fields($field_names)
->values($values)
->execute();
}
}