Skip to content
Snippets Groups Projects
Commit ad244e2c authored by David Rothstein's avatar David Rothstein
Browse files

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

parent 7d76caba
No related branches found
No related tags found
No related merge requests found
......@@ -1898,8 +1898,13 @@ public function __toString() {
function __clone() {
$this->changed = TRUE;
foreach ($this->conditions as $key => $condition) {
if ($key !== '#conjunction' && $condition['field'] instanceOf QueryConditionInterface) {
$this->conditions[$key]['field'] = clone($condition['field']);
if ($key !== '#conjunction') {
if ($condition['field'] instanceOf QueryConditionInterface) {
$this->conditions[$key]['field'] = clone($condition['field']);
}
if ($condition['value'] instanceOf SelectQueryInterface) {
$this->conditions[$key]['value'] = clone($condition['value']);
}
}
}
}
......
......@@ -281,6 +281,45 @@ class DatabaseConnectionTestCase extends DatabaseTestCase {
}
}
/**
* Test cloning Select queries.
*/
class DatabaseSelectCloneTest extends DatabaseTestCase {
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');
}
}
/**
* Test fetch actions, part 1.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment