[FEATURE] Add TypoScript condition for ApplicationContext 12/22412/13
authorSteffen Müller <typo3@t3node.com>
Thu, 18 Jul 2013 23:10:21 +0000 (01:10 +0200)
committerMarkus Klein <klein.t3@mfc-linz.at>
Fri, 11 Oct 2013 12:04:27 +0000 (14:04 +0200)
The following condition is available in TypoScript and TSconfig
for testing the ApplicationContext:

[applicationContext = value1, value2, ...]

One of the given values matching the applicationContext.
Wildcards + and * are allowed, as well as regular expressions
delimited with /PREG_PATTERN/

Examples:

[applicationContext = Development/Debugging, Development/Profiling]
  # matching exactly "Development/Debugging" or "Development/Profiling"
[global]

[applicationContext = Production*]
  # matching any applicationContext with a rootContext of "Production",
  # for example "Production/Live" or "Production/Staging"
[global]

[applicationContext = /^Production\/Staging\/Server\d+$/]
  # matching "Production/Staging/Server1", "Production/Staging/Server2"
  # and so on
[global]

Resolves: #50132
Documentation: #52701
Releases: 6.2
Change-Id: I4f0f69ca4208d36d8eb8966ec9ef034f6b703b34
Reviewed-on: https://review.typo3.org/22412
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Markus Klein
Tested-by: Markus Klein
typo3/sysext/core/Classes/Configuration/TypoScript/ConditionMatching/AbstractConditionMatcher.php
typo3/sysext/core/Tests/Unit/Configuration/TypoScript/ConditionMatching/AbstractConditionMatcherTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Configuration/TypoScript/ConditionMatching/Fixtures/GeneralUtilityFixture.php [new file with mode: 0644]

index c35722c..c5bd903 100644 (file)
@@ -212,6 +212,15 @@ abstract class AbstractConditionMatcher {
                }
                $keyParts = GeneralUtility::trimExplode('|', $key);
                switch ($keyParts[0]) {
+                       case 'applicationContext':
+                               $values = GeneralUtility::trimExplode(',', $value, TRUE);
+                               $currentApplicationContext = GeneralUtility::getApplicationContext();
+                               foreach ($values as $applicationContext) {
+                                       if ($this->searchStringWildcard($currentApplicationContext, $applicationContext)) {
+                                               return TRUE;
+                                       }
+                               }
+                               break;
                        case 'browser':
                                $values = GeneralUtility::trimExplode(',', $value, TRUE);
                                // take all identified browsers into account, eg chrome deliver
diff --git a/typo3/sysext/core/Tests/Unit/Configuration/TypoScript/ConditionMatching/AbstractConditionMatcherTest.php b/typo3/sysext/core/Tests/Unit/Configuration/TypoScript/ConditionMatching/AbstractConditionMatcherTest.php
new file mode 100644 (file)
index 0000000..549b2c6
--- /dev/null
@@ -0,0 +1,153 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Configuration\TypoScript\ConditionMatching;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2013 Steffen Müller <typo3@t3node.com>
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+use TYPO3\CMS\Core\Core\ApplicationContext;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Testcases for
+ * TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher
+ *
+ * @author Steffen Müller <typo3@t3node.com>
+ */
+class AbstractConditionMatcherTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+
+       /**
+        * @var \TYPO3\CMS\Core\Core\ApplicationContext
+        */
+       protected $backupApplicationContext = NULL;
+
+       /**
+        *
+        */
+       public function setUp() {
+               $this->backupApplicationContext = GeneralUtility::getApplicationContext();
+       }
+
+       /**
+        *
+        */
+       public function tearDown() {
+               Fixtures\GeneralUtilityFixture::setApplicationContext($this->backupApplicationContext);
+               unset($this->backupApplicationContext);
+       }
+
+       /**
+        * Data provider with matching applicationContext conditions.
+        *
+        * @return array
+        */
+       public function matchingApplicationContextConditions() {
+               return array(
+                       array('Production*'),
+                       array('Production/Staging/*'),
+                       array('Production/Staging/Server2'),
+                       array('/^Production.*$/'),
+                       array('/^Production\/.+\/Server\d+$/'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider matchingApplicationContextConditions
+        */
+       public function evaluateConditionCommonReturnsTrueForMatchingContexts($matchingContextCondition) {
+
+               /** @var \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext */
+               $applicationContext = new ApplicationContext('Production/Staging/Server2');
+               Fixtures\GeneralUtilityFixture::setApplicationContext($applicationContext);
+
+               /** @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);
+
+               $this->assertTrue(
+                       $method->invokeArgs($abstractConditionMatcherMock, array('applicationContext', $matchingContextCondition))
+               );
+       }
+
+       /**
+        * Data provider with not matching applicationContext conditions.
+        *
+        * @return array
+        */
+       public function notMatchingApplicationContextConditions() {
+               return array(
+                       array('Production'),
+                       array('Testing*'),
+                       array('Development/Profiling, Testing/Unit'),
+                       array('Testing/Staging/Server2'),
+                       array('/^Testing.*$/'),
+                       array('/^Production\/.+\/Host\d+$/'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider notMatchingApplicationContextConditions
+        */
+       public function evaluateConditionCommonReturnsNullForNotMatchingApplicationContexts($notMatchingApplicationContextCondition) {
+
+               /** @var \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext */
+               $applicationContext = new ApplicationContext('Production/Staging/Server2');
+               Fixtures\GeneralUtilityFixture::setApplicationContext($applicationContext);
+
+               /** @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);
+
+               $this->assertNull(
+                       $method->invokeArgs($abstractConditionMatcherMock, array('applicationContext', $notMatchingApplicationContextCondition))
+               );
+       }
+}
+?>
\ No newline at end of file
diff --git a/typo3/sysext/core/Tests/Unit/Configuration/TypoScript/ConditionMatching/Fixtures/GeneralUtilityFixture.php b/typo3/sysext/core/Tests/Unit/Configuration/TypoScript/ConditionMatching/Fixtures/GeneralUtilityFixture.php
new file mode 100644 (file)
index 0000000..e515f03
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Configuration\TypoScript\ConditionMatching\Fixtures;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2013 Steffen Müller <typo3@t3node.com>
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+/**
+ * Fixture for TYPO3\CMS\Core\Utility\GeneralUtility
+ *
+ * @author Steffen Müller <typo3@t3node.com>
+ */
+class GeneralUtilityFixture extends \TYPO3\CMS\Core\Utility\GeneralUtility {
+
+       /**
+        * @param \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext
+        * @return void
+        */
+       static public function setApplicationContext($applicationContext) {
+               static::$applicationContext = $applicationContext;
+       }
+}