Issue #2881030 by Manuel Garcia, pk188, keesje, vaplas, larowlan: $total_items can be negative if pager has offset

8.4.x
Nathaniel Catchpole 2017-06-15 16:41:52 +01:00
parent 7cc1987fbc
commit 9fa529def1
2 changed files with 27 additions and 0 deletions

View File

@ -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;
}

View File

@ -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));
}
}
/**