function PagerTest::assertPagerItems
Same name in other branches
- 9 core/modules/system/tests/src/Functional/Pager/PagerTest.php \Drupal\Tests\system\Functional\Pager\PagerTest::assertPagerItems()
- 8.9.x core/modules/system/tests/src/Functional/Pager/PagerTest.php \Drupal\Tests\system\Functional\Pager\PagerTest::assertPagerItems()
- 10 core/modules/system/tests/src/Functional/Pager/PagerTest.php \Drupal\Tests\system\Functional\Pager\PagerTest::assertPagerItems()
Asserts pager items and links.
@internal
Parameters
int $current_page: The current pager page the internal browser is on.
1 call to PagerTest::assertPagerItems()
- PagerTest::testActiveClass in core/
modules/ system/ tests/ src/ Functional/ Pager/ PagerTest.php - Tests markup and CSS classes of pager links.
File
-
core/
modules/ system/ tests/ src/ Functional/ Pager/ PagerTest.php, line 224
Class
- PagerTest
- Tests pager functionality.
Namespace
Drupal\Tests\system\Functional\PagerCode
protected function assertPagerItems(int $current_page) : void {
$elements = $this->xpath('//ul[contains(@class, :class)]/li', [
':class' => 'pager__items',
]);
$this->assertNotEmpty($elements, 'Pager found.');
// Make current page 1-based.
$current_page++;
// Extract first/previous and next/last items.
// first/previous only exist, if the current page is not the first.
if ($current_page > 1) {
$first = array_shift($elements);
$previous = array_shift($elements);
}
// next/last always exist, unless the current page is the last.
if ($current_page != count($elements)) {
$last = array_pop($elements);
$next = array_pop($elements);
}
// We remove elements from the $elements array in the following code, so
// we store the total number of pages for verifying the "last" link.
$total_pages = count($elements);
// Verify items and links to pages.
foreach ($elements as $page => $element) {
// Make item/page index 1-based.
$page++;
if ($current_page == $page) {
$this->assertClass($element, 'is-active', 'Element for current page has .is-active class.');
$link = $element->find('css', 'a');
$this->assertNotEmpty($link, 'Element for current page has link.');
$destination = $link->getAttribute('href');
// URL query string param is 0-indexed.
$this->assertEquals('?page=' . ($page - 1), $destination);
}
else {
$this->assertNoClass($element, 'is-active', "Element for page {$page} has no .is-active class.");
$this->assertClass($element, 'pager__item', "Element for page {$page} has .pager__item class.");
$link = $element->find('css', 'a');
$this->assertNotEmpty($link, "Link to page {$page} found.");
// Pager link has an attribute set in pager_test_preprocess_pager().
$this->assertEquals('yes', $link->getAttribute('pager-test'));
$destination = $link->getAttribute('href');
$this->assertEquals('?page=' . ($page - 1), $destination);
}
unset($elements[--$page]);
}
// Verify that no other items remain untested.
$this->assertEmpty($elements, 'All expected items found.');
// Verify first/previous and next/last items and links.
if (isset($first)) {
$this->assertClass($first, 'pager__item--first', 'Element for first page has .pager__item--first class.');
$link = $first->find('css', 'a');
$this->assertNotEmpty($link, 'Link to first page found.');
$this->assertNoClass($link, 'is-active', 'Link to first page is not active.');
$this->assertEquals('first', $link->getAttribute('pager-test'));
$destination = $link->getAttribute('href');
$this->assertEquals('?page=0', $destination);
}
if (isset($previous)) {
$this->assertClass($previous, 'pager__item--previous', 'Element for first page has .pager__item--previous class.');
$link = $previous->find('css', 'a');
$this->assertNotEmpty($link, 'Link to previous page found.');
$this->assertNoClass($link, 'is-active', 'Link to previous page is not active.');
$this->assertEquals('previous', $link->getAttribute('pager-test'));
$destination = $link->getAttribute('href');
// URL query string param is 0-indexed, $current_page is 1-indexed.
$this->assertEquals('?page=' . ($current_page - 2), $destination);
}
if (isset($next)) {
$this->assertClass($next, 'pager__item--next', 'Element for next page has .pager__item--next class.');
$link = $next->find('css', 'a');
$this->assertNotEmpty($link, 'Link to next page found.');
$this->assertNoClass($link, 'is-active', 'Link to next page is not active.');
$this->assertEquals('next', $link->getAttribute('pager-test'));
$destination = $link->getAttribute('href');
// URL query string param is 0-indexed, $current_page is 1-indexed.
$this->assertEquals('?page=' . $current_page, $destination);
}
if (isset($last)) {
$link = $last->find('css', 'a');
$this->assertClass($last, 'pager__item--last', 'Element for last page has .pager__item--last class.');
$this->assertNotEmpty($link, 'Link to last page found.');
$this->assertNoClass($link, 'is-active', 'Link to last page is not active.');
$this->assertEquals('last', $link->getAttribute('pager-test'));
$destination = $link->getAttribute('href');
// URL query string param is 0-indexed.
$this->assertEquals('?page=' . ($total_pages - 1), $destination);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.