diff --git a/core/modules/views/tests/Drupal/views/Tests/Plugin/pager/PagerPluginBaseTest.php b/core/modules/views/tests/Drupal/views/Tests/Plugin/pager/PagerPluginBaseTest.php index 9acf5c8..bdeb1b4 100644 --- a/core/modules/views/tests/Drupal/views/Tests/Plugin/pager/PagerPluginBaseTest.php +++ b/core/modules/views/tests/Drupal/views/Tests/Plugin/pager/PagerPluginBaseTest.php @@ -8,6 +8,7 @@ namespace Drupal\views\Tests\Plugin\pager; use Drupal\Tests\UnitTestCase; +use Drupal\Core\Database\StatementInterface; /** * Tests the PagerPluginBase class @@ -57,6 +58,8 @@ public function setUp() { /** * Tests the getItemsPerPage() method. + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::getItemsPerPage() */ public function testGetItemsPerPage() { $this->assertEquals(5, $this->pager->getItemsPerPage()); @@ -64,6 +67,8 @@ public function testGetItemsPerPage() { /** * Tests the setItemsPerPage() method. + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::setItemsPerPage() */ public function testSetItemsPerPage() { $this->pager->setItemsPerPage(6); @@ -72,6 +77,8 @@ public function testSetItemsPerPage() { /** * Tests the getOffset() method. + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::getOffset() */ public function testGetOffset() { $this->assertEquals(1, $this->pager->getOffset()); @@ -79,6 +86,8 @@ public function testGetOffset() { /** * Tests the setOffset() method. + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::setOffset() */ public function testSetOffset() { $this->pager->setOffset(2); @@ -87,6 +96,8 @@ public function testSetOffset() { /** * Tests the getCurrentPage() method. + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::getCurrentPage() */ public function testGetCurrentPage() { $this->assertEquals(1, $this->pager->getCurrentPage()); @@ -94,6 +105,8 @@ public function testGetCurrentPage() { /** * Tests the setCurrentPage() method. + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::setCurrentPage() */ public function testSetCurrentPage() { $this->pager->setCurrentPage(2); @@ -109,6 +122,8 @@ public function testSetCurrentPage() { /** * Tests the getTotalItems() method. + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::getTotalItems() */ public function testGetTotalItems() { // Should return 0 by default. @@ -120,6 +135,8 @@ public function testGetTotalItems() { /** * Tests the getPagerId() method. + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::getPagerId() */ public function testGetPagerId() { // Should return 0 if 'id' is not set. @@ -132,6 +149,8 @@ public function testGetPagerId() { /** * Tests the usePager() method. + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::usePager() */ public function testUsePager() { $this->assertTrue($this->pager->usePager()); @@ -139,6 +158,8 @@ public function testUsePager() { /** * Tests the useCountQuery() method. + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::useCountQuery() */ public function testUseCountQuery() { $this->assertTrue($this->pager->useCountQuery()); @@ -146,11 +167,100 @@ public function testUseCountQuery() { /** * Tests the usesExposed() method. + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::usedExposed() */ public function testUsesExposed() { $this->assertFalse($this->pager->usesExposed()); } - // @todo Test the executeCountQuery() and hasMoreRecords() methods. + /** + * Tests the hasMoreRecords() method. + * + * @dataProvider providerTestHasMoreRecords + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::hasMoreRecords() + */ + public function testHasMoreRecords($items_per_page, $total_items, $current_page, $has_more_records) { + $this->pager->setItemsPerPage($items_per_page); + $this->pager->total_items = $total_items; + $this->pager->setCurrentPage($current_page); + $this->assertEquals($has_more_records, $this->pager->hasMoreRecords()); + } + + /** + * Provides test data for the hasMoreRecord method test. + * + * @see self::testHasMoreRecords + */ + public function providerTestHasMoreRecords() { + return array( + // No items per page, so there can't be more available records. + array(0, 0, 0, FALSE), + array(0, 10, 0, FALSE), + // The amount of total items equals the items per page, so there is no + // next page available. + array(5, 5, 0, FALSE), + // There is one more item, and we are at the first page. + array(5, 6, 0, TRUE), + // Now we are on the second page, which has just a single one left. + array(5, 6, 1, FALSE), + // Increase the total items, so we have some available on the third page. + array(5, 12, 1, TRUE) + ); + } + + /** + * Tests the executeCountQuery method without a set offset. + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::executeCountQuery() + */ + public function testExecuteCountQueryWithoutOffset() { + $statement = $this->getMock('\Drupal\views\Tests\Plugin\pager\TestStatementInterface'); + + $statement->expects($this->once()) + ->method('fetchField') + ->will($this->returnValue(3)); + + $query = $this->getMockBuilder('\Drupal\Core\Database\Query\Select') + ->disableOriginalConstructor() + ->getMock(); + + $query->expects($this->once()) + ->method('execute') + ->will($this->returnValue($statement)); + + $this->pager->setOffset(0); + $this->assertEquals(3, $this->pager->executeCountQuery($query)); + } + + /** + * Tests the executeCountQuery method with a set offset. + * + * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::executeCountQuery() + */ + public function testExecuteCountQueryWithOffset() { + $statement = $this->getMock('\Drupal\views\Tests\Plugin\pager\TestStatementInterface'); + + $statement->expects($this->once()) + ->method('fetchField') + ->will($this->returnValue(3)); + + $query = $this->getMockBuilder('\Drupal\Core\Database\Query\Select') + ->disableOriginalConstructor() + ->getMock(); + + $query->expects($this->once()) + ->method('execute') + ->will($this->returnValue($statement)); + + $this->pager->setOffset(2); + $this->assertEquals(1, $this->pager->executeCountQuery($query)); + } } + +// As StatementInterface extends \Transversable, which though always needs +// an additional interface. The Statement class itself can't be mocked because +// of its __wakeup function. +interface TestStatementInterface extends StatementInterface, \Iterator {} diff --git a/core/phpunit.xml.dist b/core/phpunit.xml.dist index f7c97fc..6ca7e89 100644 --- a/core/phpunit.xml.dist +++ b/core/phpunit.xml.dist @@ -1,6 +1,6 @@ - + ./tests/*