View source
<?php
namespace Drupal\Tests\migrate\Unit\destination;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\migrate\destination\EntityRevision as RealEntityRevision;
use Drupal\migrate\Row;
use Drupal\Tests\UnitTestCase;
class EntityRevisionTest extends UnitTestCase {
protected $migration;
protected $storage;
protected $entityFieldManager;
protected $fieldTypeManager;
protected function setUp() {
parent::setUp();
$this->migration = $this
->prophesize(MigrationInterface::class);
$this->storage = $this
->prophesize('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$entity_type = $this
->prophesize(EntityTypeInterface::class);
$entity_type
->getSingularLabel()
->willReturn('crazy');
$entity_type
->getPluralLabel()
->willReturn('craziness');
$this->storage
->getEntityType()
->willReturn($entity_type
->reveal());
$this->entityFieldManager = $this
->prophesize('\\Drupal\\Core\\Entity\\EntityFieldManagerInterface');
$this->fieldTypeManager = $this
->prophesize('\\Drupal\\Core\\Field\\FieldTypePluginManagerInterface');
}
public function testGetEntityDestinationValues() {
$destination = $this
->getEntityRevisionDestination([]);
$entity = $this
->prophesize('\\Drupal\\Core\\Entity\\RevisionableInterface');
$this->storage
->loadRevision(12)
->shouldBeCalled()
->willReturn($entity
->reveal());
$row = new Row();
$this
->assertEquals($entity
->reveal(), $destination
->getEntity($row, [
12,
13,
]));
}
public function testGetEntityUpdateRevision() {
$destination = $this
->getEntityRevisionDestination([]);
$entity = $this
->prophesize('\\Drupal\\Core\\Entity\\RevisionableInterface');
$entity_type = $this
->prophesize('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$entity_type
->getKey('id')
->willReturn('nid');
$entity_type
->getKey('revision')
->willReturn('vid');
$this->storage
->getEntityType()
->willReturn($entity_type
->reveal());
$this->storage
->loadRevision(2)
->shouldBeCalled()
->willReturn($entity
->reveal());
$entity
->setNewRevision(FALSE)
->shouldBeCalled();
$entity
->isDefaultRevision(FALSE)
->shouldBeCalled();
$row = new Row([
'nid' => 1,
'vid' => 2,
], [
'nid' => 1,
'vid' => 2,
]);
$row
->setDestinationProperty('vid', 2);
$this
->assertEquals($entity
->reveal(), $destination
->getEntity($row, []));
}
public function testGetEntityNewRevision() {
$destination = $this
->getEntityRevisionDestination([]);
$entity = $this
->prophesize('\\Drupal\\Core\\Entity\\RevisionableInterface');
$entity_type = $this
->prophesize('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$entity_type
->getKey('id')
->willReturn('nid');
$entity_type
->getKey('revision')
->willReturn('vid');
$this->storage
->getEntityType()
->willReturn($entity_type
->reveal());
$entity
->enforceIsNew(FALSE)
->shouldBeCalled();
$entity
->setNewRevision(TRUE)
->shouldBeCalled();
$entity
->isDefaultRevision(FALSE)
->shouldBeCalled();
$this->storage
->load(1)
->shouldBeCalled()
->willReturn($entity
->reveal());
$row = new Row([
'nid' => 1,
'vid' => 2,
], [
'nid' => 1,
'vid' => 2,
]);
$row
->setDestinationProperty('nid', 1);
$this
->assertEquals($entity
->reveal(), $destination
->getEntity($row, []));
}
public function testGetEntityLoadFailure() {
$destination = $this
->getEntityRevisionDestination([]);
$entity_type = $this
->prophesize('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$entity_type
->getKey('id')
->willReturn('nid');
$entity_type
->getKey('revision')
->willReturn('vid');
$this->storage
->getEntityType()
->willReturn($entity_type
->reveal());
$this->storage
->load(1)
->shouldBeCalled()
->willReturn(FALSE);
$row = new Row([
'nid' => 1,
'vid' => 2,
], [
'nid' => 1,
'vid' => 2,
]);
$row
->setDestinationProperty('nid', 1);
$this
->assertFalse($destination
->getEntity($row, []));
}
public function testSave() {
$entity = $this
->prophesize('\\Drupal\\Core\\Entity\\ContentEntityInterface');
$entity
->save()
->shouldBeCalled();
$entity
->getRevisionId()
->shouldBeCalled()
->willReturn(1234);
$destination = $this
->getEntityRevisionDestination();
$this
->assertEquals([
1234,
], $destination
->save($entity
->reveal(), []));
}
protected function getEntityRevisionDestination(array $configuration = [], $plugin_id = 'entity_revision', array $plugin_definition = []) {
return new EntityRevision($configuration, $plugin_id, $plugin_definition, $this->migration
->reveal(), $this->storage
->reveal(), [], $this->entityFieldManager
->reveal(), $this->fieldTypeManager
->reveal());
}
}
class EntityRevision extends RealEntityRevision {
public function getEntity(Row $row, array $old_destination_id_values) {
return parent::getEntity($row, $old_destination_id_values);
}
public function save(ContentEntityInterface $entity, array $old_destination_id_values = []) {
return parent::save($entity, $old_destination_id_values);
}
protected function updateEntity(EntityInterface $entity, Row $row) {
return $entity;
}
}