Update to Drupal 8.0.5. For more information, see https://www.drupal.org/node/2679347
This commit is contained in:
parent
2a9f1f148d
commit
fd3b12cf27
251 changed files with 5439 additions and 957 deletions
|
@ -40,6 +40,17 @@ migrate.process.dedupe_entity:
|
|||
type: integer
|
||||
label: 'Length'
|
||||
|
||||
migrate.process.explode:
|
||||
type: migrate_process
|
||||
label: 'Explode process'
|
||||
mapping:
|
||||
delimiter:
|
||||
type: string
|
||||
label: 'Delimiter'
|
||||
limit:
|
||||
type: integer
|
||||
label: 'Limit'
|
||||
|
||||
migrate.process.extract:
|
||||
type: migrate_process
|
||||
label: 'Extract process'
|
||||
|
|
|
@ -74,7 +74,7 @@ class MigrateTemplateStorage implements MigrateTemplateStorageInterface {
|
|||
if (file_exists($full_directory)) {
|
||||
$files = scandir($full_directory);
|
||||
foreach ($files as $file) {
|
||||
if ($file[0] !== '.' && fnmatch('*.yml', $file)) {
|
||||
if ($file[0] !== '.' && preg_match('/\.yml$/', $file)) {
|
||||
$templates[basename($file, '.yml')] = Yaml::decode(file_get_contents("$full_directory/$file"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -112,7 +112,7 @@ abstract class Entity extends DestinationBase implements ContainerFactoryPluginI
|
|||
* The entity we are importing into.
|
||||
*/
|
||||
protected function getEntity(Row $row, array $old_destination_id_values) {
|
||||
$entity_id = $old_destination_id_values ? reset($old_destination_id_values) : $this->getEntityId($row);
|
||||
$entity_id = reset($old_destination_id_values) ?: $this->getEntityId($row);
|
||||
if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) {
|
||||
$this->updateEntity($entity, $row);
|
||||
}
|
||||
|
|
|
@ -33,6 +33,11 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|||
*/
|
||||
class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* Column name of hashed source id values.
|
||||
*/
|
||||
const SOURCE_IDS_HASH = 'source_ids_hash';
|
||||
|
||||
/**
|
||||
* An event dispatcher instance to use for map events.
|
||||
*
|
||||
|
@ -171,6 +176,34 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the hash of the source identifier values.
|
||||
*
|
||||
* It is public only for testing purposes.
|
||||
*
|
||||
* @param array $source_id_values
|
||||
* The source identifiers
|
||||
*
|
||||
* @return string
|
||||
* An hash containing the hashed values of the source identifiers.
|
||||
*/
|
||||
public function getSourceIDsHash(array $source_id_values) {
|
||||
// When looking up the destination ID we require an array with both the
|
||||
// source key and value, e.g. ['nid' => 41]. In this case, $source_id_values
|
||||
// need to be ordered the same order as $this->sourceIdFields().
|
||||
// However, the Migration process plugin doesn't currently have a way to get
|
||||
// the source key so we presume the values have been passed through in the
|
||||
// correct order.
|
||||
if (!isset($source_id_values[0])) {
|
||||
$source_id_values_keyed = [];
|
||||
foreach ($this->sourceIdFields() as $field_name => $source_id) {
|
||||
$source_id_values_keyed[] = $source_id_values[$field_name];
|
||||
}
|
||||
$source_id_values = $source_id_values_keyed;
|
||||
}
|
||||
return hash('sha256', serialize(array_map('strval', $source_id_values)));
|
||||
}
|
||||
|
||||
/**
|
||||
* The source ID fields.
|
||||
*
|
||||
|
@ -285,28 +318,25 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
// and map from the source field names to the map/msg field names.
|
||||
$count = 1;
|
||||
$source_id_schema = array();
|
||||
$pks = array();
|
||||
foreach ($this->migration->getSourcePlugin()->getIds() as $id_definition) {
|
||||
$mapkey = 'sourceid' . $count++;
|
||||
$source_id_schema[$mapkey] = $this->getFieldSchema($id_definition);
|
||||
$source_id_schema[$mapkey]['not null'] = TRUE;
|
||||
|
||||
// With InnoDB, utf8mb4-based primary keys can't be over 191 characters.
|
||||
// Use ASCII-based primary keys instead.
|
||||
if (isset($source_id_schema[$mapkey]['type']) && $source_id_schema[$mapkey]['type'] == 'varchar') {
|
||||
$source_id_schema[$mapkey]['type'] = 'varchar_ascii';
|
||||
}
|
||||
$pks[] = $mapkey;
|
||||
}
|
||||
|
||||
$fields = $source_id_schema;
|
||||
$source_ids_hash[static::SOURCE_IDS_HASH] = array(
|
||||
'type' => 'varchar',
|
||||
'length' => '64',
|
||||
'not null' => TRUE,
|
||||
'description' => 'Hash of source ids. Used as primary key',
|
||||
);
|
||||
$fields = $source_ids_hash + $source_id_schema;
|
||||
|
||||
// Add destination identifiers to map table.
|
||||
// @todo How do we discover the destination schema?
|
||||
$count = 1;
|
||||
foreach ($this->migration->getDestinationPlugin()->getIds() as $id_definition) {
|
||||
// Allow dest identifier fields to be NULL (for IGNORED/FAILED
|
||||
// cases).
|
||||
// Allow dest identifier fields to be NULL (for IGNORED/FAILED cases).
|
||||
$mapkey = 'destid' . $count++;
|
||||
$fields[$mapkey] = $this->getFieldSchema($id_definition);
|
||||
$fields[$mapkey]['not null'] = FALSE;
|
||||
|
@ -343,10 +373,8 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
$schema = array(
|
||||
'description' => 'Mappings from source identifier value(s) to destination identifier value(s).',
|
||||
'fields' => $fields,
|
||||
'primary key' => array(static::SOURCE_IDS_HASH),
|
||||
);
|
||||
if ($pks) {
|
||||
$schema['primary key'] = $pks;
|
||||
}
|
||||
$this->getDatabase()->schema()->createTable($this->mapTableName, $schema);
|
||||
|
||||
// Now do the message table.
|
||||
|
@ -357,7 +385,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
);
|
||||
$fields += $source_id_schema;
|
||||
$fields += $source_ids_hash;
|
||||
|
||||
$fields['level'] = array(
|
||||
'type' => 'int',
|
||||
|
@ -375,9 +403,6 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
'fields' => $fields,
|
||||
'primary key' => array('msgid'),
|
||||
);
|
||||
if ($pks) {
|
||||
$schema['indexes']['sourcekey'] = $pks;
|
||||
}
|
||||
$this->getDatabase()->schema()->createTable($this->messageTableName(), $schema);
|
||||
}
|
||||
}
|
||||
|
@ -406,6 +431,14 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
)
|
||||
);
|
||||
}
|
||||
if (!$this->getDatabase()->schema()->fieldExists($this->mapTableName, static::SOURCE_IDS_HASH)) {
|
||||
$this->getDatabase()->schema()->addField($this->mapTableName, static::SOURCE_IDS_HASH, array(
|
||||
'type' => 'varchar',
|
||||
'length' => '64',
|
||||
'not null' => TRUE,
|
||||
'description' => 'Hash of source ids. Used as primary key',
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -435,9 +468,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
public function getRowBySource(array $source_id_values) {
|
||||
$query = $this->getDatabase()->select($this->mapTableName(), 'map')
|
||||
->fields('map');
|
||||
foreach ($this->sourceIdFields() as $field_name => $source_id) {
|
||||
$query->condition("map.$source_id", $source_id_values[$field_name], '=');
|
||||
}
|
||||
$query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
|
||||
$result = $query->execute();
|
||||
return $result->fetchAssoc();
|
||||
}
|
||||
|
@ -497,14 +528,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
$query = $this->getDatabase()->select($this->mapTableName(), 'map')
|
||||
->fields('map', $this->destinationIdFields());
|
||||
|
||||
// When looking up the destination ID we require an array with both the
|
||||
// source key and value, e.g. ['nid' => 41]. However, the Migration process
|
||||
// plugin doesn't currently have a way to get the source key so we presume
|
||||
// the values have been passed through in the correct order.
|
||||
$have_keys = !isset($source_id_values[0]);
|
||||
foreach ($this->sourceIdFields() as $field_name => $source_id) {
|
||||
$query->condition("map.$source_id", $have_keys ? $source_id_values[$field_name] : array_shift($source_id_values), '=');
|
||||
}
|
||||
$query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
|
||||
$result = $query->execute();
|
||||
$destination_id = $result->fetchAssoc();
|
||||
return array_values($destination_id ?: array());
|
||||
|
@ -517,19 +541,23 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
// Construct the source key.
|
||||
$source_id_values = $row->getSourceIdValues();
|
||||
// Construct the source key and initialize to empty variable keys.
|
||||
$keys = array();
|
||||
$fields = [];
|
||||
foreach ($this->sourceIdFields() as $field_name => $key_name) {
|
||||
// A NULL key value will fail.
|
||||
// A NULL key value is usually an indication of a problem.
|
||||
if (!isset($source_id_values[$field_name])) {
|
||||
$this->message->display(t(
|
||||
'Could not save to map table due to NULL value for key field @field',
|
||||
$this->message->display($this->t(
|
||||
'Did not save to map table due to NULL value for key field @field',
|
||||
array('@field' => $field_name)), 'error');
|
||||
return;
|
||||
}
|
||||
$keys[$key_name] = $source_id_values[$field_name];
|
||||
$fields[$key_name] = $source_id_values[$field_name];
|
||||
}
|
||||
|
||||
$fields = array(
|
||||
if (!$fields) {
|
||||
return;
|
||||
}
|
||||
|
||||
$fields += array(
|
||||
'source_row_status' => (int) $source_row_status,
|
||||
'rollback_action' => (int) $rollback_action,
|
||||
'hash' => $row->getHash(),
|
||||
|
@ -545,14 +573,13 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
if ($this->migration->get('trackLastImported')) {
|
||||
$fields['last_imported'] = time();
|
||||
}
|
||||
if ($keys) {
|
||||
// Notify anyone listening of the map row we're about to save.
|
||||
$this->eventDispatcher->dispatch(MigrateEvents::MAP_SAVE, new MigrateMapSaveEvent($this, $keys + $fields));
|
||||
$this->getDatabase()->merge($this->mapTableName())
|
||||
->key($keys)
|
||||
->fields($fields)
|
||||
->execute();
|
||||
}
|
||||
$keys = [static::SOURCE_IDS_HASH => $this->getSourceIDsHash($source_id_values)];
|
||||
// Notify anyone listening of the map row we're about to save.
|
||||
$this->eventDispatcher->dispatch(MigrateEvents::MAP_SAVE, new MigrateMapSaveEvent($this, $fields));
|
||||
$this->getDatabase()->merge($this->mapTableName())
|
||||
->key($keys)
|
||||
->fields($fields)
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -564,8 +591,8 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
if (!isset($source_id_values[$field_name])) {
|
||||
return;
|
||||
}
|
||||
$fields[$source_id] = $source_id_values[$field_name];
|
||||
}
|
||||
$fields[static::SOURCE_IDS_HASH] = $this->getSourceIDsHash($source_id_values);
|
||||
$fields['level'] = $level;
|
||||
$fields['message'] = $message;
|
||||
$this->getDatabase()->insert($this->messageTableName())
|
||||
|
@ -583,10 +610,8 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
public function getMessageIterator(array $source_id_values = [], $level = NULL) {
|
||||
$query = $this->getDatabase()->select($this->messageTableName(), 'msg')
|
||||
->fields('msg');
|
||||
foreach ($this->sourceIdFields() as $field_name => $source_id) {
|
||||
if (isset($source_id_values[$field_name])) {
|
||||
$query->condition($source_id, $source_id_values[$field_name]);
|
||||
}
|
||||
if ($source_id_values) {
|
||||
$query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
|
||||
}
|
||||
|
||||
if ($level) {
|
||||
|
@ -672,22 +697,16 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
if (empty($source_id_values)) {
|
||||
throw new MigrateException('Without source identifier values it is impossible to find the row to delete.');
|
||||
}
|
||||
if (!$messages_only) {
|
||||
$map_query = $this->getDatabase()->delete($this->mapTableName());
|
||||
}
|
||||
$message_query = $this->getDatabase()->delete($this->messageTableName());
|
||||
foreach ($this->sourceIdFields() as $field_name => $source_id) {
|
||||
if (!$messages_only) {
|
||||
$map_query->condition($source_id, $source_id_values[$field_name]);
|
||||
}
|
||||
$message_query->condition($source_id, $source_id_values[$field_name]);
|
||||
}
|
||||
|
||||
if (!$messages_only) {
|
||||
$map_query = $this->getDatabase()->delete($this->mapTableName());
|
||||
$map_query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
|
||||
// Notify anyone listening of the map row we're about to delete.
|
||||
$this->eventDispatcher->dispatch(MigrateEvents::MAP_DELETE, new MigrateMapDeleteEvent($this, $source_id_values));
|
||||
$map_query->execute();
|
||||
}
|
||||
$message_query = $this->getDatabase()->delete($this->messageTableName());
|
||||
$message_query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
|
||||
$message_query->execute();
|
||||
}
|
||||
|
||||
|
@ -705,11 +724,8 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
// Notify anyone listening of the map row we're about to delete.
|
||||
$this->eventDispatcher->dispatch(MigrateEvents::MAP_DELETE, new MigrateMapDeleteEvent($this, $source_id_values));
|
||||
$map_query->execute();
|
||||
$count = 1;
|
||||
foreach ($this->sourceIdFields() as $field_name => $source_id) {
|
||||
$message_query->condition($source_id, $source_id_values[$field_name]);
|
||||
$count++;
|
||||
}
|
||||
|
||||
$message_query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
|
||||
$message_query->execute();
|
||||
}
|
||||
}
|
||||
|
@ -762,6 +778,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
|
|||
}
|
||||
$this->result = $this->getDatabase()->select($this->mapTableName(), 'map')
|
||||
->fields('map', $fields)
|
||||
->orderBy('destid1')
|
||||
->execute();
|
||||
$this->next();
|
||||
}
|
||||
|
|
49
core/modules/migrate/src/Plugin/migrate/process/Explode.php
Normal file
49
core/modules/migrate/src/Plugin/migrate/process/Explode.php
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\migrate\Plugin\migrate\process\Explode.
|
||||
*/
|
||||
|
||||
namespace Drupal\migrate\Plugin\migrate\process;
|
||||
|
||||
use Drupal\migrate\ProcessPluginBase;
|
||||
use Drupal\migrate\MigrateException;
|
||||
use Drupal\migrate\MigrateExecutableInterface;
|
||||
use Drupal\migrate\Row;
|
||||
|
||||
/**
|
||||
* This plugin explodes a delimited string into an array of values.
|
||||
*
|
||||
* @MigrateProcessPlugin(
|
||||
* id = "explode"
|
||||
* )
|
||||
*/
|
||||
class Explode extends ProcessPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
|
||||
if (is_string($value)) {
|
||||
if (!empty($this->configuration['delimiter'])) {
|
||||
$limit = isset($this->configuration['limit']) ? $this->configuration['limit'] : PHP_INT_MAX;
|
||||
return explode($this->configuration['delimiter'], $value, $limit);
|
||||
}
|
||||
else {
|
||||
throw new MigrateException('delimiter is empty');
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new MigrateException(sprintf('%s is not a string', var_export($value, TRUE)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function multiple() {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
}
|
|
@ -102,6 +102,10 @@ class Migration extends ProcessPluginBase implements ContainerFactoryPluginInter
|
|||
}
|
||||
}
|
||||
|
||||
if (!$destination_ids && !empty($this->configuration['no_stub'])) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!$destination_ids && ($self || isset($this->configuration['stub_id']) || count($migrations) == 1)) {
|
||||
// If the lookup didn't succeed, figure out which migration will do the
|
||||
// stubbing.
|
||||
|
|
|
@ -75,7 +75,7 @@ abstract class SqlBase extends SourcePluginBase implements ContainerFactoryPlugi
|
|||
* The query string.
|
||||
*/
|
||||
public function __toString() {
|
||||
return (string) $this->query;
|
||||
return (string) $this->query();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -29,6 +29,21 @@ class MigrateSqlIdMapEnsureTablesTest extends MigrateTestCase {
|
|||
* Tests the ensureTables method when the tables do not exist.
|
||||
*/
|
||||
public function testEnsureTablesNotExist() {
|
||||
$fields['source_ids_hash'] = Array(
|
||||
'type' => 'varchar',
|
||||
'length' => 64,
|
||||
'not null' => 1,
|
||||
'description' => 'Hash of source ids. Used as primary key'
|
||||
);
|
||||
$fields['sourceid1'] = array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
);
|
||||
$fields['destid1'] = array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => FALSE,
|
||||
);
|
||||
$fields['source_row_status'] = array(
|
||||
'type' => 'int',
|
||||
'size' => 'tiny',
|
||||
|
@ -58,19 +73,10 @@ class MigrateSqlIdMapEnsureTablesTest extends MigrateTestCase {
|
|||
'not null' => FALSE,
|
||||
'description' => 'Hash of source row data, for detecting changes',
|
||||
);
|
||||
$fields['sourceid1'] = array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
);
|
||||
$fields['destid1'] = array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => FALSE,
|
||||
);
|
||||
$map_table_schema = array(
|
||||
'description' => 'Mappings from source identifier value(s) to destination identifier value(s).',
|
||||
'fields' => $fields,
|
||||
'primary key' => array('sourceid1'),
|
||||
'primary key' => array('source_ids_hash'),
|
||||
);
|
||||
$schema = $this->getMockBuilder('Drupal\Core\Database\Schema')
|
||||
->disableOriginalConstructor()
|
||||
|
@ -89,9 +95,11 @@ class MigrateSqlIdMapEnsureTablesTest extends MigrateTestCase {
|
|||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
);
|
||||
$fields['sourceid1'] = array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
$fields['source_ids_hash'] = Array(
|
||||
'type' => 'varchar',
|
||||
'length' => 64,
|
||||
'not null' => 1,
|
||||
'description' => 'Hash of source ids. Used as primary key'
|
||||
);
|
||||
$fields['level'] = array(
|
||||
'type' => 'int',
|
||||
|
@ -109,7 +117,6 @@ class MigrateSqlIdMapEnsureTablesTest extends MigrateTestCase {
|
|||
'fields' => $fields,
|
||||
'primary key' => array('msgid'),
|
||||
);
|
||||
$table_schema['indexes']['sourcekey'] = array('sourceid1');
|
||||
|
||||
$schema->expects($this->at(2))
|
||||
->method('tableExists')
|
||||
|
@ -162,7 +169,20 @@ class MigrateSqlIdMapEnsureTablesTest extends MigrateTestCase {
|
|||
$schema->expects($this->at(4))
|
||||
->method('addField')
|
||||
->with('migrate_map_sql_idmap_test', 'hash', $field_schema);
|
||||
$schema->expects($this->exactly(5))
|
||||
$schema->expects($this->at(5))
|
||||
->method('fieldExists')
|
||||
->with('migrate_map_sql_idmap_test', 'source_ids_hash')
|
||||
->will($this->returnValue(FALSE));
|
||||
$field_schema = array(
|
||||
'type' => 'varchar',
|
||||
'length' => '64',
|
||||
'not null' => TRUE,
|
||||
'description' => 'Hash of source ids. Used as primary key',
|
||||
);
|
||||
$schema->expects($this->at(6))
|
||||
->method('addField')
|
||||
->with('migrate_map_sql_idmap_test', 'source_ids_hash', $field_schema);
|
||||
$schema->expects($this->exactly(7))
|
||||
->method($this->anything());
|
||||
$this->runEnsureTablesTest($schema);
|
||||
}
|
||||
|
|
|
@ -171,6 +171,7 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
$expected_result = [
|
||||
[
|
||||
'sourceid1' => 'source_value',
|
||||
'source_ids_hash' => $this->getIdMap()->getSourceIDsHash($source),
|
||||
'destid1' => 2,
|
||||
] + $this->idMapDefaults(),
|
||||
];
|
||||
|
@ -182,6 +183,7 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
$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);
|
||||
|
@ -239,6 +241,7 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
$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,
|
||||
|
@ -296,20 +299,26 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
*/
|
||||
public function testMessageSave() {
|
||||
$message = 'Hello world.';
|
||||
$expected_results = [
|
||||
$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 ($expected_results as $key => $expected_result) {
|
||||
$id_map->saveMessage(['source_id_property' => $key], $message, $expected_result['level']);
|
||||
foreach ($original_values as $key => $original_value) {
|
||||
$id_map->saveMessage(['source_id_property' => $key], $message, $original_value['level']);
|
||||
}
|
||||
|
||||
foreach ($id_map->getMessageIterator() as $message_row) {
|
||||
$key = $message_row->sourceid1;
|
||||
$key = $message_row->source_ids_hash;
|
||||
$this->assertEquals($expected_results[$key]['message'], $message_row->message);
|
||||
$this->assertEquals($expected_results[$key]['level'], $message_row->level);
|
||||
}
|
||||
|
@ -344,12 +353,14 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
$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);
|
||||
|
@ -413,6 +424,7 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
$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();
|
||||
// Test for a valid hit.
|
||||
|
@ -430,12 +442,14 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
$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);
|
||||
|
@ -487,9 +501,11 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
$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"] = [];
|
||||
}
|
||||
|
@ -501,6 +517,7 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
$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();
|
||||
// Test for a valid hit.
|
||||
|
@ -607,6 +624,7 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
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);
|
||||
|
@ -614,6 +632,7 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
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);
|
||||
|
@ -653,6 +672,7 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
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);
|
||||
|
@ -660,6 +680,7 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
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);
|
||||
|
@ -687,6 +708,7 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
$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,
|
||||
|
@ -815,6 +837,7 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
|
|||
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']];
|
||||
|
|
82
core/modules/migrate/tests/src/Unit/process/ExplodeTest.php
Normal file
82
core/modules/migrate/tests/src/Unit/process/ExplodeTest.php
Normal file
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\migrate\Unit\process\ExplodeTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\migrate\Unit\process;
|
||||
|
||||
use Drupal\migrate\Plugin\migrate\process\Explode;
|
||||
use Drupal\migrate\Plugin\migrate\process\Concat;
|
||||
|
||||
/**
|
||||
* Tests the Explode process plugin.
|
||||
*
|
||||
* @group migrate
|
||||
*/
|
||||
class ExplodeTest extends MigrateProcessTestCase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$configuration = [
|
||||
'delimiter' => ',',
|
||||
];
|
||||
$this->plugin = new Explode($configuration, 'map', []);
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test explode transform process works.
|
||||
*/
|
||||
public function testTransform() {
|
||||
$value = $this->plugin->transform('foo,bar,tik', $this->migrateExecutable, $this->row, 'destinationproperty');
|
||||
$this->assertSame($value, ['foo', 'bar', 'tik']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test explode transform process works with a limit.
|
||||
*/
|
||||
public function testTransformLimit() {
|
||||
$plugin = new Explode(['delimiter' => '_', 'limit' => 2], 'map', []);
|
||||
$value = $plugin->transform('foo_bar_tik', $this->migrateExecutable, $this->row, 'destinationproperty');
|
||||
$this->assertSame($value, ['foo', 'bar_tik']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the explode process can be chained with a handles_multiple process.
|
||||
*/
|
||||
public function testChainedTransform() {
|
||||
$exploded = $this->plugin->transform('foo,bar,tik', $this->migrateExecutable, $this->row, 'destinationproperty');
|
||||
|
||||
$concat = new Concat([], 'map', []);
|
||||
$concatenated = $concat->transform($exploded, $this->migrateExecutable, $this->row, 'destinationproperty');
|
||||
$this->assertSame($concatenated, 'foobartik');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test explode fails properly on non-strings.
|
||||
*
|
||||
* @expectedException \Drupal\migrate\MigrateException
|
||||
*
|
||||
* @expectedExceptionMessage is not a string
|
||||
*/
|
||||
public function testExplodeWithNonString() {
|
||||
$this->plugin->transform(['foo'], $this->migrateExecutable, $this->row, 'destinationproperty');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test explode fails with empty delimiter.
|
||||
*
|
||||
* @expectedException \Drupal\migrate\MigrateException
|
||||
*
|
||||
* @expectedExceptionMessage delimiter is empty
|
||||
*/
|
||||
public function testExplodeWithEmptyDelimiter() {
|
||||
$plugin = new Explode(['delimiter' => ''], 'map', []);
|
||||
$plugin->transform('foo,bar', $this->migrateExecutable, $this->row, 'destinationproperty');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\migrate\Unit\process\MigrationTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\migrate\Unit\process;
|
||||
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\migrate\Entity\MigrationInterface;
|
||||
use Drupal\migrate\Plugin\migrate\process\Migration;
|
||||
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
|
||||
use Drupal\migrate\Plugin\MigrateDestinationInterface;
|
||||
use Drupal\migrate\Plugin\MigrateIdMapInterface;
|
||||
use Drupal\migrate\Plugin\MigratePluginManager;
|
||||
use Drupal\migrate\Plugin\MigrateSourceInterface;
|
||||
use Prophecy\Argument;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\Migration
|
||||
* @group migrate
|
||||
*/
|
||||
class MigrationTest extends MigrateProcessTestCase {
|
||||
|
||||
/**
|
||||
* @covers ::transform
|
||||
*/
|
||||
public function testTransformWithStubSkipping() {
|
||||
$migration_entity = $this->prophesize(MigrationInterface::class);
|
||||
$migration_storage = $this->prophesize(EntityStorageInterface::class);
|
||||
$process_plugin_manager = $this->prophesize(MigratePluginManager::class);
|
||||
|
||||
$destination_id_map = $this->prophesize(MigrateIdMapInterface::class);
|
||||
$destination_migration = $this->prophesize(MigrationInterface::class);
|
||||
$destination_migration->getIdMap()->willReturn($destination_id_map->reveal());
|
||||
$migration_storage->loadMultiple(['destination_migration'])
|
||||
->willReturn(['destination_migration' => $destination_migration->reveal()]);
|
||||
$destination_id_map->lookupDestinationId([1])->willReturn(NULL);
|
||||
|
||||
$configuration = [
|
||||
'no_stub' => TRUE,
|
||||
'migration' => 'destination_migration',
|
||||
];
|
||||
|
||||
$migration_entity->id()->willReturn('actual_migration');
|
||||
$destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();
|
||||
|
||||
$migration = new Migration($configuration, '', [], $migration_entity->reveal(), $migration_storage->reveal(), $process_plugin_manager->reveal());
|
||||
$result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::transform
|
||||
*/
|
||||
public function testTransformWithStubbing() {
|
||||
$migration_entity = $this->prophesize(MigrationInterface::class);
|
||||
$migration_storage = $this->prophesize(EntityStorageInterface::class);
|
||||
$process_plugin_manager = $this->prophesize(MigratePluginManager::class);
|
||||
|
||||
$destination_id_map = $this->prophesize(MigrateIdMapInterface::class);
|
||||
$destination_migration = $this->prophesize(MigrationInterface::class);
|
||||
$destination_migration->getIdMap()->willReturn($destination_id_map->reveal());
|
||||
$migration_storage->loadMultiple(['destination_migration'])
|
||||
->willReturn(['destination_migration' => $destination_migration->reveal()]);
|
||||
$destination_id_map->lookupDestinationId([1])->willReturn(NULL);
|
||||
|
||||
$configuration = [
|
||||
'no_stub' => FALSE,
|
||||
'migration' => 'destination_migration',
|
||||
];
|
||||
|
||||
$migration_entity->id()->willReturn('actual_migration');
|
||||
$destination_migration->id()->willReturn('destination_migration');
|
||||
$destination_migration->getDestinationPlugin(TRUE)->shouldBeCalled();
|
||||
$destination_migration->get('process')->willReturn([]);
|
||||
$destination_migration->get('source')->willReturn([]);
|
||||
|
||||
$source_plugin = $this->prophesize(MigrateSourceInterface::class);
|
||||
$source_plugin->getIds()->willReturn(['nid']);
|
||||
$destination_migration->getSourcePlugin()->willReturn($source_plugin->reveal());
|
||||
$destination_plugin = $this->prophesize(MigrateDestinationInterface::class);
|
||||
$destination_plugin->import(Argument::any())->willReturn([2]);
|
||||
$destination_migration->getDestinationPlugin(TRUE)->willReturn($destination_plugin->reveal());
|
||||
|
||||
$migration = new Migration($configuration, '', [], $migration_entity->reveal(), $migration_storage->reveal(), $process_plugin_manager->reveal());
|
||||
$result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
|
||||
$this->assertEquals(2, $result);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue