You are here

public function EntityPagerLinkTest::testGetLink in Entity Pager 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Kernel/EntityPagerLinkTest.php \Drupal\Tests\entity_pager\Kernel\EntityPagerLinkTest::testGetLink()

@covers ::__construct @covers ::getLink

File

tests/src/Kernel/EntityPagerLinkTest.php, line 34

Class

EntityPagerLinkTest
@coversDefaultClass \Drupal\entity_pager\EntityPagerLink @group entity_pager

Namespace

Drupal\Tests\entity_pager\Kernel

Code

public function testGetLink() {
  $text = $this
    ->randomMachineName();
  $build = (new EntityPagerLink("<b>{$text}</b>"))
    ->getLink();
  $this
    ->setRawContent($this->renderer
    ->renderPlain($build));
  $elements = $this
    ->xpath('//span[@class="inactive"]/b[text()=:text]', [
    ':text' => $text,
  ]);
  $this
    ->assertCount(1, $elements, 'Rendered link result with no entity.');
  $text = $this
    ->randomMachineName();
  $entity = EntityTest::create();
  $entity
    ->save();
  $build = (new EntityPagerLink("<i>{$text}</i>", $entity))
    ->getLink();
  $this
    ->setRawContent($this->renderer
    ->renderPlain($build));
  $elements = $this
    ->cssSelect('a');
  $this
    ->assertCount(1, $elements, 'Link rendered.');

  /** @var \SimpleXMLElement $link */
  $link = reset($elements);
  $href = (string) $link
    ->attributes()->href;
  $this
    ->assertEquals($entity
    ->toUrl()
    ->toString(), $href, 'Link goes to given entity.');
  $this
    ->assertCount(1, $link
    ->xpath("./i[text()='{$text}']"), 'Rendered link content.');
}