3 namespace Drupal\Tests\entity_reference_revisions\Kernel\Plugin\migrate\destination;
5 use Drupal\Core\Entity\EntityStorageBase;
6 use Drupal\entity_reference_revisions\Plugin\migrate\destination\EntityReferenceRevisions;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\KernelTests\KernelTestBase;
10 use Drupal\migrate\MigrateExecutable;
11 use Drupal\migrate\MigrateMessageInterface;
12 use Drupal\migrate\Plugin\Migration;
13 use Drupal\node\Entity\NodeType;
16 * Tests the migration destination plugin.
18 * @coversDefaultClass \Drupal\entity_reference_revisions\Plugin\migrate\destination\EntityReferenceRevisions
19 * @group entity_reference_revisions
21 class EntityReferenceRevisionsDestinationTest extends KernelTestBase implements MigrateMessageInterface {
24 * @var \Drupal\migrate\Plugin\MigrationPluginManager $migrationManager
26 * The migration plugin manager.
28 protected $migrationPluginManager;
33 public static $modules = [
35 'entity_reference_revisions',
36 'entity_composite_relationship_test',
45 protected function setUp() {
47 $this->installEntitySchema('entity_test_composite');
48 $this->installSchema('system', ['sequences']);
49 $this->installConfig($this->modules);
51 $this->migrationPluginManager = \Drupal::service('plugin.manager.migration');
55 * Tests get entity type id.
57 * @dataProvider getEntityTypeIdDataProvider
59 * @covers ::getEntityTypeId
61 public function testGetEntityTypeId(array $definition, $expected) {
62 /** @var Migration $migration */
63 $migration = $this->migrationPluginManager->createStubMigration($definition);
64 /** @var EntityReferenceRevisions $destination */
65 $destination = $migration->getDestinationPlugin();
67 /** @var EntityStorageBase $storage */
68 $storage = $this->readAttribute($destination, 'storage');
69 $actual = $this->readAttribute($storage, 'entityTypeId');
71 $this->assertEquals($expected, $actual);
75 * Provides multiple migration definitions for "getEntityTypeId" test.
77 public function getEntityTypeIdDataProvider() {
78 $datas = $this->getEntityDataProvider();
80 foreach ($datas as &$data) {
81 $data['expected'] = 'entity_test_composite';
90 * @dataProvider getEntityDataProvider
94 * @covers ::rollbackNonTranslation
96 public function testGetEntity(array $definition, array $expected) {
97 /** @var Migration $migration */
98 $migration = $this->migrationPluginManager->createStubMigration($definition);
99 $migrationExecutable = (new MigrateExecutable($migration, $this));
100 /** @var EntityStorageBase $storage */
101 $storage = $this->readAttribute($migration->getDestinationPlugin(), 'storage');
102 // Test inserting and updating by looping twice.
103 for ($i = 0; $i < 2; $i++) {
104 $migrationExecutable->import();
105 $migration->getIdMap()->prepareUpdate();
106 foreach ($expected as $data) {
107 $entity = $storage->loadRevision($data['id']);
108 $this->assertEquals($data['label'], $entity->label());
111 $migrationExecutable->rollback();
112 foreach ($expected as $data) {
113 $entity = $storage->loadRevision($data['id']);
114 $this->assertEmpty($entity);
119 * Provides multiple migration definitions for "getEntity" test.
121 public function getEntityDataProvider() {
126 'plugin' => 'embedded_data',
128 ['id' => 1, 'name' => 'content item 1a'],
129 ['id' => 1, 'name' => 'content item 1b'],
130 ['id' => 2, 'name' => 'content item 2'],
133 'id' => ['type' => 'integer'],
134 'name' => ['type' => 'text'],
141 'plugin' => 'entity_reference_revisions:entity_test_composite',
145 ['id' => 1, 'label' => 'content item 1a'],
146 ['id' => 2, 'label' => 'content item 1b'],
147 ['id' => 3, 'label' => 'content item 2'],
153 'plugin' => 'embedded_data',
155 ['id' => 1, 'revision_id' => 1, 'name' => 'content item 1'],
156 ['id' => 2, 'revision_id' => 2, 'name' => 'content item 2'],
157 ['id' => 3, 'revision_id' => 3, 'name' => 'content item 3'],
160 'id' => ['type' => 'integer'],
161 'name' => ['type' => 'text'],
166 'revision_id' => 'revision_id',
170 'plugin' => 'entity_reference_revisions:entity_test_composite',
174 ['id' => 1, 'label' => 'content item 1'],
175 ['id' => 2, 'label' => 'content item 2'],
176 ['id' => 3, 'label' => 'content item 3'],
183 * Tests multi-value and single-value destination field linkage.
185 * @dataProvider destinationFieldMappingDataProvider
188 public function testDestinationFieldMapping(array $datas) {
189 $this->enableModules(['node', 'field']);
190 $this->installEntitySchema('node');
191 $this->installEntitySchema('user');
192 $this->installSchema('node', ['node_access']);
194 // Create new content type.
195 $values = ['type' => 'article', 'name' => 'Article'];
196 $node_type = NodeType::create($values);
199 // Add the field_err_single field to the node type.
200 $field_storage = FieldStorageConfig::create([
201 'field_name' => 'field_err_single',
202 'entity_type' => 'node',
203 'type' => 'entity_reference_revisions',
205 'target_type' => 'entity_test_composite'
209 $field_storage->save();
210 $field = FieldConfig::create([
211 'field_storage' => $field_storage,
212 'bundle' => 'article',
216 // Add the field_err_multiple field to the node type.
217 $field_storage = FieldStorageConfig::create([
218 'field_name' => 'field_err_multiple',
219 'entity_type' => 'node',
220 'type' => 'entity_reference_revisions',
222 'target_type' => 'entity_test_composite'
226 $field_storage->save();
227 $field = FieldConfig::create([
228 'field_storage' => $field_storage,
229 'bundle' => 'article',
235 foreach ($datas as $data) {
236 $definitions[$data['definition']['id']] = $data['definition'];
237 $instances[$data['definition']['id']] = $this->migrationPluginManager->createStubMigration($data['definition']);
240 // Reflection is easier than mocking. We need to use createInstance for
241 // purposes of registering the migration for the migration process plugin.
242 $reflector = new \ReflectionObject($this->migrationPluginManager);
243 $property = $reflector->getProperty('definitions');
244 $property->setAccessible(TRUE);
245 $property->setValue($this->migrationPluginManager, $definitions);
246 $this->container->set('plugin.manager.migration', $this->migrationPluginManager);
248 foreach ($datas as $data) {
249 $migration = $this->migrationPluginManager->createInstance($data['definition']['id']);
250 $migrationExecutable = (new MigrateExecutable($migration, $this));
251 /** @var EntityStorageBase $storage */
252 $storage = $this->readAttribute($migration->getDestinationPlugin(), 'storage');
253 $migrationExecutable->import();
254 foreach ($data['expected'] as $expected) {
255 $entity = $storage->loadRevision($expected['id']);
256 $properties = array_diff_key($expected, array_flip(['id']));
257 foreach ($properties as $property => $value) {
258 if (is_array($value)) {
259 foreach ($value as $delta => $text) {
260 $this->assertNotEmpty($entity->{$property}[$delta]->entity, "Entity property $property with $delta is empty");
261 $this->assertEquals($text, $entity->{$property}[$delta]->entity->label());
265 $this->assertNotEmpty($entity, 'Entity with label ' . $expected[$property] .' is empty');
266 $this->assertEquals($expected[$property], $entity->label());
274 * Provides multiple migration definitions for "getEntity" test.
276 public function destinationFieldMappingDataProvider() {
282 'id' => 'single_err',
283 'class' => Migration::class,
285 'plugin' => 'embedded_data',
289 'photo' => 'Photo1 here',
293 'photo' => 'Photo2 here',
297 'id' => ['type' => 'integer'],
304 'plugin' => 'entity_reference_revisions:entity_test_composite',
308 ['id' => 1, 'name' => 'Photo1 here'],
309 ['id' => 2, 'name' => 'Photo2 here'],
312 'multiple err author1' => [
314 'id' => 'multiple_err_author1',
315 'class' => Migration::class,
317 'plugin' => 'embedded_data',
321 'author' => 'Author 1',
325 'author' => 'Author 2',
329 'author' => ['type' => 'text'],
336 'plugin' => 'entity_reference_revisions:entity_test_composite',
340 ['id' => 3, 'name' => 'Author 1'],
341 ['id' => 4, 'name' => 'Author 2'],
344 'multiple err author 2' => [
346 'id' => 'multiple_err_author2',
347 'class' => Migration::class,
349 'plugin' => 'embedded_data',
353 'author' => 'Author 3',
357 'author' => 'Author 4',
361 'author' => ['type' => 'text'],
368 'plugin' => 'entity_reference_revisions:entity_test_composite',
372 ['id' => 5, 'name' => 'Author 3'],
373 ['id' => 6, 'name' => 'Author 4'],
376 'destination entity' => [
378 'id' => 'node_migration',
379 'class' => Migration::class,
381 'plugin' => 'embedded_data',
385 'title' => 'Article 1',
386 'photo' => 'Photo1 here',
387 'author' => ['Author 1', 'Author 3'],
391 'title' => 'Article 2',
392 'photo' => 'Photo2 here',
393 'author' => ['Author 2', 'Author 4'],
397 'id' => ['type' => 'integer'],
403 'plugin' => 'default_value',
404 'default_value' => 'article',
406 'field_err_single/target_id' => [
408 'plugin' => 'migration',
409 'migration' => ['single_err'],
414 'plugin' => 'extract',
420 'field_err_single/target_revision_id' => [
422 'plugin' => 'migration',
423 'migration' => ['single_err'],
428 'plugin' => 'extract',
434 'field_err_multiple' => [
436 'plugin' => 'migration',
438 'multiple_err_author1',
439 'multiple_err_author2',
442 'source' => 'author',
445 'plugin' => 'iterator',
448 'target_revision_id' => '1',
454 'plugin' => 'entity:node',
460 'title' => 'Article 1',
461 'field_err_single' => ['Photo1 here'],
462 'field_err_multiple' => ['Author 1', 'Author 3'],
466 'title' => 'Article 2',
467 'field_err_single' => ['Photo2 here'],
468 'field_err_multiple' => ['Author 2', 'Author 4'],
480 public function display($message, $type = 'status') {
481 $this->assertTrue($type == 'status', $message);