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