‪TYPO3CMS  ‪main
RecordAccessVoterTest.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;
22 use Psr\EventDispatcher\EventDispatcherInterface;
27 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
28 
29 final class ‪RecordAccessVoterTest extends FunctionalTestCase
30 {
32 
33  protected function ‪setUp(): void
34  {
35  parent::setUp();
36 
37  $this->subject = new ‪RecordAccessVoter(
38  $this->get(EventDispatcherInterface::class)
39  );
40  }
41 
42  public static function ‪accessGrantedTestDataProvider(): \Generator
43  {
44  yield 'No access permissions set' => [
45  'pages',
46  [
47  'uid' => 1,
48  ],
49  true,
50  ];
51  yield 'Record disabled' => [
52  'pages',
53  [
54  'uid' => 1,
55  'hidden' => 1,
56  ],
57  false,
58  ];
59  yield 'Starttime set' => [
60  'pages',
61  [
62  'uid' => 1,
63  'starttime' => 43,
64  ],
65  false,
66  ];
67  yield 'Endtime set' => [
68  'pages',
69  [
70  'uid' => 1,
71  'endtime' => 41,
72  ],
73  false,
74  ];
75  yield 'Invalid endtime' => [
76  'pages',
77  [
78  'uid' => 1,
79  'endtime' => 0,
80  ],
81  true,
82  ];
83  yield 'group access set' => [
84  'pages',
85  [
86  'uid' => 1,
87  'fe_group' => '1,2',
88  ],
89  false,
90  ];
91  yield 'all enable fields set - valid' => [
92  'pages',
93  [
94  'uid' => 1,
95  'hidden' => 0,
96  'starttime' => 41,
97  'endtime' => 43,
98  'fe_group' => '3,4',
99  ],
100  true,
101  ];
102  }
103 
104  #[DataProvider('accessGrantedTestDataProvider')]
105  #[Test]
106  public function ‪accessGrantedTest(string $table, array ‪$record, bool $access): void
107  {
108  ‪$GLOBALS['SIM_ACCESS_TIME'] = 42;
109  $context = new ‪Context();
110  $context->setAspect('frontend.user', new ‪UserAspect(null, [3, 4]));
111  self::assertEquals($access, $this->subject->accessGranted($table, ‪$record, $context));
112  }
113 
114  #[Test]
116  {
117  // Page is available even if the "disabled" flag is set
118  $context = new ‪Context();
119  $context->setAspect('visibility', new ‪VisibilityAspect(includeHiddenPages: true));
120  self::assertTrue($this->subject->accessGranted(
121  'pages',
122  ['uid' => 1, 'hidden' => 1],
123  $context
124  ));
125 
126  // Content is available even if the "disabled" flag is set
127  $context = new ‪Context();
128  $context->setAspect('visibility', new ‪VisibilityAspect(includeHiddenContent: true));
129  self::assertTrue($this->subject->accessGranted(
130  'tt_content',
131  ['uid' => 1, 'hidden' => 1],
132  $context
133  ));
134  }
135 
136  public static function ‪groupAccessGrantedTestDataProvider(): \Generator
137  {
138  yield 'No enable field' => [
139  'aTable',
140  [
141  'uid' => 1,
142  ],
143  true,
144  ];
145  yield 'No group access defined' => [
146  'pages',
147  [
148  'uid' => 1,
149  'fe_group' => '',
150  ],
151  true,
152  ];
153  yield 'Insufficient permissions' => [
154  'pages',
155  [
156  'uid' => 1,
157  'fe_group' => '1,2',
158  ],
159  false,
160  ];
161  yield 'Sufficient permissions' => [
162  'pages',
163  [
164  'uid' => 1,
165  'fe_group' => '3,4',
166  ],
167  true,
168  ];
169  }
170 
171  #[DataProvider('groupAccessGrantedTestDataProvider')]
172  #[Test]
173  public function ‪groupAccessGrantedTest(string $table, array ‪$record, bool $access): void
174  {
175  $context = new ‪Context();
176  $context->setAspect('frontend.user', new ‪UserAspect(null, [3, 4]));
177  self::assertEquals($access, $this->subject->groupAccessGranted($table, ‪$record, $context));
178  }
179 
180  #[Test]
182  {
183  self::assertTrue($this->subject->accessGrantedForPageInRootLine(['uid ' => 1, 'hidden' => 1], new ‪Context()));
184  }
185 }
‪TYPO3\CMS\Core\Context\VisibilityAspect
Definition: VisibilityAspect.php:31
‪TYPO3\CMS\Core\Tests\Functional\Domain\Access\RecordAccessVoterTest\accessGrantedForPageInRootLineReturnsTrueForDisabledExtendToSubpages
‪accessGrantedForPageInRootLineReturnsTrueForDisabledExtendToSubpages()
Definition: RecordAccessVoterTest.php:181
‪TYPO3\CMS\Core\Tests\Functional\Domain\Access\RecordAccessVoterTest\accessGrantedTestDataProvider
‪static accessGrantedTestDataProvider()
Definition: RecordAccessVoterTest.php:42
‪TYPO3\CMS\Core\Tests\Functional\Domain\Access\RecordAccessVoterTest\groupAccessGrantedTestDataProvider
‪static groupAccessGrantedTestDataProvider()
Definition: RecordAccessVoterTest.php:136
‪TYPO3\CMS\Core\Tests\Functional\Domain\Access\RecordAccessVoterTest\groupAccessGrantedTest
‪groupAccessGrantedTest(string $table, array $record, bool $access)
Definition: RecordAccessVoterTest.php:173
‪TYPO3\CMS\Core\Tests\Functional\Domain\Access\RecordAccessVoterTest\accessGrantedTest
‪accessGrantedTest(string $table, array $record, bool $access)
Definition: RecordAccessVoterTest.php:106
‪TYPO3\CMS\Core\Tests\Functional\Domain\Access\RecordAccessVoterTest\$subject
‪RecordAccessVoter $subject
Definition: RecordAccessVoterTest.php:31
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Core\Tests\Functional\Domain\Access\RecordAccessVoterTest
Definition: RecordAccessVoterTest.php:30
‪TYPO3\CMS\Core\Tests\Functional\Domain\Access\RecordAccessVoterTest\accessGrantedRespectsVisibilityAspect
‪accessGrantedRespectsVisibilityAspect()
Definition: RecordAccessVoterTest.php:115
‪TYPO3\CMS\Core\Tests\Functional\Domain\Access\RecordAccessVoterTest\setUp
‪setUp()
Definition: RecordAccessVoterTest.php:33
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪TYPO3\CMS\Core\Tests\Functional\Domain\Access
Definition: RecordAccessVoterTest.php:18
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Domain\Access\RecordAccessVoter
Definition: RecordAccessVoter.php:31
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:37