[TASK] Simplify frontend/backend security viewhelpers 61/56561/3
authorMathias Brodala <mbrodala@pagemachine.de>
Thu, 5 Apr 2018 12:31:10 +0000 (14:31 +0200)
committerBenni Mack <benni@typo3.org>
Fri, 6 Apr 2018 04:49:10 +0000 (06:49 +0200)
This logic is already implemented in
AbstractConditionViewHelper::renderStatic().

Resolves: #84627
Releases: master
Change-Id: Iaf1fa4630fcc5df0ef22bec55d4fa15df1aa0986
Reviewed-on: https://review.typo3.org/56561
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/fluid/Classes/ViewHelpers/Be/Security/IfAuthenticatedViewHelper.php
typo3/sysext/fluid/Classes/ViewHelpers/Be/Security/IfHasRoleViewHelper.php
typo3/sysext/fluid/Classes/ViewHelpers/Security/IfAuthenticatedViewHelper.php
typo3/sysext/fluid/Classes/ViewHelpers/Security/IfHasRoleViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Be/Security/IfAuthenticatedViewHelperTest.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Be/Security/IfHasRoleViewHelperTest.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Security/IfAuthenticatedViewHelperTest.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Security/IfHasRoleViewHelperTest.php

index b63c2a8..01bba6d 100644 (file)
@@ -59,15 +59,4 @@ class IfAuthenticatedViewHelper extends AbstractConditionViewHelper
     {
         return isset($GLOBALS['BE_USER']) && $GLOBALS['BE_USER']->user['uid'] > 0;
     }
-
-    /**
-     * @return mixed
-     */
-    public function render()
-    {
-        if (static::evaluateCondition($this->arguments)) {
-            return $this->renderThenChild();
-        }
-        return $this->renderElseChild();
-    }
 }
index 3cf6785..f03b625 100644 (file)
@@ -95,15 +95,4 @@ class IfHasRoleViewHelper extends AbstractConditionViewHelper
         }
         return false;
     }
-
-    /**
-     * @return mixed
-     */
-    public function render()
-    {
-        if (static::evaluateCondition($this->arguments)) {
-            return $this->renderThenChild();
-        }
-        return $this->renderElseChild();
-    }
 }
index 8268ab7..8f3b70a 100644 (file)
@@ -59,15 +59,4 @@ class IfAuthenticatedViewHelper extends AbstractConditionViewHelper
     {
         return isset($GLOBALS['TSFE']) && $GLOBALS['TSFE']->loginUser;
     }
-
-    /**
-     * @return mixed
-     */
-    public function render()
-    {
-        if (static::evaluateCondition($this->arguments)) {
-            return $this->renderThenChild();
-        }
-        return $this->renderElseChild();
-    }
 }
index b9f61e3..704702b 100644 (file)
@@ -85,15 +85,4 @@ class IfHasRoleViewHelper extends AbstractConditionViewHelper
         }
         return is_array($GLOBALS['TSFE']->fe_user->groupData['title']) && in_array($role, $GLOBALS['TSFE']->fe_user->groupData['title']);
     }
-
-    /**
-     * @return mixed
-     */
-    public function render()
-    {
-        if (static::evaluateCondition($this->arguments)) {
-            return $this->renderThenChild();
-        }
-        return $this->renderElseChild();
-    }
 }
index 2587a69..89cafbd 100644 (file)
@@ -17,6 +17,7 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Be\Security;
 
 use TYPO3\CMS\Fluid\ViewHelpers\Be\Security\IfAuthenticatedViewHelper;
 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
