‪TYPO3CMS  ‪main
StateUtilityTest.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;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
27 final class ‪StateUtilityTest extends UnitTestCase
28 {
29  #[Test]
31  {
32  ‪$GLOBALS['BE_USER'] = false;
34  self::assertFalse($isEnabled);
35  }
36 
37  #[Test]
39  {
40  ‪$GLOBALS['BE_USER'] = $this->getMockBuilder(BackendUserAuthentication::class)->disableOriginalConstructor()->getMock();
42  self::assertFalse($isEnabled);
43  }
44 
45  public static function ‪tsConfigEnabledDataProvider(): array
46  {
47  return [
48  '1 module enabled' => [
49  [
50  'admPanel.' => [
51  'enable.' => [
52  'preview' => 1,
53  ],
54  ],
55  ],
56  ],
57  'all modules enabled' => [
58  [
59  'admPanel.' => [
60  'enable.' => [
61  'all' => 1,
62  ],
63  ],
64  ],
65  ],
66  ];
67  }
68 
69  #[DataProvider('tsConfigEnabledDataProvider')]
70  #[Test]
71  public function ‪isEnabledReturnsTrueIfAtLeastOneModuleIsEnabled(array $tsConfig): void
72  {
73  $beUserMock = $this->getMockBuilder(FrontendBackendUserAuthentication::class)->disableOriginalConstructor()->getMock();
74  $beUserMock->method('getTSConfig')->willReturn($tsConfig);
75  ‪$GLOBALS['BE_USER'] = $beUserMock;
77  self::assertTrue($isEnabled);
78  }
79 
80  public static function ‪tsConfigDisabledDataProvider(): array
81  {
82  return [
83  'no config set' => [
84  [],
85  ],
86  'all modules disabled' => [
87  [
88  'admPanel.' => [
89  'enable.' => [
90  'all' => 0,
91  ],
92  ],
93  ],
94  ],
95  'single module configured, disabled' => [
96  [
97  'admPanel.' => [
98  'enable.' => [
99  'preview' => 0,
100  ],
101  ],
102  ],
103  ],
104  ];
105  }
106 
107  #[DataProvider('tsConfigDisabledDataProvider')]
108  #[Test]
109  public function ‪isEnabledReturnsFalseIfNoModulesEnabled(array $tsConfig): void
110  {
111  $beUserMock = $this->getMockBuilder(FrontendBackendUserAuthentication::class)->disableOriginalConstructor()->getMock();
112  $beUserMock->method('getTSConfig')->willReturn($tsConfig);
113  ‪$GLOBALS['BE_USER'] = $beUserMock;
115  self::assertFalse($isEnabled);
116  }
117 
118  public static function ‪tsConfigHideDataProvider(): array
119  {
120  return [
121  'no config set' => [
122  [],
123  false,
124  ],
125  'defined as not hidden' => [
126  [
127  'admPanel.' => [
128  'hide' => '0',
129  ],
130  ],
131  false,
132  ],
133  'defined as hidden' => [
134  [
135  'admPanel.' => [
136  'hide' => '1',
137  ],
138  ],
139  true,
140  ],
141  ];
142  }
143 
144  #[DataProvider('tsConfigHideDataProvider')]
145  #[Test]
146  public function ‪isHiddenForUserReturnsCorrectValue(array $tsConfig, bool $expected): void
147  {
148  $beUserMock = $this->getMockBuilder(FrontendBackendUserAuthentication::class)->disableOriginalConstructor()->getMock();
149  $beUserMock->method('getTSConfig')->willReturn($tsConfig);
150  ‪$GLOBALS['BE_USER'] = $beUserMock;
151  $isEnabled = ‪StateUtility::isHiddenForUser();
152  self::assertSame($expected, $isEnabled);
153  }
154 
155  #[Test]
157  {
158  ‪$GLOBALS['BE_USER'] = null;
159  $isEnabled = ‪StateUtility::isHiddenForUser();
160  self::assertFalse($isEnabled);
161  }
162 
163  public static function ‪ucDisplayOpenDataProvider(): array
164  {
165  return [
166  'no config set' => [
167  [],
168  false,
169  ],
170  'defined as display_top=false' => [
171  [
172  'AdminPanel' => [
173  'display_top' => false,
174  ],
175  ],
176  false,
177  ],
178  'defined as display_top=true' => [
179  [
180  'AdminPanel' => [
181  'display_top' => true,
182  ],
183  ],
184  true,
185  ],
186  ];
187  }
188 
189  #[DataProvider('ucDisplayOpenDataProvider')]
190  #[Test]
191  public function ‪isOpenForUserReturnsCorrectValue(array $uc, bool $expected): void
192  {
193  $beUser = new ‪FrontendBackendUserAuthentication();
194  $beUser->uc = $uc;
195  ‪$GLOBALS['BE_USER'] = $beUser;
196  $isOpen = ‪StateUtility::isOpen();
197  self::assertSame($expected, $isOpen);
198  }
199 
200  #[Test]
202  {
203  ‪$GLOBALS['BE_USER'] = null;
204  $isOpen = ‪StateUtility::isOpen();
205  self::assertFalse($isOpen);
206  }
207 }
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility
Definition: StateUtilityTest.php:18
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility\StateUtilityTest\isHiddenForUserReturnsFalseIfUserIsNotAvailable
‪isHiddenForUserReturnsFalseIfUserIsNotAvailable()
Definition: StateUtilityTest.php:156
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility\StateUtilityTest\isEnabledReturnsFalseIfNoBackendUserInFrontendContextIsLoggedIn
‪isEnabledReturnsFalseIfNoBackendUserInFrontendContextIsLoggedIn()
Definition: StateUtilityTest.php:38
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility\StateUtilityTest
Definition: StateUtilityTest.php:28
‪TYPO3\CMS\Backend\FrontendBackendUserAuthentication
Definition: FrontendBackendUserAuthentication.php:29
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility\StateUtilityTest\isHiddenForUserReturnsCorrectValue
‪isHiddenForUserReturnsCorrectValue(array $tsConfig, bool $expected)
Definition: StateUtilityTest.php:146
‪TYPO3\CMS\Adminpanel\Utility\StateUtility\isHiddenForUser
‪static isHiddenForUser()
Definition: StateUtility.php:55
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility\StateUtilityTest\tsConfigHideDataProvider
‪static tsConfigHideDataProvider()
Definition: StateUtilityTest.php:118
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility\StateUtilityTest\isOpenForUserReturnsCorrectValue
‪isOpenForUserReturnsCorrectValue(array $uc, bool $expected)
Definition: StateUtilityTest.php:191
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility\StateUtilityTest\ucDisplayOpenDataProvider
‪static ucDisplayOpenDataProvider()
Definition: StateUtilityTest.php:163
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility\StateUtilityTest\isEnabledReturnsFalseIfNoBackendUserExists
‪isEnabledReturnsFalseIfNoBackendUserExists()
Definition: StateUtilityTest.php:30
‪TYPO3\CMS\Adminpanel\Utility\StateUtility
Definition: StateUtility.php:28
‪TYPO3\CMS\Adminpanel\Utility\StateUtility\isActivatedForUser
‪static isActivatedForUser()
Definition: StateUtility.php:32
‪TYPO3\CMS\Adminpanel\Utility\StateUtility\isOpen
‪static isOpen()
Definition: StateUtility.php:49
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility\StateUtilityTest\tsConfigEnabledDataProvider
‪static tsConfigEnabledDataProvider()
Definition: StateUtilityTest.php:45
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility\StateUtilityTest\isOpenForUserReturnsFalseIfUserIsNotAvailable
‪isOpenForUserReturnsFalseIfUserIsNotAvailable()
Definition: StateUtilityTest.php:201
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility\StateUtilityTest\isEnabledReturnsTrueIfAtLeastOneModuleIsEnabled
‪isEnabledReturnsTrueIfAtLeastOneModuleIsEnabled(array $tsConfig)
Definition: StateUtilityTest.php:71
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility\StateUtilityTest\isEnabledReturnsFalseIfNoModulesEnabled
‪isEnabledReturnsFalseIfNoModulesEnabled(array $tsConfig)
Definition: StateUtilityTest.php:109
‪TYPO3\CMS\Adminpanel\Tests\Unit\Utility\StateUtilityTest\tsConfigDisabledDataProvider
‪static tsConfigDisabledDataProvider()
Definition: StateUtilityTest.php:80