Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Core / ParamConverter / EntityRevisionParamConverterTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\ParamConverter;
4
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;
12
13 /**
14  * @coversDefaultClass \Drupal\Core\ParamConverter\EntityRevisionParamConverter
15  * @group entity
16  */
17 class EntityRevisionParamConverterTest extends UnitTestCase {
18
19   /**
20    * The tested entity revision param converter.
21    *
22    * @var \Drupal\entity\ParamConverter\EntityRevisionParamConverter
23    */
24   protected $converter;
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function setUp() {
30     parent::setUp();
31
32     $this->converter = new EntityRevisionParamConverter(
33       $this->prophesize(EntityTypeManagerInterface::class)->reveal(),
34       $this->prophesize(EntityRepositoryInterface::class)->reveal()
35     );
36   }
37
38   protected function getTestRoute() {
39     $route = new Route('/test/{test_revision}');
40     $route->setOption('parameters', [
41       'test_revision' => [
42         'type' => 'entity_revision:test',
43       ],
44     ]);
45     return $route;
46   }
47
48   /**
49    * @covers ::applies
50    */
51   public function testNonApplyingRoute() {
52     $route = new Route('/test');
53     $this->assertFalse($this->converter->applies([], 'test_revision', $route));
54   }
55
56   /**
57    * @covers ::applies
58    */
59   public function testApplyingRoute() {
60     $route = $this->getTestRoute();
61     $this->assertTrue($this->converter->applies($route->getOption('parameters')['test_revision'], 'test_revision', $route));
62   }
63
64   /**
65    * @covers ::convert
66    */
67   public function testConvert() {
68     $entity = $this->prophesize(EntityInterface::class)->reveal();
69     $storage = $this->prophesize(EntityStorageInterface::class);
70     $storage->loadRevision(1)->willReturn($entity);
71
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());
77
78     $route = $this->getTestRoute();
79     $result = $converter->convert(1, $route->getOption('parameters')['test_revision'], 'test_revision', ['test_revision' => 1]);
80     $this->assertSame($entity, $result);
81   }
82
83 }