3 namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\process;
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;
15 * Tests the migration plugin.
17 * @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate
20 class EntityGenerateTest extends KernelTestBase implements MigrateMessageInterface {
22 use EntityReferenceTestTrait;
27 public static $modules = [
40 * The bundle used in this test.
44 protected $bundle = 'page';
47 * The name of the field used in this test.
51 protected $fieldName = 'field_entity_reference';
58 protected $vocabulary = 'fruit';
61 * The migration plugin manager.
63 * @var \Drupal\migrate\Plugin\MigrationPluginManager
65 protected $migrationPluginManager;
70 protected function setUp() {
72 // Create article content type.
74 'type' => $this->bundle,
77 $node_type = NodeType::create($values);
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);
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,
98 $this->createEntityReferenceField(
105 ['target_bundles' => [$this->vocabulary]],
106 FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
109 $this->migrationPluginManager = \Drupal::service('plugin.manager.migration');
113 * Tests generating an entity.
115 * @dataProvider transformDataProvider
117 * @covers ::transform
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);
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();
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.");
146 elseif ($entity->{$property}->getValue()) {
147 $this->assertEquals($expectedValue, $entity->{$property}[0]->entity->$key->value);
150 $this->fail("Expected value: $expectedValue does not exist in $property.");
155 $this->assertNotEmpty($entity, 'Entity with label ' . $row[$property] . ' is empty');
156 $this->assertEquals($row[$property], $entity->label());
163 * Provides multiple migration definitions for "transform" test.
165 public function transformDataProvider() {
170 'plugin' => 'embedded_data',
174 'title' => 'content item 1',
179 'title' => 'content item 2',
184 'title' => 'content item 3',
189 'id' => ['type' => 'integer'],
195 'plugin' => 'default_value',
196 'default_value' => $this->bundle,
199 $this->fieldName => [
200 'plugin' => 'entity_generate',
205 'plugin' => 'entity:node',
211 'title' => 'content item 1',
212 $this->fieldName => [
219 'title' => 'content item 2',
220 $this->fieldName => [
227 'title' => 'content item 3',
228 $this->fieldName => [
237 'vid' => $this->vocabulary,
238 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
242 'no arguments_lookup_only' => [
245 'plugin' => 'embedded_data',
249 'title' => 'content item 1',
254 'title' => 'content item 2',
259 'title' => 'content item 3',
264 'id' => ['type' => 'integer'],
270 'plugin' => 'default_value',
271 'default_value' => $this->bundle,
274 $this->fieldName => [
275 'plugin' => 'entity_lookup',
280 'plugin' => 'entity:node',
286 'title' => 'content item 1',
287 $this->fieldName => [
294 'title' => 'content item 2',
295 $this->fieldName => [
302 'title' => 'content item 3',
303 $this->fieldName => [
312 'vid' => $this->vocabulary,
313 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
323 public function display($message, $type = 'status') {
324 $this->assertTrue($type == 'status', $message);