From 595b233e93b21f579dd18528faf3bd0f2d1d55a9 Mon Sep 17 00:00:00 2001
From: damiankloip <damiankloip@1037976.no-reply.drupal.org>
Date: Sat, 8 Sep 2012 11:37:30 +0200
Subject: [PATCH] Issue #1777466 by damiankloip, tim.plunkett: Fixed View copy
 method doesn't work.

---
 lib/Drupal/views/Tests/ViewTest.php | 39 +++++++++++++++++++++++++++++
 lib/Drupal/views/View.php           | 18 +++++++------
 2 files changed, 50 insertions(+), 7 deletions(-)

diff --git a/lib/Drupal/views/Tests/ViewTest.php b/lib/Drupal/views/Tests/ViewTest.php
index 17ee4f3c4c38..3197734136cf 100644
--- a/lib/Drupal/views/Tests/ViewTest.php
+++ b/lib/Drupal/views/Tests/ViewTest.php
@@ -105,6 +105,45 @@ function testValidate() {
     // - Multiple displays are validating and the errors are merged together.
   }
 
+  /**
+   * Tests the createDuplicate() View method.
+   */
+  public function testCreateDuplicate() {
+    $view = views_get_view('archive');
+    $copy = $view->createDuplicate();
+
+    $this->assertTrue($copy instanceof View, 'The copied object is a View.');
+
+    // Check that the original view and the copy have different uuids.
+    $this->assertNotIdentical($view->uuid(), $copy->uuid(), 'The copied view has a new uuid.');
+
+    // Check the 'name' (id) is using the View objects default value ('') as it
+    // gets unset.
+    $this->assertIdentical($copy->id(), '', 'The ID has been reset.');
+
+    // Check the other properties.
+    // @todo Create a reusable property on the base test class for these?
+    $config_properties = array(
+      'disabled',
+      'api_version',
+      'description',
+      'tag',
+      'base_table',
+      'human_name',
+      'core',
+    );
+
+    foreach ($config_properties as $property) {
+      $this->assertIdentical($view->{$property}, $copy->{$property}, format_string('@property property is identical.', array('@property' => $property)));
+    }
+
+    // Check the displays are the same.
+    foreach ($view->display as $id => $display) {
+      // assertIdentical will not work here.
+      $this->assertEqual($display, $copy->display[$id], format_string('The @display display has been copied correctly.', array('@display' => $id)));
+    }
+  }
+
   /**
    * Overrides Drupal\views\Tests\ViewTestBase::getBasicView().
    */
diff --git a/lib/Drupal/views/View.php b/lib/Drupal/views/View.php
index 7247c08be652..1d9aadbcb9a9 100644
--- a/lib/Drupal/views/View.php
+++ b/lib/Drupal/views/View.php
@@ -1907,15 +1907,19 @@ public function endQueryCapture() {
   }
 
   /**
-   * Make a copy of this view that has been sanitized of all database IDs
-   * and handlers and other stuff.
+   * Overrides Drupal\entity\Entity::createDuplicate().
    *
-   * I'd call this clone() but it's reserved.
+   * Makes a copy of this view that has been sanitized of handlers, any runtime
+   *  data, ID, and UUID.
    */
-  public function copy() {
-    $code = $this->export();
-    eval($code);
-    return $view;
+  public function createDuplicate() {
+    $data = config('views.view.' . $this->id())->get();
+
+    // Reset the name and UUID.
+    unset($data['name']);
+    unset($data['uuid']);
+
+    return entity_create('view', $data);
   }
 
   /**
-- 
GitLab