Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Entity / EntityTypeRepositoryTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Entity;
4
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Entity\EntityTypeRepository;
10 use Drupal\Core\Entity\Exception\AmbiguousEntityClassException;
11 use Drupal\Core\Entity\Exception\NoCorrespondingEntityClassException;
12 use Drupal\Tests\UnitTestCase;
13 use Prophecy\Argument;
14
15 /**
16  * @coversDefaultClass \Drupal\Core\Entity\EntityTypeRepository
17  * @group Entity
18  */
19 class EntityTypeRepositoryTest extends UnitTestCase {
20
21   /**
22    * The entity type repository under test.
23    *
24    * @var \Drupal\Core\Entity\EntityTypeRepository
25    */
26   protected $entityTypeRepository;
27
28   /**
29    * The entity type manager.
30    *
31    * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ProphecyInterface
32    */
33   protected $entityTypeManager;
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function setUp() {
39     parent::setUp();
40
41     $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
42
43     $this->entityTypeRepository = new EntityTypeRepository($this->entityTypeManager->reveal());
44   }
45
46   /**
47    * Sets up the entity type manager to be tested.
48    *
49    * @param \Drupal\Core\Entity\EntityTypeInterface[]|\Prophecy\Prophecy\ProphecyInterface[] $definitions
50    *   (optional) An array of entity type definitions.
51    */
52   protected function setUpEntityTypeDefinitions($definitions = []) {
53     $class = $this->getMockClass(EntityInterface::class);
54     foreach ($definitions as $key => $entity_type) {
55       // \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called
56       // by \Drupal\Core\Entity\EntityManager::processDefinition() so it must
57       // always be mocked.
58       $entity_type->getLinkTemplates()->willReturn([]);
59
60       // Give the entity type a legitimate class to return.
61       $entity_type->getClass()->willReturn($class);
62
63       $definitions[$key] = $entity_type->reveal();
64     }
65
66     $this->entityTypeManager->getDefinition(Argument::cetera())
67       ->will(function ($args) use ($definitions) {
68         $entity_type_id = $args[0];
69         $exception_on_invalid = $args[1];
70         if (isset($definitions[$entity_type_id])) {
71           return $definitions[$entity_type_id];
72         }
73         elseif (!$exception_on_invalid) {
74           return NULL;
75         }
76         else throw new PluginNotFoundException($entity_type_id);
77       });
78     $this->entityTypeManager->getDefinitions()->willReturn($definitions);
79   }
80
81   /**
82    * Tests the getEntityTypeLabels() method.
83    *
84    * @covers ::getEntityTypeLabels
85    */
86   public function testGetEntityTypeLabels() {
87     $apple = $this->prophesize(EntityTypeInterface::class);
88     $apple->getLabel()->willReturn('Apple');
89     $apple->getBundleOf()->willReturn(NULL);
90
91     $banana = $this->prophesize(EntityTypeInterface::class);
92     $banana->getLabel()->willReturn('Banana');
93     $banana->getBundleOf()->willReturn(NULL);
94
95     $this->setUpEntityTypeDefinitions([
96       'apple' => $apple,
97       'banana' => $banana,
98     ]);
99
100     $expected = [
101       'apple' => 'Apple',
102       'banana' => 'Banana',
103     ];
104     $this->assertSame($expected, $this->entityTypeRepository->getEntityTypeLabels());
105   }
106
107   /**
108    * @covers ::getEntityTypeFromClass
109    */
110   public function testGetEntityTypeFromClass() {
111     $apple = $this->prophesize(EntityTypeInterface::class);
112     $banana = $this->prophesize(EntityTypeInterface::class);
113
114     $this->setUpEntityTypeDefinitions([
115       'apple' => $apple,
116       'banana' => $banana,
117     ]);
118
119     $apple->getOriginalClass()->willReturn('\Drupal\apple\Entity\Apple');
120
121     $banana->getOriginalClass()->willReturn('\Drupal\banana\Entity\Banana');
122     $banana->getClass()->willReturn('\Drupal\mango\Entity\Mango');
123     $banana->id()
124       ->willReturn('banana')
125       ->shouldBeCalledTimes(2);
126
127     $entity_type_id = $this->entityTypeRepository->getEntityTypeFromClass('\Drupal\banana\Entity\Banana');
128     $this->assertSame('banana', $entity_type_id);
129     $entity_type_id = $this->entityTypeRepository->getEntityTypeFromClass('\Drupal\mango\Entity\Mango');
130     $this->assertSame('banana', $entity_type_id);
131   }
132
133   /**
134    * @covers ::getEntityTypeFromClass
135    */
136   public function testGetEntityTypeFromClassNoMatch() {
137     $apple = $this->prophesize(EntityTypeInterface::class);
138     $banana = $this->prophesize(EntityTypeInterface::class);
139
140     $this->setUpEntityTypeDefinitions([
141       'apple' => $apple,
142       'banana' => $banana,
143     ]);
144
145     $apple->getOriginalClass()->willReturn('\Drupal\apple\Entity\Apple');
146     $banana->getOriginalClass()->willReturn('\Drupal\banana\Entity\Banana');
147
148     $this->setExpectedException(NoCorrespondingEntityClassException::class, 'The \Drupal\pear\Entity\Pear class does not correspond to an entity type.');
149     $this->entityTypeRepository->getEntityTypeFromClass('\Drupal\pear\Entity\Pear');
150   }
151
152   /**
153    * @covers ::getEntityTypeFromClass
154    */
155   public function testGetEntityTypeFromClassAmbiguous() {
156     $boskoop = $this->prophesize(EntityTypeInterface::class);
157     $boskoop->getOriginalClass()->willReturn('\Drupal\apple\Entity\Apple');
158     $boskoop->id()->willReturn('boskop');
159
160     $gala = $this->prophesize(EntityTypeInterface::class);
161     $gala->getOriginalClass()->willReturn('\Drupal\apple\Entity\Apple');
162     $gala->id()->willReturn('gala');
163
164     $this->setUpEntityTypeDefinitions([
165       'boskoop' => $boskoop,
166       'gala' => $gala,
167     ]);
168
169     $this->setExpectedException(AmbiguousEntityClassException::class, 'Multiple entity types found for \Drupal\apple\Entity\Apple.');
170     $this->entityTypeRepository->getEntityTypeFromClass('\Drupal\apple\Entity\Apple');
171   }
172
173 }