Security update for Core, with self-updated composer
[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 {
77           throw new PluginNotFoundException($entity_type_id);
78         }
79       });
80     $this->entityTypeManager->getDefinitions()->willReturn($definitions);
81   }
82
83   /**
84    * Tests the getEntityTypeLabels() method.
85    *
86    * @covers ::getEntityTypeLabels
87    */
88   public function testGetEntityTypeLabels() {
89     $apple = $this->prophesize(EntityTypeInterface::class);
90     $apple->getLabel()->willReturn('Apple');
91     $apple->getBundleOf()->willReturn(NULL);
92
93     $banana = $this->prophesize(EntityTypeInterface::class);
94     $banana->getLabel()->willReturn('Banana');
95     $banana->getBundleOf()->willReturn(NULL);
96
97     $this->setUpEntityTypeDefinitions([
98       'apple' => $apple,
99       'banana' => $banana,
100     ]);
101
102     $expected = [
103       'apple' => 'Apple',
104       'banana' => 'Banana',
105     ];
106     $this->assertSame($expected, $this->entityTypeRepository->getEntityTypeLabels());
107   }
108
109   /**
110    * @covers ::getEntityTypeFromClass
111    */
112   public function testGetEntityTypeFromClass() {
113     $apple = $this->prophesize(EntityTypeInterface::class);
114     $banana = $this->prophesize(EntityTypeInterface::class);
115
116     $this->setUpEntityTypeDefinitions([
117       'apple' => $apple,
118       'banana' => $banana,
119     ]);
120
121     $apple->getOriginalClass()->willReturn('\Drupal\apple\Entity\Apple');
122
123     $banana->getOriginalClass()->willReturn('\Drupal\banana\Entity\Banana');
124     $banana->getClass()->willReturn('\Drupal\mango\Entity\Mango');
125     $banana->id()
126       ->willReturn('banana')
127       ->shouldBeCalledTimes(2);
128
129     $entity_type_id = $this->entityTypeRepository->getEntityTypeFromClass('\Drupal\banana\Entity\Banana');
130     $this->assertSame('banana', $entity_type_id);
131     $entity_type_id = $this->entityTypeRepository->getEntityTypeFromClass('\Drupal\mango\Entity\Mango');
132     $this->assertSame('banana', $entity_type_id);
133   }
134
135   /**
136    * @covers ::getEntityTypeFromClass
137    */
138   public function testGetEntityTypeFromClassNoMatch() {
139     $apple = $this->prophesize(EntityTypeInterface::class);
140     $banana = $this->prophesize(EntityTypeInterface::class);
141
142     $this->setUpEntityTypeDefinitions([
143       'apple' => $apple,
144       'banana' => $banana,
145     ]);
146
147     $apple->getOriginalClass()->willReturn('\Drupal\apple\Entity\Apple');
148     $banana->getOriginalClass()->willReturn('\Drupal\banana\Entity\Banana');
149
150     $this->setExpectedException(NoCorrespondingEntityClassException::class, 'The \Drupal\pear\Entity\Pear class does not correspond to an entity type.');
151     $this->entityTypeRepository->getEntityTypeFromClass('\Drupal\pear\Entity\Pear');
152   }
153
154   /**
155    * @covers ::getEntityTypeFromClass
156    */
157   public function testGetEntityTypeFromClassAmbiguous() {
158     $boskoop = $this->prophesize(EntityTypeInterface::class);
159     $boskoop->getOriginalClass()->willReturn('\Drupal\apple\Entity\Apple');
160     $boskoop->id()->willReturn('boskop');
161
162     $gala = $this->prophesize(EntityTypeInterface::class);
163     $gala->getOriginalClass()->willReturn('\Drupal\apple\Entity\Apple');
164     $gala->id()->willReturn('gala');
165
166     $this->setUpEntityTypeDefinitions([
167       'boskoop' => $boskoop,
168       'gala' => $gala,
169     ]);
170
171     $this->setExpectedException(AmbiguousEntityClassException::class, 'Multiple entity types found for \Drupal\apple\Entity\Apple.');
172     $this->entityTypeRepository->getEntityTypeFromClass('\Drupal\apple\Entity\Apple');
173   }
174
175 }