From a4208fd3c8844cb700de07f4afcd63efe3e27e3c Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 7 Aug 2018 15:48:44 +0100
Subject: [PATCH] Issue #2961285 by pavlosdan, hchonov, borisson_: Entity query
 condition count is faulty

---
 .../Entity/Query/ConditionFundamentals.php    |  2 +-
 .../Core/Entity/Query/ConditionInterface.php  |  3 +--
 .../Core/Entity/EntityQueryTest.php           | 25 +++++++++++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/core/lib/Drupal/Core/Entity/Query/ConditionFundamentals.php b/core/lib/Drupal/Core/Entity/Query/ConditionFundamentals.php
index d03e2b2e8109..6de10bf3542f 100644
--- a/core/lib/Drupal/Core/Entity/Query/ConditionFundamentals.php
+++ b/core/lib/Drupal/Core/Entity/Query/ConditionFundamentals.php
@@ -65,7 +65,7 @@ public function getConjunction() {
    * {@inheritdoc}
    */
   public function count() {
-    return count($this->conditions) - 1;
+    return count($this->conditions);
   }
 
   /**
diff --git a/core/lib/Drupal/Core/Entity/Query/ConditionInterface.php b/core/lib/Drupal/Core/Entity/Query/ConditionInterface.php
index 7657c006c31b..bf53a92c2c44 100644
--- a/core/lib/Drupal/Core/Entity/Query/ConditionInterface.php
+++ b/core/lib/Drupal/Core/Entity/Query/ConditionInterface.php
@@ -19,8 +19,7 @@ public function getConjunction();
    * Implements \Countable::count().
    *
    * Returns the size of this conditional. The size of the conditional is the
-   * size of its conditional array minus one, because one element is the
-   * conjunction.
+   * size of its conditional array.
    */
   public function count();
 
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php
index 2c9b86826d76..a47d9a9a6f9a 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php
@@ -556,6 +556,31 @@ public function testNestedConditionGroups() {
     $this->assertResult(4, 6, 12, 14);
   }
 
+  /**
+   * Tests that condition count returns expected number of conditions.
+   */
+  public function testConditionCount() {
+    // Query for all entities of the first bundle that
+    // have red as a colour AND are triangle shaped.
+    $query = $this->storage->getQuery();
+
+    // Add an AND condition group with 2 conditions in it.
+    $and_condition_group = $query->andConditionGroup()
+      ->condition($this->figures . '.color', 'red')
+      ->condition($this->figures . '.shape', 'triangle');
+
+    // We added 2 conditions so count should be 2.
+    $this->assertEqual($and_condition_group->count(), 2);
+
+    // Add an OR condition group with 2 conditions in it.
+    $or_condition_group = $query->orConditionGroup()
+      ->condition($this->figures . '.color', 'red')
+      ->condition($this->figures . '.shape', 'triangle');
+
+    // We added 2 conditions so count should be 2.
+    $this->assertEqual($or_condition_group->count(), 2);
+  }
+
   /**
    * Test queries with delta conditions.
    */
-- 
GitLab