f68c2bea1fb329e8918131211dbd034b6dd3e5b3
[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\migrate\Plugin\MigrationInterface;
13 use Drupal\migrate\Plugin\migrate\destination\EntityRevision as RealEntityRevision;
14 use Drupal\migrate\Row;
15 use Drupal\Tests\UnitTestCase;
16
17 /**
18  * Tests entity revision destination.
19  *
20  * @group migrate
21  * @coversDefaultClass \Drupal\migrate\Plugin\migrate\destination\EntityRevision
22  */
23 class EntityRevisionTest extends UnitTestCase {
24
25   /**
26    * @var \Drupal\migrate\Plugin\MigrationInterface
27    */
28   protected $migration;
29
30   /**
31    * @var \Drupal\Core\Entity\EntityStorageInterface
32    */
33   protected $storage;
34
35   /**
36    * @var \Drupal\Core\Entity\EntityManagerInterface
37    */
38   protected $entityManager;
39
40   /**
41    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
42    */
43   protected $fieldTypeManager;
44
45   protected function setUp() {
46     parent::setUp();
47
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');
53   }
54
55   /**
56    * Test that passed old destination values are used by default.
57    *
58    * @covers ::getEntity
59    */
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
66     // entity.
67     $this->storage->loadRevision(12)
68       ->shouldBeCalled()
69       ->willReturn($entity->reveal());
70     $row = new Row();
71     $this->assertEquals($entity->reveal(), $destination->getEntity($row, [12, 13]));
72   }
73
74   /**
75    * Test that revision updates update.
76    *
77    * @covers ::getEntity
78    */
79   public function testGetEntityUpdateRevision() {
80     $destination = $this->getEntityRevisionDestination([]);
81     $entity = $this->prophesize('\Drupal\Core\Entity\EntityInterface')
82       ->willImplement('\Drupal\Core\Entity\RevisionableInterface');
83
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());
88
89     // Assert we load the correct revision.
90     $this->storage->loadRevision(2)
91       ->shouldBeCalled()
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();
96
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, []));
100   }
101
102   /**
103    * Test that new revisions are flagged to be written as new.
104    *
105    * @covers ::getEntity
106    */
107   public function testGetEntityNewRevision() {
108     $destination = $this->getEntityRevisionDestination([]);
109     $entity = $this->prophesize('\Drupal\Core\Entity\EntityInterface')
110       ->willImplement('\Drupal\Core\Entity\RevisionableInterface');
111
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());
116
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();
122
123     // Assert we load the correct revision.
124     $this->storage->load(1)
125       ->shouldBeCalled()
126       ->willReturn($entity->reveal());
127
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, []));
131   }
132
133   /**
134    * Test entity load failure.
135    *
136    * @covers ::getEntity
137    */
138   public function testGetEntityLoadFailure() {
139     $destination = $this->getEntityRevisionDestination([]);
140
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());
145
146     // Return a failed load and make sure we don't fail and we return FALSE.
147     $this->storage->load(1)
148       ->shouldBeCalled()
149       ->willReturn(FALSE);
150
151     $row = new Row(['nid' => 1, 'vid' => 2], ['nid' => 1, 'vid' => 2]);
152     $row->setDestinationProperty('nid', 1);
153     $this->assertFalse($destination->getEntity($row, []));
154   }
155
156   /**
157    * Test entity revision save.
158    *
159    * @covers ::save
160    */
161   public function testSave() {
162     $entity = $this->prophesize('\Drupal\Core\Entity\ContentEntityInterface');
163     $entity->save()
164       ->shouldBeCalled();
165     $entity->getRevisionId()
166       ->shouldBeCalled()
167       ->willReturn(1234);
168     $destination = $this->getEntityRevisionDestination();
169     $this->assertEquals([1234], $destination->save($entity->reveal(), []));
170   }
171
172
173   /**
174    * Helper method to create an entity revision destination with mock services.
175    *
176    * @see \Drupal\Tests\migrate\Unit\Destination\EntityRevision
177    *
178    * @param $configuration
179    *   Configuration for the destination.
180    * @param string $plugin_id
181    *   The plugin id.
182    * @param array $plugin_definition
183    *   The plugin definition.
184    *
185    * @return \Drupal\Tests\migrate\Unit\destination\EntityRevision
186    *   Mocked destination.
187    */
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(),
192       [],
193       $this->entityManager->reveal(),
194       $this->fieldTypeManager->reveal()
195     );
196   }
197
198 }
199
200 /**
201  * Mock that exposes from internal methods for testing.
202  */
203 class EntityRevision extends RealEntityRevision {
204
205   /**
206    * Allow public access for testing.
207    */
208   public function getEntity(Row $row, array $old_destination_id_values) {
209     return parent::getEntity($row, $old_destination_id_values);
210   }
211
212   /**
213    * Allow public access for testing.
214    */
215   public function save(ContentEntityInterface $entity, array $old_destination_id_values = []) {
216     return parent::save($entity, $old_destination_id_values);
217   }
218
219   /**
220    * Don't test method from base class.
221    *
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.
225    */
226   protected function updateEntity(EntityInterface $entity, Row $row) {}
227
228 }