b501b3a067e3ef1a074f28879259cc976c45e259
[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 $subject->getAspect('uncoolio');
112 }
113
114 /**
115 * @test
116 */
117 public function getAspectReturnsValidAspect()
118 {
119 $aspect = new UserAspect();
120 $subject = new Context([
121 'coolio' => $aspect
122 ]);
123
124 $this->assertSame($aspect, $subject->getAspect('coolio'));
125 }
126
127 /**
128 * @test
129 */
130 public function invalidAspectFromgetPropertyFromAspectThrowsException()
131 {
132 $aspect = new UserAspect();
133 $subject = new Context([
134 'coolio' => $aspect
135 ]);
136
137 $this->expectException(AspectNotFoundException::class);
138 $subject->getPropertyFromAspect('uncoolio', 'does not matter');
139 }
140
141 /**
142 * @test
143 */
144 public function invalidPropertyFromgetPropertyFromAspectReturnsDefault()
145 {
146 $defaultValue = 'default value';
147 $aspect = new UserAspect();
148 $subject = new Context([
149 'coolio' => $aspect
150 ]);
151
152 $result = $subject->getPropertyFromAspect('coolio', 'unknownproperty', $defaultValue);
153 $this->assertEquals($defaultValue, $result);
154 }
155
156 /**
157 * @test
158 */
159 public function validPropertyFromgetPropertyFromAspectReturnsValue()
160 {
161 $aspect = new WorkspaceAspect(13);
162 $subject = new Context([
163 'coolio' => $aspect
164 ]);
165
166 $result = $subject->getPropertyFromAspect('coolio', 'id');
167 $this->assertEquals(13, $result);
168 }
169
170 /**
171 * @test
172 */
173 public function setAspectSetsAnAspectAndCanReturnIt()
174 {
175 $aspect = new UserAspect();
176 $subject = new Context();
177
178 $subject->setAspect('coolio', $aspect);
179 $this->assertSame($aspect, $subject->getAspect('coolio'));
180 }
181
182 /**
183 * @test
184 */
185 public function setAspectOverridesAnExisting()
186 {
187 $initialAspect = new UserAspect();
188 $aspectOverride = new UserAspect();
189 $subject = new Context([
190 'coolio' => $initialAspect
191 ]);
192
193 $subject->setAspect('coolio', $aspectOverride);
194 $this->assertNotSame($initialAspect, $subject->getAspect('coolio'));
195 $this->assertSame($aspectOverride, $subject->getAspect('coolio'));
196 }
197 }