Commit 9fa529de authored by catch's avatar catch

Issue #2881030 by Manuel Garcia, pk188, keesje, vaplas, larowlan: $total_items...

Issue #2881030 by Manuel Garcia, pk188, keesje, vaplas, larowlan: $total_items can be negative if pager has offset
parent 7cc1987f
......@@ -156,6 +156,8 @@ public function executeCountQuery(&$count_query) {
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;
}
......
......@@ -9,6 +9,7 @@
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 @@ public function testExecuteCountQueryWithOffset() {
$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));
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment