Verified Commit ee2a2c7f authored by Juraj Nemec's avatar Juraj Nemec
Browse files

Issue #3000191 by poker10, drunken monkey: Complex cloned query dependent on __toString() call

parent 782debf1
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -1624,6 +1624,8 @@ public function __toString() {
  }

  public function __clone() {
    parent::__clone();

    // On cloning, also clone the dependent objects. However, we do not
    // want to clone the database connection object as that would duplicate the
    // connection itself.
@@ -1633,6 +1635,11 @@ public function __clone() {
    foreach ($this->union as $key => $aggregate) {
      $this->union[$key]['query'] = clone($aggregate['query']);
    }
    foreach ($this->tables as $alias => $table) {
      if ($table['table'] instanceof SelectQueryInterface) {
        $this->tables[$alias]['table'] = clone $table['table'];
      }
    }
  }
}

+31 −0
Original line number Diff line number Diff line
@@ -325,6 +325,10 @@ class DatabaseSelectCloneTest extends DatabaseTestCase {
    $query->condition('id', $subquery, 'IN');

    $clone = clone $query;

    // Cloned query should have a different unique identifier.
    $this->assertNotEqual($query->uniqueIdentifier(), $clone->uniqueIdentifier());

    // Cloned query should not be altered by the following modification
    // happening on original query.
    $subquery->condition('age', 25, '>');
@@ -336,6 +340,33 @@ class DatabaseSelectCloneTest extends DatabaseTestCase {
    $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');
  }

  /**
   * Tests that nested SELECT queries are cloned properly.
   */
  public function testNestedQueryCloning() {
    $sub_query = db_select('test', 't');
    $sub_query->addField('t', 'id', 'id');
    $sub_query->condition('age', 28, '<');

    $query = db_select($sub_query, 't');

    $clone = clone $query;

    // Cloned query should have a different unique identifier.
    $this->assertNotEqual($query->uniqueIdentifier(), $clone->uniqueIdentifier());

    // Cloned query should not be altered by the following modification
    // happening on original query.
    $sub_query->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');
  }
}

/**