From 65898a54a61865c4badf8d2cda97e17731a4976e Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Fri, 26 Aug 2016 14:20:00 +0100
Subject: [PATCH] Issue #2706947 by Jo Fitzgerald: Field migration rollback
 fails

---
 .../Kernel/Migrate/d7/RollbackFieldTest.php   | 77 +++++++++++++++++++
 .../destination/EntityFieldStorageConfig.php  |  8 ++
 2 files changed, 85 insertions(+)
 create mode 100644 core/modules/field/tests/src/Kernel/Migrate/d7/RollbackFieldTest.php

diff --git a/core/modules/field/tests/src/Kernel/Migrate/d7/RollbackFieldTest.php b/core/modules/field/tests/src/Kernel/Migrate/d7/RollbackFieldTest.php
new file mode 100644
index 000000000000..7c5c6fd41e5d
--- /dev/null
+++ b/core/modules/field/tests/src/Kernel/Migrate/d7/RollbackFieldTest.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace Drupal\Tests\field\Kernel\Migrate\d7;
+
+use Drupal\field\Entity\FieldStorageConfig;
+use Drupal\migrate\MigrateExecutable;
+
+/**
+ * Migrates and rolls back Drupal 7 fields.
+ *
+ * @group field
+ */
+class RollbackFieldTest extends MigrateFieldTest {
+
+  /**
+   * Tests migrating D7 fields to field_storage_config entities, then rolling back.
+   */
+  public function testFields() {
+    // Test that the fields have migrated (prior to rollback).
+    parent::testFields();
+
+    $this->executeRollback('d7_field');
+
+    // Check that fields have been rolled back.
+    $rolled_back_field_ids = [
+      'comment.field_integer',
+      'node.taxonomy_forums',
+      'node.field_integer',
+      'node.field_tags',
+      'node.field_term_reference',
+      'node.field_text_list',
+      'node.field_text',
+      'node.field_phone',
+      'node.field_file',
+      'node.field_images',
+      'node.field_image',
+      'node.field_long_text',
+      'node.field_date_with_end_time',
+      'node.field_integer_list',
+      'node.field_date',
+      'node.field_link',
+      'node.field_float',
+      'node.field_boolean',
+      'node.field_email',
+      'user.field_file',
+    ];
+    foreach ($rolled_back_field_ids as $field_id) {
+      $this->assertNull(FieldStorageConfig::load($field_id));
+    }
+
+    // Check that fields that should persist have not been rolled back.
+    $non_rolled_back_field_ids = [
+      'node.body',
+      'comment.comment_body',
+    ];
+    foreach ($non_rolled_back_field_ids as $field_id) {
+      $this->assertNotNull(FieldStorageConfig::load($field_id));
+    }
+  }
+
+  /**
+   * Executes a single rollback.
+   *
+   * @param string|\Drupal\migrate\Plugin\MigrationInterface $migration
+   *   The migration to rollback, or its ID.
+   */
+  protected function executeRollback($migration) {
+    if (is_string($migration)) {
+      $this->migration = $this->getMigration($migration);
+    }
+    else {
+      $this->migration = $migration;
+    }
+    (new MigrateExecutable($this->migration, $this))->rollback();
+  }
+
+}
diff --git a/core/modules/migrate/src/Plugin/migrate/destination/EntityFieldStorageConfig.php b/core/modules/migrate/src/Plugin/migrate/destination/EntityFieldStorageConfig.php
index 6851f2708047..7ad01045a61d 100644
--- a/core/modules/migrate/src/Plugin/migrate/destination/EntityFieldStorageConfig.php
+++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityFieldStorageConfig.php
@@ -20,4 +20,12 @@ public function getIds() {
     return $ids;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function rollback(array $destination_identifier) {
+    $destination_identifier = implode('.', $destination_identifier);
+    parent::rollback(array($destination_identifier));
+  }
+
 }
-- 
GitLab