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\Core\Entity\EntityTypeInterface;
13 use Drupal\migrate\Plugin\MigrationInterface;
14 use Drupal\migrate\Plugin\migrate\destination\EntityRevision as RealEntityRevision;
15 use Drupal\migrate\Row;
16 use Drupal\Tests\UnitTestCase;
19 * Tests entity revision destination.
22 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\destination\EntityRevision
24 class EntityRevisionTest extends UnitTestCase {
27 * @var \Drupal\migrate\Plugin\MigrationInterface
32 * @var \Drupal\Core\Entity\EntityStorageInterface
37 * @var \Drupal\Core\Entity\EntityManagerInterface
39 protected $entityManager;
42 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
44 protected $fieldTypeManager;
46 protected function setUp() {
49 // Setup mocks to be used when creating a revision destination.
50 $this->migration = $this->prophesize(MigrationInterface::class);
51 $this->storage = $this->prophesize('\Drupal\Core\Entity\EntityStorageInterface');
53 $entity_type = $this->prophesize(EntityTypeInterface::class);
54 $entity_type->getSingularLabel()->willReturn('crazy');
55 $entity_type->getPluralLabel()->willReturn('craziness');
56 $this->storage->getEntityType()->willReturn($entity_type->reveal());
58 $this->entityManager = $this->prophesize('\Drupal\Core\Entity\EntityManagerInterface');
59 $this->fieldTypeManager = $this->prophesize('\Drupal\Core\Field\FieldTypePluginManagerInterface');
63 * Test that passed old destination values are used by default.
67 public function testGetEntityDestinationValues() {
68 $destination = $this->getEntityRevisionDestination([]);
69 // Return a dummy because we don't care what gets called.
70 $entity = $this->prophesize('\Drupal\Core\Entity\EntityInterface')
71 ->willImplement('\Drupal\Core\Entity\RevisionableInterface');
72 // Assert that the first ID from the destination values is used to load the
74 $this->storage->loadRevision(12)
76 ->willReturn($entity->reveal());
78 $this->assertEquals($entity->reveal(), $destination->getEntity($row, [12, 13]));
82 * Test that revision updates update.
86 public function testGetEntityUpdateRevision() {
87 $destination = $this->getEntityRevisionDestination([]);
88 $entity = $this->prophesize('\Drupal\Core\Entity\EntityInterface')
89 ->willImplement('\Drupal\Core\Entity\RevisionableInterface');
91 $entity_type = $this->prophesize('\Drupal\Core\Entity\EntityTypeInterface');
92 $entity_type->getKey('id')->willReturn('nid');
93 $entity_type->getKey('revision')->willReturn('vid');
94 $this->storage->getEntityType()->willReturn($entity_type->reveal());
96 // Assert we load the correct revision.
97 $this->storage->loadRevision(2)
99 ->willReturn($entity->reveal());
100 // Make sure its set as an update and not the default revision.
101 $entity->setNewRevision(FALSE)->shouldBeCalled();
102 $entity->isDefaultRevision(FALSE)->shouldBeCalled();
104 $row = new Row(['nid' => 1, 'vid' => 2], ['nid' => 1, 'vid' => 2]);
105 $row->setDestinationProperty('vid', 2);
106 $this->assertEquals($entity->reveal(), $destination->getEntity($row, []));
110 * Test that new revisions are flagged to be written as new.
112 * @covers ::getEntity
114 public function testGetEntityNewRevision() {
115 $destination = $this->getEntityRevisionDestination([]);
116 $entity = $this->prophesize('\Drupal\Core\Entity\EntityInterface')
117 ->willImplement('\Drupal\Core\Entity\RevisionableInterface');
119 $entity_type = $this->prophesize('\Drupal\Core\Entity\EntityTypeInterface');
120 $entity_type->getKey('id')->willReturn('nid');
121 $entity_type->getKey('revision')->willReturn('vid');
122 $this->storage->getEntityType()->willReturn($entity_type->reveal());
124 // Enforce is new should be disabled.
125 $entity->enforceIsNew(FALSE)->shouldBeCalled();
126 // And toggle this as new revision but not the default revision.
127 $entity->setNewRevision(TRUE)->shouldBeCalled();
128 $entity->isDefaultRevision(FALSE)->shouldBeCalled();
130 // Assert we load the correct revision.
131 $this->storage->load(1)
133 ->willReturn($entity->reveal());
135 $row = new Row(['nid' => 1, 'vid' => 2], ['nid' => 1, 'vid' => 2]);
136 $row->setDestinationProperty('nid', 1);
137 $this->assertEquals($entity->reveal(), $destination->getEntity($row, []));
141 * Test entity load failure.
143 * @covers ::getEntity
145 public function testGetEntityLoadFailure() {
146 $destination = $this->getEntityRevisionDestination([]);
148 $entity_type = $this->prophesize('\Drupal\Core\Entity\EntityTypeInterface');
149 $entity_type->getKey('id')->willReturn('nid');
150 $entity_type->getKey('revision')->willReturn('vid');
151 $this->storage->getEntityType()->willReturn($entity_type->reveal());
153 // Return a failed load and make sure we don't fail and we return FALSE.
154 $this->storage->load(1)
158 $row = new Row(['nid' => 1, 'vid' => 2], ['nid' => 1, 'vid' => 2]);
159 $row->setDestinationProperty('nid', 1);
160 $this->assertFalse($destination->getEntity($row, []));
164 * Test entity revision save.
168 public function testSave() {
169 $entity = $this->prophesize('\Drupal\Core\Entity\ContentEntityInterface');
172 $entity->getRevisionId()
175 $destination = $this->getEntityRevisionDestination();
176 $this->assertEquals([1234], $destination->save($entity->reveal(), []));
180 * Helper method to create an entity revision destination with mock services.
182 * @see \Drupal\Tests\migrate\Unit\Destination\EntityRevision
184 * @param $configuration
185 * Configuration for the destination.
186 * @param string $plugin_id
188 * @param array $plugin_definition
189 * The plugin definition.
191 * @return \Drupal\Tests\migrate\Unit\destination\EntityRevision
192 * Mocked destination.
194 protected function getEntityRevisionDestination(array $configuration = [], $plugin_id = 'entity_revision', array $plugin_definition = []) {
195 return new EntityRevision($configuration, $plugin_id, $plugin_definition,
196 $this->migration->reveal(),
197 $this->storage->reveal(),
199 $this->entityManager->reveal(),
200 $this->fieldTypeManager->reveal()
207 * Mock that exposes from internal methods for testing.
209 class EntityRevision extends RealEntityRevision {
212 * Allow public access for testing.
214 public function getEntity(Row $row, array $old_destination_id_values) {
215 return parent::getEntity($row, $old_destination_id_values);
219 * Allow public access for testing.
221 public function save(ContentEntityInterface $entity, array $old_destination_id_values = []) {
222 return parent::save($entity, $old_destination_id_values);
226 * Don't test method from base class.
228 * This method is from the parent and we aren't concerned with the inner
229 * workings of its implementation which would trickle into mock assertions. An
230 * empty implementation avoids this.
232 protected function updateEntity(EntityInterface $entity, Row $row) {