View source
<?php
namespace Drupal\Tests\migrate\Unit\Plugin\migrate\destination;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Row;
class EntityContentBaseTest extends EntityTestBase {
public function testImport() {
$bundles = [];
$destination = new EntityTestDestination([], '', [], $this->migration
->reveal(), $this->storage
->reveal(), $bundles, $this->entityFieldManager
->reveal(), $this
->prophesize(FieldTypePluginManagerInterface::class)
->reveal(), $this
->prophesize(AccountSwitcherInterface::class)
->reveal());
$entity = $this
->prophesize(ContentEntityInterface::class);
$entity
->isValidationRequired()
->shouldBeCalledTimes(1);
$entity
->save()
->shouldBeCalledTimes(1);
$entity
->id()
->willReturn(5);
$destination
->setEntity($entity
->reveal());
$this
->assertEquals([
5,
], $destination
->import(new Row()));
$this
->assertEquals(MigrateIdMapInterface::ROLLBACK_DELETE, $destination
->rollbackAction());
}
public function testImportEntityLoadFailure() {
$bundles = [];
$destination = new EntityTestDestination([], '', [], $this->migration
->reveal(), $this->storage
->reveal(), $bundles, $this->entityFieldManager
->reveal(), $this
->prophesize(FieldTypePluginManagerInterface::class)
->reveal(), $this
->prophesize(AccountSwitcherInterface::class)
->reveal());
$destination
->setEntity(FALSE);
$this
->expectException(MigrateException::class);
$this
->expectExceptionMessage('Unable to get entity');
$destination
->import(new Row());
}
public function testUntranslatable() {
$this->entityType
->getKey('langcode')
->willReturn('');
$this->entityType
->getKey('id')
->willReturn('id');
$this->entityFieldManager
->getBaseFieldDefinitions('foo')
->willReturn([
'id' => BaseFieldDefinitionTest::create('integer'),
]);
$destination = new EntityTestDestination([
'translations' => TRUE,
], '', [], $this->migration
->reveal(), $this->storage
->reveal(), [], $this->entityFieldManager
->reveal(), $this
->prophesize(FieldTypePluginManagerInterface::class)
->reveal(), $this
->prophesize(AccountSwitcherInterface::class)
->reveal());
$this
->expectException(MigrateException::class);
$this
->expectExceptionMessage('The "foo" entity type does not support translations.');
$destination
->getIds();
}
}
class EntityTestDestination extends EntityContentBase {
private $entity = NULL;
public function setEntity($entity) {
$this->entity = $entity;
}
protected function getEntity(Row $row, array $old_destination_id_values) {
return $this->entity;
}
public static function getEntityTypeId($plugin_id) {
return 'foo';
}
}