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