From 5c9d1a92f167cd0a8a8b1b9c576b8d6ddbac76a7 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Fri, 21 Oct 2016 12:39:32 +0100
Subject: [PATCH] Issue #2798521 by jamesrward, Manuel Garcia, mikeker,
 Lendude, przemek_leczycki, aklump, dawehner: Views result summary returns
 float number when using the mini pager

---
 .../views/src/Plugin/views/pager/Mini.php     | 40 +++++++++----------
 .../views/src/Tests/Plugin/MiniPagerTest.php  | 12 ++++++
 .../test_views/views.view.test_mini_pager.yml | 23 +++++++++++
 3 files changed, 55 insertions(+), 20 deletions(-)

diff --git a/core/modules/views/src/Plugin/views/pager/Mini.php b/core/modules/views/src/Plugin/views/pager/Mini.php
index 5f1750ea3cf0..c6520f4fb601 100644
--- a/core/modules/views/src/Plugin/views/pager/Mini.php
+++ b/core/modules/views/src/Plugin/views/pager/Mini.php
@@ -47,14 +47,17 @@ public function summaryTitle() {
   public function query() {
     parent::query();
 
-    // Don't query for the next page if we have a pager that has a limited
-    // amount of pages.
-    if ($this->getItemsPerPage() > 0 && (empty($this->options['total_pages']) || ($this->getCurrentPage() < $this->options['total_pages']))) {
-      // Increase the items in the query in order to be able to find out whether
-      // there is another page.
-      $limit = $this->view->query->getLimit();
-      $limit += 1;
-      $this->view->query->setLimit($limit);
+    // Only modify the query if we don't want to do a total row count
+    if (!$this->view->get_total_rows) {
+      // Don't query for the next page if we have a pager that has a limited
+      // amount of pages.
+      if ($this->getItemsPerPage() > 0 && (empty($this->options['total_pages']) || ($this->getCurrentPage() < $this->options['total_pages']))) {
+        // Increase the items in the query in order to be able to find out
+        // whether there is another page.
+        $limit = $this->view->query->getLimit();
+        $limit += 1;
+        $this->view->query->setLimit($limit);
+      }
     }
   }
 
@@ -69,19 +72,16 @@ public function useCountQuery() {
    * {@inheritdoc}
    */
   public function postExecute(&$result) {
-    // In query() one more item might have been retrieved than necessary. If so,
-    // the next link needs to be displayed and the item removed.
-    if ($this->getItemsPerPage() > 0 && count($result) > $this->getItemsPerPage()) {
-      array_pop($result);
-      // Make sure the pager shows the next link by setting the total items to
-      // the biggest possible number but prevent failing calculations like
-      // ceil(PHP_INT_MAX) we take PHP_INT_MAX / 2.
-      $total = PHP_INT_MAX / 2;
+    // Only modify the result if we didn't do a total row count
+    if (!$this->view->get_total_rows) {
+      $this->total_items = $this->getCurrentPage() * $this->getItemsPerPage() + count($result);
+      // query() checks if we need a next link by setting limit 1 record past
+      // this page If we got the extra record we need to remove it before we
+      // render the result.
+      if ($this->getItemsPerPage() > 0 && count($result) > $this->getItemsPerPage()) {
+        array_pop($result);
+      }
     }
-    else {
-      $total = $this->getCurrentPage() * $this->getItemsPerPage() + count($result);
-    }
-    $this->total_items = $total;
   }
 
   /**
diff --git a/core/modules/views/src/Tests/Plugin/MiniPagerTest.php b/core/modules/views/src/Tests/Plugin/MiniPagerTest.php
index f39a9d65f28b..1a73b6980889 100644
--- a/core/modules/views/src/Tests/Plugin/MiniPagerTest.php
+++ b/core/modules/views/src/Tests/Plugin/MiniPagerTest.php
@@ -70,6 +70,18 @@ public function testMiniPagerRender() {
     $this->assertText($this->nodes[18]->label());
     $this->assertText($this->nodes[19]->label());
 
+    // Test @total value in result summary
+    $view = Views::getView('test_mini_pager');
+    $view->setDisplay('page_4');
+    $this->executeView($view);
+    $this->assertIdentical($view->get_total_rows, TRUE, 'The query was set to calculate the total number of rows.');
+    $this->assertEqual(count($this->nodes), $view->total_rows, 'The total row count is equal to the number of nodes.');
+
+    $this->drupalGet('test_mini_pager_total', array('query' => array('page' => 1)));
+    $this->assertText('of ' . count($this->nodes), 'The first page shows the total row count.');
+    $this->drupalGet('test_mini_pager_total', array('query' => array('page' => 6)));
+    $this->assertText('of ' . count($this->nodes), 'The last page shows the total row count.');
+
     // Test a mini pager with just one item per page.
     $this->drupalGet('test_mini_pager_one');
     $this->assertText('››');
diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_mini_pager.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_mini_pager.yml
index e65ea4a4e46f..289f83db9774 100644
--- a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_mini_pager.yml
+++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_mini_pager.yml
@@ -118,3 +118,26 @@ display:
         type: mini
         options:
           items_per_page: 0
+  page_4:
+    display_plugin: page
+    id: page_4
+    display_title: 'Page 4'
+    position: 4
+    display_options:
+      display_extenders: {  }
+      path: test_mini_pager_total
+      empty: {  }
+      defaults:
+        empty: false
+        header: false
+      header:
+        result:
+          id: result
+          table: views
+          field: result
+          relationship: none
+          group_type: group
+          admin_label: ''
+          empty: false
+          content: 'Displaying @start - @end of @total'
+          plugin_id: result
-- 
GitLab