View source
<?php
namespace Drupal\Tests\migrate\Kernel;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate_entity_test\Entity\StringIdEntityTest;
class MigrateEntityContentBaseTest extends KernelTestBase {
protected static $modules = [
'migrate',
'user',
'language',
'entity_test',
];
protected $storage;
protected $destination;
protected function setUp() : void {
parent::setUp();
\Drupal::state()
->set('entity_test.required_default_field', TRUE);
\Drupal::state()
->set('entity_test.required_multi_default_field', TRUE);
$this
->installEntitySchema('entity_test_mul');
ConfigurableLanguage::createFromLangcode('en')
->save();
ConfigurableLanguage::createFromLangcode('fr')
->save();
$this->storage = $this->container
->get('entity_type.manager')
->getStorage('entity_test_mul');
}
protected function assertTranslations($id, $default, $others = []) {
$entity = $this->storage
->load($id);
$this
->assertNotEmpty($entity, "Entity exists");
$this
->assertEquals($default, $entity
->language()
->getId(), "Entity default translation");
$translations = array_keys($entity
->getTranslationLanguages(FALSE));
sort($others);
sort($translations);
$this
->assertEquals($others, $translations, "Entity translations");
}
protected function createDestination(array $configuration) {
$this->destination = new EntityContentBase($configuration, 'fake_plugin_id', [], $this
->createMock(MigrationInterface::class), $this->storage, [], $this->container
->get('entity_field.manager'), $this->container
->get('plugin.manager.field.field_type'));
}
public function testTranslated() {
$this
->createDestination([
'translations' => TRUE,
]);
$this->storage
->create([
'id' => 1,
'langcode' => 'en',
])
->save();
$this->storage
->create([
'id' => 2,
'langcode' => 'fr',
])
->save();
$translated = $this->storage
->create([
'id' => 3,
'langcode' => 'en',
]);
$translated
->save();
$translated
->addTranslation('fr')
->save();
$this
->assertTranslations(1, 'en');
$this
->assertTranslations(2, 'fr');
$this
->assertTranslations(3, 'en', [
'fr',
]);
$this
->assertNull($this->storage
->load(4));
$destination_rows = [
[
'id' => 1,
'langcode' => 'en',
'action' => MigrateIdMapInterface::ROLLBACK_PRESERVE,
],
[
'id' => 2,
'langcode' => 'en',
'action' => MigrateIdMapInterface::ROLLBACK_DELETE,
],
[
'id' => 3,
'langcode' => 'fr',
'action' => MigrateIdMapInterface::ROLLBACK_PRESERVE,
],
[
'id' => 4,
'langcode' => 'fr',
'action' => MigrateIdMapInterface::ROLLBACK_DELETE,
],
];
$rollback_actions = [];
foreach ($destination_rows as $idx => $destination_row) {
$row = new Row();
foreach ($destination_row as $key => $value) {
$row
->setDestinationProperty($key, $value);
}
$this->destination
->import($row);
$this
->assertEquals($destination_row['action'], $this->destination
->rollbackAction());
$rollback_actions[$idx] = $this->destination
->rollbackAction();
}
$this
->assertTranslations(1, 'en');
$this
->assertTranslations(2, 'fr', [
'en',
]);
$this
->assertTranslations(3, 'en', [
'fr',
]);
$this
->assertTranslations(4, 'fr');
foreach ($destination_rows as $idx => $destination_row) {
if ($rollback_actions[$idx] == MigrateIdMapInterface::ROLLBACK_DELETE) {
$this->destination
->rollback($destination_row);
}
}
$this
->assertTranslations(1, 'en');
$this
->assertTranslations(2, 'fr');
$this
->assertTranslations(3, 'en', [
'fr',
]);
$this
->assertTranslations(4, 'fr');
}
public function testEntityWithStringId() {
$this
->enableModules([
'migrate_entity_test',
]);
$this
->installEntitySchema('migrate_string_id_entity_test');
$definition = [
'source' => [
'plugin' => 'embedded_data',
'data_rows' => [
[
'id' => 123,
'version' => 'foo',
],
[
'id' => 123456789012,
'version' => 'bar',
],
],
'ids' => [
'id' => [
'type' => 'integer',
'size' => 'big',
],
'version' => [
'type' => 'string',
],
],
],
'process' => [
'id' => 'id',
'version' => 'version',
],
'destination' => [
'plugin' => 'entity:migrate_string_id_entity_test',
],
];
$migration = \Drupal::service('plugin.manager.migration')
->createStubMigration($definition);
$executable = new MigrateExecutable($migration);
$result = $executable
->import();
$this
->assertEquals(MigrationInterface::RESULT_COMPLETED, $result);
$id_map_plugin = $migration
->getIdMap();
$map_row = $id_map_plugin
->getRowBySource([
'id' => 123,
'version' => 'foo',
]);
$this
->assertEquals(123, $map_row['destid1']);
$map_row = $id_map_plugin
->getRowBySource([
'id' => 123456789012,
'version' => 'bar',
]);
$this
->assertEquals(123456789012, $map_row['destid1']);
}
public function testEmptyDestinations() {
$this
->enableModules([
'migrate_entity_test',
]);
$this
->installEntitySchema('migrate_string_id_entity_test');
$definition = [
'source' => [
'plugin' => 'embedded_data',
'data_rows' => [
[
'id' => 123,
'version' => 'foo',
],
[
'id' => 123456789012,
'version' => 'bar',
],
],
'ids' => [
'id' => [
'type' => 'integer',
'size' => 'big',
],
'version' => [
'type' => 'string',
],
],
'constants' => [
'null' => NULL,
],
],
'process' => [
'id' => 'id',
'version' => 'version',
],
'destination' => [
'plugin' => 'entity:migrate_string_id_entity_test',
],
];
$migration = \Drupal::service('plugin.manager.migration')
->createStubMigration($definition);
$executable = new MigrateExecutable($migration);
$executable
->import();
$entity = StringIdEntityTest::load('123');
$this
->assertSame('foo', $entity->version->value);
$entity = StringIdEntityTest::load('123456789012');
$this
->assertSame('bar', $entity->version->value);
$definition['process'] = [
'id' => 'id',
'version' => 'constants/null',
];
$migration = \Drupal::service('plugin.manager.migration')
->createStubMigration($definition);
$executable = new MigrateExecutable($migration);
$executable
->import();
$entity = StringIdEntityTest::load('123');
$this
->assertNull($entity->version->value);
$entity = StringIdEntityTest::load('123456789012');
$this
->assertNull($entity->version->value);
}
public function testStubRows() {
$this
->createDestination([]);
$row = new Row([], [], TRUE);
$row
->setDestinationProperty('type', 'test');
$ids = $this->destination
->import($row);
$this
->assertCount(1, $ids);
$entity = EntityTestMul::load(reset($ids));
$this
->assertInstanceOf(EntityTestMul::class, $entity);
$single_field_name = 'required_default_field';
$single_default_value = $entity
->getFieldDefinition($single_field_name)
->getDefaultValueLiteral();
$this
->assertSame($single_default_value, $entity
->get($single_field_name)
->getValue());
$multi_field_name = 'required_multi_default_field';
$multi_default_value = $entity
->getFieldDefinition($multi_field_name)
->getDefaultValueLiteral();
$count = 3;
$this
->assertCount($count, $multi_default_value);
for ($i = 0; $i < $count; ++$i) {
$this
->assertSame($multi_default_value[$i], $entity
->get($multi_field_name)
->get($i)
->getValue());
}
}
}