3 namespace Drupal\Tests\Core\ParamConverter;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityRepositoryInterface;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\ParamConverter\EntityRevisionParamConverter;
10 use Drupal\Tests\UnitTestCase;
11 use Symfony\Component\Routing\Route;
14 * @coversDefaultClass \Drupal\Core\ParamConverter\EntityRevisionParamConverter
17 class EntityRevisionParamConverterTest extends UnitTestCase {
20 * The tested entity revision param converter.
22 * @var \Drupal\entity\ParamConverter\EntityRevisionParamConverter
29 protected function setUp() {
32 $this->converter = new EntityRevisionParamConverter(
33 $this->prophesize(EntityTypeManagerInterface::class)->reveal(),
34 $this->prophesize(EntityRepositoryInterface::class)->reveal()
38 protected function getTestRoute() {
39 $route = new Route('/test/{test_revision}');
40 $route->setOption('parameters', [
42 'type' => 'entity_revision:test',
51 public function testNonApplyingRoute() {
52 $route = new Route('/test');
53 $this->assertFalse($this->converter->applies([], 'test_revision', $route));
59 public function testApplyingRoute() {
60 $route = $this->getTestRoute();
61 $this->assertTrue($this->converter->applies($route->getOption('parameters')['test_revision'], 'test_revision', $route));
67 public function testConvert() {
68 $entity = $this->prophesize(EntityInterface::class)->reveal();
69 $storage = $this->prophesize(EntityStorageInterface::class);
70 $storage->loadRevision(1)->willReturn($entity);
72 $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
73 $entity_type_manager->getStorage('test')->willReturn($storage->reveal());
74 $entity_repository = $this->prophesize(EntityRepositoryInterface::class);
75 $entity_repository->getTranslationFromContext($entity)->willReturn($entity);
76 $converter = new EntityRevisionParamConverter($entity_type_manager->reveal(), $entity_repository->reveal());
78 $route = $this->getTestRoute();
79 $result = $converter->convert(1, $route->getOption('parameters')['test_revision'], 'test_revision', ['test_revision' => 1]);
80 $this->assertSame($entity, $result);