Commit 80e1ff2c authored by catch's avatar catch

Issue #1792380 by theo_: Fixed DatabaseCondition not cloning SelectQuery value object.

parent 0244e9c8
......@@ -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']);
}
}
}
}
......
<?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');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment