Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / destination / EntityRevisionTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\migrate\Unit\destination\EntityRevisionTest.
6  */
7
8 namespace Drupal\Tests\migrate\Unit\destination;
9
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;
17
18 /**
19  * Tests entity revision destination.
20  *
21  * @group migrate
22  * @coversDefaultClass \Drupal\migrate\Plugin\migrate\destination\EntityRevision
23  */
24 class EntityRevisionTest extends UnitTestCase {
25
26   /**
27    * @var \Drupal\migrate\Plugin\MigrationInterface
28    */
29   protected $migration;
30
31   /**
32    * @var \Drupal\Core\Entity\EntityStorageInterface
33    */
34   protected $storage;
35
36   /**
37    * @var \Drupal\Core\Entity\EntityManagerInterface
38    */
39   protected $entityManager;
40
41   /**
42    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
43    */
44   protected $fieldTypeManager;
45
46   protected function setUp() {
47     parent::setUp();
48
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');
52
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());
57
58     $this->entityManager = $this->prophesize('\Drupal\Core\Entity\EntityManagerInterface');
59     $this->fieldTypeManager = $this->prophesize('\Drupal\Core\Field\FieldTypePluginManagerInterface');
60   }
61
62   /**
63    * Test that passed old destination values are used by default.
64    *
65    * @covers ::getEntity
66    */
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
73     // entity.
74     $this->storage->loadRevision(12)
75       ->shouldBeCalled()
76       ->willReturn($entity->reveal());
77     $row = new Row();
78     $this->assertEquals($entity->reveal(), $destination->getEntity($row, [12, 13]));
79   }
80
81   /**
82    * Test that revision updates update.
83    *
84    * @covers ::getEntity
85    */
86   public function testGetEntityUpdateRevision() {
87     $destination = $this->getEntityRevisionDestination([]);
88     $entity = $this->prophesize('\Drupal\Core\Entity\EntityInterface')
89       ->willImplement('\Drupal\Core\Entity\RevisionableInterface');
90
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());
95
96     // Assert we load the correct revision.
97     $this->storage->loadRevision(2)
98       ->shouldBeCalled()
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();
103
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, []));
107   }
108
109   /**
110    * Test that new revisions are flagged to be written as new.
111    *
112    * @covers ::getEntity
113    */
114   public function testGetEntityNewRevision() {
115     $destination = $this->getEntityRevisionDestination([]);
116     $entity = $this->prophesize('\Drupal\Core\Entity\EntityInterface')
117       ->willImplement('\Drupal\Core\Entity\RevisionableInterface');
118
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());
123
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();
129
130     // Assert we load the correct revision.
131     $this->storage->load(1)
132       ->shouldBeCalled()
133       ->willReturn($entity->reveal());
134
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, []));
138   }
139
140   /**
141    * Test entity load failure.
142    *
143    * @covers ::getEntity
144    */
145   public function testGetEntityLoadFailure() {
146     $destination = $this->getEntityRevisionDestination([]);
147
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());
152
153     // Return a failed load and make sure we don't fail and we return FALSE.
154     $this->storage->load(1)
155       ->shouldBeCalled()
156       ->willReturn(FALSE);
157
158     $row = new Row(['nid' => 1, 'vid' => 2], ['nid' => 1, 'vid' => 2]);
159     $row->setDestinationProperty('nid', 1);
160     $this->assertFalse($destination->getEntity($row, []));
161   }
162
163   /**
164    * Test entity revision save.
165    *
166    * @covers ::save
167    */
168   public function testSave() {
169     $entity = $this->prophesize('\Drupal\Core\Entity\ContentEntityInterface');
170     $entity->save()
171       ->shouldBeCalled();
172     $entity->getRevisionId()
173       ->shouldBeCalled()
174       ->willReturn(1234);
175     $destination = $this->getEntityRevisionDestination();
176     $this->assertEquals([1234], $destination->save($entity->reveal(), []));
177   }
178
179   /**
180    * Helper method to create an entity revision destination with mock services.
181    *
182    * @see \Drupal\Tests\migrate\Unit\Destination\EntityRevision
183    *
184    * @param $configuration
185    *   Configuration for the destination.
186    * @param string $plugin_id
187    *   The plugin id.
188    * @param array $plugin_definition
189    *   The plugin definition.
190    *
191    * @return \Drupal\Tests\migrate\Unit\destination\EntityRevision
192    *   Mocked destination.
193    */
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(),
198       [],
199       $this->entityManager->reveal(),
200       $this->fieldTypeManager->reveal()
201     );
202   }
203
204 }
205
206 /**
207  * Mock that exposes from internal methods for testing.
208  */
209 class EntityRevision extends RealEntityRevision {
210
211   /**
212    * Allow public access for testing.
213    */
214   public function getEntity(Row $row, array $old_destination_id_values) {
215     return parent::getEntity($row, $old_destination_id_values);
216   }
217
218   /**
219    * Allow public access for testing.
220    */
221   public function save(ContentEntityInterface $entity, array $old_destination_id_values = []) {
222     return parent::save($entity, $old_destination_id_values);
223   }
224
225   /**
226    * Don't test method from base class.
227    *
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.
231    */
232   protected function updateEntity(EntityInterface $entity, Row $row) {
233     return $entity;
234   }
235
236 }