[FEATURE] Add Contexts for storing data access modes
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Security / IfAuthenticatedViewHelperTest.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\IfAuthenticatedViewHelper;
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.ifAuthenticated view helper
27 */
28 class IfAuthenticatedViewHelperTest extends ViewHelperBaseTestcase
29 {
30 /**
31 * @var \TYPO3\CMS\Fluid\ViewHelpers\Security\IfAuthenticatedViewHelper
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 $this->viewHelper = new IfAuthenticatedViewHelper();
45 $this->injectDependenciesIntoViewHelper($this->viewHelper);
46 $this->viewHelper->initializeArguments();
47 }
48
49 protected function tearDown()
50 {
51 GeneralUtility::removeSingletonInstance(Context::class, $this->context);
52 }
53
54 /**
55 * @test
56 */
57 public function viewHelperRendersThenChildIfFeUserIsLoggedIn()
58 {
59 $user = new FrontendUserAuthentication();
60 $user->user['uid'] = 13;
61 $this->context->setAspect('frontend.user', new UserAspect($user));
62
63 $actualResult = $this->viewHelper->renderStatic(
64 ['then' => 'then child', 'else' => 'else child'],
65 function () {
66 },
67 $this->prophesize(RenderingContextInterface::class)->reveal()
68 );
69
70 $this->assertEquals('then child', $actualResult);
71 }
72
73 /**
74 * @test
75 */
76 public function viewHelperRendersElseChildIfFeUserIsNotLoggedIn()
77 {
78 $this->context->setAspect('frontend.user', new UserAspect());
79
80 $actualResult = $this->viewHelper->renderStatic(
81 ['then' => 'then child', 'else' => 'else child'],
82 function () {
83 },
84 $this->prophesize(RenderingContextInterface::class)->reveal()
85 );
86
87 $this->assertEquals('else child', $actualResult);
88 }
89 }