5 * Contains \Drupal\Tests\migrate\Unit\destination\EntityRevisionTest.
8 namespace Drupal\Tests\migrate\Unit\destination;
10 use Drupal\Core\Entity\ContentEntityInterface;
11 use Drupal\Core\Entity\EntityInterface;
12 use Drupal\migrate\Plugin\MigrationInterface;
13 use Drupal\migrate\Plugin\migrate\destination\EntityRevision as RealEntityRevision;
14 use Drupal\migrate\Row;
15 use Drupal\Tests\UnitTestCase;
18 * Tests entity revision destination.
21 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\destination\EntityRevision
23 class EntityRevisionTest extends UnitTestCase {
26 * @var \Drupal\migrate\Plugin\MigrationInterface
31 * @var \Drupal\Core\Entity\EntityStorageInterface
36 * @var \Drupal\Core\Entity\EntityManagerInterface
38 protected $entityManager;
41 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
43 protected $fieldTypeManager;
45 protected function setUp() {
48 // Setup mocks to be used when creating a revision destination.
49 $this->migration = $this->prophesize(MigrationInterface::class);
50 $this->storage = $this->prophesize('\Drupal\Core\Entity\EntityStorageInterface');
51 $this->entityManager = $this->prophesize('\Drupal\Core\Entity\EntityManagerInterface');
52 $this->fieldTypeManager = $this->prophesize('\Drupal\Core\Field\FieldTypePluginManagerInterface');
56 * Test that passed old destination values are used by default.
60 public function testGetEntityDestinationValues() {
61 $destination = $this->getEntityRevisionDestination([]);
62 // Return a dummy because we don't care what gets called.
63 $entity = $this->prophesize('\Drupal\Core\Entity\EntityInterface')
64 ->willImplement('\Drupal\Core\Entity\RevisionableInterface');
65 // Assert that the first ID from the destination values is used to load the
67 $this->storage->loadRevision(12)
69 ->willReturn($entity->reveal());
71 $this->assertEquals($entity->reveal(), $destination->getEntity($row, [12, 13]));
75 * Test that revision updates update.
79 public function testGetEntityUpdateRevision() {
80 $destination = $this->getEntityRevisionDestination([]);
81 $entity = $this->prophesize('\Drupal\Core\Entity\EntityInterface')
82 ->willImplement('\Drupal\Core\Entity\RevisionableInterface');
84 $entity_type = $this->prophesize('\Drupal\Core\Entity\EntityTypeInterface');
85 $entity_type->getKey('id')->willReturn('nid');
86 $entity_type->getKey('revision')->willReturn('vid');
87 $this->storage->getEntityType()->willReturn($entity_type->reveal());
89 // Assert we load the correct revision.
90 $this->storage->loadRevision(2)
92 ->willReturn($entity->reveal());
93 // Make sure its set as an update and not the default revision.
94 $entity->setNewRevision(FALSE)->shouldBeCalled();
95 $entity->isDefaultRevision(FALSE)->shouldBeCalled();
97 $row = new Row(['nid' => 1, 'vid' => 2], ['nid' => 1, 'vid' => 2]);
98 $row->setDestinationProperty('vid', 2);
99 $this->assertEquals($entity->reveal(), $destination->getEntity($row, []));
103 * Test that new revisions are flagged to be written as new.
105 * @covers ::getEntity
107 public function testGetEntityNewRevision() {
108 $destination = $this->getEntityRevisionDestination([]);
109 $entity = $this->prophesize('\Drupal\Core\Entity\EntityInterface')
110 ->willImplement('\Drupal\Core\Entity\RevisionableInterface');
112 $entity_type = $this->prophesize('\Drupal\Core\Entity\EntityTypeInterface');
113 $entity_type->getKey('id')->willReturn('nid');
114 $entity_type->getKey('revision')->willReturn('vid');
115 $this->storage->getEntityType()->willReturn($entity_type->reveal());
117 // Enforce is new should be disabled.
118 $entity->enforceIsNew(FALSE)->shouldBeCalled();
119 // And toggle this as new revision but not the default revision.
120 $entity->setNewRevision(TRUE)->shouldBeCalled();
121 $entity->isDefaultRevision(FALSE)->shouldBeCalled();
123 // Assert we load the correct revision.
124 $this->storage->load(1)
126 ->willReturn($entity->reveal());
128 $row = new Row(['nid' => 1, 'vid' => 2], ['nid' => 1, 'vid' => 2]);
129 $row->setDestinationProperty('nid', 1);
130 $this->assertEquals($entity->reveal(), $destination->getEntity($row, []));
134 * Test entity load failure.
136 * @covers ::getEntity
138 public function testGetEntityLoadFailure() {
139 $destination = $this->getEntityRevisionDestination([]);
141 $entity_type = $this->prophesize('\Drupal\Core\Entity\EntityTypeInterface');
142 $entity_type->getKey('id')->willReturn('nid');
143 $entity_type->getKey('revision')->willReturn('vid');
144 $this->storage->getEntityType()->willReturn($entity_type->reveal());
146 // Return a failed load and make sure we don't fail and we return FALSE.
147 $this->storage->load(1)
151 $row = new Row(['nid' => 1, 'vid' => 2], ['nid' => 1, 'vid' => 2]);
152 $row->setDestinationProperty('nid', 1);
153 $this->assertFalse($destination->getEntity($row, []));
157 * Test entity revision save.
161 public function testSave() {
162 $entity = $this->prophesize('\Drupal\Core\Entity\ContentEntityInterface');
165 $entity->getRevisionId()
168 $destination = $this->getEntityRevisionDestination();
169 $this->assertEquals([1234], $destination->save($entity->reveal(), []));
174 * Helper method to create an entity revision destination with mock services.
176 * @see \Drupal\Tests\migrate\Unit\Destination\EntityRevision
178 * @param $configuration
179 * Configuration for the destination.
180 * @param string $plugin_id
182 * @param array $plugin_definition
183 * The plugin definition.
185 * @return \Drupal\Tests\migrate\Unit\destination\EntityRevision
186 * Mocked destination.
188 protected function getEntityRevisionDestination(array $configuration = [], $plugin_id = 'entity_revision', array $plugin_definition = []) {
189 return new EntityRevision($configuration, $plugin_id, $plugin_definition,
190 $this->migration->reveal(),
191 $this->storage->reveal(),
193 $this->entityManager->reveal(),
194 $this->fieldTypeManager->reveal()
201 * Mock that exposes from internal methods for testing.
203 class EntityRevision extends RealEntityRevision {
206 * Allow public access for testing.
208 public function getEntity(Row $row, array $old_destination_id_values) {
209 return parent::getEntity($row, $old_destination_id_values);
213 * Allow public access for testing.
215 public function save(ContentEntityInterface $entity, array $old_destination_id_values = []) {
216 return parent::save($entity, $old_destination_id_values);
220 * Don't test method from base class.
222 * This method is from the parent and we aren't concerned with the inner
223 * workings of its implementation which would trickle into mock assertions. An
224 * empty implementation avoids this.
226 protected function updateEntity(EntityInterface $entity, Row $row) {}