You are here

function PagerTest::testPagerApi in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Plugin/PagerTest.php \Drupal\views\Tests\Plugin\PagerTest::testPagerApi()

Test the api functions on the view object.

File

core/modules/views/src/Tests/Plugin/PagerTest.php, line 290
Contains \Drupal\views\Tests\Plugin\PagerTest.

Class

PagerTest
Tests the pluggable pager system.

Namespace

Drupal\views\Tests\Plugin

Code

function testPagerApi() {
  $view = Views::getView('test_pager_full');
  $view
    ->setDisplay();

  // On the first round don't initialize the pager.
  $this
    ->assertEqual($view
    ->getItemsPerPage(), NULL, 'If the pager is not initialized and no manual override there is no items per page.');
  $rand_number = rand(1, 5);
  $view
    ->setItemsPerPage($rand_number);
  $this
    ->assertEqual($view
    ->getItemsPerPage(), $rand_number, 'Make sure getItemsPerPage uses the settings of setItemsPerPage.');
  $this
    ->assertEqual($view
    ->getOffset(), NULL, 'If the pager is not initialized and no manual override there is no offset.');
  $rand_number = rand(1, 5);
  $view
    ->setOffset($rand_number);
  $this
    ->assertEqual($view
    ->getOffset(), $rand_number, 'Make sure getOffset uses the settings of setOffset.');
  $this
    ->assertEqual($view
    ->getCurrentPage(), NULL, 'If the pager is not initialized and no manual override there is no current page.');
  $rand_number = rand(1, 5);
  $view
    ->setCurrentPage($rand_number);
  $this
    ->assertEqual($view
    ->getCurrentPage(), $rand_number, 'Make sure getCurrentPage uses the settings of set_current_page.');
  $view
    ->destroy();

  // On this round enable the pager.
  $view
    ->initDisplay();
  $view
    ->initQuery();
  $view
    ->initPager();
  $this
    ->assertEqual($view
    ->getItemsPerPage(), 5, 'Per default the view has 5 items per page.');
  $rand_number = rand(1, 5);
  $view
    ->setItemsPerPage($rand_number);
  $rand_number = rand(6, 11);
  $view->pager
    ->setItemsPerPage($rand_number);
  $this
    ->assertEqual($view
    ->getItemsPerPage(), $rand_number, 'Make sure getItemsPerPage uses the settings of setItemsPerPage.');
  $this
    ->assertEqual($view
    ->getOffset(), 0, 'Per default a view has a 0 offset.');
  $rand_number = rand(1, 5);
  $view
    ->setOffset($rand_number);
  $rand_number = rand(6, 11);
  $view->pager
    ->setOffset($rand_number);
  $this
    ->assertEqual($view
    ->getOffset(), $rand_number, 'Make sure getOffset uses the settings of setOffset.');
  $this
    ->assertEqual($view
    ->getCurrentPage(), 0, 'Per default the current page is 0.');
  $rand_number = rand(1, 5);
  $view
    ->setCurrentPage($rand_number);
  $rand_number = rand(6, 11);
  $view->pager
    ->setCurrentPage($rand_number);
  $this
    ->assertEqual($view
    ->getCurrentPage(), $rand_number, 'Make sure getCurrentPage uses the settings of set_current_page.');

  // Set an invalid page and make sure the method takes care about it.
  $view
    ->setCurrentPage(-1);
  $this
    ->assertEqual($view
    ->getCurrentPage(), 0, 'Make sure setCurrentPage always sets a valid page number.');
}