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\migrate\Plugin\Migration;
12 use Drupal\node\Entity\NodeType;
13 use Drupal\taxonomy\Entity\Vocabulary;
16 * Tests the migration plugin.
18 * @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate
21 class EntityGenerateTest extends KernelTestBase implements MigrateMessageInterface {
23 use EntityReferenceTestTrait;
28 public static $modules = [
41 * The bundle used in this test.
45 protected $bundle = 'page';
48 * The name of the field used in this test.
52 protected $fieldName = 'field_entity_reference';
59 protected $vocabulary = 'fruit';
62 * @var \Drupal\migrate\Plugin\MigrationPluginManager $migrationManager
64 * The migration plugin manager.
66 protected $migrationPluginManager;
71 protected function setUp() {
73 // Create article content type.
75 'type' => $this->bundle,
78 $node_type = NodeType::create($values);
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);
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,
99 $this->createEntityReferenceField(
106 ['target_bundles' => [$this->vocabulary]],
107 FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
110 $this->migrationPluginManager = \Drupal::service('plugin.manager.migration');
114 * Tests generating an entity.
116 * @dataProvider transformDataProvider
118 * @covers ::transform
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);
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();
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.");
147 elseif ($entity->{$property}->getValue()) {
148 $this->assertEquals($expectedValue, $entity->{$property}[0]->entity->$key->value);
151 $this->fail("Expected value: $expectedValue does not exist in $property.");
156 $this->assertNotEmpty($entity, 'Entity with label ' . $row[$property] .' is empty');
157 $this->assertEquals($row[$property], $entity->label());
164 * Provides multiple migration definitions for "transform" test.
166 public function transformDataProvider() {
171 'plugin' => 'embedded_data',
175 'title' => 'content item 1',
180 'title' => 'content item 2',
185 'title' => 'content item 3',
190 'id' => ['type' => 'integer'],
196 'plugin' => 'default_value',
197 'default_value' => $this->bundle,
200 $this->fieldName => [
201 'plugin' => 'entity_generate',
206 'plugin' => 'entity:node',
212 'title' => 'content item 1',
213 $this->fieldName => [
220 'title' => 'content item 2',
221 $this->fieldName => [
228 'title' => 'content item 3',
229 $this->fieldName => [
238 'vid' => $this->vocabulary,
239 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
243 'no arguments_lookup_only' => [
246 'plugin' => 'embedded_data',
250 'title' => 'content item 1',
255 'title' => 'content item 2',
260 'title' => 'content item 3',
265 'id' => ['type' => 'integer'],
271 'plugin' => 'default_value',
272 'default_value' => $this->bundle,
275 $this->fieldName => [
276 'plugin' => 'entity_lookup',
281 'plugin' => 'entity:node',
287 'title' => 'content item 1',
288 $this->fieldName => [
295 'title' => 'content item 2',
296 $this->fieldName => [
303 'title' => 'content item 3',
304 $this->fieldName => [
313 'vid' => $this->vocabulary,
314 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
324 public function display($message, $type = 'status') {
325 $this->assertTrue($type == 'status', $message);