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 and content entities. Chris@0: */ Chris@0: public function testRollback() { Chris@0: // We use vocabularies to demonstrate importing and rolling back Chris@0: // configuration entities. 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: $vocabulary_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition); Chris@0: $vocabulary_id_map = $vocabulary_migration->getIdMap(); Chris@0: Chris@0: $this->assertTrue($vocabulary_migration->getDestinationPlugin()->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@12: /** @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: // We use taxonomy terms to demonstrate importing and rolling back content Chris@0: // entities. Chris@0: $term_data_rows = [ Chris@0: ['id' => '1', 'vocab' => '1', 'name' => 'music'], Chris@0: ['id' => '2', 'vocab' => '2', 'name' => 'Bach'], Chris@0: ['id' => '3', 'vocab' => '2', 'name' => 'Beethoven'], Chris@0: ]; Chris@0: $ids = ['id' => ['type' => 'integer']]; Chris@0: $definition = [ Chris@0: 'id' => 'terms', Chris@0: 'migration_tags' => ['Import and rollback test'], Chris@0: 'source' => [ Chris@0: 'plugin' => 'embedded_data', Chris@0: 'data_rows' => $term_data_rows, Chris@0: 'ids' => $ids, Chris@0: ], Chris@0: 'process' => [ Chris@0: 'tid' => 'id', Chris@0: 'vid' => 'vocab', Chris@0: 'name' => 'name', Chris@0: ], Chris@0: 'destination' => ['plugin' => 'entity:taxonomy_term'], Chris@0: 'migration_dependencies' => ['required' => ['vocabularies']], Chris@0: ]; Chris@0: Chris@0: $term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition); Chris@0: $term_id_map = $term_migration->getIdMap(); Chris@0: Chris@0: $this->assertTrue($term_migration->getDestinationPlugin()->supportsRollback()); Chris@0: Chris@0: // Pre-create a term, to make sure it isn't deleted on rollback. Chris@0: $preserved_term_ids[] = 1; Chris@0: $new_term = Term::create(['tid' => 1, 'vid' => 1, 'name' => 'music']); Chris@0: $new_term->save(); Chris@0: Chris@0: // Import and validate term entities were created. Chris@0: $term_executable = new MigrateExecutable($term_migration, $this); Chris@0: $term_executable->import(); Chris@0: // Also explicitly mark one row to be preserved on rollback. Chris@0: $preserved_term_ids[] = 2; Chris@0: $map_row = $term_id_map->getRowBySource(['id' => 2]); Chris@0: $dummy_row = new Row(['id' => 2], $ids); Chris@0: $term_id_map->saveIdMapping($dummy_row, [$map_row['destid1']], Chris@0: $map_row['source_row_status'], MigrateIdMapInterface::ROLLBACK_PRESERVE); Chris@0: Chris@0: foreach ($term_data_rows as $row) { Chris@12: /** @var \Drupal\taxonomy\Entity\Term $term */ Chris@0: $term = Term::load($row['id']); Chris@0: $this->assertTrue($term); Chris@0: $map_row = $term_id_map->getRowBySource(['id' => $row['id']]); Chris@0: $this->assertNotNull($map_row['destid1']); Chris@0: } Chris@0: Chris@0: // Add a failed row to test if this can be rolled back without errors. Chris@0: $this->mockFailure($term_migration, ['id' => '4', 'vocab' => '2', 'name' => 'FAIL']); Chris@0: Chris@0: // Rollback and verify the entities are gone. Chris@0: $term_executable->rollback(); Chris@0: foreach ($term_data_rows as $row) { Chris@0: $term = Term::load($row['id']); Chris@0: if (in_array($row['id'], $preserved_term_ids)) { Chris@0: $this->assertNotNull($term); Chris@0: } Chris@0: else { Chris@0: $this->assertNull($term); Chris@0: } Chris@0: $map_row = $term_id_map->getRowBySource(['id' => $row['id']]); Chris@0: $this->assertFalse($map_row); Chris@0: } Chris@0: $vocabulary_executable->rollback(); Chris@0: foreach ($vocabulary_data_rows as $row) { Chris@0: $term = Vocabulary::load($row['id']); Chris@0: $this->assertNull($term); Chris@0: $map_row = $vocabulary_id_map->getRowBySource(['id' => $row['id']]); Chris@0: $this->assertFalse($map_row); Chris@0: } Chris@0: Chris@0: // Test that simple configuration is not rollbackable. Chris@0: $term_setting_rows = [ Chris@0: ['id' => 1, 'override_selector' => '0', 'terms_per_page_admin' => '10'], Chris@0: ]; Chris@0: $ids = ['id' => ['type' => 'integer']]; Chris@0: $definition = [ Chris@0: 'id' => 'taxonomy_settings', Chris@0: 'migration_tags' => ['Import and rollback test'], Chris@0: 'source' => [ Chris@0: 'plugin' => 'embedded_data', Chris@0: 'data_rows' => $term_setting_rows, Chris@0: 'ids' => $ids, Chris@0: ], Chris@0: 'process' => [ Chris@0: 'override_selector' => 'override_selector', Chris@0: 'terms_per_page_admin' => 'terms_per_page_admin', Chris@0: ], Chris@0: 'destination' => [ Chris@0: 'plugin' => 'config', Chris@0: 'config_name' => 'taxonomy.settings', Chris@0: ], Chris@0: 'migration_dependencies' => ['required' => ['vocabularies']], Chris@0: ]; Chris@0: Chris@0: $settings_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition); Chris@0: $this->assertFalse($settings_migration->getDestinationPlugin()->supportsRollback()); Chris@0: } Chris@0: Chris@0: }