+use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 
 /**
  * Testcase for be.security.ifAuthenticated view helper
@@ -32,20 +33,30 @@ class IfAuthenticatedViewHelperTest extends ViewHelperBaseTestcase
     {
         parent::setUp();
         $GLOBALS['BE_USER'] = new \stdClass();
-        $this->viewHelper = $this->getAccessibleMock(IfAuthenticatedViewHelper::class, ['renderThenChild', 'renderElseChild']);
-        $this->viewHelper->expects($this->any())->method('renderThenChild')->will($this->returnValue('then child'));
-        $this->viewHelper->expects($this->any())->method('renderElseChild')->will($this->returnValue('else child'));
+        $this->viewHelper = new IfAuthenticatedViewHelper();
         $this->injectDependenciesIntoViewHelper($this->viewHelper);
         $this->viewHelper->initializeArguments();
     }
 
+    protected function tearDown()
+    {
+        unset($GLOBALS['BE_USER']);
+    }
+
     /**
      * @test
      */
     public function viewHelperRendersThenChildIfBeUserIsLoggedIn()
     {
         $GLOBALS['BE_USER']->user = ['uid' => 1];
-        $actualResult = $this->viewHelper->render();
+
+        $actualResult = $this->viewHelper->renderStatic(
+            ['then' => 'then child', 'else' => 'else child'],
+            function () {
+            },
+            $this->prophesize(RenderingContextInterface::class)->reveal()
+        );
+
         $this->assertEquals('then child', $actualResult);
     }
 
@@ -55,7 +66,14 @@ class IfAuthenticatedViewHelperTest extends ViewHelperBaseTestcase
     public function viewHelperRendersElseChildIfBeUserIsNotLoggedIn()
     {
         $GLOBALS['BE_USER']->user = ['uid' => 0];
-        $actualResult = $this->viewHelper->render();
+
+        $actualResult = $this->viewHelper->renderStatic(
+            ['then' => 'then child', 'else' => 'else child'],
+            function () {
+            },
+            $this->prophesize(RenderingContextInterface::class)->reveal()
+        );
+
         $this->assertEquals('else child', $actualResult);
     }
 }
index 13e668b..20f3318 100644 (file)
@@ -14,7 +14,9 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Be\Security;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\ViewHelpers\Be\Security\IfHasRoleViewHelper;
 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
+use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 
 /**
  * Testcase for be.security.ifHasRole view helper
@@ -40,22 +42,28 @@ class IfHasRoleViewHelperTest extends ViewHelperBaseTestcase
                 'title' => 'OtherRole'
             ]
         ];
-        $this->viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\Be\Security\IfHasRoleViewHelper::class, ['renderThenChild', 'renderElseChild']);
-        $this->viewHelper->expects($this->any())->method('renderThenChild')->will($this->returnValue('then child'));
-        $this->viewHelper->expects($this->any())->method('renderElseChild')->will($this->returnValue('else child'));
+        $this->viewHelper = new IfHasRoleViewHelper();
         $this->injectDependenciesIntoViewHelper($this->viewHelper);
         $this->viewHelper->initializeArguments();
     }
 
+    protected function tearDown()
+    {
+        unset($GLOBALS['BE_USER']);
+    }
+
     /**
      * @test
      */
     public function viewHelperRendersThenChildIfBeUserWithSpecifiedRoleIsLoggedIn()
     {
-        $this->arguments['role'] = 'Editor';
-        $this->injectDependenciesIntoViewHelper($this->viewHelper);
+        $actualResult = $this->viewHelper->renderStatic(
+            ['role' => 'Editor', 'then' => 'then child', 'else' => 'else child'],
+            function () {
+            },
+            $this->prophesize(RenderingContextInterface::class)->reveal()
+        );
 
-        $actualResult = $this->viewHelper->render('Editor');
         $this->assertEquals('then child', $actualResult);
     }
 
@@ -64,10 +72,13 @@ class IfHasRoleViewHelperTest extends ViewHelperBaseTestcase
      */
     public function viewHelperRendersThenChildIfBeUserWithSpecifiedRoleIdIsLoggedIn()
     {
-        $this->arguments['role'] = 1;
-        $this->injectDependenciesIntoViewHelper($this->viewHelper);
+        $actualResult = $this->viewHelper->renderStatic(
+            ['role' => 1, 'then' => 'then child', 'else' => 'else child'],
+            function () {
+            },
+            $this->prophesize(RenderingContextInterface::class)->reveal()
+        );
 
-        $actualResult = $this->viewHelper->render(1);
         $this->assertEquals('then child', $actualResult);
     }
 
