comparison core/modules/migrate/tests/src/Kernel/MigrateEntityContentBaseTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\migrate\Kernel; 3 namespace Drupal\Tests\migrate\Kernel;
4 4
5 use Drupal\entity_test\Entity\EntityTestMul;
5 use Drupal\KernelTests\KernelTestBase; 6 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\language\Entity\ConfigurableLanguage; 7 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\migrate\MigrateExecutable; 8 use Drupal\migrate\MigrateExecutable;
8 use Drupal\migrate\MigrateMessage;
9 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase; 9 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
10 use Drupal\migrate\Plugin\MigrateIdMapInterface; 10 use Drupal\migrate\Plugin\MigrateIdMapInterface;
11 use Drupal\migrate\Plugin\MigrationInterface; 11 use Drupal\migrate\Plugin\MigrationInterface;
12 use Drupal\migrate\Row; 12 use Drupal\migrate\Row;
13 use Drupal\migrate_entity_test\Entity\StringIdEntityTest; 13 use Drupal\migrate_entity_test\Entity\StringIdEntityTest;
43 /** 43 /**
44 * {@inheritdoc} 44 * {@inheritdoc}
45 */ 45 */
46 protected function setUp() { 46 protected function setUp() {
47 parent::setUp(); 47 parent::setUp();
48
49 // Enable two required fields with default values: a single-value field and
50 // a multi-value field.
51 \Drupal::state()->set('entity_test.required_default_field', TRUE);
52 \Drupal::state()->set('entity_test.required_multi_default_field', TRUE);
48 $this->installEntitySchema('entity_test_mul'); 53 $this->installEntitySchema('entity_test_mul');
49 54
50 ConfigurableLanguage::createFromLangcode('en')->save(); 55 ConfigurableLanguage::createFromLangcode('en')->save();
51 ConfigurableLanguage::createFromLangcode('fr')->save(); 56 ConfigurableLanguage::createFromLangcode('fr')->save();
52 57
189 'plugin' => 'entity:migrate_string_id_entity_test', 194 'plugin' => 'entity:migrate_string_id_entity_test',
190 ], 195 ],
191 ]; 196 ];
192 197
193 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition); 198 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
194 $executable = new MigrateExecutable($migration, new MigrateMessage()); 199 $executable = new MigrateExecutable($migration);
195 $result = $executable->import(); 200 $result = $executable->import();
196 $this->assertEquals(MigrationInterface::RESULT_COMPLETED, $result); 201 $this->assertEquals(MigrationInterface::RESULT_COMPLETED, $result);
197 202
198 /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map_plugin */ 203 /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map_plugin */
199 $id_map_plugin = $migration->getIdMap(); 204 $id_map_plugin = $migration->getIdMap();
237 ], 242 ],
238 ]; 243 ];
239 244
240 $migration = \Drupal::service('plugin.manager.migration') 245 $migration = \Drupal::service('plugin.manager.migration')
241 ->createStubMigration($definition); 246 ->createStubMigration($definition);
242 $executable = new MigrateExecutable($migration, new MigrateMessage()); 247 $executable = new MigrateExecutable($migration);
243 $executable->import(); 248 $executable->import();
244 249
245 /** @var \Drupal\migrate_entity_test\Entity\StringIdEntityTest $entity */ 250 /** @var \Drupal\migrate_entity_test\Entity\StringIdEntityTest $entity */
246 $entity = StringIdEntityTest::load('123'); 251 $entity = StringIdEntityTest::load('123');
247 $this->assertSame('foo', $entity->version->value); 252 $this->assertSame('foo', $entity->version->value);
254 'version' => 'constants/null', 259 'version' => 'constants/null',
255 ]; 260 ];
256 261
257 $migration = \Drupal::service('plugin.manager.migration') 262 $migration = \Drupal::service('plugin.manager.migration')
258 ->createStubMigration($definition); 263 ->createStubMigration($definition);
259 $executable = new MigrateExecutable($migration, new MigrateMessage()); 264 $executable = new MigrateExecutable($migration);
260 $executable->import(); 265 $executable->import();
261 266
262 /** @var \Drupal\migrate_entity_test\Entity\StringIdEntityTest $entity */ 267 /** @var \Drupal\migrate_entity_test\Entity\StringIdEntityTest $entity */
263 $entity = StringIdEntityTest::load('123'); 268 $entity = StringIdEntityTest::load('123');
264 $this->assertNull($entity->version->value); 269 $this->assertNull($entity->version->value);
265 $entity = StringIdEntityTest::load('123456789012'); 270 $entity = StringIdEntityTest::load('123456789012');
266 $this->assertNull($entity->version->value); 271 $this->assertNull($entity->version->value);
267 } 272 }
268 273
274 /**
275 * Tests stub rows.
276 */
277 public function testStubRows() {
278 // Create a destination.
279 $this->createDestination([]);
280
281 // Import a stub row.
282 $row = new Row([], [], TRUE);
283 $row->setDestinationProperty('type', 'test');
284 $ids = $this->destination->import($row);
285 $this->assertCount(1, $ids);
286
287 // Make sure the entity was saved.
288 $entity = EntityTestMul::load(reset($ids));
289 $this->assertInstanceOf(EntityTestMul::class, $entity);
290 // Make sure the default value was applied to the required fields.
291 $single_field_name = 'required_default_field';
292 $single_default_value = $entity->getFieldDefinition($single_field_name)->getDefaultValueLiteral();
293 $this->assertSame($single_default_value, $entity->get($single_field_name)->getValue());
294
295 $multi_field_name = 'required_multi_default_field';
296 $multi_default_value = $entity->getFieldDefinition($multi_field_name)->getDefaultValueLiteral();
297 $count = 3;
298 $this->assertCount($count, $multi_default_value);
299 for ($i = 0; $i < $count; ++$i) {
300 $this->assertSame($multi_default_value[$i], $entity->get($multi_field_name)->get($i)->getValue());
301 }
302 }
303
269 } 304 }