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\EntityManagerInterface;
12 use Drupal\Core\Entity\EntityStorageInterface;
13 use Drupal\Core\Entity\EntityTypeInterface;
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\EntityTypeInterface
44 protected $entityType;
47 * @var \Drupal\Core\Entity\EntityManagerInterface
49 protected $entityManager;
54 protected function setUp() {
57 $this->migration = $this->prophesize(MigrationInterface::class);
58 $this->storage = $this->prophesize(EntityStorageInterface::class);
60 $this->entityType = $this->prophesize(EntityTypeInterface::class);
61 $this->entityType->getPluralLabel()->willReturn('wonkiness');
62 $this->storage->getEntityType()->willReturn($this->entityType->reveal());
64 $this->entityManager = $this->prophesize(EntityManagerInterface::class);
68 * Test basic entity save.
72 public function testImport() {
74 $destination = new EntityTestDestination([], '', [],
75 $this->migration->reveal(),
76 $this->storage->reveal(),
78 $this->entityManager->reveal(),
79 $this->prophesize(FieldTypePluginManagerInterface::class)->reveal());
80 $entity = $this->prophesize(ContentEntityInterface::class);
81 // Assert that save is called.
83 ->shouldBeCalledTimes(1);
84 // Set an id for the entity
87 $destination->setEntity($entity->reveal());
88 // Ensure the id is saved entity id is returned from import.
89 $this->assertEquals([5], $destination->import(new Row()));
90 // Assert that import set the rollback action.
91 $this->assertEquals(MigrateIdMapInterface::ROLLBACK_DELETE, $destination->rollbackAction());
95 * Test row skipping when we can't get an entity to save.
99 public function testImportEntityLoadFailure() {
101 $destination = new EntityTestDestination([], '', [],
102 $this->migration->reveal(),
103 $this->storage->reveal(),
105 $this->entityManager->reveal(),
106 $this->prophesize(FieldTypePluginManagerInterface::class)->reveal());
107 $destination->setEntity(FALSE);
108 $this->setExpectedException(MigrateException::class, 'Unable to get entity');
109 $destination->import(new Row());
113 * Test that translation destination fails for untranslatable entities.
115 public function testUntranslatable() {
116 // An entity type without a language.
117 $this->entityType->getKey('langcode')->willReturn('');
118 $this->entityType->getKey('id')->willReturn('id');
119 $this->entityManager->getBaseFieldDefinitions('foo')
120 ->willReturn(['id' => BaseFieldDefinitionTest::create('integer')]);
122 $destination = new EntityTestDestination(
123 ['translations' => TRUE],
126 $this->migration->reveal(),
127 $this->storage->reveal(),
129 $this->entityManager->reveal(),
130 $this->prophesize(FieldTypePluginManagerInterface::class)->reveal()
132 $this->setExpectedException(MigrateException::class, 'This entity type does not support translation');
133 $destination->getIds();
139 * Stub class for testing EntityContentBase methods.
141 * We want to test things without testing the base class implementations.
143 class EntityTestDestination extends EntityContentBase {
145 private $entity = NULL;
147 public function setEntity($entity) {
148 $this->entity = $entity;
151 protected function getEntity(Row $row, array $old_destination_id_values) {
152 return $this->entity;
155 public static function getEntityTypeId($plugin_id) {
162 * Stub class for BaseFieldDefinition.
164 class BaseFieldDefinitionTest extends BaseFieldDefinition {
166 public static function create($type) {
167 return new static([]);
170 public function getSettings() {
174 public function getType() {