function EntityUrlTest::testToUrlDefaultFallback

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

Tests the toUrl() method without specifying the $rel parameter.

It should return the edit-form or canonical link templates by default if they are registered.

@covers ::toUrl

File

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

Class

EntityUrlTest
Tests URL handling of the <a href="/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityBase.php/class/EntityBase/11.x" title="Defines a base entity class." class="local">\Drupal\Core\Entity\EntityBase</a> class.

Namespace

Drupal\Tests\Core\Entity

Code

public function testToUrlDefaultFallback() : void {
    $values = [
        'id' => static::ENTITY_ID,
        'langcode' => $this->langcode,
    ];
    $entity = $this->getEntity(UrlTestEntity::class, $values);
    $this->registerLinkTemplate('edit-form');
    
    /** @var \Drupal\Core\Url $url */
    $url = $entity->toUrl();
    $this->assertUrl('entity.test_entity.edit_form', [
        'test_entity' => static::ENTITY_ID,
    ], $entity, TRUE, $url);
    $this->registerLinkTemplate('canonical');
    
    /** @var \Drupal\Core\Url $url */
    $url = $entity->toUrl();
    $this->assertUrl('entity.test_entity.canonical', [
        'test_entity' => static::ENTITY_ID,
    ], $entity, TRUE, $url);
    // Register multiple link templates with 2 that share the same path.
    $this->entityType
        ->getLinkTemplates()
        ->willReturn([
        'canonical' => "/test-entity/{test_entity}/canonical",
        'edit-form' => "/test-entity/{test_entity}/edit-form",
        'foobar' => "/test-entity/{test_entity}/canonical",
    ]);
    $url = $entity->toUrl();
    $this->assertUrl('entity.test_entity.canonical', [
        'test_entity' => static::ENTITY_ID,
    ], $entity, TRUE, $url);
}

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