Commit c2f3876b authored by Mathias Brodala's avatar Mathias Brodala Committed by Benni Mack
Browse files

[TASK] Simplify frontend/backend security viewhelpers

This logic is already implemented in
AbstractConditionViewHelper::renderStatic().

Resolves: #84627
Releases: master
Change-Id: Iaf1fa4630fcc5df0ef22bec55d4fa15df1aa0986
Reviewed-on: https://review.typo3.org/56561


Tested-by: default avatarTYPO3com <no-reply@typo3.com>
Reviewed-by: default avatarStefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: default avatarStefan Neufeind <typo3.neufeind@speedpartner.de>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent 8b171543
......@@ -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();
}
}
......@@ -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();
}
}
......@@ -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();
}
}
......@@ -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();
}
}
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment