function PreviewTest::testPreviewWithPagersUI

Same name in other branches
  1. 9 core/modules/views_ui/tests/src/FunctionalJavascript/PreviewTest.php \Drupal\Tests\views_ui\FunctionalJavascript\PreviewTest::testPreviewWithPagersUI()
  2. 8.9.x core/modules/views_ui/tests/src/FunctionalJavascript/PreviewTest.php \Drupal\Tests\views_ui\FunctionalJavascript\PreviewTest::testPreviewWithPagersUI()
  3. 10 core/modules/views_ui/tests/src/FunctionalJavascript/PreviewTest.php \Drupal\Tests\views_ui\FunctionalJavascript\PreviewTest::testPreviewWithPagersUI()

Tests pagers in the preview form.

File

core/modules/views_ui/tests/src/FunctionalJavascript/PreviewTest.php, line 127

Class

PreviewTest
Tests the UI preview functionality.

Namespace

Drupal\Tests\views_ui\FunctionalJavascript

Code

public function testPreviewWithPagersUI() : void {
    // Create 11 nodes and make sure that everyone is returned.
    $this->drupalCreateContentType([
        'type' => 'page',
    ]);
    for ($i = 0; $i < 11; $i++) {
        $this->drupalCreateNode();
    }
    // Test Full Pager.
    $this->getPreviewAJAX('test_pager_full_ajax', 'default', 5);
    // Test that the pager is present and rendered.
    $elements = $this->xpath('//ul[contains(@class, "pager__items")]/li');
    $this->assertNotEmpty($elements);
    // Verify elements and links to pages.
    // We expect to find 5 elements: current page == 1, links to pages 2 and
    // and 3, links to 'next >' and 'last >>' pages.
    $this->assertClass($elements[0], 'is-active', 'Element for current page has .is-active class.');
    $this->assertNotEmpty($elements[0]->find('css', 'a'), 'Element for current page has link.');
    $this->assertClass($elements[1], 'pager__item', 'Element for page 2 has .pager__item class.');
    $this->assertNotEmpty($elements[1]->find('css', 'a'), 'Link to page 2 found.');
    $this->assertClass($elements[2], 'pager__item', 'Element for page 3 has .pager__item class.');
    $this->assertNotEmpty($elements[2]->find('css', 'a'), 'Link to page 3 found.');
    $this->assertClass($elements[3], 'pager__item--next', 'Element for next page has .pager__item--next class.');
    $this->assertNotEmpty($elements[3]->find('css', 'a'), 'Link to next page found.');
    $this->assertClass($elements[4], 'pager__item--last', 'Element for last page has .pager__item--last class.');
    $this->assertNotEmpty($elements[4]->find('css', 'a'), 'Link to last page found.');
    // Navigate to next page.
    $element = $this->assertSession()
        ->elementExists('xpath', '//li[contains(@class, "pager__item--next")]/a');
    $this->clickPreviewLinkAJAX($element, 5);
    // Test that the pager is present and rendered.
    $elements = $this->xpath('//ul[contains(@class, "pager__items")]/li');
    $this->assertNotEmpty($elements);
    // Verify elements and links to pages.
    // We expect to find 7 elements: links to '<< first' and '< previous'
    // pages, link to page 1, current page == 2, link to page 3 and links
    // to 'next >' and 'last >>' pages.
    $this->assertClass($elements[0], 'pager__item--first', 'Element for first page has .pager__item--first class.');
    $this->assertNotEmpty($elements[0]->find('css', 'a'), 'Link to first page found.');
    $this->assertClass($elements[1], 'pager__item--previous', 'Element for previous page has .pager__item--previous class.');
    $this->assertNotEmpty($elements[1]->find('css', 'a'), 'Link to previous page found.');
    $this->assertClass($elements[2], 'pager__item', 'Element for page 1 has .pager__item class.');
    $this->assertNotEmpty($elements[2]->find('css', 'a'), 'Link to page 1 found.');
    $this->assertClass($elements[3], 'is-active', 'Element for current page has .is-active class.');
    $this->assertNotEmpty($elements[3]->find('css', 'a'), 'Element for current page has link.');
    $this->assertClass($elements[4], 'pager__item', 'Element for page 3 has .pager__item class.');
    $this->assertNotEmpty($elements[4]->find('css', 'a'), 'Link to page 3 found.');
    $this->assertClass($elements[5], 'pager__item--next', 'Element for next page has .pager__item--next class.');
    $this->assertNotEmpty($elements[5]->find('css', 'a'), 'Link to next page found.');
    $this->assertClass($elements[6], 'pager__item--last', 'Element for last page has .pager__item--last class.');
    $this->assertNotEmpty($elements[6]->find('css', 'a'), 'Link to last page found.');
    // Test Mini Pager.
    $this->getPreviewAJAX('test_mini_pager_ajax', 'default', 3);
    // Test that the pager is present and rendered.
    $elements = $this->xpath('//ul[contains(@class, "pager__items")]/li');
    $this->assertNotEmpty($elements);
    // Verify elements and links to pages.
    // We expect to find current pages element with no link, next page element
    // with a link, and not to find previous page element.
    $this->assertEquals('Page 1', trim($elements[0]->getHtml()), 'Element for current page is not a link.');
    $next_page_link = $elements[1]->find('css', 'a');
    $this->assertNotEmpty($next_page_link, 'Link to next page found.');
    $this->assertEquals('Go to next page', $next_page_link->getAttribute('title'));
    // Navigate to next page.
    $this->clickPreviewLinkAJAX($next_page_link, 3);
    // Test that the pager is present and rendered.
    $elements = $this->xpath('//ul[contains(@class, "pager__items")]/li');
    $this->assertNotEmpty($elements);
    // Verify elements and links to pages.
    // We expect to find 3 elements: previous page with a link, current
    // page with no link, and next page with a link.
    $previous_page_link = $elements[0]->find('css', 'a');
    $this->assertNotEmpty($previous_page_link, 'Link to previous page found.');
    $this->assertEquals('Go to previous page', $previous_page_link->getAttribute('title'));
    $this->assertEquals('Page 2', trim($elements[1]->getHtml()), 'Element for current page is not a link.');
    $next_page_link = $elements[2]->find('css', 'a');
    $this->assertNotEmpty($next_page_link, 'Link to next page found.');
    $this->assertEquals('Go to next page', $next_page_link->getAttribute('title'));
}

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