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\Field\FieldTypePluginManagerInterface;
12 use Drupal\migrate\MigrateException;
13 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
14 use Drupal\migrate\Plugin\MigrateIdMapInterface;
15 use Drupal\migrate\Row;
18 * Tests base entity migration destination functionality.
20 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\destination\EntityContentBase
23 class EntityContentBaseTest extends EntityTestBase {
26 * Test basic entity save.
30 public function testImport() {
32 $destination = new EntityTestDestination([], '', [],
33 $this->migration->reveal(),
34 $this->storage->reveal(),
36 $this->entityManager->reveal(),
37 $this->prophesize(FieldTypePluginManagerInterface::class)->reveal());
38 $entity = $this->prophesize(ContentEntityInterface::class);
39 // Assert that save is called.
41 ->shouldBeCalledTimes(1);
42 // Set an id for the entity
45 $destination->setEntity($entity->reveal());
46 // Ensure the id is saved entity id is returned from import.
47 $this->assertEquals([5], $destination->import(new Row()));
48 // Assert that import set the rollback action.
49 $this->assertEquals(MigrateIdMapInterface::ROLLBACK_DELETE, $destination->rollbackAction());
53 * Test row skipping when we can't get an entity to save.
57 public function testImportEntityLoadFailure() {
59 $destination = new EntityTestDestination([], '', [],
60 $this->migration->reveal(),
61 $this->storage->reveal(),
63 $this->entityManager->reveal(),
64 $this->prophesize(FieldTypePluginManagerInterface::class)->reveal());
65 $destination->setEntity(FALSE);
66 $this->setExpectedException(MigrateException::class, 'Unable to get entity');
67 $destination->import(new Row());
71 * Test that translation destination fails for untranslatable entities.
73 public function testUntranslatable() {
74 // An entity type without a language.
75 $this->entityType->getKey('langcode')->willReturn('');
76 $this->entityType->getKey('id')->willReturn('id');
77 $this->entityManager->getBaseFieldDefinitions('foo')
78 ->willReturn(['id' => BaseFieldDefinitionTest::create('integer')]);
80 $destination = new EntityTestDestination(
81 ['translations' => TRUE],
84 $this->migration->reveal(),
85 $this->storage->reveal(),
87 $this->entityManager->reveal(),
88 $this->prophesize(FieldTypePluginManagerInterface::class)->reveal()
90 $this->setExpectedException(MigrateException::class, 'The "foo" entity type does not support translations.');
91 $destination->getIds();
97 * Stub class for testing EntityContentBase methods.
99 * We want to test things without testing the base class implementations.
101 class EntityTestDestination extends EntityContentBase {
103 private $entity = NULL;
105 public function setEntity($entity) {
106 $this->entity = $entity;
109 protected function getEntity(Row $row, array $old_destination_id_values) {
110 return $this->entity;
113 public static function getEntityTypeId($plugin_id) {