function EntityUrlTest::testUrl

Tests the url() method.

@covers ::url @covers ::hasLinkTemplate @covers ::linkTemplates

@dataProvider providerTestUrl

@group legacy @expectedDeprecation EntityInterface::url() is deprecated in Drupal 8.0.0 and will be removed in Drupal 9.0.0. EntityInterface::toUrl() instead. Note, a \Drupal\Core\Url object is returned. See https://www.drupal.org/node/2614344

Parameters

string $rel: The link relation to test.

array $options: An array of URL options to call url() with.

array $default_options: An array of URL options that toUrl() should generate.

array $expected_options: An array of combined URL options that should be set on the final URL.

File

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

Class

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

Namespace

Drupal\Tests\Core\Entity

Code

public function testUrl($rel, $options, $default_options, $expected_options) {
    $entity = $this->getEntity(EntityBase::class, [
        'id' => $this->entityId,
    ], [
        'toUrl',
    ]);
    $this->registerLinkTemplate($rel);
    $uri = $this->prophesize(Url::class);
    $uri->getOptions()
        ->willReturn($default_options);
    $uri->setOptions($expected_options)
        ->shouldBeCalled();
    $url_string = "/test-entity/{$this->entityId}/{$rel}";
    $uri->toString()
        ->willReturn($url_string);
    $entity->expects($this->once())
        ->method('toUrl')
        ->with($rel)
        ->willReturn($uri->reveal());
    $this->assertEquals($url_string, $entity->url($rel, $options));
}

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