You are here

function PagerTest::testPagerApi in Views (for Drupal 7) 8.3

Test the api functions on the view object.

File

lib/Drupal/views/Tests/Plugin/PagerTest.php, line 269
Definition of Drupal\views\Tests\Plugin\PagerTest.

Class

PagerTest
Tests the pluggable pager system.

Namespace

Drupal\views\Tests\Plugin

Code

function testPagerApi() {
  $view = $this
    ->createViewFromConfig('test_pager_full');

  // 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 get_items_per_page uses the settings of set_items_per_page.');
  $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 get_offset uses the settings of set_offset.');
  $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 get_current_page 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
    ->set_items_per_page($rand_number);
  $this
    ->assertEqual($view
    ->getItemsPerPage(), $rand_number, 'Make sure get_items_per_page uses the settings of set_items_per_page.');
  $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
    ->set_offset($rand_number);
  $this
    ->assertEqual($view
    ->getOffset(), $rand_number, 'Make sure get_offset uses the settings of set_offset.');
  $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
    ->set_current_page($rand_number);
  $this
    ->assertEqual($view
    ->getCurrentPage(), $rand_number, 'Make sure get_current_page 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.');
}