[TASK] Unit tests for devIP condition
[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 * Copyright notice
6 *
7 * (c) 2013 Steffen Müller <typo3@t3node.com>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Core\Core\ApplicationContext;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30 /**
31 * Testcases for
32 * TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher
33 *
34 * @author Steffen Müller <typo3@t3node.com>
35 */
36 class AbstractConditionMatcherTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
37
38 /**
39 * @var \TYPO3\CMS\Core\Core\ApplicationContext
40 */
41 protected $backupApplicationContext = NULL;
42
43 /**
44 *
45 */
46 public function setUp() {
47 $this->backupApplicationContext = GeneralUtility::getApplicationContext();
48 }
49
50 /**
51 *
52 */
53 public function tearDown() {
54 Fixtures\GeneralUtilityFixture::setApplicationContext($this->backupApplicationContext);
55 unset($this->backupApplicationContext);
56 }
57
58 /**
59 * Data provider with matching applicationContext conditions.
60 *
61 * @return array
62 */
63 public function matchingApplicationContextConditionsDataProvider() {
64 return array(
65 array('Production*'),
66 array('Production/Staging/*'),
67 array('Production/Staging/Server2'),
68 array('/^Production.*$/'),
69 array('/^Production\/.+\/Server\d+$/'),
70 );
71 }
72
73 /**
74 * @test
75 * @dataProvider matchingApplicationContextConditionsDataProvider
76 */
77 public function evaluateConditionCommonReturnsTrueForMatchingContexts($matchingContextCondition) {
78 /** @var \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext */
79 $applicationContext = new ApplicationContext('Production/Staging/Server2');
80 Fixtures\GeneralUtilityFixture::setApplicationContext($applicationContext);
81
82 /** @var \TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $abstractConditionMatcherMock */
83 $abstractConditionMatcherMock = $this->getMockForAbstractClass(
84 'TYPO3\\CMS\\Core\\Configuration\\TypoScript\\ConditionMatching\\AbstractConditionMatcher',
85 array(),
86 '',
87 TRUE,
88 TRUE,
89 TRUE,
90 array('evaluateConditionCommon')
91 );
92
93 $method = new \ReflectionMethod(
94 'TYPO3\\CMS\\Core\\Configuration\\TypoScript\\ConditionMatching\\AbstractConditionMatcher',
95 'evaluateConditionCommon'
96 );
97 $method->setAccessible(TRUE);
98
99 $this->assertTrue(
100 $method->invokeArgs($abstractConditionMatcherMock, array('applicationContext', $matchingContextCondition))
101 );
102 }
103
104 /**
105 * Data provider with not matching applicationContext conditions.
106 *
107 * @return array
108 */
109 public function notMatchingApplicationContextConditionsDataProvider() {
110 return array(
111 array('Production'),
112 array('Testing*'),
113 array('Development/Profiling, Testing/Unit'),
114 array('Testing/Staging/Server2'),
115 array('/^Testing.*$/'),
116 array('/^Production\/.+\/Host\d+$/'),
117 );
118 }
119
120 /**
121 * @test
122 * @dataProvider notMatchingApplicationContextConditionsDataProvider
123 */
124 public function evaluateConditionCommonReturnsNullForNotMatchingApplicationContexts($notMatchingApplicationContextCondition) {
125 /** @var \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext */
126 $applicationContext = new ApplicationContext('Production/Staging/Server2');
127 Fixtures\GeneralUtilityFixture::setApplicationContext($applicationContext);
128
129 /** @var \TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $abstractConditionMatcherMock */
130 $abstractConditionMatcherMock = $this->getMockForAbstractClass(
131 'TYPO3\\CMS\\Core\\Configuration\\TypoScript\\ConditionMatching\\AbstractConditionMatcher',
132 array(),
133 '',
134 TRUE,
135 TRUE,
136 TRUE,
137 array('evaluateConditionCommon')
138 );
139
140 $method = new \ReflectionMethod(
141 'TYPO3\\CMS\\Core\\Configuration\\TypoScript\\ConditionMatching\\AbstractConditionMatcher',
142 'evaluateConditionCommon'
143 );
144 $method->setAccessible(TRUE);
145
146 $this->assertNull(
147 $method->invokeArgs($abstractConditionMatcherMock, array('applicationContext', $notMatchingApplicationContextCondition))
148 );
149 }
150
151 /**
152 * Data provider for evaluateConditionCommonEvaluatesIpAddressesCorrectly
153 *
154 * @return array
155 */
156 public function evaluateConditionCommonDevIpMaskDataProvider() {
157 return array(
158 // [0] $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']
159 // [1] Actual IP
160 // [2] Expected condition result
161 'IP matches' => array(
162 '127.0.0.1',
163 '127.0.0.1',
164 TRUE
165 ),
166 'ipv4 wildcard subnet' => array(
167 '127.0.0.1/24',
168 '127.0.0.2',
169 TRUE
170 ),
171 'ipv6 wildcard subnet' => array(
172 '0:0::1/128',
173 '::1',
174 TRUE
175 ),
176 'List of addresses matches' => array(
177 '1.2.3.4, 5.6.7.8',
178 '5.6.7.8',
179 TRUE
180 ),
181 'IP does not match' => array(
182 '127.0.0.1',
183 '127.0.0.2',
184 NULL
185 ),
186 'ipv4 subnet does not match' => array(
187 '127.0.0.1/8',
188 '126.0.0.1',
189 NULL
190 ),
191 'ipv6 subnet does not match' => array(
192 '::1/127',
193 '::2',
194 NULL
195 ),
196 'List of addresses does not match' => array(
197 '127.0.0.1, ::1',
198 '::2',
199 NULL
200 ),
201 );
202 }
203
204 /**
205 * @test
206 * @dataProvider evaluateConditionCommonDevIpMaskDataProvider
207 */
208 public function evaluateConditionCommonEvaluatesIpAddressesCorrectly($devIpMask, $actualIp, $expectedResult) {
209 /** @var \TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $abstractConditionMatcherMock */
210 $abstractConditionMatcherMock = $this->getMockForAbstractClass(
211 'TYPO3\\CMS\\Core\\Configuration\\TypoScript\\ConditionMatching\\AbstractConditionMatcher',
212 array(),
213 '',
214 TRUE,
215 TRUE,
216 TRUE,
217 array('evaluateConditionCommon')
218 );
219 $method = new \ReflectionMethod(
220 'TYPO3\\CMS\\Core\\Configuration\\TypoScript\\ConditionMatching\\AbstractConditionMatcher',
221 'evaluateConditionCommon'
222 );
223 $method->setAccessible(TRUE);
224
225 // Do not trigger proxy stuff of GeneralUtility::getIndPEnv
226 unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP']);
227
228 $_SERVER['REMOTE_ADDR'] = $actualIp;
229 $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] = $devIpMask;
230
231 $actualResult = $method->invokeArgs($abstractConditionMatcherMock, array('IP', 'devIP'));
232 $this->assertSame($expectedResult, $actualResult);
233 }
234 }