TYPO3 CMS  TYPO3_8-7
AbstractConditionMatcherTest.php
Go to the documentation of this file.
1 <?php
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 
20 
24 class AbstractConditionMatcherTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
25 {
29  protected $backupApplicationContext = null;
30 
34  protected $conditionMatcher;
35 
40 
44  protected function setUp()
45  {
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 
57  protected function tearDown()
58  {
59  Fixtures\GeneralUtilityFixture::setApplicationContext($this->backupApplicationContext);
60  parent::tearDown();
61  }
62 
69  {
70  return [
71  ['Production*'],
72  ['Production/Staging/*'],
73  ['Production/Staging/Server2'],
74  ['/^Production.*$/'],
75  ['/^Production\\/.+\\/Server\\d+$/'],
76  ];
77  }
78 
83  public function evaluateConditionCommonReturnsTrueForMatchingContexts($matchingContextCondition)
84  {
86  $applicationContext = new ApplicationContext('Production/Staging/Server2');
88 
89  $this->assertTrue(
90  $this->evaluateConditionCommonMethod->invokeArgs($this->conditionMatcher, ['applicationContext', $matchingContextCondition])
91  );
92  }
93 
100  {
101  return [
102  ['Production'],
103  ['Testing*'],
104  ['Development/Profiling, Testing/Unit'],
105  ['Testing/Staging/Server2'],
106  ['/^Testing.*$/'],
107  ['/^Production\\/.+\\/Host\\d+$/'],
108  ];
109  }
110 
115  public function evaluateConditionCommonReturnsNullForNotMatchingApplicationContexts($notMatchingApplicationContextCondition)
116  {
118  $applicationContext = new ApplicationContext('Production/Staging/Server2');
120 
121  $this->assertFalse(
122  $this->evaluateConditionCommonMethod->invokeArgs($this->conditionMatcher, ['applicationContext', $notMatchingApplicationContextCondition])
123  );
124  }
125 
132  {
133  return [
134  // [0] $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']
135  // [1] Actual IP
136  // [2] Expected condition result
137  'IP matches' => [
138  '127.0.0.1',
139  '127.0.0.1',
140  true,
141  ],
142  'ipv4 wildcard subnet' => [
143  '127.0.0.1/24',
144  '127.0.0.2',
145  true,
146  ],
147  'ipv6 wildcard subnet' => [
148  '0:0::1/128',
149  '::1',
150  true,
151  ],
152  'List of addresses matches' => [
153  '1.2.3.4, 5.6.7.8',
154  '5.6.7.8',
155  true,
156  ],
157  'IP does not match' => [
158  '127.0.0.1',
159  '127.0.0.2',
160  false,
161  ],
162  'ipv4 subnet does not match' => [
163  '127.0.0.1/8',
164  '126.0.0.1',
165  false,
166  ],
167  'ipv6 subnet does not match' => [
168  '::1/127',
169  '::2',
170  false
171  ],
172  'List of addresses does not match' => [
173  '127.0.0.1, ::1',
174  '::2',
175  false,
176  ],
177  ];
178  }
179 
184  public function evaluateConditionCommonEvaluatesIpAddressesCorrectly($devIpMask, $actualIp, $expectedResult)
185  {
186  // Do not trigger proxy stuff of GeneralUtility::getIndPEnv
187  unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP']);
188 
189  $_SERVER['REMOTE_ADDR'] = $actualIp;
190  $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] = $devIpMask;
191 
192  $actualResult = $this->evaluateConditionCommonMethod->invokeArgs($this->conditionMatcher, ['IP', 'devIP']);
193  $this->assertSame($expectedResult, $actualResult);
194  }
195 
199  public function testUserFuncIsCalled()
200  {
201  $this->assertTrue(
202  $this->evaluateConditionCommonMethod->invokeArgs(
203  $this->conditionMatcher,
204  ['userFunc', 'user_testFunction']
205  )
206  );
207  }
208 
213  {
214  $this->assertTrue(
215  $this->evaluateConditionCommonMethod->invokeArgs(
216  $this->conditionMatcher,
217  ['userFunc', 'user_testFunctionWithSingleArgument(x)']
218  )
219  );
220  }
221 
226  {
227  $this->assertTrue(
228  $this->evaluateConditionCommonMethod->invokeArgs(
229  $this->conditionMatcher,
230  ['userFunc', 'user_testFunctionWithSingleArgument(0)']
231  )
232  );
233  }
234 
239  {
240  $this->assertTrue(
241  $this->evaluateConditionCommonMethod->invokeArgs(
242  $this->conditionMatcher,
243  ['userFunc', 'user_testFunctionWithNoArgument( )']
244  )
245  );
246  }
247 
252  {
253  $this->assertTrue(
254  $this->evaluateConditionCommonMethod->invokeArgs(
255  $this->conditionMatcher,
256  ['userFunc', 'user_testFunctionWithThreeArguments(1,2,3)']
257  )
258  );
259  }
260 
265  {
266  $this->assertTrue(
267  $this->evaluateConditionCommonMethod->invokeArgs(
268  $this->conditionMatcher,
269  ['userFunc', 'user_testFunctionWithThreeArguments(0,true,"foo")']
270  )
271  );
272  }
273 
278  {
279  $this->assertTrue(
280  $this->evaluateConditionCommonMethod->invokeArgs(
281  $this->conditionMatcher,
282  ['userFunc', 'user_testFunctionWithThreeArguments(0,"foo",true)']
283  )
284  );
285  }
286 
291  {
292  $this->assertTrue(
293  $this->evaluateConditionCommonMethod->invokeArgs(
294  $this->conditionMatcher,
295  ['userFunc', 'user_testFunctionWithThreeArguments("foo",true,0)']
296  )
297  );
298  }
299 
304  {
305  $this->assertTrue(
306  $this->evaluateConditionCommonMethod->invokeArgs(
307  $this->conditionMatcher,
308  ['userFunc', 'user_testFunctionWithThreeArguments("foo",0,true)']
309  )
310  );
311  }
312 
317  {
318  $this->assertTrue(
319  $this->evaluateConditionCommonMethod->invokeArgs(
320  $this->conditionMatcher,
321  ['userFunc', 'user_testFunctionWithThreeArguments(true,0,"foo")']
322  )
323  );
324  }
325 
330  {
331  $this->assertTrue(
332  $this->evaluateConditionCommonMethod->invokeArgs(
333  $this->conditionMatcher,
334  ['userFunc', 'user_testFunctionWithThreeArguments(true,"foo",0)']
335  )
336  );
337  }
338 
343  {
344  $this->assertTrue(
345  $this->evaluateConditionCommonMethod->invokeArgs(
346  $this->conditionMatcher,
347  ['userFunc', "user_testFunctionWithThreeArguments(0,true,'foo')"]
348  )
349  );
350  }
351 
356  {
357  $this->assertTrue(
358  $this->evaluateConditionCommonMethod->invokeArgs(
359  $this->conditionMatcher,
360  ['userFunc', "user_testFunctionWithThreeArguments(0,'foo',true)"]
361  )
362  );
363  }
364 
369  {
370  $this->assertTrue(
371  $this->evaluateConditionCommonMethod->invokeArgs(
372  $this->conditionMatcher,
373  ['userFunc', "user_testFunctionWithThreeArguments('foo',true,0)"]
374  )
375  );
376  }
377 
382  {
383  $this->assertTrue(
384  $this->evaluateConditionCommonMethod->invokeArgs(
385  $this->conditionMatcher,
386  ['userFunc', "user_testFunctionWithThreeArguments('foo',0,true)"]
387  )
388  );
389  }
390 
395  {
396  $this->assertTrue(
397  $this->evaluateConditionCommonMethod->invokeArgs(
398  $this->conditionMatcher,
399  ['userFunc', "user_testFunctionWithThreeArguments(true,0,'foo')"]
400  )
401  );
402  }
403 
408  {
409  $this->assertTrue(
410  $this->evaluateConditionCommonMethod->invokeArgs(
411  $this->conditionMatcher,
412  ['userFunc', "user_testFunctionWithThreeArguments(true,'foo',0)"]
413  )
414  );
415  }
416 
421  {
422  $this->assertTrue(
423  $this->evaluateConditionCommonMethod->invokeArgs(
424  $this->conditionMatcher,
425  ['userFunc', "user_testFunctionWithThreeArguments('foo','bar', 'baz')"]
426  )
427  );
428  }
429 
434  {
435  $this->assertTrue(
436  $this->evaluateConditionCommonMethod->invokeArgs(
437  $this->conditionMatcher,
438  ['userFunc', 'user_testFunctionWithThreeArguments("foo","bar","baz")']
439  )
440  );
441  }
442 
446  public function testUserFuncReturnsFalse()
447  {
448  $this->assertFalse(
449  $this->evaluateConditionCommonMethod->invokeArgs(
450  $this->conditionMatcher,
451  ['userFunc', 'user_testFunctionFalse']
452  )
453  );
454  }
455 
460  {
461  $this->assertTrue(
462  $this->evaluateConditionCommonMethod->invokeArgs(
463  $this->conditionMatcher,
464  ['userFunc', 'user_testFunctionWithThreeArguments(1,2,"3,4,5,6")']
465  )
466  );
467  }
468 
473  {
474  $this->assertTrue(
475  $this->evaluateConditionCommonMethod->invokeArgs(
476  $this->conditionMatcher,
477  ['userFunc', 'user_testFunctionWithThreeArguments ( 1 , 2, "3, 4, 5, 6" )']
478  )
479  );
480  }
481 
486  {
487  $this->assertTrue(
488  $this->evaluateConditionCommonMethod->invokeArgs(
489  $this->conditionMatcher,
490  ['userFunc', 'user_testFunctionWithThreeArgumentsSpaces ( 1 , 2, "3, 4, 5, 6" )']
491  )
492  );
493  }
494 
499  {
500  $this->assertTrue(
501  $this->evaluateConditionCommonMethod->invokeArgs(
502  $this->conditionMatcher,
503  ['userFunc', 'user_testFunctionWithSpaces(" 3, 4, 5, 6 ")']
504  )
505  );
506  }
507 
512  {
513  $this->assertTrue(
514  $this->evaluateConditionCommonMethod->invokeArgs(
515  $this->conditionMatcher,
516  ['userFunc', 'user_testFunctionWithThreeArgumentsSpaces ( 1 , 2, "3, \"4, 5\", 6" )']
517  )
518  );
519  }
520 
525  {
526  $this->assertTrue(
527  $this->evaluateConditionCommonMethod->invokeArgs(
528  $this->conditionMatcher,
529  ['userFunc', 'user_testFunctionWithQuoteMissing ("value \")']
530  )
531  );
532  }
533 
538  {
539  $this->assertTrue(
540  $this->evaluateConditionCommonMethod->invokeArgs(
541  $this->conditionMatcher,
542  ['userFunc', 'user_testQuotes("1 \" 2")']
543  )
544  );
545  }
546 
551  {
552  $this->assertTrue(
553  $this->evaluateConditionCommonMethod->invokeArgs(
554  $this->conditionMatcher,
555  ['userFunc', 'ConditionMatcherUserFunctions::isTrue(1)']
556  )
557  );
558  }
559 }
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']