3 namespace Drupal\Tests\Core\ParamConverter;
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;
18 * @coversDefaultClass \Drupal\Core\ParamConverter\EntityConverter
19 * @group ParamConverter
22 class EntityConverterTest extends UnitTestCase {
25 * The mocked entity manager.
27 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
29 protected $entityManager;
32 * The tested entity converter.
34 * @var \Drupal\Core\ParamConverter\EntityConverter
36 protected $entityConverter;
41 protected function setUp() {
44 $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
46 $this->entityConverter = new EntityConverter($this->entityManager);
50 * Tests the applies() method.
52 * @dataProvider providerTestApplies
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;
62 $this->assertEquals($applies, $this->entityConverter->applies($definition, $name, $route));
66 * Provides test data for testApplies()
68 public function providerTestApplies() {
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];
81 * Tests the convert() method.
83 * @dataProvider providerTestConvert
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')
92 ->willReturn($entity_storage);
93 $entity_storage->expects($this->any())
96 ['valid_id', (object) ['id' => 'valid_id']],
100 $this->assertEquals($expected_result, $this->entityConverter->convert($value, $definition, 'foo', $defaults));
104 * Provides test data for testConvert
106 public function providerTestConvert() {
108 // Existing entity type.
109 $data[] = ['valid_id', ['type' => 'entity:entity_test'], ['foo' => 'valid_id'], (object) ['id' => 'valid_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']];
119 * Tests the convert() method with an invalid entity type.
121 public function testConvertWithInvalidEntityType() {
122 $this->entityManager->expects($this->once())
123 ->method('getStorage')
125 ->willThrowException(new InvalidPluginDefinitionException('invalid_id'));
127 $this->setExpectedException(InvalidPluginDefinitionException::class);
128 $this->entityConverter->convert('id', ['type' => 'entity:invalid_id'], 'foo', ['foo' => 'id']);
132 * Tests the convert() method with an invalid dynamic entity type.
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']);
140 * Tests that omitting the language manager triggers a deprecation error.
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.
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())
154 $entity->expects($this->any())
155 ->method('isTranslatable')
157 $entity->expects($this->any())
158 ->method('getLoadedRevisionId')
159 ->willReturn('revision_id');
161 $storage = $this->createMock(ContentEntityStorageInterface::class);
162 $storage->expects($this->any())
165 ->willReturn($entity);
166 $storage->expects($this->any())
167 ->method('getLatestRevisionId')
169 ->willReturn('revision_id');
171 $this->entityManager->expects($this->any())
172 ->method('getStorage')
173 ->with('entity_test')
174 ->willReturn($storage);
176 $entity_type = $this->createMock(ContentEntityTypeInterface::class);
177 $entity_type->expects($this->any())
178 ->method('isRevisionable')
181 $this->entityManager->expects($this->any())
182 ->method('getDefinition')
183 ->with('entity_test')
184 ->willReturn($entity_type);
186 $language = $this->createMock(LanguageInterface::class);
187 $language->expects($this->any())
191 $language_manager = $this->createMock(LanguageManagerInterface::class);
192 $language_manager->expects($this->any())
193 ->method('getCurrentLanguage')
194 ->with(LanguageInterface::TYPE_CONTENT)
195 ->willReturn($language);
197 /** @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit_Framework_MockObject_MockObject $container */
198 $container = $this->createMock(ContainerInterface::class);
199 $container->expects($this->any())
201 ->with('language_manager')
202 ->willReturn($language_manager);
204 \Drupal::setContainer($container);
205 $definition = ['type' => 'entity:entity_test', 'load_latest_revision' => TRUE];
206 $this->entityConverter->convert('id', $definition, 'foo', []);