[TASK] UnitTests for TranslateViewHelper 16/37416/8
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Sun, 1 Mar 2015 15:19:46 +0000 (16:19 +0100)
committerHelmut Hummel <helmut.hummel@typo3.org>
Sun, 1 Mar 2015 18:03:42 +0000 (19:03 +0100)
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>
typo3/sysext/fluid/Classes/ViewHelpers/TranslateViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForEmptyString.php [new file with mode: 0644]
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForTranslatedString.php [new file with mode: 0644]
typo3/sysext/fluid/Tests/Unit/ViewHelpers/TranslateViewHelperTest.php [new file with mode: 0644]

index b8533d5..dde96d3 100644 (file)
@@ -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 (file)
index 0000000..d021b4a
--- /dev/null
@@ -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 (file)
index 0000000..3331474
--- /dev/null
@@ -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 (file)
index 0000000..a4c9a35
--- /dev/null
@@ -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('&lt;p&gt;hello world&lt;/p&gt;', $this->subject->render('foo', NULL, NULL, TRUE));
+       }
+
+}