More updates to stop using dev or alpha or beta versions.
[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\Entity\ContentEntityInterface;
7 use Drupal\Core\Entity\ContentEntityStorageInterface;
8 use Drupal\Core\Entity\ContentEntityTypeInterface;
9 use Drupal\Core\Language\LanguageInterface;
10 use Drupal\Core\Language\LanguageManagerInterface;
11 use Drupal\Core\ParamConverter\EntityConverter;
12 use Drupal\Core\ParamConverter\ParamNotConvertedException;
13 use Drupal\Tests\UnitTestCase;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use Symfony\Component\Routing\Route;
16
17 /**
18  * @coversDefaultClass \Drupal\Core\ParamConverter\EntityConverter
19  * @group ParamConverter
20  * @group Entity
21  */
22 class EntityConverterTest extends UnitTestCase {
23
24   /**
25    * The mocked entity manager.
26    *
27    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
28    */
29   protected $entityManager;
30
31   /**
32    * The tested entity converter.
33    *
34    * @var \Drupal\Core\ParamConverter\EntityConverter
35    */
36   protected $entityConverter;
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp() {
42     parent::setUp();
43
44     $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
45
46     $this->entityConverter = new EntityConverter($this->entityManager);
47   }
48
49   /**
50    * Tests the applies() method.
51    *
52    * @dataProvider providerTestApplies
53    *
54    * @covers ::applies
55    */
56   public function testApplies(array $definition, $name, Route $route, $applies) {
57     $this->entityManager->expects($this->any())
58       ->method('hasDefinition')
59       ->willReturnCallback(function ($entity_type) {
60         return 'entity_test' == $entity_type;
61       });
62     $this->assertEquals($applies, $this->entityConverter->applies($definition, $name, $route));
63   }
64
65   /**
66    * Provides test data for testApplies()
67    */
68   public function providerTestApplies() {
69     $data = [];
70     $data[] = [['type' => 'entity:foo'], 'foo', new Route('/test/{foo}/bar'), FALSE];
71     $data[] = [['type' => 'entity:entity_test'], 'foo', new Route('/test/{foo}/bar'), TRUE];
72     $data[] = [['type' => 'entity:entity_test'], 'entity_test', new Route('/test/{entity_test}/bar'), TRUE];
73     $data[] = [['type' => 'entity:{entity_test}'], 'entity_test', new Route('/test/{entity_test}/bar'), FALSE];
74     $data[] = [['type' => 'entity:{entity_type}'], 'entity_test', new Route('/test/{entity_type}/{entity_test}/bar'), TRUE];
75     $data[] = [['type' => 'foo'], 'entity_test', new Route('/test/{entity_type}/{entity_test}/bar'), FALSE];
76
77     return $data;
78   }
79
80   /**
81    * Tests the convert() method.
82    *
83    * @dataProvider providerTestConvert
84    *
85    * @covers ::convert
86    */
87   public function testConvert($value, array $definition, array $defaults, $expected_result) {
88     $entity_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
89     $this->entityManager->expects($this->once())
90       ->method('getStorage')
91       ->with('entity_test')
92       ->willReturn($entity_storage);
93     $entity_storage->expects($this->any())
94       ->method('load')
95       ->willReturnMap([
96         ['valid_id', (object) ['id' => 'valid_id']],
97         ['invalid_id', NULL],
98       ]);
99
100     $this->assertEquals($expected_result, $this->entityConverter->convert($value, $definition, 'foo', $defaults));
101   }
102
103   /**
104    * Provides test data for testConvert
105    */
106   public function providerTestConvert() {
107     $data = [];
108     // Existing entity type.
109     $data[] = ['valid_id', ['type' => 'entity:entity_test'], ['foo' => 'valid_id'], (object) ['id' => 'valid_id']];
110     // Invalid ID.
111     $data[] = ['invalid_id', ['type' => 'entity:entity_test'], ['foo' => 'invalid_id'], NULL];
112     // Entity type placeholder.
113     $data[] = ['valid_id', ['type' => 'entity:{entity_type}'], ['foo' => 'valid_id', 'entity_type' => 'entity_test'], (object) ['id' => 'valid_id']];
114
115     return $data;
116   }
117
118   /**
119    * Tests the convert() method with an invalid entity type.
120    */
121   public function testConvertWithInvalidEntityType() {
122     $this->entityManager->expects($this->once())
123       ->method('getStorage')
124       ->with('invalid_id')
125       ->willThrowException(new InvalidPluginDefinitionException('invalid_id'));
126
127     $this->setExpectedException(InvalidPluginDefinitionException::class);
128     $this->entityConverter->convert('id', ['type' => 'entity:invalid_id'], 'foo', ['foo' => 'id']);
129   }
130
131   /**
132    * Tests the convert() method with an invalid dynamic entity type.
133    */
134   public function testConvertWithInvalidDynamicEntityType() {
135     $this->setExpectedException(ParamNotConvertedException::class, 'The "foo" parameter was not converted because the "invalid_id" parameter is missing');
136     $this->entityConverter->convert('id', ['type' => 'entity:{invalid_id}'], 'foo', ['foo' => 'id']);
137   }
138
139   /**
140    * Tests that omitting the language manager triggers a deprecation error.
141    *
142    * @group legacy
143    *
144    * @expectedDeprecation The language manager parameter has been added to EntityConverter since version 8.5.0 and will be made required in version 9.0.0 when requesting the latest translation-affected revision of an entity.
145    */
146   public function testDeprecatedOptionalLanguageManager() {
147     $entity = $this->createMock(ContentEntityInterface::class);
148     $entity->expects($this->any())
149       ->method('getEntityTypeId')
150       ->willReturn('entity_test');
151     $entity->expects($this->any())
152       ->method('id')
153       ->willReturn('id');
154     $entity->expects($this->any())
155       ->method('isTranslatable')
156       ->willReturn(FALSE);
157     $entity->expects($this->any())
158       ->method('getLoadedRevisionId')
159       ->willReturn('revision_id');
160
161     $storage = $this->createMock(ContentEntityStorageInterface::class);
162     $storage->expects($this->any())
163       ->method('load')
164       ->with('id')
165       ->willReturn($entity);
166     $storage->expects($this->any())
167       ->method('getLatestRevisionId')
168       ->with('id')
169       ->willReturn('revision_id');
170
171     $this->entityManager->expects($this->any())
172       ->method('getStorage')
173       ->with('entity_test')
174       ->willReturn($storage);
175
176     $entity_type = $this->createMock(ContentEntityTypeInterface::class);
177     $entity_type->expects($this->any())
178       ->method('isRevisionable')
179       ->willReturn(TRUE);
180
181     $this->entityManager->expects($this->any())
182       ->method('getDefinition')
183       ->with('entity_test')
184       ->willReturn($entity_type);
185
186     $language = $this->createMock(LanguageInterface::class);
187     $language->expects($this->any())
188       ->method('getId')
189       ->willReturn('en');
190
191     $language_manager = $this->createMock(LanguageManagerInterface::class);
192     $language_manager->expects($this->any())
193       ->method('getCurrentLanguage')
194       ->with(LanguageInterface::TYPE_CONTENT)
195       ->willReturn($language);
196
197     /** @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit_Framework_MockObject_MockObject $container */
198     $container = $this->createMock(ContainerInterface::class);
199     $container->expects($this->any())
200       ->method('get')
201       ->with('language_manager')
202       ->willReturn($language_manager);
203
204     \Drupal::setContainer($container);
205     $definition = ['type' => 'entity:entity_test', 'load_latest_revision' => TRUE];
206     $this->entityConverter->convert('id', $definition, 'foo', []);
207   }
208
209 }