public function EntityReferenceRevisionsDestinationTest::testDestinationFieldMapping in Entity Reference Revisions 8
Tests multi-value and single-value destination field linkage.
@dataProvider destinationFieldMappingDataProvider
File
- tests/
src/ Kernel/ Plugin/ migrate/ destination/ EntityReferenceRevisionsDestinationTest.php, line 354
Class
- EntityReferenceRevisionsDestinationTest
- Tests the migration destination plugin.
Namespace
Drupal\Tests\entity_reference_revisions\Kernel\Plugin\migrate\destinationCode
public function testDestinationFieldMapping(array $data) {
$this
->enableModules([
'node',
'field',
]);
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installSchema('node', [
'node_access',
]);
// Create new content type.
$values = [
'type' => 'article',
'name' => 'Article',
];
$node_type = NodeType::create($values);
$node_type
->save();
// Add the field_err_single field to the node type.
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_err_single',
'entity_type' => 'node',
'type' => 'entity_reference_revisions',
'settings' => [
'target_type' => 'entity_test_composite',
],
'cardinality' => 1,
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'article',
]);
$field
->save();
// Add the field_err_multiple field to the node type.
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_err_multiple',
'entity_type' => 'node',
'type' => 'entity_reference_revisions',
'settings' => [
'target_type' => 'entity_test_composite',
],
'cardinality' => -1,
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'article',
]);
$field
->save();
$definitions = [];
$instances = [];
foreach ($data as $datum) {
$definitions[$datum['definition']['id']] = $datum['definition'];
$instances[$datum['definition']['id']] = $this->migrationPluginManager
->createStubMigration($datum['definition']);
}
// Reflection is easier than mocking. We need to use createInstance for
// purposes of registering the migration for the migration process plugin.
$reflector = new \ReflectionObject($this->migrationPluginManager);
$property = $reflector
->getProperty('definitions');
$property
->setAccessible(TRUE);
$property
->setValue($this->migrationPluginManager, $definitions);
$this->container
->set('plugin.manager.migration', $this->migrationPluginManager);
foreach ($data as $datum) {
$migration = $this->migrationPluginManager
->createInstance($datum['definition']['id']);
$migrationExecutable = new MigrateExecutable($migration, $this);
/** @var \Drupal\Core\Entity\EntityStorageBase $storage */
$storage = $this
->readAttribute($migration
->getDestinationPlugin(), 'storage');
$migrationExecutable
->import();
foreach ($datum['expected'] as $expected) {
$entity = $storage
->loadRevision($expected['id']);
$properties = array_diff_key($expected, array_flip([
'id',
]));
foreach ($properties as $property => $value) {
if (is_array($value)) {
foreach ($value as $delta => $text) {
$this
->assertNotEmpty($entity->{$property}[$delta]->entity, "Entity property {$property} with {$delta} is empty");
$this
->assertEquals($text, $entity->{$property}[$delta]->entity
->label());
}
}
else {
$this
->assertNotEmpty($entity, 'Entity with label ' . $expected[$property] . ' is empty');
$this
->assertEquals($expected[$property], $entity
->label());
}
}
}
}
}