a4c0f27f67ce607a42aad55befee22879268ec7f
[yaffs-website] / web / modules / contrib / migrate_plus / tests / src / Kernel / Plugin / migrate / process / EntityGenerateTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\process;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
8 use Drupal\KernelTests\KernelTestBase;
9 use Drupal\migrate\MigrateExecutable;
10 use Drupal\migrate\MigrateMessageInterface;
11 use Drupal\node\Entity\NodeType;
12 use Drupal\taxonomy\Entity\Vocabulary;
13
14 /**
15  * Tests the migration plugin.
16  *
17  * @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate
18  * @group migrate_plus
19  */
20 class EntityGenerateTest extends KernelTestBase implements MigrateMessageInterface {
21
22   use EntityReferenceTestTrait;
23
24   /**
25    * {@inheritdoc}
26    */
27   public static $modules = [
28     'migrate_plus',
29     'migrate',
30     'user',
31     'system',
32     'node',
33     'taxonomy',
34     'field',
35     'text',
36     'filter',
37   ];
38
39   /**
40    * The bundle used in this test.
41    *
42    * @var string
43    */
44   protected $bundle = 'page';
45
46   /**
47    * The name of the field used in this test.
48    *
49    * @var string
50    */
51   protected $fieldName = 'field_entity_reference';
52
53   /**
54    * The vocabulary id.
55    *
56    * @var string
57    */
58   protected $vocabulary = 'fruit';
59
60   /**
61    * The migration plugin manager.
62    *
63    * @var \Drupal\migrate\Plugin\MigrationPluginManager
64    */
65   protected $migrationPluginManager;
66
67   /**
68    * {@inheritdoc}
69    */
70   protected function setUp() {
71     parent::setUp();
72     // Create article content type.
73     $values = [
74       'type' => $this->bundle,
75       'name' => 'Page',
76     ];
77     $node_type = NodeType::create($values);
78     $node_type->save();
79
80     $this->installEntitySchema('node');
81     $this->installEntitySchema('taxonomy_term');
82     $this->installEntitySchema('taxonomy_vocabulary');
83     $this->installEntitySchema('user');
84     $this->installSchema('system', ['sequences']);
85     $this->installSchema('user', 'users_data');
86     $this->installConfig($this->modules);
87
88     // Create a vocabulary.
89     $vocabulary = Vocabulary::create([
90       'name' => $this->vocabulary,
91       'description' => $this->vocabulary,
92       'vid' => $this->vocabulary,
93       'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
94     ]);
95     $vocabulary->save();
96
97     // Create a field.
98     $this->createEntityReferenceField(
99       'node',
100       $this->bundle,
101       $this->fieldName,
102       'Term reference',
103       'taxonomy_term',
104       'default',
105       ['target_bundles' => [$this->vocabulary]],
106       FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
107     );
108
109     $this->migrationPluginManager = \Drupal::service('plugin.manager.migration');
110   }
111
112   /**
113    * Tests generating an entity.
114    *
115    * @dataProvider transformDataProvider
116    *
117    * @covers ::transform
118    */
119   public function testTransform(array $definition, array $expected, array $preSeed = []) {
120     // Pre seed some test data.
121     foreach ($preSeed as $storageName => $values) {
122       /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
123       $storage = $this->container
124         ->get('entity_type.manager')
125         ->getStorage($storageName);
126       $entity = $storage->create($values);
127       $entity->save();
128     }
129
130     /** @var \Drupal\migrate\Plugin\Migration $migration */
131     $migration = $this->migrationPluginManager->createStubMigration($definition);
132     /** @var EntityStorageBase $storage */
133     $storage = $this->readAttribute($migration->getDestinationPlugin(), 'storage');
134     $migrationExecutable = (new MigrateExecutable($migration, $this));
135     $migrationExecutable->import();
136
137     foreach ($expected as $row) {
138       $entity = $storage->load($row['id']);
139       $properties = array_diff_key($row, array_flip(['id']));
140       foreach ($properties as $property => $value) {
141         if (is_array($value)) {
142           foreach ($value as $key => $expectedValue) {
143             if (empty($expectedValue)) {
144               $this->assertEmpty($entity->{$property}->getValue(), "Expected value is empty but field $property is not empty.");
145             }
146             elseif ($entity->{$property}->getValue()) {
147               $this->assertEquals($expectedValue, $entity->{$property}[0]->entity->$key->value);
148             }
149             else {
150               $this->fail("Expected value: $expectedValue does not exist in $property.");
151             }
152           }
153         }
154         else {
155           $this->assertNotEmpty($entity, 'Entity with label ' . $row[$property] . ' is empty');
156           $this->assertEquals($row[$property], $entity->label());
157         }
158       }
159     }
160   }
161
162   /**
163    * Provides multiple migration definitions for "transform" test.
164    */
165   public function transformDataProvider() {
166     return [
167       'no arguments' => [
168         'definition' => [
169           'source' => [
170             'plugin' => 'embedded_data',
171             'data_rows' => [
172               [
173                 'id' => 1,
174                 'title' => 'content item 1',
175                 'term' => 'Apples',
176               ],
177               [
178                 'id' => 2,
179                 'title' => 'content item 2',
180                 'term' => 'Bananas',
181               ],
182               [
183                 'id' => 3,
184                 'title' => 'content item 3',
185                 'term' => 'Grapes',
186               ],
187             ],
188             'ids' => [
189               'id' => ['type' => 'integer'],
190             ],
191           ],
192           'process' => [
193             'id' => 'id',
194             'type' => [
195               'plugin' => 'default_value',
196               'default_value' => $this->bundle,
197             ],
198             'title' => 'title',
199             $this->fieldName => [
200               'plugin' => 'entity_generate',
201               'source' => 'term',
202             ],
203           ],
204           'destination' => [
205             'plugin' => 'entity:node',
206           ],
207         ],
208         'expected' => [
209           'row 1' => [
210             'id' => 1,
211             'title' => 'content item 1',
212             $this->fieldName => [
213               'tid' => 2,
214               'name' => 'Apples',
215             ],
216           ],
217           'row 2' => [
218             'id' => 2,
219             'title' => 'content item 2',
220             $this->fieldName => [
221               'tid' => 3,
222               'name' => 'Bananas',
223             ],
224           ],
225           'row 3' => [
226             'id' => 3,
227             'title' => 'content item 3',
228             $this->fieldName => [
229               'tid' => 1,
230               'name' => 'Grapes',
231             ],
232           ],
233         ],
234         'pre seed' => [
235           'taxonomy_term' => [
236             'name' => 'Grapes',
237             'vid' => $this->vocabulary,
238             'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
239           ],
240         ],
241       ],
242       'no arguments_lookup_only' => [
243         'definition' => [
244           'source' => [
245             'plugin' => 'embedded_data',
246             'data_rows' => [
247               [
248                 'id' => 1,
249                 'title' => 'content item 1',
250                 'term' => 'Apples',
251               ],
252               [
253                 'id' => 2,
254                 'title' => 'content item 2',
255                 'term' => 'Bananas',
256               ],
257               [
258                 'id' => 3,
259                 'title' => 'content item 3',
260                 'term' => 'Grapes',
261               ],
262             ],
263             'ids' => [
264               'id' => ['type' => 'integer'],
265             ],
266           ],
267           'process' => [
268             'id' => 'id',
269             'type' => [
270               'plugin' => 'default_value',
271               'default_value' => $this->bundle,
272             ],
273             'title' => 'title',
274             $this->fieldName => [
275               'plugin' => 'entity_lookup',
276               'source' => 'term',
277             ],
278           ],
279           'destination' => [
280             'plugin' => 'entity:node',
281           ],
282         ],
283         'expected' => [
284           'row 1' => [
285             'id' => 1,
286             'title' => 'content item 1',
287             $this->fieldName => [
288               'tid' => NULL,
289               'name' => NULL,
290             ],
291           ],
292           'row 2' => [
293             'id' => 2,
294             'title' => 'content item 2',
295             $this->fieldName => [
296               'tid' => NULL,
297               'name' => NULL,
298             ],
299           ],
300           'row 3' => [
301             'id' => 3,
302             'title' => 'content item 3',
303             $this->fieldName => [
304               'tid' => 1,
305               'name' => 'Grapes',
306             ],
307           ],
308         ],
309         'pre seed' => [
310           'taxonomy_term' => [
311             'name' => 'Grapes',
312             'vid' => $this->vocabulary,
313             'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
314           ],
315         ],
316       ],
317     ];
318   }
319
320   /**
321    * {@inheritdoc}
322    */
323   public function display($message, $type = 'status') {
324     $this->assertTrue($type == 'status', $message);
325   }
326
327 }