View source
<?php
namespace Drupal\Tests\system\Functional\Pager;
use Behat\Mink\Element\NodeElement;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
class PagerTest extends BrowserTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected static $modules = [
'dblog',
'image',
'pager_test',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $profile = 'testing';
protected function setUp() : void {
parent::setUp();
$logger = $this->container
->get('logger.factory')
->get('pager_test');
for ($i = 0; $i < 300; $i++) {
$logger
->debug($this
->randomString());
}
$this->adminUser = $this
->drupalCreateUser([
'access site reports',
'administer image styles',
]);
$this
->drupalLogin($this->adminUser);
}
public function testActiveClass() {
$this
->drupalGet('admin/reports/dblog');
$current_page = 0;
$this
->assertPagerItems($current_page);
$current_page++;
$this
->drupalGet('admin/reports/dblog', [
'query' => [
'page' => $current_page,
],
]);
$this
->assertPagerItems($current_page);
$element = $this
->assertSession()
->elementExists('xpath', '//li[contains(@class, "pager__item--last")]/a');
preg_match('@page=(\\d+)@', $element
->getAttribute('href'), $matches);
$current_page = (int) $matches[1];
$this
->drupalGet($GLOBALS['base_root'] . parse_url($this
->getUrl())['path'] . $element
->getAttribute('href'), [
'external' => TRUE,
]);
$this
->assertPagerItems($current_page);
$this
->drupalGet('admin/config/media/image-styles');
$this
->assertSession()
->elementNotExists('css', '.pager');
}
public function testPagerQueryParametersAndCacheContext() {
$this
->drupalGet('pager-test/query-parameters');
$this
->assertSession()
->pageTextContains('Pager calls: 0');
$this
->assertSession()
->pageTextContains('[url.query_args.pagers:0]=0.0');
$this
->assertCacheContext('url.query_args');
$this
->assertSession()
->elementExists('xpath', '//li[contains(@class, "pager__item--last")]/a')
->click();
$this
->assertSession()
->pageTextContains('Pager calls: 1');
$this
->assertSession()
->pageTextContains('[url.query_args.pagers:0]=0.60');
$this
->assertCacheContext('url.query_args');
$this
->drupalGet('pager-test/query-parameters');
$this
->assertSession()
->elementExists('xpath', '//li[contains(@class, "pager__item--last")]/a')
->click();
$this
->assertSession()
->elementExists('xpath', '//li[contains(@class, "pager__item--first")]/a')
->click();
$this
->assertSession()
->pageTextContains('Pager calls: 2');
$this
->assertSession()
->pageTextContains('[url.query_args.pagers:0]=0.0');
$this
->assertCacheContext('url.query_args');
}
public function testMultiplePagers() {
$this
->drupalGet('pager-test/multiple-pagers');
$test_data = [
[
'input_query' => '',
'expected_page' => [
0 => '1',
1 => '1',
4 => '1',
],
'expected_query' => '?page=0,0,,,0',
],
[
'input_query' => '?page=2 , 10,,, 5 ,,',
'expected_page' => [
0 => '3',
1 => '11',
4 => '6',
],
'expected_query' => '?page=2,10,,,5',
],
[
'input_query' => '?page=2 , 3 0,,, 4 13 ,,',
'expected_page' => [
0 => '3',
1 => '4',
4 => '5',
],
'expected_query' => '?page=2,3,,,4',
],
[
'input_query' => '?page=2.1,6.999,,,5.',
'expected_page' => [
0 => '3',
1 => '7',
4 => '6',
],
'expected_query' => '?page=2,6,,,5',
],
[
'input_query' => '?page=5,2',
'expected_page' => [
0 => '6',
1 => '3',
4 => '1',
],
'expected_query' => '?page=5,2,,,0',
],
[
'input_query' => '?page=,2',
'expected_page' => [
0 => '1',
1 => '3',
4 => '1',
],
'expected_query' => '?page=0,2,,,0',
],
[
'input_query' => '?page=,',
'expected_page' => [
0 => '1',
1 => '1',
4 => '1',
],
'expected_query' => '?page=0,0,,,0',
],
[
'input_query' => '?page=99,99,,,99',
'expected_page' => [
0 => '16',
1 => '16',
4 => '16',
],
'expected_query' => '?page=15,15,,,15',
],
[
'input_query' => '?page=bar,5,foo,qux,bet',
'expected_page' => [
0 => '1',
1 => '6',
4 => '1',
],
'expected_query' => '?page=0,5,,,0',
],
];
foreach ($test_data as $data) {
$input_query = str_replace(' ', '%20', $data['input_query']);
$this
->drupalGet($GLOBALS['base_root'] . parse_url($this
->getUrl())['path'] . $input_query, [
'external' => TRUE,
]);
foreach ([
0,
1,
4,
] as $pager_element) {
$active_page = $this
->cssSelect("div.test-pager-{$pager_element} ul.pager__items li.is-active:contains('{$data['expected_page'][$pager_element]}')");
$destination = str_replace('%2C', ',', $active_page[0]
->find('css', 'a')
->getAttribute('href'));
$this
->assertEquals($data['expected_query'], $destination);
}
}
}
public function testPagerEllipsis() {
$logger = $this->container
->get('logger.factory')
->get('pager_test');
for ($i = 0; $i < 100; $i++) {
$logger
->debug($this
->randomString());
}
$this
->drupalGet('admin/reports/dblog');
$elements = $this
->cssSelect(".pager__item--ellipsis:contains('…')");
$this
->assertCount(0, $elements, 'No ellipsis has been set.');
$logger = $this->container
->get('logger.factory')
->get('pager_test');
for ($i = 0; $i < 50; $i++) {
$logger
->debug($this
->randomString());
}
$this
->drupalGet('admin/reports/dblog');
$elements = $this
->cssSelect(".pager__item--ellipsis:contains('…')");
$this
->assertCount(1, $elements, 'Found the ellipsis.');
}
protected function assertPagerItems(int $current_page) : void {
$elements = $this
->xpath('//ul[contains(@class, :class)]/li', [
':class' => 'pager__items',
]);
$this
->assertNotEmpty($elements, 'Pager found.');
$current_page++;
if ($current_page > 1) {
$first = array_shift($elements);
$previous = array_shift($elements);
}
if ($current_page != count($elements)) {
$last = array_pop($elements);
$next = array_pop($elements);
}
$total_pages = count($elements);
foreach ($elements as $page => $element) {
$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');
$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.");
$this
->assertEquals('yes', $link
->getAttribute('pager-test'));
$destination = $link
->getAttribute('href');
$this
->assertEquals('?page=' . ($page - 1), $destination);
}
unset($elements[--$page]);
}
$this
->assertEmpty($elements, 'All expected items found.');
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');
$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');
$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');
$this
->assertEquals('?page=' . ($total_pages - 1), $destination);
}
}
protected function assertClass(NodeElement $element, string $class, string $message = NULL) : void {
if (!isset($message)) {
$message = "Class .{$class} found.";
}
$this
->assertTrue($element
->hasClass($class), $message);
}
protected function assertNoClass(NodeElement $element, string $class, string $message = NULL) : void {
if (!isset($message)) {
$message = "Class .{$class} not found.";
}
$this
->assertFalse($element
->hasClass($class), $message);
}
}