d76ef5ee88f949234479785e978bdc3b1a7c69de
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Tests / Unit / Middleware / AdminPanelInitiatorTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Adminpanel\Tests\Unit\Middleware;
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
18 use Prophecy\Argument;
19 use Prophecy\Prophecy\ObjectProphecy;
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
23 use TYPO3\CMS\Adminpanel\Controller\MainController;
24 use TYPO3\CMS\Adminpanel\Middleware\AdminPanelInitiator;
25 use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29
30 /**
31 * Test case
32 */
33 class AdminPanelInitiatorTest extends UnitTestCase
34 {
35 /**
36 * @var bool Reset singletons created by subject
37 */
38 protected $resetSingletonInstances = true;
39
40 /**
41 * @test
42 */
43 public function processCallsInitialize(): void
44 {
45 $tsConfig = [
46 'admPanel.' => [
47 'enable.' => [
48 'all',
49 ],
50 ],
51 ];
52 $uc = [
53 'AdminPanel' => [
54 'display_top' => true
55 ]
56 ];
57 $userAuthentication = $this->prophesize(FrontendBackendUserAuthentication::class);
58 $userAuthentication->getTSConfig(Argument::any())->willReturn($tsConfig);
59 $userAuthentication->uc = $uc;
60 $GLOBALS['BE_USER'] = $userAuthentication->reveal();
61
62 $tsfe = $this->prophesize(TypoScriptFrontendController::class);
63 $GLOBALS['TSFE'] = $tsfe;
64
65 $controller = $this->prophesize(MainController::class);
66 GeneralUtility::setSingletonInstance(MainController::class, $controller->reveal());
67 $handler = $this->prophesizeHandler();
68 $request = $this->prophesize(ServerRequestInterface::class);
69 $request->withAttribute(Argument::cetera())->willReturn($request);
70 // Act
71 $adminPanelInitiator = new AdminPanelInitiator();
72 $adminPanelInitiator->process(
73 $request->reveal(),
74 $handler->reveal()
75 );
76 // Assert
77 $controller->initialize(Argument::any())->shouldHaveBeenCalled();
78 }
79
80 /**
81 * @test
82 */
83 public function processDoesNotCallInitializeIfAdminPanelIsNotEnabledInUC(): void
84 {
85 $tsConfig = [
86 'admPanel.' => [
87 'enable.' => [
88 'all',
89 ],
90 ],
91 ];
92 $uc = [
93 'AdminPanel' => [
94 'display_top' => false
95 ]
96 ];
97 $this->checkAdminPanelDoesNotCallInitialize($tsConfig, $uc);
98 }
99
100 /**
101 * @test
102 */
103 public function processDoesNotCallInitializeIfNoAdminPanelModuleIsEnabled(): void
104 {
105 $tsConfig = [
106 'admPanel.' => [],
107 ];
108 $uc = [
109 'AdminPanel' => [
110 'display_top' => true
111 ]
112 ];
113 $this->checkAdminPanelDoesNotCallInitialize($tsConfig, $uc);
114 }
115
116 /**
117 * @param $tsConfig
118 * @param $uc
119 * @param $typoScript
120 */
121 protected function checkAdminPanelDoesNotCallInitialize($tsConfig, $uc): void
122 {
123 $userAuthentication = $this->prophesize(FrontendBackendUserAuthentication::class);
124 $userAuthentication->getTSConfig(Argument::any())->willReturn($tsConfig);
125 $userAuthentication->uc = $uc;
126 $GLOBALS['BE_USER'] = $userAuthentication->reveal();
127
128 $tsfe = $this->prophesize(TypoScriptFrontendController::class);
129 $GLOBALS['TSFE'] = $tsfe;
130
131 $controller = $this->prophesize(MainController::class);
132 GeneralUtility::setSingletonInstance(MainController::class, $controller->reveal());
133 $handler = $this->prophesizeHandler();
134 $request = $this->prophesize(ServerRequestInterface::class);
135 // Act
136 $adminPanelInitiator = new AdminPanelInitiator();
137 $adminPanelInitiator->process(
138 $request->reveal(),
139 $handler->reveal()
140 );
141 // Assert
142 $controller->initialize(Argument::any())->shouldNotHaveBeenCalled();
143 }
144
145 /**
146 * @return ObjectProphecy|RequestHandlerInterface
147 */
148 protected function prophesizeHandler()
149 {
150 $handler = $this->prophesize(RequestHandlerInterface::class);
151 $handler
152 ->handle(Argument::any())
153 ->willReturn(
154 $this->prophesize(ResponseInterface::class)->reveal()
155 );
156 return $handler;
157 }
158 }