‪TYPO3CMS  ‪main
IfHasRoleViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
27 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
28 use TYPO3Fluid\Fluid\View\TemplateView;
29 
30 final class ‪IfHasRoleViewHelperTest extends FunctionalTestCase
31 {
32  protected bool ‪$initializeDatabase = false;
33 
34  protected function ‪setUp(): void
35  {
36  parent::setUp();
37  $context = new ‪Context();
38  GeneralUtility::setSingletonInstance(Context::class, $context);
39  $user = new ‪FrontendUserAuthentication();
40  $user->user['uid'] = 13;
41  $user->userGroups = [
42  1 => ['uid' => 1, 'title' => 'Editor'],
43  2 => ['uid' => 2, 'title' => 'OtherRole'],
44  ];
45  $context->setAspect('frontend.user', new ‪UserAspect($user, [1, 2]));
46  }
47 
48  public static function ‪renderDataProvider(): array
49  {
50  return [
51  'viewHelperRendersThenChildIfFeUserWithSpecifiedRoleIsLoggedIn' => [
52  '<f:security.ifHasRole role="Editor"><f:then>then child</f:then><f:else>else child</f:else></f:security.ifHasRole>',
53  'then child',
54  ],
55  'viewHelperRendersThenChildIfFeUserWithSpecifiedRoleIdIsLoggedIn' => [
56  '<f:security.ifHasRole role="1"><f:then>then child</f:then><f:else>else child</f:else></f:security.ifHasRole>',
57  'then child',
58  ],
59  'viewHelperRendersElseChildIfFeUserWithSpecifiedRoleIsNotLoggedIn' => [
60  '<f:security.ifHasRole role="NonExistingRole"><f:then>then child</f:then><f:else>else child</f:else></f:security.ifHasRole>',
61  'else child',
62  ],
63  'viewHelperRendersElseChildIfFeUserWithSpecifiedRoleIdIsNotLoggedIn' => [
64  '<f:security.ifHasRole role="123"><f:then>then child</f:then><f:else>else child</f:else></f:security.ifHasRole>',
65  'else child',
66  ],
67  ];
68  }
69 
70  #[DataProvider('renderDataProvider')]
71  #[Test]
72  public function ‪render(string $template, string $expected): void
73  {
74  $context = $this->get(RenderingContextFactory::class)->create();
75  $context->getTemplatePaths()->setTemplateSource($template);
76  self::assertEquals($expected, (new TemplateView($context))->‪render());
77  }
78 }
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Security\IfHasRoleViewHelperTest\setUp
‪setUp()
Definition: IfHasRoleViewHelperTest.php:34
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Security\IfHasRoleViewHelperTest\renderDataProvider
‪static renderDataProvider()
Definition: IfHasRoleViewHelperTest.php:48
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Security\IfHasRoleViewHelperTest\render
‪render(string $template, string $expected)
Definition: IfHasRoleViewHelperTest.php:72
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Security
Definition: IfAuthenticatedViewHelperTest.php:18
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Security\IfHasRoleViewHelperTest
Definition: IfHasRoleViewHelperTest.php:31
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory
Definition: RenderingContextFactory.php:51
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:37
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Security\IfHasRoleViewHelperTest\$initializeDatabase
‪bool $initializeDatabase
Definition: IfHasRoleViewHelperTest.php:32