5 * Contains \Drupal\Tests\migrate\Unit\Plugin\migrate\destination\EntityContentBaseTest
8 namespace Drupal\Tests\migrate\Unit\Plugin\migrate\destination;
10 use Drupal\Core\Entity\ContentEntityInterface;
11 use Drupal\Core\Entity\ContentEntityType;
12 use Drupal\Core\Entity\EntityManagerInterface;
13 use Drupal\Core\Entity\EntityStorageInterface;
14 use Drupal\Core\Field\BaseFieldDefinition;
15 use Drupal\Core\Field\FieldTypePluginManagerInterface;
16 use Drupal\migrate\MigrateException;
17 use Drupal\migrate\Plugin\MigrationInterface;
18 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
19 use Drupal\migrate\Plugin\MigrateIdMapInterface;
20 use Drupal\migrate\Row;
21 use Drupal\Tests\UnitTestCase;
24 * Tests base entity migration destination functionality.
26 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\destination\EntityContentBase
29 class EntityContentBaseTest extends UnitTestCase {
32 * @var \Drupal\migrate\Plugin\MigrationInterface
37 * @var \Drupal\Core\Entity\EntityStorageInterface
42 * @var \Drupal\Core\Entity\EntityManagerInterface
44 protected $entityManager;
49 protected function setUp() {
52 $this->migration = $this->prophesize(MigrationInterface::class);
53 $this->storage = $this->prophesize(EntityStorageInterface::class);
54 $this->entityManager = $this->prophesize(EntityManagerInterface::class);
58 * Test basic entity save.
62 public function testImport() {
64 $destination = new EntityTestDestination([], '', [],
65 $this->migration->reveal(),
66 $this->storage->reveal(),
68 $this->entityManager->reveal(),
69 $this->prophesize(FieldTypePluginManagerInterface::class)->reveal());
70 $entity = $this->prophesize(ContentEntityInterface::class);
71 // Assert that save is called.
73 ->shouldBeCalledTimes(1);
74 // Set an id for the entity
77 $destination->setEntity($entity->reveal());
78 // Ensure the id is saved entity id is returned from import.
79 $this->assertEquals([5], $destination->import(new Row()));
80 // Assert that import set the rollback action.
81 $this->assertEquals(MigrateIdMapInterface::ROLLBACK_DELETE, $destination->rollbackAction());
85 * Test row skipping when we can't get an entity to save.
89 public function testImportEntityLoadFailure() {
91 $destination = new EntityTestDestination([], '', [],
92 $this->migration->reveal(),
93 $this->storage->reveal(),
95 $this->entityManager->reveal(),
96 $this->prophesize(FieldTypePluginManagerInterface::class)->reveal());
97 $destination->setEntity(FALSE);
98 $this->setExpectedException(MigrateException::class, 'Unable to get entity');
99 $destination->import(new Row());
103 * Test that translation destination fails for untranslatable entities.
105 public function testUntranslatable() {
106 // An entity type without a language.
107 $entity_type = $this->prophesize(ContentEntityType::class);
108 $entity_type->getKey('langcode')->willReturn('');
109 $entity_type->getKey('id')->willReturn('id');
110 $this->entityManager->getBaseFieldDefinitions('foo')
111 ->willReturn(['id' => BaseFieldDefinitionTest::create('integer')]);
113 $this->storage->getEntityType()->willReturn($entity_type->reveal());
115 $destination = new EntityTestDestination(
116 ['translations' => TRUE],
119 $this->migration->reveal(),
120 $this->storage->reveal(),
122 $this->entityManager->reveal(),
123 $this->prophesize(FieldTypePluginManagerInterface::class)->reveal()
125 $this->setExpectedException(MigrateException::class, 'This entity type does not support translation');
126 $destination->getIds();
132 * Stub class for testing EntityContentBase methods.
134 * We want to test things without testing the base class implementations.
136 class EntityTestDestination extends EntityContentBase {
138 private $entity = NULL;
140 public function setEntity($entity) {
141 $this->entity = $entity;
144 protected function getEntity(Row $row, array $old_destination_id_values) {
145 return $this->entity;
148 public static function getEntityTypeId($plugin_id) {
155 * Stub class for BaseFieldDefinition.
157 class BaseFieldDefinitionTest extends BaseFieldDefinition {
159 public static function create($type) {
160 return new static([]);
163 public function getSettings() {
167 public function getType() {