Verified Commit ed5dfebd authored by Lee Rowlands's avatar Lee Rowlands
Browse files

fix: #3593346 Entity query __toString() does not invoke the query alter hooks

By: amateescu
By: smustgrave
By: larowlan
parent 6108aa28
Loading
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -390,7 +390,8 @@ public function __toString() {
    // Clone the query so the prepare and compile doesn't get repeated.
    $clone = clone($this);

    $clone->prepare()
    $clone->alter()
      ->prepare()
      ->compile()
      ->addSort()
      ->finish();
+10 −5
Original line number Diff line number Diff line
@@ -1404,7 +1404,9 @@ public function testWithTwoEntityReferenceFieldsToSameEntityType(): void {
   * Test the entity query alter hooks are invoked.
   *
   * Hook functions in field_test.module add additional conditions to the query
   * removing entities with specific ids.
   * removing entities with specific ids. The alterations are also applied when
   * the query is cast to a string, so its string representation matches the
   * query that is executed.
   */
  public function testAlterHook(): void {
    $basicQuery = $this->storage
@@ -1416,15 +1418,18 @@ public function testAlterHook(): void {

    // Verify assumptions about the unaltered result.
    $query = clone $basicQuery;
    // The unaltered query does not contain the condition added by the hook.
    $this->assertDoesNotMatchRegularExpression("/<>\s*'5'/", (string) $query);
    $this->queryResults = $query->execute();
    $this->assertResult(5, 7, 13, 15);

    // field_test_entity_query_alter() removes the entity with id '5'.
    $query = clone $basicQuery;
    $this->queryResults = $query
      // Add a tag that no hook function matches.
      ->addTag('entity_query_alter_hook_test')
      ->execute();
    $query->addTag('entity_query_alter_hook_test');
    // Casting the query to a string applies the alter hooks, so the added
    // condition is reflected even before the query is executed.
    $this->assertMatchesRegularExpression("/<>\s*'5'/", (string) $query);
    $this->queryResults = $query->execute();
    $this->assertResult(7, 13, 15);

    // field_test_entity_query_entity_test_mulrev_alter() removes the