Commit 7aa16e85 authored by alexpott's avatar alexpott

Issue #2924338 by hchonov, cburschka, RoSk0, asherry, Wim Leers:...

Issue #2924338 by hchonov, cburschka, RoSk0, asherry, Wim Leers: Entity::uriRelationships() throws exceptions if an URL cannot be generated because of missing mandatory parameters
parent 59eb794a
......@@ -13,6 +13,7 @@
use Drupal\Core\Link;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
/**
......@@ -344,6 +345,9 @@ public function uriRelationships() {
catch (RouteNotFoundException $e) {
return FALSE;
}
catch (MissingMandatoryParametersException $e) {
return FALSE;
}
return TRUE;
});
}
......
......@@ -2,14 +2,18 @@
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\Entity;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\GeneratedUrl;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Url;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
/**
* Tests URL handling of the \Drupal\Core\Entity\Entity class.
......@@ -496,6 +500,38 @@ public function providerTestUrl() {
return $test_cases;
}
/**
* Tests the uriRelationships() method.
*
* @covers ::uriRelationships
*/
public function testUriRelationships() {
$entity = $this->getEntity(Entity::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());
}
/**
* Returns a mock entity for testing.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment