3 namespace Drupal\Tests\migrate\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\migrate\MigrateExecutable;
8 use Drupal\migrate\MigrateMessage;
9 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
10 use Drupal\migrate\Plugin\MigrateIdMapInterface;
11 use Drupal\migrate\Plugin\MigrationInterface;
12 use Drupal\migrate\Row;
13 use Drupal\migrate_entity_test\Entity\StringIdEntityTest;
16 * Tests the EntityContentBase destination.
20 class MigrateEntityContentBaseTest extends KernelTestBase {
27 public static $modules = ['migrate', 'user', 'language', 'entity_test'];
30 * The storage for entity_test_mul.
32 * @var \Drupal\Core\Entity\ContentEntityStorageInterface
37 * A content migrate destination.
39 * @var \Drupal\migrate\Plugin\MigrateDestinationInterface
41 protected $destination;
46 protected function setUp() {
48 $this->installEntitySchema('entity_test_mul');
50 ConfigurableLanguage::createFromLangcode('en')->save();
51 ConfigurableLanguage::createFromLangcode('fr')->save();
53 $this->storage = $this->container->get('entity.manager')->getStorage('entity_test_mul');
57 * Check the existing translations of an entity.
61 * @param string $default
62 * The expected default translation language code.
63 * @param string[] $others
64 * The expected other translation language codes.
66 protected function assertTranslations($id, $default, $others = []) {
67 $entity = $this->storage->load($id);
68 $this->assertTrue($entity, "Entity exists");
69 $this->assertEquals($default, $entity->language()->getId(), "Entity default translation");
70 $translations = array_keys($entity->getTranslationLanguages(FALSE));
73 $this->assertEquals($others, $translations, "Entity translations");
77 * Create the destination plugin to test.
79 * @param array $configuration
80 * The plugin configuration.
82 protected function createDestination(array $configuration) {
83 $this->destination = new EntityContentBase(
87 $this->getMock(MigrationInterface::class),
90 $this->container->get('entity.manager'),
91 $this->container->get('plugin.manager.field.field_type')
96 * Test importing and rolling back translated entities.
98 public function testTranslated() {
99 // Create a destination.
100 $this->createDestination(['translations' => TRUE]);
102 // Create some pre-existing entities.
103 $this->storage->create(['id' => 1, 'langcode' => 'en'])->save();
104 $this->storage->create(['id' => 2, 'langcode' => 'fr'])->save();
105 $translated = $this->storage->create(['id' => 3, 'langcode' => 'en']);
107 $translated->addTranslation('fr')->save();
109 // Pre-assert that things are as expected.
110 $this->assertTranslations(1, 'en');
111 $this->assertTranslations(2, 'fr');
112 $this->assertTranslations(3, 'en', ['fr']);
113 $this->assertFalse($this->storage->load(4));
115 $destination_rows = [
116 // Existing default translation.
117 ['id' => 1, 'langcode' => 'en', 'action' => MigrateIdMapInterface::ROLLBACK_PRESERVE],
119 ['id' => 2, 'langcode' => 'en', 'action' => MigrateIdMapInterface::ROLLBACK_DELETE],
120 // Existing non-default translation.
121 ['id' => 3, 'langcode' => 'fr', 'action' => MigrateIdMapInterface::ROLLBACK_PRESERVE],
123 ['id' => 4, 'langcode' => 'fr', 'action' => MigrateIdMapInterface::ROLLBACK_DELETE],
125 $rollback_actions = [];
128 foreach ($destination_rows as $idx => $destination_row) {
130 foreach ($destination_row as $key => $value) {
131 $row->setDestinationProperty($key, $value);
133 $this->destination->import($row);
135 // Check that the rollback action is correct, and save it.
136 $this->assertEquals($destination_row['action'], $this->destination->rollbackAction());
137 $rollback_actions[$idx] = $this->destination->rollbackAction();
140 $this->assertTranslations(1, 'en');
141 $this->assertTranslations(2, 'fr', ['en']);
142 $this->assertTranslations(3, 'en', ['fr']);
143 $this->assertTranslations(4, 'fr');
145 // Rollback the rows.
146 foreach ($destination_rows as $idx => $destination_row) {
147 if ($rollback_actions[$idx] == MigrateIdMapInterface::ROLLBACK_DELETE) {
148 $this->destination->rollback($destination_row);
152 // No change, update of existing translation.
153 $this->assertTranslations(1, 'en');
154 // Remove added translation.
155 $this->assertTranslations(2, 'fr');
156 // No change, update of existing translation.
157 $this->assertTranslations(3, 'en', ['fr']);
158 // No change, can't remove default translation.
159 $this->assertTranslations(4, 'fr');
163 * Tests creation of ID columns table with definitions taken from entity type.
165 public function testEntityWithStringId() {
166 $this->enableModules(['migrate_entity_test']);
167 $this->installEntitySchema('migrate_string_id_entity_test');
171 'plugin' => 'embedded_data',
173 ['id' => 123, 'version' => 'foo'],
174 // This integer needs an 'int' schema with 'big' size. If 'destid1'
175 // is not correctly taking the definition from the destination entity
176 // type, the import will fail with a SQL exception.
177 ['id' => 123456789012, 'version' => 'bar'],
180 'id' => ['type' => 'integer', 'size' => 'big'],
181 'version' => ['type' => 'string'],
186 'version' => 'version',
189 'plugin' => 'entity:migrate_string_id_entity_test',
193 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
194 $executable = new MigrateExecutable($migration, new MigrateMessage());
195 $result = $executable->import();
196 $this->assertEquals(MigrationInterface::RESULT_COMPLETED, $result);
198 /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map_plugin */
199 $id_map_plugin = $migration->getIdMap();
201 // Check that the destination has been stored.
202 $map_row = $id_map_plugin->getRowBySource(['id' => 123, 'version' => 'foo']);
203 $this->assertEquals(123, $map_row['destid1']);
204 $map_row = $id_map_plugin->getRowBySource(['id' => 123456789012, 'version' => 'bar']);
205 $this->assertEquals(123456789012, $map_row['destid1']);
209 * Tests empty destinations.
211 public function testEmptyDestinations() {
212 $this->enableModules(['migrate_entity_test']);
213 $this->installEntitySchema('migrate_string_id_entity_test');
217 'plugin' => 'embedded_data',
219 ['id' => 123, 'version' => 'foo'],
220 // This integer needs an 'int' schema with 'big' size. If 'destid1'
221 // is not correctly taking the definition from the destination entity
222 // type, the import will fail with an SQL exception.
223 ['id' => 123456789012, 'version' => 'bar'],
226 'id' => ['type' => 'integer', 'size' => 'big'],
227 'version' => ['type' => 'string'],
229 'constants' => ['null' => NULL],
233 'version' => 'version',
236 'plugin' => 'entity:migrate_string_id_entity_test',
240 $migration = \Drupal::service('plugin.manager.migration')
241 ->createStubMigration($definition);
242 $executable = new MigrateExecutable($migration, new MigrateMessage());
243 $executable->import();
245 /** @var \Drupal\migrate_entity_test\Entity\StringIdEntityTest $entity */
246 $entity = StringIdEntityTest::load('123');
247 $this->assertSame('foo', $entity->version->value);
248 $entity = StringIdEntityTest::load('123456789012');
249 $this->assertSame('bar', $entity->version->value);
251 // Rerun the migration forcing the version to NULL.
252 $definition['process'] = [
254 'version' => 'constants/null',
257 $migration = \Drupal::service('plugin.manager.migration')
258 ->createStubMigration($definition);
259 $executable = new MigrateExecutable($migration, new MigrateMessage());
260 $executable->import();
262 /** @var \Drupal\migrate_entity_test\Entity\StringIdEntityTest $entity */
263 $entity = StringIdEntityTest::load('123');
264 $this->assertNull($entity->version->value);
265 $entity = StringIdEntityTest::load('123456789012');
266 $this->assertNull($entity->version->value);