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