From 80e1ff2c0fb8ff8428296cfa0ebf31c1c0919fbb Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Wed, 2 Jan 2013 12:09:57 +0000
Subject: [PATCH] Issue #1792380 by theo_: Fixed DatabaseCondition not cloning
 SelectQuery value object.

---
 .../Drupal/Core/Database/Query/Condition.php  |  9 +++-
 .../system/Tests/Database/SelectCloneTest.php | 47 +++++++++++++++++++
 2 files changed, 54 insertions(+), 2 deletions(-)
 create mode 100644 core/modules/system/lib/Drupal/system/Tests/Database/SelectCloneTest.php

diff --git a/core/lib/Drupal/Core/Database/Query/Condition.php b/core/lib/Drupal/Core/Database/Query/Condition.php
index 784ed78309c6..c23ad7695f13 100644
--- a/core/lib/Drupal/Core/Database/Query/Condition.php
+++ b/core/lib/Drupal/Core/Database/Query/Condition.php
@@ -259,8 +259,13 @@ public function __toString() {
   function __clone() {
     $this->changed = TRUE;
     foreach ($this->conditions as $key => $condition) {
-      if ($key !== '#conjunction' && $condition['field'] instanceOf ConditionInterface) {
-        $this->conditions[$key]['field'] = clone($condition['field']);
+      if ($key !== '#conjunction') {
+        if ($condition['field'] instanceOf ConditionInterface) {
+          $this->conditions[$key]['field'] = clone($condition['field']);
+        }
+        if ($condition['value'] instanceOf SelectInterface) {
+          $this->conditions[$key]['value'] = clone($condition['value']);
+        }
       }
     }
   }
diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/SelectCloneTest.php b/core/modules/system/lib/Drupal/system/Tests/Database/SelectCloneTest.php
new file mode 100644
index 000000000000..63e825a40516
--- /dev/null
+++ b/core/modules/system/lib/Drupal/system/Tests/Database/SelectCloneTest.php
@@ -0,0 +1,47 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\system\Tests\Database\SelectCloneTest.
+ */
+
+namespace Drupal\system\Tests\Database;
+
+/**
+ * Test cloning Select queries.
+ */
+class SelectCloneTest extends DatabaseTestBase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Select tests, cloning',
+      'description' => 'Test cloning Select queries.',
+      'group' => 'Database',
+    );
+  }
+
+  /**
+   * Test that subqueries as value within conditions are cloned properly.
+   */
+  function testSelectConditionSubQueryCloning() {
+    $subquery = db_select('test', 't');
+    $subquery->addField('t', 'id', 'id');
+    $subquery->condition('age', 28, '<');
+
+    $query = db_select('test', 't');
+    $query->addField('t', 'name', 'name');
+    $query->condition('id', $subquery, 'IN');
+
+    $clone = clone $query;
+    // Cloned query should not be altered by the following modification
+    // happening on original query.
+    $subquery->condition('age', 25, '>');
+
+    $clone_result = $clone->countQuery()->execute()->fetchField();
+    $query_result = $query->countQuery()->execute()->fetchField();
+
+    // Make sure the cloned query has not been modified
+    $this->assertEqual(3, $clone_result, 'The cloned query returns the expected number of rows');
+    $this->assertEqual(2, $query_result, 'The query returns the expected number of rows');
+  }
+}
-- 
GitLab