‪TYPO3CMS  ‪main
AdminPanelInitiatorTest.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\Test;
21 use PHPUnit\Framework\MockObject\MockObject;
22 use Psr\Http\Message\ResponseInterface;
23 use Psr\Http\Message\ServerRequestInterface;
24 use Psr\Http\Server\RequestHandlerInterface;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 final class ‪AdminPanelInitiatorTest extends UnitTestCase
32 {
33  #[Test]
34  public function ‪processCallsInitialize(): void
35  {
36  $tsConfig = [
37  'admPanel.' => [
38  'enable.' => [
39  'all',
40  ],
41  ],
42  ];
43  $uc = [
44  'AdminPanel' => [
45  'display_top' => true,
46  ],
47  ];
48  $userAuthentication = $this->getMockBuilder(FrontendBackendUserAuthentication::class)->getMock();
49  $userAuthentication->expects(self::once())->method('getTSConfig')->willReturn($tsConfig);
50  $userAuthentication->uc = $uc;
51  ‪$GLOBALS['BE_USER'] = $userAuthentication;
52 
53  $controller = $this->getMockBuilder(MainController::class)->disableOriginalConstructor()->getMock();
54  GeneralUtility::addInstance(MainController::class, $controller);
55  $handler = $this->‪getHandlerMock();
56  $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
57  $request->expects(self::any())->method('withAttribute')->withAnyParameters()->willReturn($request);
58  $controller->expects(self::once())->method('initialize')->with($request)->willReturn($request);
59 
60  $adminPanelInitiator = new ‪AdminPanelInitiator();
61  $adminPanelInitiator->process($request, $handler);
62  }
63 
64  #[Test]
66  {
67  $tsConfig = [
68  'admPanel.' => [
69  'enable.' => [
70  'all',
71  ],
72  ],
73  ];
74  $uc = [
75  'AdminPanel' => [
76  'display_top' => false,
77  ],
78  ];
79  $this->‪checkAdminPanelDoesNotCallInitialize($tsConfig, $uc);
80  }
81 
82  #[Test]
84  {
85  $tsConfig = [
86  'admPanel.' => [],
87  ];
88  $uc = [
89  'AdminPanel' => [
90  'display_top' => true,
91  ],
92  ];
93  $this->‪checkAdminPanelDoesNotCallInitialize($tsConfig, $uc);
94  }
95 
96  protected function ‪checkAdminPanelDoesNotCallInitialize(array $tsConfig, array $uc): void
97  {
98  $userAuthentication = $this->getMockBuilder(FrontendBackendUserAuthentication::class)->getMock();
99  $userAuthentication->expects(self::once())->method('getTSConfig')->willReturn($tsConfig);
100  $userAuthentication->uc = $uc;
101  ‪$GLOBALS['BE_USER'] = $userAuthentication;
102 
103  $controller = $this->getMockBuilder(MainController::class)->disableOriginalConstructor()->getMock();
104  GeneralUtility::addInstance(MainController::class, $controller);
105  $handler = $this->‪getHandlerMock();
106  $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
107  $controller->expects(self::never())->method('initialize');
108 
109  $adminPanelInitiator = new ‪AdminPanelInitiator();
110  $adminPanelInitiator->process($request, $handler);
111 
112  GeneralUtility::makeInstance(MainController::class);
113  }
114 
115  protected function ‪getHandlerMock(): RequestHandlerInterface&MockObject
116  {
117  $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
118  $handler = $this->getMockBuilder(RequestHandlerInterface::class)->onlyMethods(['handle'])->getMock();
119  $handler->expects(self::any())->method('handle')->withAnyParameters()->willReturn($response);
120  return $handler;
121  }
122 }
‪TYPO3\CMS\Adminpanel\Controller\MainController
Definition: MainController.php:49
‪TYPO3\CMS\Adminpanel\Tests\Unit\Middleware
Definition: AdminPanelInitiatorTest.php:18
‪TYPO3\CMS\Adminpanel\Tests\Unit\Middleware\AdminPanelInitiatorTest\processCallsInitialize
‪processCallsInitialize()
Definition: AdminPanelInitiatorTest.php:34
‪TYPO3\CMS\Adminpanel\Middleware\AdminPanelInitiator
Definition: AdminPanelInitiator.php:35
‪TYPO3\CMS\Adminpanel\Tests\Unit\Middleware\AdminPanelInitiatorTest\getHandlerMock
‪getHandlerMock()
Definition: AdminPanelInitiatorTest.php:115
‪TYPO3\CMS\Backend\FrontendBackendUserAuthentication
Definition: FrontendBackendUserAuthentication.php:29
‪TYPO3\CMS\Adminpanel\Tests\Unit\Middleware\AdminPanelInitiatorTest\processDoesNotCallInitializeIfAdminPanelIsNotEnabledInUC
‪processDoesNotCallInitializeIfAdminPanelIsNotEnabledInUC()
Definition: AdminPanelInitiatorTest.php:65
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Adminpanel\Tests\Unit\Middleware\AdminPanelInitiatorTest\processDoesNotCallInitializeIfNoAdminPanelModuleIsEnabled
‪processDoesNotCallInitializeIfNoAdminPanelModuleIsEnabled()
Definition: AdminPanelInitiatorTest.php:83
‪TYPO3\CMS\Adminpanel\Tests\Unit\Middleware\AdminPanelInitiatorTest
Definition: AdminPanelInitiatorTest.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Adminpanel\Tests\Unit\Middleware\AdminPanelInitiatorTest\checkAdminPanelDoesNotCallInitialize
‪checkAdminPanelDoesNotCallInitialize(array $tsConfig, array $uc)
Definition: AdminPanelInitiatorTest.php:96