From b1fdbd405ed420e767db66dc02fba59694e338a1 Mon Sep 17 00:00:00 2001 From: Anja Leichsenring <aleichsenring@ab-softlab.de> Date: Sun, 1 Mar 2015 16:19:46 +0100 Subject: [PATCH] [TASK] UnitTests for TranslateViewHelper The TranslateViewHelper has no unit tests so far. Change-Id: Ib7bdedf7e64276fcfcb7280fafc5d48c860f21c8 Resolves: #65436 Releases: master Reviewed-on: http://review.typo3.org/37416 Reviewed-by: Helmut Hummel <helmut.hummel@typo3.org> Tested-by: Helmut Hummel <helmut.hummel@typo3.org> Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch> Tested-by: Christian Kuhn <lolli@schwarzbu.ch> Reviewed-by: Michael Oehlhof <typo3@oehlhof.de> Tested-by: Michael Oehlhof <typo3@oehlhof.de> --- .../ViewHelpers/TranslateViewHelper.php | 16 +++- ...anslateViewHelperFixtureForEmptyString.php | 36 +++++++++ ...teViewHelperFixtureForTranslatedString.php | 36 +++++++++ .../ViewHelpers/TranslateViewHelperTest.php | 79 +++++++++++++++++++ 4 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForEmptyString.php create mode 100644 typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForTranslatedString.php create mode 100644 typo3/sysext/fluid/Tests/Unit/ViewHelpers/TranslateViewHelperTest.php diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/TranslateViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/TranslateViewHelper.php index b8533d59da7d..dde96d3de32d 100644 --- a/typo3/sysext/fluid/Classes/ViewHelpers/TranslateViewHelper.php +++ b/typo3/sysext/fluid/Classes/ViewHelpers/TranslateViewHelper.php @@ -14,6 +14,7 @@ namespace TYPO3\CMS\Fluid\ViewHelpers; * Public License for more details. * * */ +use TYPO3\CMS\Extbase\Utility\LocalizationUtility; use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface; use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper; use TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException; @@ -126,7 +127,7 @@ class TranslateViewHelper extends AbstractViewHelper implements CompilableInterf $request = $renderingContext->getControllerContext()->getRequest(); $extensionName = $extensionName === NULL ? $request->getControllerExtensionName() : $extensionName; - $value = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($id, $extensionName, $arguments); + $value = static::translate($id, $extensionName, $arguments); if ($value === NULL) { $value = $default !== NULL ? $default : $renderChildrenClosure(); if (!empty($arguments)) { @@ -138,4 +139,17 @@ class TranslateViewHelper extends AbstractViewHelper implements CompilableInterf return $value; } + /** + * Wrapper call to static LocalizationUtility + * + * @param string $id Translation Key compatible to TYPO3 Flow + * @param string $extensionName UpperCamelCased extension key (for example BlogExample) + * @param array $arguments Arguments to be replaced in the resulting string + * + * @return NULL|string + */ + static protected function translate($id, $extensionName, $arguments) { + return LocalizationUtility::translate($id, $extensionName, $arguments); + } + } diff --git a/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForEmptyString.php b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForEmptyString.php new file mode 100644 index 000000000000..d021b4a3c4a1 --- /dev/null +++ b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForEmptyString.php @@ -0,0 +1,36 @@ +<?php +namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures; + +/* + * This file is part of the TYPO3 CMS project. + * + * It is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, either version 2 + * of the License, or any later version. + * + * For the full copyright and license information, please read the + * LICENSE.txt file that was distributed with this source code. + * + * The TYPO3 project - inspiring people to share! + */ + +use TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper; + +/** + * Fixture class for mocking static translate function + */ +class TranslateViewHelperFixtureForEmptyString extends TranslateViewHelper { + + /** + * In original class this is wrapper call to static LocalizationUtility + * + * @param string $id Translation Key compatible to TYPO3 Flow + * @param string $extensionName UpperCamelCased extension key (for example BlogExample) + * @param array $arguments Arguments to be replaced in the resulting string + * + * @return NULL + */ + static protected function translate($id, $extensionName, $arguments) { + return NULL; + } +} diff --git a/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForTranslatedString.php b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForTranslatedString.php new file mode 100644 index 000000000000..33314749eefe --- /dev/null +++ b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForTranslatedString.php @@ -0,0 +1,36 @@ +<?php +namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures; + +/* + * This file is part of the TYPO3 CMS project. + * + * It is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, either version 2 + * of the License, or any later version. + * + * For the full copyright and license information, please read the + * LICENSE.txt file that was distributed with this source code. + * + * The TYPO3 project - inspiring people to share! + */ + +use TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper; + +/** + * Fixture class for mocking static translate function + */ +class TranslateViewHelperFixtureForTranslatedString extends TranslateViewHelper { + + /** + * In original class this is wrapper call to static LocalizationUtility + * + * @param string $id Translation Key compatible to TYPO3 Flow + * @param string $extensionName UpperCamelCased extension key (for example BlogExample) + * @param array $arguments Arguments to be replaced in the resulting string + * + * @return NULL|string + */ + static protected function translate($id, $extensionName, $arguments) { + return '<p>hello world</p>'; + } +} diff --git a/typo3/sysext/fluid/Tests/Unit/ViewHelpers/TranslateViewHelperTest.php b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/TranslateViewHelperTest.php new file mode 100644 index 000000000000..a4c9a3567000 --- /dev/null +++ b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/TranslateViewHelperTest.php @@ -0,0 +1,79 @@ +<?php +namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers; + +/* + * This file is part of the TYPO3 CMS project. + * + * It is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, either version 2 + * of the License, or any later version. + * + * For the full copyright and license information, please read the + * LICENSE.txt file that was distributed with this source code. + * + * The TYPO3 project - inspiring people to share! + */ + +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\TranslateViewHelperFixtureForEmptyString; +use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\TranslateViewHelperFixtureForTranslatedString; +use TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper; + +/** + * Test class for TranslateViewHelper + */ +class TranslateViewHelperTest extends ViewHelperBaseTestcase { + + /** + * @var TranslateViewHelper + */ + protected $subject; + + /** + * @test + * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException + * @expectedExceptionCode 1351584844 + */ + public function renderThrowsExceptionIfNoKeyOrIdParameterIsGiven() { + $this->subject = GeneralUtility::makeInstance(TranslateViewHelper::class); + $this->injectDependenciesIntoViewHelper($this->subject); + $this->subject->render(); + } + + /** + * @test + */ + public function renderReturnsStringForGivenKey() { + $this->subject = GeneralUtility::makeInstance(TranslateViewHelperFixtureForTranslatedString::class); + $this->injectDependenciesIntoViewHelper($this->subject); + $this->assertEquals('<p>hello world</p>', $this->subject->render('foo')); + } + + /** + * @test + */ + public function renderReturnsStringForGivenId() { + $this->subject = GeneralUtility::makeInstance(TranslateViewHelperFixtureForTranslatedString::class); + $this->injectDependenciesIntoViewHelper($this->subject); + $this->assertEquals('<p>hello world</p>', $this->subject->render(NULL, 'bar')); + } + + /** + * @test + */ + public function renderReturnsDefaultIfNoTranslationIsFound() { + $this->subject = GeneralUtility::makeInstance(TranslateViewHelperFixtureForEmptyString::class); + $this->injectDependenciesIntoViewHelper($this->subject); + $this->assertEquals('default', $this->subject->render(NULL, 'bar', 'default')); + } + + /** + * @test + */ + public function resultIsNotHtmlEscapedIfSoRequested() { + $this->subject = GeneralUtility::makeInstance(TranslateViewHelperFixtureForTranslatedString::class); + $this->injectDependenciesIntoViewHelper($this->subject); + $this->assertEquals('<p>hello world</p>', $this->subject->render('foo', NULL, NULL, TRUE)); + } + +} -- GitLab