3 namespace Drupal\Tests\Core\ParamConverter;
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;
12 * @coversDefaultClass \Drupal\Core\ParamConverter\EntityConverter
13 * @group ParamConverter
16 class EntityConverterTest extends UnitTestCase {
19 * The mocked entity manager.
21 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
23 protected $entityManager;
26 * The tested entity converter.
28 * @var \Drupal\Core\ParamConverter\EntityConverter
30 protected $entityConverter;
35 protected function setUp() {
38 $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
40 $this->entityConverter = new EntityConverter($this->entityManager);
44 * Tests the applies() method.
46 * @dataProvider providerTestApplies
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;
56 $this->assertEquals($applies, $this->entityConverter->applies($definition, $name, $route));
60 * Provides test data for testApplies()
62 public function providerTestApplies() {
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];
75 * Tests the convert() method.
77 * @dataProvider providerTestConvert
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')
86 ->willReturn($entity_storage);
87 $entity_storage->expects($this->any())
90 ['valid_id', (object) ['id' => 'valid_id']],
94 $this->assertEquals($expected_result, $this->entityConverter->convert($value, $definition, 'foo', $defaults));
98 * Provides test data for testConvert
100 public function providerTestConvert() {
102 // Existing entity type.
103 $data[] = ['valid_id', ['type' => 'entity:entity_test'], ['foo' => 'valid_id'], (object) ['id' => 'valid_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']];
113 * Tests the convert() method with an invalid entity type.
115 public function testConvertWithInvalidEntityType() {
116 $this->entityManager->expects($this->once())
117 ->method('getStorage')
119 ->willThrowException(new InvalidPluginDefinitionException('invalid_id'));
121 $this->setExpectedException(InvalidPluginDefinitionException::class);
122 $this->entityConverter->convert('id', ['type' => 'entity:invalid_id'], 'foo', ['foo' => 'id']);
126 * Tests the convert() method with an invalid dynamic entity type.
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']);