Issue by hchonov, cburschka, RoSk0, asherry, Wim Leers: Entity::uriRelationships() throws exceptions if an URL cannot be generated because of missing mandatory parameters

merge-requests/1654/head
Alex Pott 2018-05-07 22:51:58 +01:00
parent 59eb794a25
commit 7aa16e856b
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
2 changed files with 40 additions and 0 deletions
core
lib/Drupal/Core/Entity
tests/Drupal/Tests/Core/Entity

View File

@ -13,6 +13,7 @@ use Drupal\Core\Language\LanguageInterface;
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 @@ abstract class Entity implements EntityInterface {
catch (RouteNotFoundException $e) {
return FALSE;
}
catch (MissingMandatoryParametersException $e) {
return FALSE;
}
return TRUE;
});
}

View File

@ -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 @@ class EntityUrlTest extends UnitTestCase {
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.
*