You are here

public function EntityLinkTest::testLink in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Entity/EntityLinkTest.php \Drupal\Tests\Core\Entity\EntityLinkTest::testLink()

Tests for the Entity::link() method

@covers ::link

@dataProvider providerTestLink

File

core/tests/Drupal/Tests/Core/Entity/EntityLinkTest.php, line 66
Contains \Drupal\Tests\Core\Entity\EntityLinkTest.

Class

EntityLinkTest
@coversDefaultClass \Drupal\Core\Entity\Entity @group Entity

Namespace

Drupal\Tests\Core\Entity

Code

public function testLink($entity_label, $link_text, $expected_text, $link_rel = 'canonical', array $link_options = []) {
  $language = new Language([
    'id' => 'es',
  ]);
  $link_options += [
    'language' => $language,
  ];
  $this->languageManager
    ->expects($this
    ->any())
    ->method('getLanguage')
    ->with('es')
    ->willReturn($language);
  $route_name_map = [
    'canonical' => 'entity.test_entity_type.canonical',
    'edit-form' => 'entity.test_entity_type.edit_form',
  ];
  $route_name = $route_name_map[$link_rel];
  $entity_id = 'test_entity_id';
  $entity_type_id = 'test_entity_type';
  $expected = '<a href="/test_entity_type/test_entity_id">' . $expected_text . '</a>';
  $entity_type = $this
    ->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
  $entity_type
    ->expects($this
    ->once())
    ->method('getLinkTemplates')
    ->willReturn($route_name_map);
  $entity_type
    ->expects($this
    ->any())
    ->method('getKey')
    ->willReturnMap([
    [
      'label',
      'label',
    ],
    [
      'langcode',
      'langcode',
    ],
  ]);
  $this->entityManager
    ->expects($this
    ->any())
    ->method('getDefinition')
    ->with($entity_type_id)
    ->will($this
    ->returnValue($entity_type));

  /** @var \Drupal\Core\Entity\Entity $entity */
  $entity = $this
    ->getMockForAbstractClass('Drupal\\Core\\Entity\\Entity', [
    [
      'id' => $entity_id,
      'label' => $entity_label,
      'langcode' => 'es',
    ],
    $entity_type_id,
  ]);
  $expected_link = Link::createFromRoute($expected_text, $route_name, [
    $entity_type_id => $entity_id,
  ], [
    'entity_type' => $entity_type_id,
    'entity' => $entity,
  ] + $link_options)
    ->setLinkGenerator($this->linkGenerator);
  $this->linkGenerator
    ->expects($this
    ->once())
    ->method('generateFromLink')
    ->with($this
    ->equalTo($expected_link))
    ->willReturn($expected);
  $this
    ->assertSame($expected, $entity
    ->link($link_text, $link_rel, $link_options));
}