function PagerTest::testNormalPager

Same name in other branches
  1. 9 core/modules/views/tests/src/Functional/Plugin/PagerTest.php \Drupal\Tests\views\Functional\Plugin\PagerTest::testNormalPager()
  2. 8.9.x core/modules/views/tests/src/Functional/Plugin/PagerTest.php \Drupal\Tests\views\Functional\Plugin\PagerTest::testNormalPager()
  3. 10 core/modules/views/tests/src/Functional/Plugin/PagerTest.php \Drupal\Tests\views\Functional\Plugin\PagerTest::testNormalPager()

Tests the normal pager.

File

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

Class

PagerTest
Tests the pluggable pager system.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testNormalPager() : void {
    // Create 11 nodes and make sure that everyone is returned.
    // We create 11 nodes, because the default pager plugin had 10 items per page.
    $this->drupalCreateContentType([
        'type' => 'page',
    ]);
    for ($i = 0; $i < 11; $i++) {
        $this->drupalCreateNode();
    }
    $view = Views::getView('test_pager_full');
    $this->executeView($view);
    $this->assertCount(5, $view->result, 'Make sure that only a certain count of items is returned');
    // Setup and test an offset.
    $view = Views::getView('test_pager_full');
    $view->setDisplay();
    $pager = [
        'type' => 'full',
        'options' => [
            'offset' => 8,
            'items_per_page' => 5,
        ],
    ];
    $view->display_handler
        ->setOption('pager', $pager);
    $this->executeView($view);
    $this->assertCount(3, $view->result, 'Make sure that only a certain count of items is returned');
    // Test items per page = 0
    $view = Views::getView('test_view_pager_full_zero_items_per_page');
    $this->executeView($view);
    $this->assertCount(11, $view->result, 'All items are return');
    // @todo Test number of pages.
    // Test items per page = 0.
    // Setup and test an offset.
    $view = Views::getView('test_pager_full');
    $view->setDisplay();
    $pager = [
        'type' => 'full',
        'options' => [
            'offset' => 0,
            'items_per_page' => 0,
        ],
    ];
    $view->display_handler
        ->setOption('pager', $pager);
    $this->executeView($view);
    $this->assertEquals(0, $view->pager
        ->getItemsPerPage());
    $this->assertCount(11, $view->result);
    // Test pager cache contexts.
    $this->drupalGet('test_pager_full');
    $this->assertCacheContexts([
        'languages:language_interface',
        'theme',
        'timezone',
        'url.query_args',
        'user.node_grants:view',
    ]);
    // Set "Number of pager links visible" to 1 and check the active page number
    // on the last page.
    $view = Views::getView('test_pager_full');
    $view->setDisplay();
    $pager = [
        'type' => 'full',
        'options' => [
            'items_per_page' => 5,
            'quantity' => 1,
        ],
    ];
    $view->display_handler
        ->setOption('pager', $pager);
    $view->save();
    $this->drupalGet('test_pager_full', [
        'query' => [
            'page' => 2,
        ],
    ]);
    $this->assertEquals('Page 3', $this->assertSession()
        ->elementExists('css', '.pager__items li.is-active')
        ->getText());
    $link = $this->assertSession()
        ->elementExists('css', '.pager__items li.is-active a');
    $this->assertSame('page', $link->getAttribute('aria-current'));
    $this->assertSame('Current page', $link->getAttribute('title'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.