Chris@0: set('entity_test.required_default_field', TRUE); Chris@14: \Drupal::state()->set('entity_test.required_multi_default_field', TRUE); Chris@0: $this->installEntitySchema('entity_test_mul'); Chris@0: Chris@0: ConfigurableLanguage::createFromLangcode('en')->save(); Chris@0: ConfigurableLanguage::createFromLangcode('fr')->save(); Chris@0: Chris@0: $this->storage = $this->container->get('entity.manager')->getStorage('entity_test_mul'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check the existing translations of an entity. Chris@0: * Chris@0: * @param int $id Chris@0: * The entity ID. Chris@0: * @param string $default Chris@0: * The expected default translation language code. Chris@0: * @param string[] $others Chris@0: * The expected other translation language codes. Chris@0: */ Chris@0: protected function assertTranslations($id, $default, $others = []) { Chris@0: $entity = $this->storage->load($id); Chris@0: $this->assertTrue($entity, "Entity exists"); Chris@0: $this->assertEquals($default, $entity->language()->getId(), "Entity default translation"); Chris@0: $translations = array_keys($entity->getTranslationLanguages(FALSE)); Chris@0: sort($others); Chris@0: sort($translations); Chris@0: $this->assertEquals($others, $translations, "Entity translations"); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Create the destination plugin to test. Chris@0: * Chris@0: * @param array $configuration Chris@0: * The plugin configuration. Chris@0: */ Chris@0: protected function createDestination(array $configuration) { Chris@0: $this->destination = new EntityContentBase( Chris@0: $configuration, Chris@0: 'fake_plugin_id', Chris@0: [], Chris@0: $this->getMock(MigrationInterface::class), Chris@0: $this->storage, Chris@0: [], Chris@0: $this->container->get('entity.manager'), Chris@0: $this->container->get('plugin.manager.field.field_type') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test importing and rolling back translated entities. Chris@0: */ Chris@0: public function testTranslated() { Chris@0: // Create a destination. Chris@0: $this->createDestination(['translations' => TRUE]); Chris@0: Chris@0: // Create some pre-existing entities. Chris@0: $this->storage->create(['id' => 1, 'langcode' => 'en'])->save(); Chris@0: $this->storage->create(['id' => 2, 'langcode' => 'fr'])->save(); Chris@0: $translated = $this->storage->create(['id' => 3, 'langcode' => 'en']); Chris@0: $translated->save(); Chris@0: $translated->addTranslation('fr')->save(); Chris@0: Chris@0: // Pre-assert that things are as expected. Chris@0: $this->assertTranslations(1, 'en'); Chris@0: $this->assertTranslations(2, 'fr'); Chris@0: $this->assertTranslations(3, 'en', ['fr']); Chris@0: $this->assertFalse($this->storage->load(4)); Chris@0: Chris@0: $destination_rows = [ Chris@0: // Existing default translation. Chris@0: ['id' => 1, 'langcode' => 'en', 'action' => MigrateIdMapInterface::ROLLBACK_PRESERVE], Chris@0: // New translation. Chris@0: ['id' => 2, 'langcode' => 'en', 'action' => MigrateIdMapInterface::ROLLBACK_DELETE], Chris@0: // Existing non-default translation. Chris@0: ['id' => 3, 'langcode' => 'fr', 'action' => MigrateIdMapInterface::ROLLBACK_PRESERVE], Chris@0: // Brand new row. Chris@0: ['id' => 4, 'langcode' => 'fr', 'action' => MigrateIdMapInterface::ROLLBACK_DELETE], Chris@0: ]; Chris@0: $rollback_actions = []; Chris@0: Chris@0: // Import some rows. Chris@0: foreach ($destination_rows as $idx => $destination_row) { Chris@0: $row = new Row(); Chris@0: foreach ($destination_row as $key => $value) { Chris@0: $row->setDestinationProperty($key, $value); Chris@0: } Chris@0: $this->destination->import($row); Chris@0: Chris@0: // Check that the rollback action is correct, and save it. Chris@0: $this->assertEquals($destination_row['action'], $this->destination->rollbackAction()); Chris@0: $rollback_actions[$idx] = $this->destination->rollbackAction(); Chris@0: } Chris@0: Chris@0: $this->assertTranslations(1, 'en'); Chris@0: $this->assertTranslations(2, 'fr', ['en']); Chris@0: $this->assertTranslations(3, 'en', ['fr']); Chris@0: $this->assertTranslations(4, 'fr'); Chris@0: Chris@0: // Rollback the rows. Chris@0: foreach ($destination_rows as $idx => $destination_row) { Chris@0: if ($rollback_actions[$idx] == MigrateIdMapInterface::ROLLBACK_DELETE) { Chris@0: $this->destination->rollback($destination_row); Chris@0: } Chris@0: } Chris@0: Chris@0: // No change, update of existing translation. Chris@0: $this->assertTranslations(1, 'en'); Chris@0: // Remove added translation. Chris@0: $this->assertTranslations(2, 'fr'); Chris@0: // No change, update of existing translation. Chris@0: $this->assertTranslations(3, 'en', ['fr']); Chris@0: // No change, can't remove default translation. Chris@0: $this->assertTranslations(4, 'fr'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests creation of ID columns table with definitions taken from entity type. Chris@0: */ Chris@0: public function testEntityWithStringId() { Chris@0: $this->enableModules(['migrate_entity_test']); Chris@0: $this->installEntitySchema('migrate_string_id_entity_test'); Chris@0: Chris@0: $definition = [ Chris@0: 'source' => [ Chris@0: 'plugin' => 'embedded_data', Chris@0: 'data_rows' => [ Chris@0: ['id' => 123, 'version' => 'foo'], Chris@0: // This integer needs an 'int' schema with 'big' size. If 'destid1' Chris@0: // is not correctly taking the definition from the destination entity Chris@0: // type, the import will fail with a SQL exception. Chris@0: ['id' => 123456789012, 'version' => 'bar'], Chris@0: ], Chris@0: 'ids' => [ Chris@0: 'id' => ['type' => 'integer', 'size' => 'big'], Chris@0: 'version' => ['type' => 'string'], Chris@0: ], Chris@0: ], Chris@0: 'process' => [ Chris@0: 'id' => 'id', Chris@0: 'version' => 'version', Chris@0: ], Chris@0: 'destination' => [ Chris@0: 'plugin' => 'entity:migrate_string_id_entity_test', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition); Chris@14: $executable = new MigrateExecutable($migration); Chris@0: $result = $executable->import(); Chris@0: $this->assertEquals(MigrationInterface::RESULT_COMPLETED, $result); Chris@0: Chris@0: /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map_plugin */ Chris@0: $id_map_plugin = $migration->getIdMap(); Chris@0: Chris@0: // Check that the destination has been stored. Chris@0: $map_row = $id_map_plugin->getRowBySource(['id' => 123, 'version' => 'foo']); Chris@0: $this->assertEquals(123, $map_row['destid1']); Chris@0: $map_row = $id_map_plugin->getRowBySource(['id' => 123456789012, 'version' => 'bar']); Chris@0: $this->assertEquals(123456789012, $map_row['destid1']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests empty destinations. Chris@0: */ Chris@0: public function testEmptyDestinations() { Chris@0: $this->enableModules(['migrate_entity_test']); Chris@0: $this->installEntitySchema('migrate_string_id_entity_test'); Chris@0: Chris@0: $definition = [ Chris@0: 'source' => [ Chris@0: 'plugin' => 'embedded_data', Chris@0: 'data_rows' => [ Chris@0: ['id' => 123, 'version' => 'foo'], Chris@0: // This integer needs an 'int' schema with 'big' size. If 'destid1' Chris@0: // is not correctly taking the definition from the destination entity Chris@0: // type, the import will fail with an SQL exception. Chris@0: ['id' => 123456789012, 'version' => 'bar'], Chris@0: ], Chris@0: 'ids' => [ Chris@0: 'id' => ['type' => 'integer', 'size' => 'big'], Chris@0: 'version' => ['type' => 'string'], Chris@0: ], Chris@0: 'constants' => ['null' => NULL], Chris@0: ], Chris@0: 'process' => [ Chris@0: 'id' => 'id', Chris@0: 'version' => 'version', Chris@0: ], Chris@0: 'destination' => [ Chris@0: 'plugin' => 'entity:migrate_string_id_entity_test', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $migration = \Drupal::service('plugin.manager.migration') Chris@0: ->createStubMigration($definition); Chris@14: $executable = new MigrateExecutable($migration); Chris@0: $executable->import(); Chris@0: Chris@0: /** @var \Drupal\migrate_entity_test\Entity\StringIdEntityTest $entity */ Chris@0: $entity = StringIdEntityTest::load('123'); Chris@0: $this->assertSame('foo', $entity->version->value); Chris@0: $entity = StringIdEntityTest::load('123456789012'); Chris@0: $this->assertSame('bar', $entity->version->value); Chris@0: Chris@0: // Rerun the migration forcing the version to NULL. Chris@0: $definition['process'] = [ Chris@0: 'id' => 'id', Chris@0: 'version' => 'constants/null', Chris@0: ]; Chris@0: Chris@0: $migration = \Drupal::service('plugin.manager.migration') Chris@0: ->createStubMigration($definition); Chris@14: $executable = new MigrateExecutable($migration); Chris@0: $executable->import(); Chris@0: Chris@0: /** @var \Drupal\migrate_entity_test\Entity\StringIdEntityTest $entity */ Chris@0: $entity = StringIdEntityTest::load('123'); Chris@0: $this->assertNull($entity->version->value); Chris@0: $entity = StringIdEntityTest::load('123456789012'); Chris@0: $this->assertNull($entity->version->value); Chris@0: } Chris@0: Chris@14: /** Chris@14: * Tests stub rows. Chris@14: */ Chris@14: public function testStubRows() { Chris@14: // Create a destination. Chris@14: $this->createDestination([]); Chris@14: Chris@14: // Import a stub row. Chris@14: $row = new Row([], [], TRUE); Chris@14: $row->setDestinationProperty('type', 'test'); Chris@14: $ids = $this->destination->import($row); Chris@14: $this->assertCount(1, $ids); Chris@14: Chris@14: // Make sure the entity was saved. Chris@14: $entity = EntityTestMul::load(reset($ids)); Chris@14: $this->assertInstanceOf(EntityTestMul::class, $entity); Chris@14: // Make sure the default value was applied to the required fields. Chris@14: $single_field_name = 'required_default_field'; Chris@14: $single_default_value = $entity->getFieldDefinition($single_field_name)->getDefaultValueLiteral(); Chris@14: $this->assertSame($single_default_value, $entity->get($single_field_name)->getValue()); Chris@14: Chris@14: $multi_field_name = 'required_multi_default_field'; Chris@14: $multi_default_value = $entity->getFieldDefinition($multi_field_name)->getDefaultValueLiteral(); Chris@14: $count = 3; Chris@14: $this->assertCount($count, $multi_default_value); Chris@14: for ($i = 0; $i < $count; ++$i) { Chris@14: $this->assertSame($multi_default_value[$i], $entity->get($multi_field_name)->get($i)->getValue()); Chris@14: } Chris@14: } Chris@14: Chris@0: }