@@ -76,10 +87,13 @@ class IfHasRoleViewHelperTest extends ViewHelperBaseTestcase
      */
     public function viewHelperRendersElseChildIfBeUserWithSpecifiedRoleIsNotLoggedIn()
     {
-        $this->arguments['role'] = 'editor';
-        $this->injectDependenciesIntoViewHelper($this->viewHelper);
+        $actualResult = $this->viewHelper->renderStatic(
+            ['role' => 'NonExistingRole', 'then' => 'then child', 'else' => 'else child'],
+            function () {
+            },
+            $this->prophesize(RenderingContextInterface::class)->reveal()
+        );
 
-        $actualResult = $this->viewHelper->render('editor');
         $this->assertEquals('else child', $actualResult);
     }
 
@@ -88,10 +102,13 @@ class IfHasRoleViewHelperTest extends ViewHelperBaseTestcase
      */
     public function viewHelperRendersElseChildIfBeUserWithSpecifiedRoleIdIsNotLoggedIn()
     {
-        $this->arguments['role'] = 123;
-        $this->injectDependenciesIntoViewHelper($this->viewHelper);
+        $actualResult = $this->viewHelper->renderStatic(
+            ['role' => 123, 'then' => 'then child', 'else' => 'else child'],
+            function () {
+            },
+            $this->prophesize(RenderingContextInterface::class)->reveal()
+        );
 
-        $actualResult = $this->viewHelper->render(123);
         $this->assertEquals('else child', $actualResult);
     }
 }
index 8d2c46f..16b87fc 100644 (file)
@@ -14,7 +14,9 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Security;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\ViewHelpers\Security\IfAuthenticatedViewHelper;
 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
+use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 
 /**
  * Testcase for security.ifAuthenticated view helper
@@ -30,20 +32,30 @@ class IfAuthenticatedViewHelperTest extends ViewHelperBaseTestcase
     {
         parent::setUp();
         $GLOBALS['TSFE'] = new \stdClass();
-        $this->viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\Security\IfAuthenticatedViewHelper::class, ['renderThenChild', 'renderElseChild']);
-        $this->viewHelper->expects($this->any())->method('renderThenChild')->will($this->returnValue('then child'));
-        $this->viewHelper->expects($this->any())->method('renderElseChild')->will($this->returnValue('else child'));
+        $this->viewHelper = new IfAuthenticatedViewHelper();
         $this->injectDependenciesIntoViewHelper($this->viewHelper);
         $this->viewHelper->initializeArguments();
     }
 
+    protected function tearDown()
+    {
+        unset($GLOBALS['TSFE']);
+    }
+
     /**
      * @test
      */
     public function viewHelperRendersThenChildIfFeUserIsLoggedIn()
     {
         $GLOBALS['TSFE']->loginUser = 1;
-        $actualResult = $this->viewHelper->render();
+
+        $actualResult = $this->viewHelper->renderStatic(
+            ['then' => 'then child', 'else' => 'else child'],
+            function () {
+            },
+            $this->prophesize(RenderingContextInterface::class)->reveal()
+        );
+
         $this->assertEquals('then child', $actualResult);
     }
 
@@ -53,7 +65,14 @@ class IfAuthenticatedViewHelperTest extends ViewHelperBaseTestcase
     public function viewHelperRendersElseChildIfFeUserIsNotLoggedIn()
     {
         $GLOBALS['TSFE']->loginUser = 0;
-        $actualResult = $this->viewHelper->render();
+
+        $actualResult = $this->viewHelper->renderStatic(
+            ['then' => 'then child', 'else' => 'else child'],
+            function () {
+            },
+            $this->prophesize(RenderingContextInterface::class)->reveal()
+        );
+
         $this->assertEquals('else child', $actualResult);
     }
 }
