[!!!][TASK] Drop "documentation" extension
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Security / IfHasRoleViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Security;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Fluid\ViewHelpers\Security\IfHasRoleViewHelper;
18 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
19 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20
21 /**
22 * Testcase for security.ifHasRole view helper
23 */
24 class IfHasRoleViewHelperTest extends ViewHelperBaseTestcase
25 {
26 /**
27 * @var \TYPO3\CMS\Fluid\ViewHelpers\Security\IfHasRoleViewHelper
28 */
29 protected $viewHelper;
30
31 protected function setUp()
32 {
33 parent::setUp();
34 $GLOBALS['TSFE'] = new \stdClass();
35 $GLOBALS['TSFE']->loginUser = 1;
36 $GLOBALS['TSFE']->fe_user = new \stdClass();
37 $GLOBALS['TSFE']->fe_user->groupData = [
38 'uid' => [1, 2],
39 'title' => ['Editor', 'OtherRole']
40 ];
41 $this->viewHelper = new IfHasRoleViewHelper();
42 $this->injectDependenciesIntoViewHelper($this->viewHelper);
43 $this->viewHelper->initializeArguments();
44 }
45
46 protected function tearDown()
47 {
48 unset($GLOBALS['TSFE']);
49 }
50
51 /**
52 * @test
53 */
54 public function viewHelperRendersThenChildIfFeUserWithSpecifiedRoleIsLoggedIn()
55 {
56 $actualResult = $this->viewHelper->renderStatic(
57 ['role' => 'Editor', 'then' => 'then child', 'else' => 'else child'],
58 function () {
59 },
60 $this->prophesize(RenderingContextInterface::class)->reveal()
61 );
62
63 $this->assertEquals('then child', $actualResult);
64 }
65
66 /**
67 * @test
68 */
69 public function viewHelperRendersThenChildIfFeUserWithSpecifiedRoleIdIsLoggedIn()
70 {
71 $actualResult = $this->viewHelper->renderStatic(
72 ['role' => 1, 'then' => 'then child', 'else' => 'else child'],
73 function () {
74 },
75 $this->prophesize(RenderingContextInterface::class)->reveal()
76 );
77
78 $this->assertEquals('then child', $actualResult);
79 }
80
81 /**
82 * @test
83 */
84 public function viewHelperRendersElseChildIfFeUserWithSpecifiedRoleIsNotLoggedIn()
85 {
86 $actualResult = $this->viewHelper->renderStatic(
87 ['role' => 'NonExistingRole', 'then' => 'then child', 'else' => 'else child'],
88 function () {
89 },
90 $this->prophesize(RenderingContextInterface::class)->reveal()
91 );
92
93 $this->assertEquals('else child', $actualResult);
94 }
95
96 /**
97 * @test
98 */
99 public function viewHelperRendersElseChildIfFeUserWithSpecifiedRoleIdIsNotLoggedIn()
100 {
101 $actualResult = $this->viewHelper->renderStatic(
102 ['role' => 123, 'then' => 'then child', 'else' => 'else child'],
103 function () {
104 },
105 $this->prophesize(RenderingContextInterface::class)->reveal()
106 );
107
108 $this->assertEquals('else child', $actualResult);
109 }
110 }