function EntityUrlTest::testUriRelationships

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php \Drupal\Tests\Core\Entity\EntityUrlTest::testUriRelationships()
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php \Drupal\Tests\Core\Entity\EntityUrlTest::testUriRelationships()
  3. 11.x 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 435

Class

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

Namespace

Drupal\Tests\Core\Entity

Code

public function testUriRelationships() : void {
  $entity = $this->getEntity(UrlTestEntity::class, [
    'id' => static::ENTITY_ID,
  ]);
  $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.' . static::ENTITY_TYPE_ID . '.canonical';
  $url_generator->expects($this->any())
    ->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.' . static::ENTITY_TYPE_ID . '.{non_default_parameter}';
  $url_generator->expects($this->any())
    ->method('generateFromRoute')
    ->with($route_name_1)
    ->willThrowException(new MissingMandatoryParametersException($route_name_1, [
    'missing_parameter',
  ]));
  $this->assertEquals([], $entity->uriRelationships());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.