From bacde179fddc586d12c65868280cdd1c6f7b6289 Mon Sep 17 00:00:00 2001 From: Benjamin Beck Date: Tue, 9 Dec 2014 22:25:20 +0100 Subject: [PATCH] [FEATURE] Context view helpers --- .../Context/IsDevelopmentViewHelper.php | 64 ++++++++++++++++++ .../Context/IsProductionViewHelper.php | 65 +++++++++++++++++++ .../Condition/Context/IsTestingViewHelper.php | 65 +++++++++++++++++++ Classes/ViewHelpers/Context/GetViewHelper.php | 53 +++++++++++++++ .../Context/IsDevelopmentViewHelperTest.php | 65 +++++++++++++++++++ .../Context/IsProductionViewHelperTest.php | 65 +++++++++++++++++++ .../Context/IsTestingViewHelperTest.php | 65 +++++++++++++++++++ .../ViewHelpers/Context/GetViewHelperTest.php | 39 +++++++++++ 8 files changed, 481 insertions(+) create mode 100755 Classes/ViewHelpers/Condition/Context/IsDevelopmentViewHelper.php create mode 100755 Classes/ViewHelpers/Condition/Context/IsProductionViewHelper.php create mode 100755 Classes/ViewHelpers/Condition/Context/IsTestingViewHelper.php create mode 100755 Classes/ViewHelpers/Context/GetViewHelper.php create mode 100755 Tests/Unit/ViewHelpers/Condition/Context/IsDevelopmentViewHelperTest.php create mode 100755 Tests/Unit/ViewHelpers/Condition/Context/IsProductionViewHelperTest.php create mode 100755 Tests/Unit/ViewHelpers/Condition/Context/IsTestingViewHelperTest.php create mode 100755 Tests/Unit/ViewHelpers/Context/GetViewHelperTest.php diff --git a/Classes/ViewHelpers/Condition/Context/IsDevelopmentViewHelper.php b/Classes/ViewHelpers/Condition/Context/IsDevelopmentViewHelper.php new file mode 100755 index 000000000..6ca353813 --- /dev/null +++ b/Classes/ViewHelpers/Condition/Context/IsDevelopmentViewHelper.php @@ -0,0 +1,64 @@ + + * 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\Fluid\Core\ViewHelper\AbstractConditionViewHelper; +use TYPO3\CMS\Core\Utility\GeneralUtility; + +/** + * ### Context: IsDevelopment + * + * Returns true if current root application context is development otherwise false. + * If no application context has been set, then the default context is production. + * + * #### Note about how to set the application context + * + * The context TYPO3 CMS runs in is specified through the environment variable TYPO3_CONTEXT. + * It can be set by .htaccess or in the server configuration + * + * See: http://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Bootstrapping/Index.html#bootstrapping-context + * + * @author Benjamin Beck + * @package Vhs + * @subpackage ViewHelpers\Condition\Context + */ +class IsDevelopmentViewHelper extends AbstractConditionViewHelper { + + /** + * Render method + * + * @return string + */ + public function render () { + if (TRUE === $this->isDevelopmentContext()) { + return $this->renderThenChild(); + } + + return $this->renderElseChild(); + } + + + /** + * @return boolean + */ + protected function isDevelopmentContext () { + return GeneralUtility::getApplicationContext()->isDevelopment(); + } +} diff --git a/Classes/ViewHelpers/Condition/Context/IsProductionViewHelper.php b/Classes/ViewHelpers/Condition/Context/IsProductionViewHelper.php new file mode 100755 index 000000000..23c6f9b37 --- /dev/null +++ b/Classes/ViewHelpers/Condition/Context/IsProductionViewHelper.php @@ -0,0 +1,65 @@ + + * 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\Fluid\Core\ViewHelper\AbstractConditionViewHelper; +use TYPO3\CMS\Core\Utility\GeneralUtility; + +/** + * ### Context: IsProduction + * + * Returns true if current root application context is production otherwise false. + * If no application context has been set, then this is the default context. + * + * #### Note about how to set the application context + * + * The context TYPO3 CMS runs in is specified through the environment variable TYPO3_CONTEXT. + * It can be set by .htaccess or in the server configuration + * + * See: http://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Bootstrapping/Index.html#bootstrapping-context + * + * @author Benjamin Beck + * @package Vhs + * @subpackage ViewHelpers\Condition\Context + */ +class IsProductionViewHelper extends AbstractConditionViewHelper { + + /** + * Render method + * + * @return string + */ + public function render () { + if (TRUE === $this->isProductionContext()) { + return $this->renderThenChild(); + } + + return $this->renderElseChild(); + } + + + /** + * @return boolean + */ + protected function isProductionContext () { + return GeneralUtility::getApplicationContext()->isProduction(); + } + +} diff --git a/Classes/ViewHelpers/Condition/Context/IsTestingViewHelper.php b/Classes/ViewHelpers/Condition/Context/IsTestingViewHelper.php new file mode 100755 index 000000000..715b57a42 --- /dev/null +++ b/Classes/ViewHelpers/Condition/Context/IsTestingViewHelper.php @@ -0,0 +1,65 @@ + + * 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\Fluid\Core\ViewHelper\AbstractConditionViewHelper; +use TYPO3\CMS\Core\Utility\GeneralUtility; + +/** + * ### Context: IsProduction + * + * Returns true if current root application context is testing otherwise false. + * If no application context has been set, then the default context is production. + * + * #### Note about how to set the application context + * + * The context TYPO3 CMS runs in is specified through the environment variable TYPO3_CONTEXT. + * It can be set by .htaccess or in the server configuration + * + * See: http://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Bootstrapping/Index.html#bootstrapping-context + * + * @author Benjamin Beck + * @package Vhs + * @subpackage ViewHelpers\Condition\Context + */ +class IsTestingViewHelper extends AbstractConditionViewHelper { + + /** + * Render method + * + * @return string + */ + public function render () { + if (TRUE === $this->isTestingContext()) { + return $this->renderThenChild(); + } + + return $this->renderElseChild(); + } + + + /** + * @return boolean + */ + protected function isTestingContext () { + return GeneralUtility::getApplicationContext()->isTesting(); + } + +} diff --git a/Classes/ViewHelpers/Context/GetViewHelper.php b/Classes/ViewHelpers/Context/GetViewHelper.php new file mode 100755 index 000000000..aa05fd09b --- /dev/null +++ b/Classes/ViewHelpers/Context/GetViewHelper.php @@ -0,0 +1,53 @@ + + * 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\Fluid\Core\ViewHelper\AbstractViewHelper; +use TYPO3\CMS\Core\Utility\GeneralUtility; + +/** + * ### Context: Get + * + * Returns the current application context which may include possible sub-contexts. + * The application context can be 'Production', 'Development' or 'Testing'. + * Additionally each context can be extended with custom sub-contexts like: 'Production/Staging' or ' Production/Staging/Server1'. + * If no application context has been set by the configuration, then the default context is 'Production'. + * + * #### Note about how to set the application context + * + * The context TYPO3 CMS runs in is specified through the environment variable TYPO3_CONTEXT. + * It can be set by .htaccess or in the server configuration + * + * See: http://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Bootstrapping/Index.html#bootstrapping-context + * + * @author Benjamin Beck + * @package Vhs + * @subpackage ViewHelpers\Context + */ +class GetViewHelper extends AbstractViewHelper { + + /** + * @return string + */ + public function render () { + return (string) GeneralUtility::getApplicationContext(); + } + +} diff --git a/Tests/Unit/ViewHelpers/Condition/Context/IsDevelopmentViewHelperTest.php b/Tests/Unit/ViewHelpers/Condition/Context/IsDevelopmentViewHelperTest.php new file mode 100755 index 000000000..a72108844 --- /dev/null +++ b/Tests/Unit/ViewHelpers/Condition/Context/IsDevelopmentViewHelperTest.php @@ -0,0 +1,65 @@ + + * 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 FluidTYPO3\Vhs\ViewHelpers\AbstractViewHelperTest; + +/** + * @protection off + * @author Benjamin Beck + * @package Vhs + */ +class IsDevelopmentViewHelperTest extends AbstractViewHelperTest { + + /** + * @test + */ + public function testIsDevelopmentContext() { + $instance = $this->createInstance(); + $result = $this->callInaccessibleMethod($instance, 'isDevelopmentContext'); + $this->assertThat($result, new \PHPUnit_Framework_Constraint_IsType(\PHPUnit_Framework_Constraint_IsType::TYPE_BOOL)); + } + + /** + * @test + * @dataProvider getRenderTestValues + * @param boolean $verdict + * @param boolean $expected + */ + public function testRender($verdict, $expected) { + $instance = $this->getMock(substr(get_class($this), 0, -4), array('isDevelopmentContext')); + $instance->expects($this->once())->method('isDevelopmentContext')->will($this->returnValue($verdict)); + $arguments = array('then' => TRUE, 'else' => FALSE); + $instance->setArguments($arguments); + $result = $instance->render(); + $this->assertEquals($expected, $result); + } + + /** + * @return array + */ + public function getRenderTestValues() { + return array( + array(FALSE, FALSE), + array(TRUE, TRUE), + ); + } + +} diff --git a/Tests/Unit/ViewHelpers/Condition/Context/IsProductionViewHelperTest.php b/Tests/Unit/ViewHelpers/Condition/Context/IsProductionViewHelperTest.php new file mode 100755 index 000000000..ad8f62144 --- /dev/null +++ b/Tests/Unit/ViewHelpers/Condition/Context/IsProductionViewHelperTest.php @@ -0,0 +1,65 @@ + + * 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 FluidTYPO3\Vhs\ViewHelpers\AbstractViewHelperTest; + +/** + * @protection off + * @author Benjamin Beck + * @package Vhs + */ +class IsProductionViewHelperTest extends AbstractViewHelperTest { + + /** + * @test + */ + public function testIsProductionContext() { + $instance = $this->createInstance(); + $result = $this->callInaccessibleMethod($instance, 'isProductionContext'); + $this->assertThat($result, new \PHPUnit_Framework_Constraint_IsType(\PHPUnit_Framework_Constraint_IsType::TYPE_BOOL)); + } + + /** + * @test + * @dataProvider getRenderTestValues + * @param boolean $verdict + * @param boolean $expected + */ + public function testRender($verdict, $expected) { + $instance = $this->getMock(substr(get_class($this), 0, -4), array('isProductionContext')); + $instance->expects($this->once())->method('isProductionContext')->will($this->returnValue($verdict)); + $arguments = array('then' => TRUE, 'else' => FALSE); + $instance->setArguments($arguments); + $result = $instance->render(); + $this->assertEquals($expected, $result); + } + + /** + * @return array + */ + public function getRenderTestValues() { + return array( + array(FALSE, FALSE), + array(TRUE, TRUE), + ); + } + +} diff --git a/Tests/Unit/ViewHelpers/Condition/Context/IsTestingViewHelperTest.php b/Tests/Unit/ViewHelpers/Condition/Context/IsTestingViewHelperTest.php new file mode 100755 index 000000000..13ac2cc41 --- /dev/null +++ b/Tests/Unit/ViewHelpers/Condition/Context/IsTestingViewHelperTest.php @@ -0,0 +1,65 @@ + + * 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 FluidTYPO3\Vhs\ViewHelpers\AbstractViewHelperTest; + +/** + * @protection off + * @author Benjamin Beck + * @package Vhs + */ +class IsTestingViewHelperTest extends AbstractViewHelperTest { + + /** + * @test + */ + public function testIsTestingContext() { + $instance = $this->createInstance(); + $result = $this->callInaccessibleMethod($instance, 'isTestingContext'); + $this->assertThat($result, new \PHPUnit_Framework_Constraint_IsType(\PHPUnit_Framework_Constraint_IsType::TYPE_BOOL)); + } + + /** + * @test + * @dataProvider getRenderTestValues + * @param boolean $verdict + * @param boolean $expected + */ + public function testRender($verdict, $expected) { + $instance = $this->getMock(substr(get_class($this), 0, -4), array('isTestingContext')); + $instance->expects($this->once())->method('isTestingContext')->will($this->returnValue($verdict)); + $arguments = array('then' => TRUE, 'else' => FALSE); + $instance->setArguments($arguments); + $result = $instance->render(); + $this->assertEquals($expected, $result); + } + + /** + * @return array + */ + public function getRenderTestValues() { + return array( + array(FALSE, FALSE), + array(TRUE, TRUE), + ); + } + +} diff --git a/Tests/Unit/ViewHelpers/Context/GetViewHelperTest.php b/Tests/Unit/ViewHelpers/Context/GetViewHelperTest.php new file mode 100755 index 000000000..459513af2 --- /dev/null +++ b/Tests/Unit/ViewHelpers/Context/GetViewHelperTest.php @@ -0,0 +1,39 @@ + + * 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 FluidTYPO3\Vhs\ViewHelpers\AbstractViewHelperTest; + +/** + * @author Benjamin Beck + * @package Vhs + */ +class GetViewHelperTest extends AbstractViewHelperTest { + + /** + * @test + */ + public function renderCompleteContext() { + $result = $this->executeViewHelper(); + $expectedResult = getenv('TYPO3_CONTEXT') ?: (getenv('REDIRECT_TYPO3_CONTEXT') ?: 'Production'); + $this->assertSame($result, $expectedResult); + } + +}