summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2017-06-15 15:41:52 (GMT)
committerNathaniel Catchpole2017-06-15 15:41:52 (GMT)
commit9fa529def1c559d66cb5e5cc5aafd7da62ee5486 (patch)
treee609c62728894863d9dac68034c6b3dfc5f65cf6
parent7cc1987fbc53d9d57a286d4f7211b6df34a46dc0 (diff)
Issue #2881030 by Manuel Garcia, pk188, keesje, vaplas, larowlan: $total_items can be negative if pager has offset
-rw-r--r--core/modules/views/src/Plugin/views/pager/PagerPluginBase.php2
-rw-r--r--core/modules/views/tests/src/Unit/Plugin/pager/PagerPluginBaseTest.php25
2 files changed, 27 insertions, 0 deletions
diff --git a/core/modules/views/src/Plugin/views/pager/PagerPluginBase.php b/core/modules/views/src/Plugin/views/pager/PagerPluginBase.php
index ec231a0..6e360b8 100644
--- a/core/modules/views/src/Plugin/views/pager/PagerPluginBase.php
+++ b/core/modules/views/src/Plugin/views/pager/PagerPluginBase.php
@@ -156,6 +156,8 @@ abstract class PagerPluginBase extends PluginBase {
if (!empty($this->options['offset'])) {
$this->total_items -= $this->options['offset'];
}
+ // Prevent from being negative.
+ $this->total_items = max(0, $this->total_items);
return $this->total_items;
}
diff --git a/core/modules/views/tests/src/Unit/Plugin/pager/PagerPluginBaseTest.php b/core/modules/views/tests/src/Unit/Plugin/pager/PagerPluginBaseTest.php
index 555c8a4..ef0e812 100644
--- a/core/modules/views/tests/src/Unit/Plugin/pager/PagerPluginBaseTest.php
+++ b/core/modules/views/tests/src/Unit/Plugin/pager/PagerPluginBaseTest.php
@@ -9,6 +9,7 @@ namespace Drupal\Tests\views\Unit\Plugin\pager;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Database\StatementInterface;
+use Drupal\Core\Database\Query\Select;
/**
* @coversDefaultClass \Drupal\views\Plugin\views\pager\PagerPluginBase
@@ -247,6 +248,30 @@ class PagerPluginBaseTest extends UnitTestCase {
$this->assertEquals(1, $this->pager->executeCountQuery($query));
}
+ /**
+ * Tests the executeCountQuery method with an offset larger than result count.
+ *
+ * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::executeCountQuery()
+ */
+ public function testExecuteCountQueryWithOffsetLargerThanResult() {
+ $statement = $this->getMock(TestStatementInterface::class);
+
+ $statement->expects($this->once())
+ ->method('fetchField')
+ ->will($this->returnValue(2));
+
+ $query = $this->getMockBuilder(Select::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $query->expects($this->once())
+ ->method('execute')
+ ->will($this->returnValue($statement));
+
+ $this->pager->setOffset(3);
+ $this->assertEquals(0, $this->pager->executeCountQuery($query));
+ }
+
}
/**