Chris@0: installEntitySchema('user'); Chris@0: $this->installEntitySchema('taxonomy_vocabulary'); Chris@0: $this->installEntitySchema('taxonomy_term'); Chris@0: $this->installConfig(['taxonomy']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests rolling back configuration entity translations. Chris@0: */ Chris@0: public function testConfigEntityRollback() { Chris@0: // We use vocabularies to demonstrate importing and rolling back Chris@0: // configuration entities with translations. First, import vocabularies. Chris@0: $vocabulary_data_rows = [ Chris@0: ['id' => '1', 'name' => 'categories', 'weight' => '2'], Chris@0: ['id' => '2', 'name' => 'tags', 'weight' => '1'], Chris@0: ]; Chris@0: $ids = ['id' => ['type' => 'integer']]; Chris@0: $definition = [ Chris@0: 'id' => 'vocabularies', Chris@0: 'migration_tags' => ['Import and rollback test'], Chris@0: 'source' => [ Chris@0: 'plugin' => 'embedded_data', Chris@0: 'data_rows' => $vocabulary_data_rows, Chris@0: 'ids' => $ids, Chris@0: ], Chris@0: 'process' => [ Chris@0: 'vid' => 'id', Chris@0: 'name' => 'name', Chris@0: 'weight' => 'weight', Chris@0: ], Chris@0: 'destination' => ['plugin' => 'entity:taxonomy_vocabulary'], Chris@0: ]; Chris@0: Chris@0: /** @var \Drupal\migrate\Plugin\Migration $vocabulary_migration */ Chris@0: $vocabulary_migration = \Drupal::service('plugin.manager.migration') Chris@0: ->createStubMigration($definition); Chris@0: $vocabulary_id_map = $vocabulary_migration->getIdMap(); Chris@0: Chris@0: $this->assertTrue($vocabulary_migration->getDestinationPlugin() Chris@0: ->supportsRollback()); Chris@0: Chris@0: // Import and validate vocabulary config entities were created. Chris@0: $vocabulary_executable = new MigrateExecutable($vocabulary_migration, $this); Chris@0: $vocabulary_executable->import(); Chris@0: foreach ($vocabulary_data_rows as $row) { Chris@0: /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */ Chris@0: $vocabulary = Vocabulary::load($row['id']); Chris@0: $this->assertTrue($vocabulary); Chris@0: $map_row = $vocabulary_id_map->getRowBySource(['id' => $row['id']]); Chris@0: $this->assertNotNull($map_row['destid1']); Chris@0: } Chris@0: Chris@0: // Second, import translations of the vocabulary name property. Chris@0: $vocabulary_i18n_data_rows = [ Chris@0: [ Chris@0: 'id' => '1', Chris@0: 'name' => '1', Chris@0: 'language' => 'fr', Chris@0: 'property' => 'name', Chris@17: 'translation' => 'fr - categories', Chris@0: ], Chris@0: [ Chris@0: 'id' => '2', Chris@0: 'name' => '2', Chris@0: 'language' => 'fr', Chris@0: 'property' => 'name', Chris@17: 'translation' => 'fr - tags', Chris@0: ], Chris@0: ]; Chris@0: $ids = [ Chris@0: 'id' => ['type' => 'integer'], Chris@0: 'language' => ['type' => 'string'], Chris@0: ]; Chris@0: $definition = [ Chris@0: 'id' => 'i18n_vocabularies', Chris@0: 'migration_tags' => ['Import and rollback test'], Chris@0: 'source' => [ Chris@0: 'plugin' => 'embedded_data', Chris@0: 'data_rows' => $vocabulary_i18n_data_rows, Chris@0: 'ids' => $ids, Chris@0: 'constants' => [ Chris@0: 'name' => 'name', Chris@17: ], Chris@0: ], Chris@0: 'process' => [ Chris@0: 'vid' => 'id', Chris@0: 'langcode' => 'language', Chris@0: 'property' => 'constants/name', Chris@0: 'translation' => 'translation', Chris@0: ], Chris@0: 'destination' => [ Chris@0: 'plugin' => 'entity:taxonomy_vocabulary', Chris@0: 'translations' => 'true', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $vocabulary_i18n__migration = \Drupal::service('plugin.manager.migration') Chris@0: ->createStubMigration($definition); Chris@0: $vocabulary_i18n_id_map = $vocabulary_i18n__migration->getIdMap(); Chris@0: Chris@0: $this->assertTrue($vocabulary_i18n__migration->getDestinationPlugin() Chris@0: ->supportsRollback()); Chris@0: Chris@0: // Import and validate vocabulary config entities were created. Chris@0: $vocabulary_i18n_executable = new MigrateExecutable($vocabulary_i18n__migration, $this); Chris@0: $vocabulary_i18n_executable->import(); Chris@0: Chris@0: $language_manager = \Drupal::service('language_manager'); Chris@0: foreach ($vocabulary_i18n_data_rows as $row) { Chris@0: $langcode = $row['language']; Chris@0: $id = 'taxonomy.vocabulary.' . $row['id']; Chris@0: /** @var \Drupal\language\Config\LanguageConfigOverride $config_translation */ Chris@0: $config_translation = $language_manager->getLanguageConfigOverride($langcode, $id); Chris@0: $this->assertSame($row['translation'], $config_translation->get('name')); Chris@0: $map_row = $vocabulary_i18n_id_map->getRowBySource(['id' => $row['id'], 'language' => $row['language']]); Chris@0: $this->assertNotNull($map_row['destid1']); Chris@0: } Chris@0: Chris@0: // Perform the rollback and confirm the translation was deleted and the map Chris@0: // table row removed. Chris@0: $vocabulary_i18n_executable->rollback(); Chris@0: foreach ($vocabulary_i18n_data_rows as $row) { Chris@0: $langcode = $row['language']; Chris@0: $id = 'taxonomy.vocabulary.' . $row['id']; Chris@0: /** @var \Drupal\language\Config\LanguageConfigOverride $config_translation */ Chris@0: $config_translation = $language_manager->getLanguageConfigOverride($langcode, $id); Chris@0: $this->assertNull($config_translation->get('name')); Chris@0: $map_row = $vocabulary_i18n_id_map->getRowBySource(['id' => $row['id'], 'language' => $row['language']]); Chris@0: $this->assertFalse($map_row); Chris@0: } Chris@0: Chris@0: // Confirm the original vocabulary still exists. Chris@0: foreach ($vocabulary_data_rows as $row) { Chris@0: /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */ Chris@0: $vocabulary = Vocabulary::load($row['id']); Chris@0: $this->assertTrue($vocabulary); Chris@0: $map_row = $vocabulary_id_map->getRowBySource(['id' => $row['id']]); Chris@0: $this->assertNotNull($map_row['destid1']); Chris@0: } Chris@0: Chris@0: } Chris@0: Chris@0: }