Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / entity_reference_revisions / tests / src / Kernel / Plugin / migrate / destination / EntityReferenceRevisionsDestinationTest.php
1 <?php
2
3 namespace Drupal\Tests\entity_reference_revisions\Kernel\Plugin\migrate\destination;
4
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;
14
15 /**
16  * Tests the migration destination plugin.
17  *
18  * @coversDefaultClass \Drupal\entity_reference_revisions\Plugin\migrate\destination\EntityReferenceRevisions
19  * @group entity_reference_revisions
20  */
21 class EntityReferenceRevisionsDestinationTest extends KernelTestBase implements MigrateMessageInterface {
22
23   /**
24    * @var \Drupal\migrate\Plugin\MigrationPluginManager $migrationManager
25    *
26    * The migration plugin manager.
27    */
28   protected $migrationPluginManager;
29
30   /**
31    * {@inheritdoc}
32    */
33   public static $modules = [
34     'migrate',
35     'entity_reference_revisions',
36     'entity_composite_relationship_test',
37     'user',
38     'system',
39     'text',
40   ];
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function setUp() {
46     parent::setUp();
47     $this->installEntitySchema('entity_test_composite');
48     $this->installSchema('system', ['sequences']);
49     $this->installConfig($this->modules);
50
51     $this->migrationPluginManager = \Drupal::service('plugin.manager.migration');
52   }
53
54   /**
55    * Tests get entity type id.
56    *
57    * @dataProvider getEntityTypeIdDataProvider
58    *
59    * @covers ::getEntityTypeId
60    */
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();
66
67     /** @var EntityStorageBase $storage */
68     $storage = $this->readAttribute($destination, 'storage');
69     $actual = $this->readAttribute($storage, 'entityTypeId');
70
71     $this->assertEquals($expected, $actual);
72   }
73
74   /**
75    * Provides multiple migration definitions for "getEntityTypeId" test.
76    */
77   public function getEntityTypeIdDataProvider() {
78     $datas  = $this->getEntityDataProvider();
79
80     foreach ($datas as &$data) {
81       $data['expected'] = 'entity_test_composite';
82     }
83
84     return $datas;
85   }
86
87   /**
88    * Tests get entity.
89    *
90    * @dataProvider getEntityDataProvider
91    *
92    * @covers ::getEntity
93    * @covers ::rollback
94    * @covers ::rollbackNonTranslation
95    */
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());
109       }
110     }
111     $migrationExecutable->rollback();
112     foreach ($expected as $data) {
113       $entity = $storage->loadRevision($data['id']);
114       $this->assertEmpty($entity);
115     }
116   }
117
118   /**
119    * Provides multiple migration definitions for "getEntity" test.
120    */
121   public function getEntityDataProvider() {
122     return [
123       'without keys' => [
124         'definition' => [
125           'source' => [
126             'plugin' => 'embedded_data',
127             'data_rows' => [
128               ['id' => 1, 'name' => 'content item 1a'],
129               ['id' => 1, 'name' => 'content item 1b'],
130               ['id' => 2, 'name' => 'content item 2'],
131             ],
132             'ids' => [
133               'id' => ['type' => 'integer'],
134               'name' => ['type' => 'text'],
135             ],
136           ],
137           'process' => [
138             'name' => 'name',
139           ],
140           'destination' => [
141             'plugin' => 'entity_reference_revisions:entity_test_composite',
142           ],
143         ],
144         'expected' => [
145           ['id' => 1, 'label' => 'content item 1a'],
146           ['id' => 2, 'label' => 'content item 1b'],
147           ['id' => 3, 'label' => 'content item 2'],
148         ],
149       ],
150       'with keys' => [
151         'definition' => [
152           'source' => [
153             'plugin' => 'embedded_data',
154             'data_rows' => [
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'],
158             ],
159             'ids' => [
160               'id' => ['type' => 'integer'],
161               'name' => ['type' => 'text'],
162             ],
163           ],
164           'process' => [
165             'id' => 'id',
166             'revision_id' => 'revision_id',
167             'name' => 'name',
168           ],
169           'destination' => [
170             'plugin' => 'entity_reference_revisions:entity_test_composite',
171           ],
172         ],
173         'expected' => [
174           ['id' => 1, 'label' => 'content item 1'],
175           ['id' => 2, 'label' => 'content item 2'],
176           ['id' => 3, 'label' => 'content item 3'],
177         ],
178       ],
179     ];
180   }
181
182   /**
183    * Tests multi-value and single-value destination field linkage.
184    *
185    * @dataProvider destinationFieldMappingDataProvider
186    *
187    */
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']);
193
194     // Create new content type.
195     $values = ['type' => 'article', 'name' => 'Article'];
196     $node_type = NodeType::create($values);
197     $node_type->save();
198
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',
204       'settings' => [
205         'target_type' => 'entity_test_composite'
206       ],
207       'cardinality' => 1,
208     ]);
209     $field_storage->save();
210     $field = FieldConfig::create([
211       'field_storage' => $field_storage,
212       'bundle' => 'article',
213     ]);
214     $field->save();
215
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',
221       'settings' => [
222         'target_type' => 'entity_test_composite'
223       ],
224       'cardinality' => -1,
225     ]);
226     $field_storage->save();
227     $field = FieldConfig::create([
228       'field_storage' => $field_storage,
229       'bundle' => 'article',
230     ]);
231     $field->save();
232
233     $definitions = [];
234     $instances = [];
235     foreach ($datas as $data) {
236       $definitions[$data['definition']['id']] = $data['definition'];
237       $instances[$data['definition']['id']] = $this->migrationPluginManager->createStubMigration($data['definition']);
238     }
239
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);
247
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());
262             }
263           }
264           else {
265             $this->assertNotEmpty($entity, 'Entity with label ' . $expected[$property] .' is empty');
266             $this->assertEquals($expected[$property], $entity->label());
267           }
268         }
269       }
270     }
271   }
272
273   /**
274    * Provides multiple migration definitions for "getEntity" test.
275    */
276   public function destinationFieldMappingDataProvider() {
277     return [
278       'scenario 1' => [
279         [
280           'single err' => [
281             'definition' => [
282               'id' => 'single_err',
283               'class' => Migration::class,
284               'source' => [
285                 'plugin' => 'embedded_data',
286                 'data_rows' => [
287                   [
288                     'id' => 1,
289                     'photo' => 'Photo1 here',
290                   ],
291                   [
292                     'id' => 2,
293                     'photo' => 'Photo2 here',
294                   ],
295                 ],
296                 'ids' => [
297                   'id' => ['type' => 'integer'],
298                 ],
299               ],
300               'process' => [
301                 'name' => 'photo',
302               ],
303               'destination' => [
304                 'plugin' => 'entity_reference_revisions:entity_test_composite',
305               ],
306             ],
307             'expected' => [
308               ['id' => 1, 'name' => 'Photo1 here'],
309               ['id' => 2, 'name' => 'Photo2 here'],
310             ],
311           ],
312           'multiple err author1' => [
313             'definition' => [
314               'id' => 'multiple_err_author1',
315               'class' => Migration::class,
316               'source' => [
317                 'plugin' => 'embedded_data',
318                 'data_rows' => [
319                   [
320                     'id' => 1,
321                     'author' => 'Author 1',
322                   ],
323                   [
324                     'id' => 2,
325                     'author' => 'Author 2',
326                   ],
327                 ],
328                 'ids' => [
329                   'author' => ['type' => 'text'],
330                 ],
331               ],
332               'process' => [
333                 'name' => 'author',
334               ],
335               'destination' => [
336                 'plugin' => 'entity_reference_revisions:entity_test_composite',
337               ],
338             ],
339             'expected' => [
340               ['id' => 3, 'name' => 'Author 1'],
341               ['id' => 4, 'name' => 'Author 2'],
342             ],
343           ],
344           'multiple err author 2' => [
345             'definition' => [
346               'id' => 'multiple_err_author2',
347               'class' => Migration::class,
348               'source' => [
349                 'plugin' => 'embedded_data',
350                 'data_rows' => [
351                   [
352                     'id' => 1,
353                     'author' => 'Author 3',
354                   ],
355                   [
356                     'id' => 2,
357                     'author' => 'Author 4',
358                   ],
359                 ],
360                 'ids' => [
361                   'author' => ['type' => 'text'],
362                 ],
363               ],
364               'process' => [
365                 'name' => 'author',
366               ],
367               'destination' => [
368                 'plugin' => 'entity_reference_revisions:entity_test_composite',
369               ],
370             ],
371             'expected' => [
372               ['id' => 5, 'name' => 'Author 3'],
373               ['id' => 6, 'name' => 'Author 4'],
374             ],
375           ],
376           'destination entity' => [
377             'definition' => [
378               'id' => 'node_migration',
379               'class' => Migration::class,
380               'source' => [
381                 'plugin' => 'embedded_data',
382                 'data_rows' => [
383                   [
384                     'id' => 1,
385                     'title' => 'Article 1',
386                     'photo' => 'Photo1 here',
387                     'author' => ['Author 1', 'Author 3'],
388                   ],
389                   [
390                     'id' => 2,
391                     'title' => 'Article 2',
392                     'photo' => 'Photo2 here',
393                     'author' => ['Author 2', 'Author 4'],
394                   ],
395                 ],
396                 'ids' => [
397                   'id' => ['type' => 'integer'],
398                 ],
399               ],
400               'process' => [
401                 'title' => 'title',
402                 'type' => [
403                   'plugin' => 'default_value',
404                   'default_value' => 'article',
405                 ],
406                 'field_err_single/target_id' => [
407                   [
408                     'plugin' => 'migration',
409                     'migration' => ['single_err'],
410                     'no_stub' => TRUE,
411                     'source' => 'id',
412                   ],
413                   [
414                     'plugin' => 'extract',
415                     'index' => [
416                       '0',
417                     ],
418                   ],
419                 ],
420                 'field_err_single/target_revision_id' => [
421                   [
422                     'plugin' => 'migration',
423                     'migration' => ['single_err'],
424                     'no_stub' => TRUE,
425                     'source' => 'id',
426                   ],
427                   [
428                     'plugin' => 'extract',
429                     'index' => [
430                       1,
431                     ],
432                   ],
433                 ],
434                 'field_err_multiple' => [
435                   [
436                     'plugin' => 'migration',
437                     'migration' => [
438                       'multiple_err_author1',
439                       'multiple_err_author2',
440                     ],
441                     'no_stub' => TRUE,
442                     'source' => 'author',
443                   ],
444                   [
445                     'plugin' => 'iterator',
446                     'process' => [
447                       'target_id' => '0',
448                       'target_revision_id' => '1',
449                     ],
450                   ],
451                 ],
452               ],
453               'destination' => [
454                 'plugin' => 'entity:node',
455               ],
456             ],
457             'expected' => [
458               [
459                 'id' => 1,
460                 'title' => 'Article 1',
461                 'field_err_single' => ['Photo1 here'],
462                 'field_err_multiple' => ['Author 1', 'Author 3'],
463               ],
464               [
465                 'id' => 2,
466                 'title' => 'Article 2',
467                 'field_err_single' => ['Photo2 here'],
468                 'field_err_multiple' => ['Author 2', 'Author 4'],
469               ],
470             ],
471           ],
472         ],
473       ],
474     ];
475   }
476
477   /**
478    * {@inheritdoc}
479    */
480   public function display($message, $type = 'status') {
481     $this->assertTrue($type == 'status', $message);
482   }
483
484 }