You are here

public function PagerTest::testPagerApi in Drupal 9

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

Tests the api functions on the view object.

File

core/modules/views/tests/src/Functional/Plugin/PagerTest.php, line 379

Class

PagerTest
Tests the pluggable pager system.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

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

  // On the first round don't initialize the pager.
  $this
    ->assertNull($view
    ->getItemsPerPage(), '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
    ->assertEquals($rand_number, $view
    ->getItemsPerPage(), 'Make sure getItemsPerPage uses the settings of setItemsPerPage.');
  $this
    ->assertNull($view
    ->getOffset(), 'If the pager is not initialized and no manual override there is no offset.');
  $rand_number = rand(1, 5);
  $view
    ->setOffset($rand_number);
  $this
    ->assertEquals($rand_number, $view
    ->getOffset(), 'Make sure getOffset uses the settings of setOffset.');
  $this
    ->assertNull($view
    ->getCurrentPage(), '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
    ->assertEquals($rand_number, $view
    ->getCurrentPage(), '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
    ->assertEquals(5, $view
    ->getItemsPerPage(), '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
    ->assertEquals($rand_number, $view
    ->getItemsPerPage(), 'Make sure getItemsPerPage uses the settings of setItemsPerPage.');
  $this
    ->assertEquals(0, $view
    ->getOffset(), '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
    ->assertEquals($rand_number, $view
    ->getOffset(), 'Make sure getOffset uses the settings of setOffset.');
  $this
    ->assertEquals(0, $view
    ->getCurrentPage(), '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
    ->assertEquals($rand_number, $view
    ->getCurrentPage(), '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
    ->assertEquals(0, $view
    ->getCurrentPage(), 'Make sure setCurrentPage always sets a valid page number.');
}