44c44d170acfa35845ab79b1580ffe7a07aea5dd
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Configuration / TypoScript / ConditionMatching / AbstractConditionMatcherTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Configuration\TypoScript\ConditionMatching;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Core\ApplicationContext;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher;
20 use TYPO3\CMS\Core\Tests\UnitTestCase;
21
22 /**
23 * Test cases
24 */
25 class AbstractConditionMatcherTest extends UnitTestCase {
26
27 /**
28 * @var \TYPO3\CMS\Core\Core\ApplicationContext
29 */
30 protected $backupApplicationContext = NULL;
31
32 /**
33 * @var AbstractConditionMatcher|\PHPUnit_Framework_MockObject_MockObject
34 */
35 protected $conditionMatcher;
36
37 /**
38 * @var \ReflectionMethod
39 */
40 protected $evaluateConditionCommonMethod;
41
42 /**
43 * Set up
44 */
45 protected function setUp() {
46 require_once('Fixtures/ConditionMatcherUserFuncs.php');
47
48 $this->backupApplicationContext = GeneralUtility::getApplicationContext();
49 $this->conditionMatcher = $this->getMockForAbstractClass(AbstractConditionMatcher::class);
50 $this->evaluateConditionCommonMethod = new \ReflectionMethod(AbstractConditionMatcher::class, 'evaluateConditionCommon');
51 $this->evaluateConditionCommonMethod->setAccessible(TRUE);
52 }
53
54 /**
55 * Tear down
56 */
57 protected function tearDown() {
58 Fixtures\GeneralUtilityFixture::setApplicationContext($this->backupApplicationContext);
59 parent::tearDown();
60 }
61
62 /**
63 * Data provider with matching applicationContext conditions.
64 *
65 * @return array[]
66 */
67 public function matchingApplicationContextConditionsDataProvider() {
68 return array(
69 array('Production*'),
70 array('Production/Staging/*'),
71 array('Production/Staging/Server2'),
72 array('/^Production.*$/'),
73 array('/^Production\\/.+\\/Server\\d+$/'),
74 );
75 }
76
77 /**
78 * @test
79 * @dataProvider matchingApplicationContextConditionsDataProvider
80 */
81 public function evaluateConditionCommonReturnsTrueForMatchingContexts($matchingContextCondition) {
82 /** @var \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext */
83 $applicationContext = new ApplicationContext('Production/Staging/Server2');
84 Fixtures\GeneralUtilityFixture::setApplicationContext($applicationContext);
85
86 $this->assertTrue(
87 $this->evaluateConditionCommonMethod->invokeArgs($this->conditionMatcher, array('applicationContext', $matchingContextCondition))
88 );
89 }
90
91 /**
92 * Data provider with not matching applicationContext conditions.
93 *
94 * @return array[]
95 */
96 public function notMatchingApplicationContextConditionsDataProvider() {
97 return array(
98 array('Production'),
99 array('Testing*'),
100 array('Development/Profiling, Testing/Unit'),
101 array('Testing/Staging/Server2'),
102 array('/^Testing.*$/'),
103 array('/^Production\\/.+\\/Host\\d+$/'),
104 );
105 }
106
107 /**
108 * @test
109 * @dataProvider notMatchingApplicationContextConditionsDataProvider
110 */
111 public function evaluateConditionCommonReturnsNullForNotMatchingApplicationContexts($notMatchingApplicationContextCondition) {
112 /** @var \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext */
113 $applicationContext = new ApplicationContext('Production/Staging/Server2');
114 Fixtures\GeneralUtilityFixture::setApplicationContext($applicationContext);
115
116 $this->assertFalse(
117 $this->evaluateConditionCommonMethod->invokeArgs($this->conditionMatcher, array('applicationContext', $notMatchingApplicationContextCondition))
118 );
119 }
120
121 /**
122 * Data provider for evaluateConditionCommonEvaluatesIpAddressesCorrectly
123 *
124 * @return array[]
125 */
126 public function evaluateConditionCommonDevIpMaskDataProvider() {
127 return array(
128 // [0] $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']
129 // [1] Actual IP
130 // [2] Expected condition result
131 'IP matches' => array(
132 '127.0.0.1',
133 '127.0.0.1',
134 TRUE,
135 ),
136 'ipv4 wildcard subnet' => array(
137 '127.0.0.1/24',
138 '127.0.0.2',
139 TRUE,
140 ),
141 'ipv6 wildcard subnet' => array(
142 '0:0::1/128',
143 '::1',
144 TRUE,
145 ),
146 'List of addresses matches' => array(
147 '1.2.3.4, 5.6.7.8',
148 '5.6.7.8',
149 TRUE,
150 ),
151 'IP does not match' => array(
152 '127.0.0.1',
153 '127.0.0.2',
154 FALSE,
155 ),
156 'ipv4 subnet does not match' => array(
157 '127.0.0.1/8',
158 '126.0.0.1',
159 FALSE,
160 ),
161 'ipv6 subnet does not match' => array(
162 '::1/127',
163 '::2',
164 FALSE
165 ),
166 'List of addresses does not match' => array(
167 '127.0.0.1, ::1',
168 '::2',
169 FALSE,
170 ),
171 );
172 }
173
174 /**
175 * @test
176 * @dataProvider evaluateConditionCommonDevIpMaskDataProvider
177 */
178 public function evaluateConditionCommonEvaluatesIpAddressesCorrectly($devIpMask, $actualIp, $expectedResult) {
179 // Do not trigger proxy stuff of GeneralUtility::getIndPEnv
180 unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP']);
181
182 $_SERVER['REMOTE_ADDR'] = $actualIp;
183 $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] = $devIpMask;
184
185 $actualResult = $this->evaluateConditionCommonMethod->invokeArgs($this->conditionMatcher, array('IP', 'devIP'));
186 $this->assertSame($expectedResult, $actualResult);
187 }
188
189 /**
190 * @test
191 */
192 public function testUserFuncIsCalled() {
193 $this->assertTrue(
194 $this->evaluateConditionCommonMethod->invokeArgs(
195 $this->conditionMatcher,
196 array('userFunc','user_testFunction')
197 )
198 );
199 }
200
201 /**
202 * @test
203 */
204 public function testUserFuncWithSingleArgument() {
205 $this->assertTrue(
206 $this->evaluateConditionCommonMethod->invokeArgs(
207 $this->conditionMatcher,
208 array('userFunc', 'user_testFunctionWithSingleArgument(x)')
209 )
210 );
211 }
212
213 /**
214 * @test
215 */
216 public function testUserFuncWithMultipleArguments() {
217 $this->assertTrue(
218 $this->evaluateConditionCommonMethod->invokeArgs(
219 $this->conditionMatcher,
220 array('userFunc', 'user_testFunctionWithThreeArguments(1,2,3)')
221 )
222 );
223 }
224
225 /**
226 * @test
227 */
228 public function testUserFuncReturnsFalse() {
229 $this->assertFalse(
230 $this->evaluateConditionCommonMethod->invokeArgs(
231 $this->conditionMatcher,
232 array('userFunc', 'user_testFunctionFalse')
233 )
234 );
235 }
236
237 /**
238 * @test
239 */
240 public function testUserFuncWithMultipleArgumentsAndQuotes() {
241 $this->assertTrue(
242 $this->evaluateConditionCommonMethod->invokeArgs(
243 $this->conditionMatcher,
244 array('userFunc', 'user_testFunctionWithThreeArguments(1,2,"3,4,5,6")')
245 )
246 );
247 }
248
249 /**
250 * @test
251 */
252 public function testUserFuncWithMultipleArgumentsAndQuotesAndSpaces() {
253 $this->assertTrue(
254 $this->evaluateConditionCommonMethod->invokeArgs(
255 $this->conditionMatcher,
256 array('userFunc', 'user_testFunctionWithThreeArguments ( 1 , 2, "3, 4, 5, 6" )')
257 )
258 );
259 }
260
261 /**
262 * @test
263 */
264 public function testUserFuncWithMultipleArgumentsAndQuotesAndSpacesStripped() {
265 $this->assertTrue(
266 $this->evaluateConditionCommonMethod->invokeArgs(
267 $this->conditionMatcher,
268 array('userFunc', 'user_testFunctionWithThreeArgumentsSpaces ( 1 , 2, "3, 4, 5, 6" )')
269 )
270 );
271 }
272
273 /**
274 * @test
275 */
276 public function testUserFuncWithSpacesInQuotes() {
277 $this->assertTrue(
278 $this->evaluateConditionCommonMethod->invokeArgs(
279 $this->conditionMatcher,
280 array('userFunc', 'user_testFunctionWithSpaces(" 3, 4, 5, 6 ")')
281 )
282 );
283 }
284
285 /**
286 * @test
287 */
288 public function testUserFuncWithMultipleArgumentsAndQuotesAndSpacesStrippedAndEscapes() {
289 $this->assertTrue(
290 $this->evaluateConditionCommonMethod->invokeArgs(
291 $this->conditionMatcher,
292 array('userFunc', 'user_testFunctionWithThreeArgumentsSpaces ( 1 , 2, "3, \"4, 5\", 6" )')
293 )
294 );
295 }
296
297 /**
298 * @test
299 */
300 public function testUserFuncWithQuoteMissing() {
301 $this->assertTrue(
302 $this->evaluateConditionCommonMethod->invokeArgs(
303 $this->conditionMatcher,
304 array('userFunc', 'user_testFunctionWithQuoteMissing ("value \")')
305 )
306 );
307 }
308
309 /**
310 * @test
311 */
312 public function testUserFuncWithQuotesInside() {
313 $this->assertTrue(
314 $this->evaluateConditionCommonMethod->invokeArgs(
315 $this->conditionMatcher,
316 array('userFunc', 'user_testQuotes("1 \" 2")')
317 )
318 );
319 }
320
321 /**
322 * @test
323 */
324 public function testUserFuncWithClassMethodCall() {
325 $this->assertTrue(
326 $this->evaluateConditionCommonMethod->invokeArgs(
327 $this->conditionMatcher,
328 array('userFunc', 'ConditionMatcherUserFunctions::isTrue(1)')
329 )
330 );
331 }
332 }