[TASK] Unit tests for devIP condition 38/24838/2
authorChristian Kuhn <lolli@schwarzbu.ch>
Wed, 16 Oct 2013 11:27:10 +0000 (13:27 +0200)
committerWouter Wolters <typo3@wouterwolters.nl>
Wed, 16 Oct 2013 17:10:57 +0000 (19:10 +0200)
Cover the condition feature of issue #50092 with some test to
verify expected behaviour.

Change-Id: I116fb22f9ac0d6b1b869afe7844f38d235cdd39a
Resolves: #52887
Related: #50092
Releases: 6.2
Reviewed-on: https://review.typo3.org/24838
Reviewed-by: Nicole Cordes
Tested-by: Nicole Cordes
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
typo3/sysext/core/Tests/Unit/Configuration/TypoScript/ConditionMatching/AbstractConditionMatcherTest.php

index ed67ed8..52f318e 100644 (file)
@@ -60,7 +60,7 @@ class AbstractConditionMatcherTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         *
         * @return array
         */
-       public function matchingApplicationContextConditions() {
+       public function matchingApplicationContextConditionsDataProvider() {
                return array(
                        array('Production*'),
                        array('Production/Staging/*'),
@@ -72,10 +72,9 @@ class AbstractConditionMatcherTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
        /**
         * @test
-        * @dataProvider matchingApplicationContextConditions
+        * @dataProvider matchingApplicationContextConditionsDataProvider
         */
        public function evaluateConditionCommonReturnsTrueForMatchingContexts($matchingContextCondition) {
-
                /** @var \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext */
                $applicationContext = new ApplicationContext('Production/Staging/Server2');
                Fixtures\GeneralUtilityFixture::setApplicationContext($applicationContext);
@@ -107,7 +106,7 @@ class AbstractConditionMatcherTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         *
         * @return array
         */
-       public function notMatchingApplicationContextConditions() {
+       public function notMatchingApplicationContextConditionsDataProvider() {
                return array(
                        array('Production'),
                        array('Testing*'),
@@ -120,10 +119,9 @@ class AbstractConditionMatcherTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
        /**
         * @test
-        * @dataProvider notMatchingApplicationContextConditions
+        * @dataProvider notMatchingApplicationContextConditionsDataProvider
         */
        public function evaluateConditionCommonReturnsNullForNotMatchingApplicationContexts($notMatchingApplicationContextCondition) {
-
                /** @var \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext */
                $applicationContext = new ApplicationContext('Production/Staging/Server2');
                Fixtures\GeneralUtilityFixture::setApplicationContext($applicationContext);
@@ -149,4 +147,88 @@ class AbstractConditionMatcherTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        $method->invokeArgs($abstractConditionMatcherMock, array('applicationContext', $notMatchingApplicationContextCondition))
                );
        }
-}
+
+       /**
+        * Data provider for evaluateConditionCommonEvaluatesIpAddressesCorrectly
+        *
+        * @return array
+        */
+       public function evaluateConditionCommonDevIpMaskDataProvider() {
+               return array(
+                       // [0] $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']
+                       // [1] Actual IP
+                       // [2] Expected condition result
+                       'IP matches' => array(
+                               '127.0.0.1',
+                               '127.0.0.1',
+                               TRUE
+                       ),
+                       'ipv4 wildcard subnet' => array(
+                               '127.0.0.1/24',
+                               '127.0.0.2',
+                               TRUE
+                       ),
+                       'ipv6 wildcard subnet' => array(
+                               '0:0::1/128',
+                               '::1',
+                               TRUE
+                       ),
+                       'List of addresses matches' => array(
+                               '1.2.3.4, 5.6.7.8',
+                               '5.6.7.8',
+                               TRUE
+                       ),
+                       'IP does not match' => array(
+                               '127.0.0.1',
+                               '127.0.0.2',
+                               NULL
+                       ),
+                       'ipv4 subnet does not match' => array(
+                               '127.0.0.1/8',
+                               '126.0.0.1',
+                               NULL
+                       ),
+                       'ipv6 subnet does not match' => array(
+                               '::1/127',
+                               '::2',
+                               NULL
+                       ),
+                       'List of addresses does not match' => array(
+                               '127.0.0.1, ::1',
+                               '::2',
+                               NULL
+                       ),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider evaluateConditionCommonDevIpMaskDataProvider
+        */
+       public function evaluateConditionCommonEvaluatesIpAddressesCorrectly($devIpMask, $actualIp, $expectedResult) {
+               /** @var \TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $abstractConditionMatcherMock */
+               $abstractConditionMatcherMock = $this->getMockForAbstractClass(
+                       'TYPO3\\CMS\\Core\\Configuration\\TypoScript\\ConditionMatching\\AbstractConditionMatcher',
+                       array(),
+                       '',
+                       TRUE,
+                       TRUE,
+                       TRUE,
+                       array('evaluateConditionCommon')
+               );
+               $method = new \ReflectionMethod(
+                       'TYPO3\\CMS\\Core\\Configuration\\TypoScript\\ConditionMatching\\AbstractConditionMatcher',
+                       'evaluateConditionCommon'
+               );
+               $method->setAccessible(TRUE);
+
+               // Do not trigger proxy stuff of GeneralUtility::getIndPEnv
+               unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP']);
+
+               $_SERVER['REMOTE_ADDR'] = $actualIp;
+               $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] = $devIpMask;
+
+               $actualResult = $method->invokeArgs($abstractConditionMatcherMock, array('IP', 'devIP'));
+               $this->assertSame($expectedResult, $actualResult);
+       }
+}
\ No newline at end of file