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