index 8d214d6..3b95df9 100644 (file)
@@ -14,7 +14,9 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Security;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\ViewHelpers\Security\IfHasRoleViewHelper;
 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
+use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 
 /**
  * Testcase for security.ifHasRole view helper
@@ -36,22 +38,28 @@ class IfHasRoleViewHelperTest extends ViewHelperBaseTestcase
             'uid' => [1, 2],
             'title' => ['Editor', 'OtherRole']
         ];
-        $this->viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\Security\IfHasRoleViewHelper::class, ['renderThenChild', 'renderElseChild']);
-        $this->viewHelper->expects($this->any())->method('renderThenChild')->will($this->returnValue('then child'));
-        $this->viewHelper->expects($this->any())->method('renderElseChild')->will($this->returnValue('else child'));
+        $this->viewHelper = new IfHasRoleViewHelper();
         $this->injectDependenciesIntoViewHelper($this->viewHelper);
         $this->viewHelper->initializeArguments();
     }
 
+    protected function tearDown()
+    {
+        unset($GLOBALS['TSFE']);
+    }
+
     /**
      * @test
      */
     public function viewHelperRendersThenChildIfFeUserWithSpecifiedRoleIsLoggedIn()
     {
-        $this->arguments['role'] = 'Editor';
-        $this->injectDependenciesIntoViewHelper($this->viewHelper);
+        $actualResult = $this->viewHelper->renderStatic(
+            ['role' => 'Editor', 'then' => 'then child', 'else' => 'else child'],
+            function () {
+            },
+            $this->prophesize(RenderingContextInterface::class)->reveal()
+        );
 
-        $actualResult = $this->viewHelper->render('Editor');
         $this->assertEquals('then child', $actualResult);
     }
 
@@ -60,10 +68,13 @@ class IfHasRoleViewHelperTest extends ViewHelperBaseTestcase
      */
     public function viewHelperRendersThenChildIfFeUserWithSpecifiedRoleIdIsLoggedIn()
     {
-        $this->arguments['role'] = 1;
-        $this->injectDependenciesIntoViewHelper($this->viewHelper);
+        $actualResult = $this->viewHelper->renderStatic(
+            ['role' => 1, 'then' => 'then child', 'else' => 'else child'],
+            function () {
+            },
+            $this->prophesize(RenderingContextInterface::class)->reveal()
+        );
 
-        $actualResult = $this->viewHelper->render(1);
         $this->assertEquals('then child', $actualResult);
     }
 
@@ -72,10 +83,13 @@ class IfHasRoleViewHelperTest extends ViewHelperBaseTestcase
      */
     public function viewHelperRendersElseChildIfFeUserWithSpecifiedRoleIsNotLoggedIn()
     {
-        $this->arguments['role'] = 'NonExistingRole';
-        $this->injectDependenciesIntoViewHelper($this->viewHelper);
+        $actualResult = $this->viewHelper->renderStatic(
+            ['role' => 'NonExistingRole', 'then' => 'then child', 'else' => 'else child'],
+            function () {
+            },
+            $this->prophesize(RenderingContextInterface::class)->reveal()
+        );
 
-        $actualResult = $this->viewHelper->render('NonExistingRole');
         $this->assertEquals('else child', $actualResult);
     }
 
@@ -84,10 +98,13 @@ class IfHasRoleViewHelperTest extends ViewHelperBaseTestcase
      */
     public function viewHelperRendersElseChildIfFeUserWithSpecifiedRoleIdIsNotLoggedIn()
     {
-        $this->arguments['role'] = 123;
-        $this->injectDependenciesIntoViewHelper($this->viewHelper);
+        $actualResult = $this->viewHelper->renderStatic(
+            ['role' => 123, 'then' => 'then child', 'else' => 'else child'],
+            function () {
+            },
+            $this->prophesize(RenderingContextInterface::class)->reveal()
+        );
 
-        $actualResult = $this->viewHelper->render(123);
         $this->assertEquals('else child', $actualResult);
     }
 }