function EntityUrlTest::testToUrlDefaultFallback

Same name 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 131

Class

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

Namespace

Drupal\Tests\Core\Entity

Code

public function testToUrlDefaultFallback() : void {
    $values = [
        'id' => static::ENTITY_ID,
        'langcode' => $this->langcode,
    ];
    $entity = $this->getEntity(StubEntityBase::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.