Updated to Drupal 8.5. Core Media not yet in use.
[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\Entity\EntityManagerInterface;
12 use Drupal\Core\Entity\EntityStorageInterface;
13 use Drupal\Core\Entity\EntityTypeInterface;
14 use Drupal\Core\Field\BaseFieldDefinition;
15 use Drupal\Core\Field\FieldTypePluginManagerInterface;
16 use Drupal\migrate\MigrateException;
17 use Drupal\migrate\Plugin\MigrationInterface;
18 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
19 use Drupal\migrate\Plugin\MigrateIdMapInterface;
20 use Drupal\migrate\Row;
21 use Drupal\Tests\UnitTestCase;
22
23 /**
24  * Tests base entity migration destination functionality.
25  *
26  * @coversDefaultClass \Drupal\migrate\Plugin\migrate\destination\EntityContentBase
27  * @group migrate
28  */
29 class EntityContentBaseTest extends UnitTestCase {
30
31   /**
32    * @var \Drupal\migrate\Plugin\MigrationInterface
33    */
34   protected $migration;
35
36   /**
37    * @var \Drupal\Core\Entity\EntityStorageInterface
38    */
39   protected $storage;
40
41   /**
42    * @var \Drupal\Core\Entity\EntityTypeInterface
43    */
44   protected $entityType;
45
46   /**
47    * @var \Drupal\Core\Entity\EntityManagerInterface
48    */
49   protected $entityManager;
50
51   /**
52    * {@inheritdoc}
53    */
54   protected function setUp() {
55     parent::setUp();
56
57     $this->migration = $this->prophesize(MigrationInterface::class);
58     $this->storage = $this->prophesize(EntityStorageInterface::class);
59
60     $this->entityType = $this->prophesize(EntityTypeInterface::class);
61     $this->entityType->getPluralLabel()->willReturn('wonkiness');
62     $this->storage->getEntityType()->willReturn($this->entityType->reveal());
63
64     $this->entityManager = $this->prophesize(EntityManagerInterface::class);
65   }
66
67   /**
68    * Test basic entity save.
69    *
70    * @covers ::import
71    */
72   public function testImport() {
73     $bundles = [];
74     $destination = new EntityTestDestination([], '', [],
75       $this->migration->reveal(),
76       $this->storage->reveal(),
77       $bundles,
78       $this->entityManager->reveal(),
79       $this->prophesize(FieldTypePluginManagerInterface::class)->reveal());
80     $entity = $this->prophesize(ContentEntityInterface::class);
81     // Assert that save is called.
82     $entity->save()
83       ->shouldBeCalledTimes(1);
84     // Set an id for the entity
85     $entity->id()
86       ->willReturn(5);
87     $destination->setEntity($entity->reveal());
88     // Ensure the id is saved entity id is returned from import.
89     $this->assertEquals([5], $destination->import(new Row()));
90     // Assert that import set the rollback action.
91     $this->assertEquals(MigrateIdMapInterface::ROLLBACK_DELETE, $destination->rollbackAction());
92   }
93
94   /**
95    * Test row skipping when we can't get an entity to save.
96    *
97    * @covers ::import
98    */
99   public function testImportEntityLoadFailure() {
100     $bundles = [];
101     $destination = new EntityTestDestination([], '', [],
102       $this->migration->reveal(),
103       $this->storage->reveal(),
104       $bundles,
105       $this->entityManager->reveal(),
106       $this->prophesize(FieldTypePluginManagerInterface::class)->reveal());
107     $destination->setEntity(FALSE);
108     $this->setExpectedException(MigrateException::class, 'Unable to get entity');
109     $destination->import(new Row());
110   }
111
112   /**
113    * Test that translation destination fails for untranslatable entities.
114    */
115   public function testUntranslatable() {
116     // An entity type without a language.
117     $this->entityType->getKey('langcode')->willReturn('');
118     $this->entityType->getKey('id')->willReturn('id');
119     $this->entityManager->getBaseFieldDefinitions('foo')
120       ->willReturn(['id' => BaseFieldDefinitionTest::create('integer')]);
121
122     $destination = new EntityTestDestination(
123       ['translations' => TRUE],
124       '',
125       [],
126       $this->migration->reveal(),
127       $this->storage->reveal(),
128       [],
129       $this->entityManager->reveal(),
130       $this->prophesize(FieldTypePluginManagerInterface::class)->reveal()
131     );
132     $this->setExpectedException(MigrateException::class, 'This entity type does not support translation');
133     $destination->getIds();
134   }
135
136 }
137
138 /**
139  * Stub class for testing EntityContentBase methods.
140  *
141  * We want to test things without testing the base class implementations.
142  */
143 class EntityTestDestination extends EntityContentBase {
144
145   private $entity = NULL;
146
147   public function setEntity($entity) {
148     $this->entity = $entity;
149   }
150
151   protected function getEntity(Row $row, array $old_destination_id_values) {
152     return $this->entity;
153   }
154
155   public static function getEntityTypeId($plugin_id) {
156     return 'foo';
157   }
158
159 }
160
161 /**
162  * Stub class for BaseFieldDefinition.
163  */
164 class BaseFieldDefinitionTest extends BaseFieldDefinition {
165
166   public static function create($type) {
167     return new static([]);
168   }
169
170   public function getSettings() {
171     return [];
172   }
173
174   public function getType() {
175     return 'integer';
176   }
177
178 }