[FEATURE] Add Contexts for storing data access modes
[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\Core\Context\Context;
18 use TYPO3\CMS\Core\Context\UserAspect;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Fluid\ViewHelpers\Security\IfHasRoleViewHelper;
21 use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
22 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
23 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
24
25 /**
26 * Testcase for security.ifHasRole view helper
27 */
28 class IfHasRoleViewHelperTest extends ViewHelperBaseTestcase
29 {
30 /**
31 * @var \TYPO3\CMS\Fluid\ViewHelpers\Security\IfHasRoleViewHelper
32 */
33 protected $viewHelper;
34
35 /**
36 * @var Context
37 */
38 protected $context;
39
40 protected function setUp()
41 {
42 parent::setUp();
43 $this->context = GeneralUtility::makeInstance(Context::class);
44 $user = new FrontendUserAuthentication();
45 $user->user['uid'] = 13;
46 $user->groupData = [
47 'uid' => [1, 2],
48 'title' => ['Editor', 'OtherRole']
49 ];
50 $this->context->setAspect('frontend.user', new UserAspect($user, [1, 2]));
51 $this->viewHelper = new IfHasRoleViewHelper();
52 $this->injectDependenciesIntoViewHelper($this->viewHelper);
53 $this->viewHelper->initializeArguments();
54 }
55
56 protected function tearDown()
57 {
58 GeneralUtility::removeSingletonInstance(Context::class, $this->context);
59 }
60
61 /**
62 * @test
63 */
64 public function viewHelperRendersThenChildIfFeUserWithSpecifiedRoleIsLoggedIn()
65 {
66 $actualResult = $this->viewHelper->renderStatic(
67 ['role' => 'Editor', 'then' => 'then child', 'else' => 'else child'],
68 function () {
69 },
70 $this->prophesize(RenderingContextInterface::class)->reveal()
71 );
72
73 $this->assertEquals('then child', $actualResult);
74 }
75
76 /**
77 * @test
78 */
79 public function viewHelperRendersThenChildIfFeUserWithSpecifiedRoleIdIsLoggedIn()
80 {
81 $actualResult = $this->viewHelper->renderStatic(
82 ['role' => 1, 'then' => 'then child', 'else' => 'else child'],
83 function () {
84 },
85 $this->prophesize(RenderingContextInterface::class)->reveal()
86 );
87
88 $this->assertEquals('then child', $actualResult);
89 }
90
91 /**
92 * @test
93 */
94 public function viewHelperRendersElseChildIfFeUserWithSpecifiedRoleIsNotLoggedIn()
95 {
96 $actualResult = $this->viewHelper->renderStatic(
97 ['role' => 'NonExistingRole', 'then' => 'then child', 'else' => 'else child'],
98 function () {
99 },
100 $this->prophesize(RenderingContextInterface::class)->reveal()
101 );
102
103 $this->assertEquals('else child', $actualResult);
104 }
105
106 /**
107 * @test
108 */
109 public function viewHelperRendersElseChildIfFeUserWithSpecifiedRoleIdIsNotLoggedIn()
110 {
111 $actualResult = $this->viewHelper->renderStatic(
112 ['role' => 123, 'then' => 'then child', 'else' => 'else child'],
113 function () {
114 },
115 $this->prophesize(RenderingContextInterface::class)->reveal()
116 );
117
118 $this->assertEquals('else child', $actualResult);
119 }
120 }