12bfd7134e81dabc4ede3c0b1436c6eacb73fce3
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / Plugin / migrate / destination / EntityContentBaseTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\migrate\Unit\Plugin\migrate\destination\EntityContentBaseTest
6  */
7
8 namespace Drupal\Tests\migrate\Unit\Plugin\migrate\destination;
9
10 use Drupal\Core\Entity\ContentEntityInterface;
11 use Drupal\Core\Field\FieldTypePluginManagerInterface;
12 use Drupal\migrate\MigrateException;
13 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
14 use Drupal\migrate\Plugin\MigrateIdMapInterface;
15 use Drupal\migrate\Row;
16
17 /**
18  * Tests base entity migration destination functionality.
19  *
20  * @coversDefaultClass \Drupal\migrate\Plugin\migrate\destination\EntityContentBase
21  * @group migrate
22  */
23 class EntityContentBaseTest extends EntityTestBase {
24
25   /**
26    * Test basic entity save.
27    *
28    * @covers ::import
29    */
30   public function testImport() {
31     $bundles = [];
32     $destination = new EntityTestDestination([], '', [],
33       $this->migration->reveal(),
34       $this->storage->reveal(),
35       $bundles,
36       $this->entityManager->reveal(),
37       $this->prophesize(FieldTypePluginManagerInterface::class)->reveal());
38     $entity = $this->prophesize(ContentEntityInterface::class);
39     // Assert that save is called.
40     $entity->save()
41       ->shouldBeCalledTimes(1);
42     // Set an id for the entity
43     $entity->id()
44       ->willReturn(5);
45     $destination->setEntity($entity->reveal());
46     // Ensure the id is saved entity id is returned from import.
47     $this->assertEquals([5], $destination->import(new Row()));
48     // Assert that import set the rollback action.
49     $this->assertEquals(MigrateIdMapInterface::ROLLBACK_DELETE, $destination->rollbackAction());
50   }
51
52   /**
53    * Test row skipping when we can't get an entity to save.
54    *
55    * @covers ::import
56    */
57   public function testImportEntityLoadFailure() {
58     $bundles = [];
59     $destination = new EntityTestDestination([], '', [],
60       $this->migration->reveal(),
61       $this->storage->reveal(),
62       $bundles,
63       $this->entityManager->reveal(),
64       $this->prophesize(FieldTypePluginManagerInterface::class)->reveal());
65     $destination->setEntity(FALSE);
66     $this->setExpectedException(MigrateException::class, 'Unable to get entity');
67     $destination->import(new Row());
68   }
69
70   /**
71    * Test that translation destination fails for untranslatable entities.
72    */
73   public function testUntranslatable() {
74     // An entity type without a language.
75     $this->entityType->getKey('langcode')->willReturn('');
76     $this->entityType->getKey('id')->willReturn('id');
77     $this->entityManager->getBaseFieldDefinitions('foo')
78       ->willReturn(['id' => BaseFieldDefinitionTest::create('integer')]);
79
80     $destination = new EntityTestDestination(
81       ['translations' => TRUE],
82       '',
83       [],
84       $this->migration->reveal(),
85       $this->storage->reveal(),
86       [],
87       $this->entityManager->reveal(),
88       $this->prophesize(FieldTypePluginManagerInterface::class)->reveal()
89     );
90     $this->setExpectedException(MigrateException::class, 'The "foo" entity type does not support translations.');
91     $destination->getIds();
92   }
93
94 }
95
96 /**
97  * Stub class for testing EntityContentBase methods.
98  *
99  * We want to test things without testing the base class implementations.
100  */
101 class EntityTestDestination extends EntityContentBase {
102
103   private $entity = NULL;
104
105   public function setEntity($entity) {
106     $this->entity = $entity;
107   }
108
109   protected function getEntity(Row $row, array $old_destination_id_values) {
110     return $this->entity;
111   }
112
113   public static function getEntityTypeId($plugin_id) {
114     return 'foo';
115   }
116
117 }