67decbed0ecd3f70a3694240667bd2215b428d21
[yaffs-website] / web / core / tests / Drupal / Tests / Core / ParamConverter / EntityConverterTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\ParamConverter;
4
5 use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
6 use Drupal\Core\ParamConverter\EntityConverter;
7 use Drupal\Core\ParamConverter\ParamNotConvertedException;
8 use Drupal\Tests\UnitTestCase;
9 use Symfony\Component\Routing\Route;
10
11 /**
12  * @coversDefaultClass \Drupal\Core\ParamConverter\EntityConverter
13  * @group ParamConverter
14  * @group Entity
15  */
16 class EntityConverterTest extends UnitTestCase {
17
18   /**
19    * The mocked entity manager.
20    *
21    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
22    */
23   protected $entityManager;
24
25   /**
26    * The tested entity converter.
27    *
28    * @var \Drupal\Core\ParamConverter\EntityConverter
29    */
30   protected $entityConverter;
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setUp() {
36     parent::setUp();
37
38     $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
39
40     $this->entityConverter = new EntityConverter($this->entityManager);
41   }
42
43   /**
44    * Tests the applies() method.
45    *
46    * @dataProvider providerTestApplies
47    *
48    * @covers ::applies
49    */
50   public function testApplies(array $definition, $name, Route $route, $applies) {
51     $this->entityManager->expects($this->any())
52       ->method('hasDefinition')
53       ->willReturnCallback(function($entity_type) {
54         return 'entity_test' == $entity_type;
55       });
56     $this->assertEquals($applies, $this->entityConverter->applies($definition, $name, $route));
57   }
58
59   /**
60    * Provides test data for testApplies()
61    */
62   public function providerTestApplies() {
63     $data = [];
64     $data[] = [['type' => 'entity:foo'], 'foo', new Route('/test/{foo}/bar'), FALSE];
65     $data[] = [['type' => 'entity:entity_test'], 'foo', new Route('/test/{foo}/bar'), TRUE];
66     $data[] = [['type' => 'entity:entity_test'], 'entity_test', new Route('/test/{entity_test}/bar'), TRUE];
67     $data[] = [['type' => 'entity:{entity_test}'], 'entity_test', new Route('/test/{entity_test}/bar'), FALSE];
68     $data[] = [['type' => 'entity:{entity_type}'], 'entity_test', new Route('/test/{entity_type}/{entity_test}/bar'), TRUE];
69     $data[] = [['type' => 'foo'], 'entity_test', new Route('/test/{entity_type}/{entity_test}/bar'), FALSE];
70
71     return $data;
72   }
73
74   /**
75    * Tests the convert() method.
76    *
77    * @dataProvider providerTestConvert
78    *
79    * @covers ::convert
80    */
81   public function testConvert($value, array $definition, array $defaults, $expected_result) {
82     $entity_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
83     $this->entityManager->expects($this->once())
84       ->method('getStorage')
85       ->with('entity_test')
86       ->willReturn($entity_storage);
87     $entity_storage->expects($this->any())
88       ->method('load')
89       ->willReturnMap([
90         ['valid_id', (object) ['id' => 'valid_id']],
91         ['invalid_id', NULL],
92       ]);
93
94     $this->assertEquals($expected_result, $this->entityConverter->convert($value, $definition, 'foo', $defaults));
95   }
96
97   /**
98    * Provides test data for testConvert
99    */
100   public function providerTestConvert() {
101     $data = [];
102     // Existing entity type.
103     $data[] = ['valid_id', ['type' => 'entity:entity_test'], ['foo' => 'valid_id'], (object) ['id' => 'valid_id']];
104     // Invalid ID.
105     $data[] = ['invalid_id', ['type' => 'entity:entity_test'], ['foo' => 'invalid_id'], NULL];
106     // Entity type placeholder.
107     $data[] = ['valid_id', ['type' => 'entity:{entity_type}'], ['foo' => 'valid_id', 'entity_type' => 'entity_test'], (object) ['id' => 'valid_id']];
108
109     return $data;
110   }
111
112   /**
113    * Tests the convert() method with an invalid entity type.
114    */
115   public function testConvertWithInvalidEntityType() {
116     $this->entityManager->expects($this->once())
117       ->method('getStorage')
118       ->with('invalid_id')
119       ->willThrowException(new InvalidPluginDefinitionException('invalid_id'));
120
121     $this->setExpectedException(InvalidPluginDefinitionException::class);
122     $this->entityConverter->convert('id', ['type' => 'entity:invalid_id'], 'foo', ['foo' => 'id']);
123   }
124
125   /**
126    * Tests the convert() method with an invalid dynamic entity type.
127    */
128   public function testConvertWithInvalidDynamicEntityType() {
129     $this->setExpectedException(ParamNotConvertedException::class, 'The "foo" parameter was not converted because the "invalid_id" parameter is missing');
130     $this->entityConverter->convert('id', ['type' => 'entity:{invalid_id}'], 'foo', ['foo' => 'id']);
131   }
132
133 }