Unverified Commit fb8ea960 authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3593233 Cloning an aggregate entity query shares its aggregate conditions with the original

By: amateescu
By: godotislate
By: amitgoyal
(cherry picked from commit 3e4104d1)
parent c6324354
Loading
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -92,4 +92,22 @@ public function &conditions();
   */
  public function compile($query);

  /**
   * Sets the query that holds this condition.
   *
   * @param \Drupal\Core\Entity\Query\QueryInterface $query
   *   New query to use.
   *
   * @return static
   */
  public function setQuery(QueryInterface $query): static;

  /**
   * Gets the query that is holding this condition.
   *
   * @return \Drupal\Core\Entity\Query\QueryInterface
   *   The query object.
   */
  public function getQuery(): QueryInterface;

}
+4 −1
Original line number Diff line number Diff line
@@ -343,10 +343,13 @@ public function tableSort(&$headers) {
  }

  /**
   * Makes sure that the Condition object is cloned as well.
   * Makes sure that condition objects are cloned as well.
   */
  public function __clone() {
    $this->condition = clone $this->condition;
    if (isset($this->conditionAggregate)) {
      $this->conditionAggregate = clone $this->conditionAggregate;
    }
  }

  /**
+4 −5
Original line number Diff line number Diff line
@@ -327,11 +327,7 @@ protected function isSimpleQuery() {
  }

  /**
   * Implements the magic __clone method.
   *
   * Reset SQL query and tables collected.
   * Reset fields and GROUP BY.
   * Ensure condition points to the new query.
   * Resets cached SQL query state and re-parents conditions on clone.
   */
  public function __clone() {
    parent::__clone();
@@ -340,6 +336,9 @@ public function __clone() {
    $this->sqlFields = [];
    $this->sqlGroupBy = [];
    $this->condition->setQuery($this);
    if (isset($this->conditionAggregate)) {
      $this->conditionAggregate->setQuery($this);
    }
  }

  /**
+30 −0
Original line number Diff line number Diff line
@@ -662,6 +662,36 @@ public function testAlterHook(): void {
    ]);
  }

  /**
   * Tests that cloning an aggregate query isolates its aggregate conditions.
   */
  public function testCloneAggregateIsolation(): void {
    // Original query: groups with id count > 1 are user 2 (3 rows) and user 3
    // (2 rows).
    $original = $this->entityStorage->getAggregateQuery()
      ->accessCheck(FALSE)
      ->aggregate('id', 'count')
      ->groupBy('user_id')
      ->conditionAggregate('id', 'count', 1, '>');

    $clone = clone $original;
    // Tightening the clone must not affect the original's conditions.
    $clone->conditionAggregate('id', 'count', 2, '>');

    $this->queryResult = $original->execute();
    $this->assertCount(2, $this->queryResult);
    $this->assertResults([
      ['user_id' => 2, 'id_count' => 3],
      ['user_id' => 3, 'id_count' => 2],
    ]);

    $this->queryResult = $clone->execute();
    $this->assertCount(1, $this->queryResult);
    $this->assertResults([
      ['user_id' => 2, 'id_count' => 3],
    ]);
  }

  /**
   * Asserts the results as expected regardless of order between and in rows.
   *