diff --git a/core/includes/pager.inc b/core/includes/pager.inc
index a5d3e6b..0faba93 100644
--- a/core/includes/pager.inc
+++ b/core/includes/pager.inc
@@ -71,6 +71,9 @@ class PagerDefault extends SelectQueryExtender {
     }
     $this->ensureElement();
 
+    $this->getCountQuery()->preExecute();
+    debug($this->getCountQuery()->__toString());
+    debug($this->getCountQuery()->getArguments());
     $total_items = $this->getCountQuery()->execute()->fetchField();
     $current_page = pager_default_initialize($total_items, $this->limit, $this->element);
     $this->range($current_page * $this->limit, $this->limit);
diff --git a/core/modules/node/node.test b/core/modules/node/node.test
index 2e9b075..0f21a2d 100644
--- a/core/modules/node/node.test
+++ b/core/modules/node/node.test
@@ -2300,3 +2300,74 @@ class NodeTokenReplaceTestCase extends DrupalWebTestCase {
     }
   }
 }
+
+/**
+ * Testcase for the comment pagination of access controlled nodes.
+ */
+class NodeAccessCommentPagerTestCase extends DrupalWebTestCase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Node comment pagination',
+      'description' => 'Test access controlled nodes have the right amount of comment pages.',
+      'group' => 'Node',
+    );
+  }
+
+  public function setUp() {
+    parent::setUp('node_access_test', 'comment');
+    node_access_rebuild();
+  }
+
+  /**
+   * Test that the comment count query is working so that the pager has no
+   * superfluous pages.
+   */
+  public function testPageCount() {
+    // Create a node.
+    $node = $this->drupalCreateNode();
+
+    // Create 60 comments.
+    for ($i = 0; $i < 60; $i++) {
+      $comment = entity_create('comment', array(
+        'nid' => $node->nid,
+        'subject' => $this->randomName(),
+        'comment_body' => array(
+          LANGUAGE_NONE => array(
+            array('value' => $this->randomName()),
+          ),
+        ),
+      ));
+      $comment->save();
+    }
+
+    // Create two roles for the test user, both allowing access using
+    // taxonomy access control.
+    $rids = array();
+    $rids[DRUPAL_AUTHENTICATED_RID] = DRUPAL_AUTHENTICATED_RID;
+    for ($i = 0; $i < 2; $i++) {
+      $rid = $this->drupalCreateRole(array('access content', 'access comments', 'node test view'));
+      $rids[$rid] = $rid;
+    }
+
+    // Create and login test user.
+    $edit = array();
+    $edit['name'] = $this->randomName();
+    $edit['mail'] = $edit['name'] . '@example.com';
+    $edit['pass'] = user_password();
+    $edit['status'] = 1;
+    $edit['roles'] = $rids;
+    $account = user_save(drupal_anonymous_user(), $edit);
+    $account->pass_raw = $edit['pass'];
+    $this->drupalLogin($account);
+    $this->assertTrue(user_access('access content', $account), t('Has access content permission.'));
+
+    // View the node page. With the default 50 comments per page there should
+    // be two pages (0, 1) but no third (2) page.
+    $this->drupalGet('node/' . $node->nid);
+    $this->assertText($node->title, t('Node title found.'));
+    $this->assertText(t('Comments'), t('Has a comments section.'));
+    $this->assertRaw('page=1', t('Secound page exists.'));
+    $this->assertNoRaw('page=2', t('No third page exists.'));
+  }
+}
