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. 10 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 544

Class

EntityUrlTest
Tests URL handling of the <a href="/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Entity.php/class/Entity/8.9.x" title="Defines a base entity class." class="local">\Drupal\Core\Entity\Entity</a> 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());
}

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