You are here

public function EntityPagerTest::testPagingLinks in Entity Pager 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Functional/EntityPagerTest.php \Drupal\Tests\entity_pager\Functional\EntityPagerTest::testPagingLinks()

Tests paging links.

File

tests/src/Functional/EntityPagerTest.php, line 83

Class

EntityPagerTest
Tests the entity pager view style.

Namespace

Drupal\Tests\entity_pager\Functional

Code

public function testPagingLinks() {
  $this
    ->drupalPlaceBlock('views_block:entity_pager_example-entity_pager_example_block', [
    'id' => 'test',
  ]);
  $this
    ->drupalGet($this->nodes[1]
    ->toUrl());
  $elements = $this
    ->cssSelect('#block-test .entity-pager-item-prev a');
  $this
    ->assertCount(1, $elements, 'Previous link exists.');
  $link = reset($elements);
  $expected = $this->nodes[0]
    ->toUrl()
    ->toString();
  $this
    ->assertEquals($expected, $link
    ->getAttribute('href'), 'Previous link points to previous view row.');
  $this
    ->assertEquals('< prev', trim($link
    ->getText()), 'Previous link text from configuration.');
  $elements = $this
    ->cssSelect('#block-test .entity-pager-item-next a');
  $this
    ->assertCount(1, $elements, 'Next link exists.');
  $link = reset($elements);
  $expected = $this->nodes[2]
    ->toUrl()
    ->toString();
  $this
    ->assertEquals($expected, $link
    ->getAttribute('href'), 'Next link points to next view row.');
  $this
    ->assertEquals('next >', trim($link
    ->getText()), 'Next link text from configuration.');
  $this
    ->updateExampleView([
    'link_prev' => '<i>Text</i>',
    'link_next' => '<b>Text</b>',
  ]);
  $this
    ->drupalGet($this
    ->getUrl());
  $elements = $this
    ->cssSelect('#block-test .entity-pager-item-prev a > i');
  $this
    ->assertCount(1, $elements, 'HTML is displayed in previous link text.');
  $elements = $this
    ->cssSelect('#block-test .entity-pager-item-next a > b');
  $this
    ->assertCount(1, $elements, 'HTML is displayed in next link text.');
  $this
    ->updateExampleView([
    'link_prev' => '[site:name]',
    'link_next' => '[site:name]',
  ]);
  $this
    ->drupalGet($this
    ->getUrl());
  $expected = \Drupal::token()
    ->replace('[site:name]');
  $link = $this
    ->cssSelect('#block-test .entity-pager-item-prev a')[0];
  $this
    ->assertEquals($expected, trim($link
    ->getText()), 'Global token replacement for previous link.');
  $link = $this
    ->cssSelect('#block-test .entity-pager-item-next a')[0];
  $this
    ->assertEquals($expected, trim($link
    ->getText()), 'Global token replacement for next link.');
  $this
    ->updateExampleView([
    'link_prev' => 'Node [node:nid]',
    'link_next' => 'Node [node:nid]',
  ]);
  $this
    ->drupalGet($this
    ->getUrl());
  $link = $this
    ->cssSelect('#block-test .entity-pager-item-prev a')[0];
  $this
    ->assertEquals('Node 1', trim($link
    ->getText()), 'Node context token replacement for previous link.');
  $link = $this
    ->cssSelect('#block-test .entity-pager-item-next a')[0];
  $this
    ->assertEquals('Node 3', trim($link
    ->getText()), 'Node context token replacement for next link.');
  $this
    ->updateExampleView([
    'link_prev' => 'Node [node:nid] [node:_invalid_token_]',
    'link_next' => 'Node [node:nid] [node:_invalid_token_]',
  ]);
  $this
    ->drupalGet($this
    ->getUrl());
  $link = $this
    ->cssSelect('#block-test .entity-pager-item-prev a')[0];
  $this
    ->assertEquals('Node 1', trim($link
    ->getText()), 'Node context token replacement for previous link clearing invalid tokens.');
  $link = $this
    ->cssSelect('#block-test .entity-pager-item-next a')[0];
  $this
    ->assertEquals('Node 3', trim($link
    ->getText()), 'Node context token replacement for next link clearing invalid tokens.');
  $first = reset($this->nodes);
  $last = end($this->nodes);
  $this
    ->drupalGet($first
    ->toUrl());
  $elements = $this
    ->cssSelect('#block-test .entity-pager-item-prev span.inactive');
  $this
    ->assertCount(1, $elements, 'Previous link inactive substitute exists.');
  $this
    ->drupalGet($last
    ->toUrl());
  $elements = $this
    ->cssSelect('#block-test .entity-pager-item-next span.inactive');
  $this
    ->assertCount(1, $elements, 'Next link inactive substitute exists.');
  $this
    ->updateExampleView([
    'show_disabled_links' => FALSE,
  ]);
  $this
    ->drupalGet($first
    ->toUrl());
  $elements = $this
    ->cssSelect('#block-test .entity-pager-item-prev');
  $this
    ->assertEmpty($elements, 'Previous link on first page does not exist with show_disabled_links disabled.');
  $this
    ->drupalGet($last
    ->toUrl());
  $elements = $this
    ->cssSelect('#block-test .entity-pager-item-next');
  $this
    ->assertEmpty($elements, 'Next link on last page does not exist with show_disabled_links disabled.');
  $this
    ->updateExampleView([
    'circular_paging' => TRUE,
  ]);
  $this
    ->drupalGet($first
    ->toUrl());
  $elements = $this
    ->cssSelect('#block-test .entity-pager-item-prev a');
  $this
    ->assertCount(1, $elements, 'Previous link exists.');
  $link = reset($elements);
  $expected = $last
    ->toUrl()
    ->toString();
  $this
    ->assertEquals($expected, $link
    ->getAttribute('href'), 'Previous link with circular paging on first page links to last result.');
  $this
    ->drupalGet($last
    ->toUrl());
  $elements = $this
    ->cssSelect('#block-test .entity-pager-item-next a');
  $this
    ->assertCount(1, $elements, 'Next link exists.');
  $link = reset($elements);
  $expected = $first
    ->toUrl()
    ->toString();
  $this
    ->assertEquals($expected, $link
    ->getAttribute('href'), 'Next link with circular paging on last page links to first result.');
}