You are here

public function EntityUrlTest::testUriRelationships in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php \Drupal\Tests\Core\Entity\EntityUrlTest::testUriRelationships()

Tests the uriRelationships() method.

@covers ::uriRelationships

File

core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php, line 544

Class

EntityUrlTest
Tests URL handling of the \Drupal\Core\Entity\Entity class.

Namespace

Drupal\Tests\Core\Entity

Code

public function testUriRelationships() {
  $entity = $this
    ->getEntity(EntityBase::class, [
    'id' => $this->entityId,
  ]);
  $container_builder = new ContainerBuilder();
  $url_generator = $this
    ->createMock(UrlGeneratorInterface::class);
  $container_builder
    ->set('url_generator', $url_generator);
  \Drupal::setContainer($container_builder);

  // Test route with no mandatory parameters.
  $this
    ->registerLinkTemplate('canonical');
  $route_name_0 = 'entity.' . $this->entityTypeId . '.canonical';
  $url_generator
    ->expects($this
    ->at(0))
    ->method('generateFromRoute')
    ->with($route_name_0)
    ->willReturn((new GeneratedUrl())
    ->setGeneratedUrl('/entity_test'));
  $this
    ->assertEquals([
    'canonical',
  ], $entity
    ->uriRelationships());

  // Test route with non-default mandatory parameters.
  $this
    ->registerLinkTemplate('{non_default_parameter}');
  $route_name_1 = 'entity.' . $this->entityTypeId . '.{non_default_parameter}';
  $url_generator
    ->expects($this
    ->at(0))
    ->method('generateFromRoute')
    ->with($route_name_1)
    ->willThrowException(new MissingMandatoryParametersException());
  $this
    ->assertEquals([], $entity
    ->uriRelationships());
}