[BUGFIX] Adapt glitches of context patch
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Context / ContextTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Context;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Context\Context;
19 use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
20 use TYPO3\CMS\Core\Context\UserAspect;
21 use TYPO3\CMS\Core\Context\WorkspaceAspect;
22 use TYPO3\CMS\Core\Registry;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24
25 class ContextTest extends UnitTestCase
26 {
27 /**
28 * Date provider for hasAspectReturnsTrueOnExistingAspect
29 *
30 * @return array
31 */
32 public function validAspectKeysDataProvider(): array
33 {
34 return [
35 ['myfirst'],
36 ['mysecond'],
37 ['date'],
38 ['visibility'],
39 ['backend.user'],
40 ['frontend.user'],
41 ['workspace'],
42 ];
43 }
44
45 /**
46 * @test
47 * @dataProvider validAspectKeysDataProvider
48 * @param string $aspectName
49 */
50 public function hasAspectReturnsTrueOnExistingAspect(string $aspectName)
51 {
52 $subject = new Context([
53 'myfirst' => new UserAspect(),
54 'mysecond' => new UserAspect(),
55 ]);
56 $this->assertTrue($subject->hasAspect($aspectName));
57 }
58
59 /**
60 * Date provider for hasAspectReturnsFalseOnNonExistingAspect
61 *
62 * @return array
63 */
64 public function invalidAspectKeysDataProvider(): array
65 {
66 return [
67 ['visible'],
68 ['frontenduser'],
69 ['compatibility'],
70 ];
71 }
72
73 /**
74 * @test
75 * @dataProvider invalidAspectKeysDataProvider
76 * @param string $aspectName
77 */
78 public function hasAspectReturnsFalseOnNonExistingAspect(string $aspectName)
79 {
80 $subject = new Context([
81 'myfirst' => new UserAspect(),
82 'mysecond' => new UserAspect(),
83 ]);
84 $this->assertFalse($subject->hasAspect($aspectName));
85 }
86
87 /**
88 * @test
89 */
90 public function constructorAddsValidAspect()
91 {
92 $subject = new Context([
93 'coolio' => new UserAspect(),
94 'uncoolio' => new Registry()
95 ]);
96 $this->assertTrue($subject->hasAspect('coolio'));
97 $this->assertFalse($subject->hasAspect('uncoolio'));
98 }
99
100 /**
101 * @test
102 */
103 public function getAspectThrowsExceptionOnInvalidAspect()
104 {
105 $aspect = new UserAspect();
106 $subject = new Context([
107 'coolio' => $aspect
108 ]);
109
110 $this->expectException(AspectNotFoundException::class);
111 $this->expectExceptionCode(1527777641);
112 $subject->getAspect('uncoolio');
113 }
114
115 /**
116 * @test
117 */
118 public function getAspectReturnsValidAspect()
119 {
120 $aspect = new UserAspect();
121 $subject = new Context([
122 'coolio' => $aspect
123 ]);
124
125 $this->assertSame($aspect, $subject->getAspect('coolio'));
126 }
127
128 /**
129 * @test
130 */
131 public function invalidAspectFromgetPropertyFromAspectThrowsException()
132 {
133 $aspect = new UserAspect();
134 $subject = new Context([
135 'coolio' => $aspect
136 ]);
137
138 $this->expectException(AspectNotFoundException::class);
139 $this->expectExceptionCode(1527777868);
140 $subject->getPropertyFromAspect('uncoolio', 'does not matter');
141 }
142
143 /**
144 * @test
145 */
146 public function invalidPropertyFromgetPropertyFromAspectReturnsDefault()
147 {
148 $defaultValue = 'default value';
149 $aspect = new UserAspect();
150 $subject = new Context([
151 'coolio' => $aspect
152 ]);
153
154 $result = $subject->getPropertyFromAspect('coolio', 'unknownproperty', $defaultValue);
155 $this->assertEquals($defaultValue, $result);
156 }
157
158 /**
159 * @test
160 */
161 public function validPropertyFromgetPropertyFromAspectReturnsValue()
162 {
163 $aspect = new WorkspaceAspect(13);
164 $subject = new Context([
165 'coolio' => $aspect
166 ]);
167
168 $result = $subject->getPropertyFromAspect('coolio', 'id');
169 $this->assertEquals(13, $result);
170 }
171
172 /**
173 * @test
174 */
175 public function setAspectSetsAnAspectAndCanReturnIt()
176 {
177 $aspect = new UserAspect();
178 $subject = new Context();
179
180 $subject->setAspect('coolio', $aspect);
181 $this->assertSame($aspect, $subject->getAspect('coolio'));
182 }
183
184 /**
185 * @test
186 */
187 public function setAspectOverridesAnExisting()
188 {
189 $initialAspect = new UserAspect();
190 $aspectOverride = new UserAspect();
191 $subject = new Context([
192 'coolio' => $initialAspect
193 ]);
194
195 $subject->setAspect('coolio', $aspectOverride);
196 $this->assertNotSame($initialAspect, $subject->getAspect('coolio'));
197 $this->assertSame($aspectOverride, $subject->getAspect('coolio'));
198 }
199 }