You are here

public function PreviewTest::testPreviewWithPagersUI in Drupal 9

Same name and namespace in other branches
  1. 8 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 125

Class

PreviewTest
Tests the UI preview functionality.

Namespace

Drupal\Tests\views_ui\FunctionalJavascript

Code

public function testPreviewWithPagersUI() {

  // 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, :class)]/li', [
    ':class' => 'pager__items',
  ]);
  $this
    ->assertTrue(!empty($elements), 'Full pager found.');

  // 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, :class)]/li', [
    ':class' => 'pager__items',
  ]);
  $this
    ->assertTrue(!empty($elements), 'Full pager found.');

  // 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, :class)]/li', [
    ':class' => 'pager__items',
  ]);
  $this
    ->assertTrue(!empty($elements), 'Mini pager found.');

  // 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
    ->assertClass($elements[0], 'is-active', 'Element for current page has .is-active class.');
  $this
    ->assertClass($elements[1], 'pager__item--next', 'Element for next page has .pager__item--next class.');
  $this
    ->assertNotEmpty($elements[1]
    ->find('css', 'a'), 'Link to next page found.');

  // Navigate to next page.
  $element = $this
    ->assertSession()
    ->elementExists('xpath', '//li[contains(@class, "pager__item--next")]/a');
  $this
    ->clickPreviewLinkAJAX($element, 3);

  // Test that the pager is present and rendered.
  $elements = $this
    ->xpath('//ul[contains(@class, :class)]/li', [
    ':class' => 'pager__items',
  ]);
  $this
    ->assertTrue(!empty($elements), 'Mini pager found.');

  // 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.
  $this
    ->assertClass($elements[0], 'pager__item--previous', 'Element for previous page has .pager__item--previous class.');
  $this
    ->assertNotEmpty($elements[0]
    ->find('css', 'a'), 'Link to previous page found.');
  $this
    ->assertClass($elements[1], 'is-active', 'Element for current page has .is-active class.');
  $this
    ->assertEmpty($elements[1]
    ->find('css', 'a'), 'Element for current page has no link.');
  $this
    ->assertClass($elements[2], 'pager__item--next', 'Element for next page has .pager__item--next class.');
  $this
    ->assertNotEmpty($elements[2]
    ->find('css', 'a'), 'Link to next page found.');
}