diff --git a/src/Tests/FlagSimpleTest.php b/src/Tests/FlagSimpleTest.php
index ab1f4e7..df600f9 100644
--- a/src/Tests/FlagSimpleTest.php
+++ b/src/Tests/FlagSimpleTest.php
@@ -84,6 +84,7 @@ class FlagSimpleTest extends WebTestBase {
 
     $this->doTestFlagAdd();
     $this->doGlobalFlag();
+    $this->doTestFlagCounts();
     $this->doTestHideFlagLinkFromTeaser();
     $this->doTestUserDeletion();
   }
@@ -292,9 +293,19 @@ class FlagSimpleTest extends WebTestBase {
    * Flags a node using different user accounts and checks flag counts.
    */
   public function doTestFlagCounts() {
+    /** \Drupal\Core\Database\Connection $connection */
+    $connection = \Drupal::database();
+
     $node = $this->drupalCreateNode(['type' => $this->nodeType]);
     $node_id = $node->id();
 
+    // Grant the flag permissions to the authenticated role, so that both
+    // users have the same roles and share the render cache.
+    $role = Role::load(DRUPAL_AUTHENTICATED_RID);
+    $role->grantPermission('flag ' . $this->id);
+    $role->grantPermission('unflag ' . $this->id);
+    $role->save();
+
     // Create and login user 1.
     $user_1 = $this->drupalCreateUser();
     $this->drupalLogin($user_1);
@@ -306,12 +317,13 @@ class FlagSimpleTest extends WebTestBase {
     $this->assertLink('Unflag this item');
 
     // Check for 1 flag count.
-    $count_flags_before = \Drupal::entityQuery('flag_counts')
+    $count_flags_before = $connection->select('flag_counts')
       ->condition('fid', $this->id)
       ->condition('entity_type', $node->getEntityTypeId())
       ->condition('entity_id', $node_id)
-      ->count()
-      ->execute();
+      ->countQuery()
+      ->execute()
+      ->fetchField();
     $this->assertTrue(1, $count_flags_before);
 
     // Logout user 1, create and login user 2.
@@ -325,12 +337,13 @@ class FlagSimpleTest extends WebTestBase {
     $this->assertLink('Unflag this item');
 
     // Check for 2 flag counts.
-    $count_flags_after = \Drupal::entityQuery('flag_counts')
+    $count_flags_after = $connection->select('flag_counts')
       ->condition('fid', $this->id)
       ->condition('entity_type', $node->getEntityTypeId())
       ->condition('entity_id', $node_id)
-      ->count()
-      ->execute();
+      ->countQuery()
+      ->execute()
+      ->fetchField();
     $this->assertTrue(2, $count_flags_after);
 
     // Unflag the node again.
@@ -340,12 +353,13 @@ class FlagSimpleTest extends WebTestBase {
     $this->assertLink('Flag this item');
 
     // Check for 1 flag count.
-    $count_flags_before = \Drupal::entityQuery('flag_counts')
+    $count_flags_before = $connection->select('flag_counts')
       ->condition('fid', $this->id)
       ->condition('entity_type', $node->getEntityTypeId())
       ->condition('entity_id', $node_id)
-      ->count()
-      ->execute();
+      ->countQuery()
+      ->execute()
+      ->fetchField();
     $this->assertEqual(1, $count_flags_before);
 
     // Delete  user 1.
@@ -353,12 +367,13 @@ class FlagSimpleTest extends WebTestBase {
 
     // Check for 0 flag counts, user deletion should lead to count decrement
     // or row deletion.
-    $count_flags_before = \Drupal::entityQuery('flag_counts')
+    $count_flags_before = $connection->select('flag_counts')
       ->condition('fid', $this->id)
       ->condition('entity_type', $node->getEntityTypeId())
       ->condition('entity_id', $node_id)
-      ->count()
-      ->execute();
+      ->countQuery()
+      ->execute()
+      ->fetchField();
 
     $this->assertEqual(0, $count_flags_before);
   }
