[TASK] Use PageRenderer directly in LoginController 80/59780/3
authorBenni Mack <benni@typo3.org>
Thu, 21 Feb 2019 15:53:08 +0000 (16:53 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sat, 23 Feb 2019 16:21:59 +0000 (17:21 +0100)
In order to emphasize the PageRenderer API and get rid
of DocumentTemplate logic in further refactorings, the inline
CSS and JavaScript is added directly to the PageRenderer
instead of the document template, which contains its own
legacy-concatenation magic.

Resolves: #87765
Releases: master
Change-Id: Id5db83f34f3ab31b016198cb90a8c84126b43dd6
Reviewed-on: https://review.typo3.org/c/59780
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/backend/Classes/Controller/LoginController.php
typo3/sysext/backend/Tests/Unit/Controller/LoginControllerTest.php

index 6a96d61..c35089c 100644 (file)
@@ -203,7 +203,7 @@ class LoginController implements LoggerAwareInterface
 
         // Checking, if we should make a redirect.
         // Might set JavaScript in the header to close window.
-        $this->checkRedirect($request);
+        $this->checkRedirect($request, $pageRenderer);
 
         // Extension Configuration
         $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('backend');
@@ -217,13 +217,13 @@ class LoginController implements LoggerAwareInterface
                     '" can\'t be resolved. Please check if the file exists and the extension is activated.'
                 );
             }
-            $this->documentTemplate->inDocStylesArray[] = '
+            $pageRenderer->addCssInlineBlock('loginBackgroundImage', '
                                .typo3-login-carousel-control.right,
                                .typo3-login-carousel-control.left,
                                .panel-login { border: 0; }
                                .typo3-login { background-image: url("' . $backgroundImage . '"); }
                                .typo3-login-footnote { background-color: #000000; color: #ffffff; opacity: 0.5; }
-                       ';
+                       ');
         }
 
         // Login Footnote
@@ -233,7 +233,7 @@ class LoginController implements LoggerAwareInterface
 
         // Add additional css to use the highlight color in the login screen
         if (!empty($extConf['loginHighlightColor'])) {
-            $this->documentTemplate->inDocStylesArray[] = '
+            $pageRenderer->addCssInlineBlock('loginHighlightColor', '
                                .btn-login.disabled, .btn-login[disabled], fieldset[disabled] .btn-login,
                                .btn-login.disabled:hover, .btn-login[disabled]:hover, fieldset[disabled] .btn-login:hover,
                                .btn-login.disabled:focus, .btn-login[disabled]:focus, fieldset[disabled] .btn-login:focus,
@@ -244,7 +244,7 @@ class LoginController implements LoggerAwareInterface
                                .btn-login:active:hover, .btn-login:active:focus,
                                .btn-login { background-color: ' . $extConf['loginHighlightColor'] . '; }
                                .panel-login .panel-body { border-color: ' . $extConf['loginHighlightColor'] . '; }
-                       ';
+                       ');
         }
 
         // Logo
@@ -263,9 +263,9 @@ class LoginController implements LoggerAwareInterface
             } else {
                 $logo = 'EXT:backend/Resources/Public/Images/typo3_orange.svg';
             }
-            $this->documentTemplate->inDocStylesArray[] = '
+            $pageRenderer->addCssInlineBlock('loginLogo', '
                                .typo3-login-logo .typo3-login-image { max-width: 150px; height:100%;}
-                       ';
+                       ');
         }
         $logo = $this->getUriForFileName($logo);
 
@@ -308,11 +308,12 @@ class LoginController implements LoggerAwareInterface
      * Do a redirect if a user is logged in
      *
      * @param ServerRequestInterface $request
+     * @param PageRenderer $pageRenderer
      * @throws \RuntimeException
      * @throws \UnexpectedValueException
      * @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
      */
-    protected function checkRedirect(ServerRequestInterface $request): void
+    protected function checkRedirect(ServerRequestInterface $request, PageRenderer $pageRenderer): void
     {
         $backendUser = $this->getBackendUserAuthentication();
         if (empty($backendUser->user['uid'])) {
@@ -367,7 +368,7 @@ class LoginController implements LoggerAwareInterface
         if ($this->loginRefresh) {
             $formProtection->setSessionTokenFromRegistry();
             $formProtection->persistSessionToken();
-            $this->documentTemplate->JScode .= GeneralUtility::wrapJS('
+            $pageRenderer->addJsInlineCode('loginRefresh', '
                                if (window.opener && window.opener.TYPO3 && window.opener.TYPO3.LoginRefresh) {
                                        window.opener.TYPO3.LoginRefresh.startTask();
                                        window.close();
index 21222e2..ec06f91 100644 (file)
@@ -15,13 +15,16 @@ namespace TYPO3\CMS\Backend\Tests\Unit\Controller;
  * The TYPO3 project - inspiring people to share!
  */
 
+use Prophecy\Argument;
+use Prophecy\Prophecy\MethodProphecy;
+use Prophecy\Prophecy\ObjectProphecy;
 use TYPO3\CMS\Backend\Controller\LoginController;
 use TYPO3\CMS\Backend\LoginProvider\UsernamePasswordLoginProvider;
-use TYPO3\CMS\Backend\Template\DocumentTemplate;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 use TYPO3\CMS\Core\FormProtection\BackendFormProtection;
 use TYPO3\CMS\Core\Http\ServerRequest;
 use TYPO3\CMS\Core\Localization\LanguageService;
+use TYPO3\CMS\Core\Page\PageRenderer;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
@@ -198,7 +201,11 @@ class LoginControllerTest extends UnitTestCase
         $this->loginControllerMock->_set('loginRefresh', false);
 
         $this->loginControllerMock->expects($this->once())->method('redirectToUrl');
-        $this->loginControllerMock->_call('checkRedirect', $this->prophesize(ServerRequest::class)->reveal());
+        $this->loginControllerMock->_call(
+            'checkRedirect',
+            $this->prophesize(ServerRequest::class)->reveal(),
+            $this->prophesize(PageRenderer::class)->reveal()
+        );
     }
 
     /**
@@ -230,11 +237,18 @@ class LoginControllerTest extends UnitTestCase
         $GLOBALS['BE_USER']->user['uid'] = 1;
         $this->loginControllerMock->method('isLoginInProgress')->willReturn(false);
         $this->loginControllerMock->_set('loginRefresh', true);
-        $this->loginControllerMock->_set('documentTemplate', $this->prophesize(DocumentTemplate::class)->reveal());
-
-        $this->loginControllerMock->_call('checkRedirect', $this->prophesize(ServerRequest::class)->reveal());
+        /** @var ObjectProphecy|PageRenderer $pageRendererProphecy */
+        $pageRendererProphecy = $this->prophesize(PageRenderer::class);
+        /** @var MethodProphecy $inlineCodeProphecy */
+        $inlineCodeProphecy = $pageRendererProphecy->addJsInlineCode('loginRefresh', Argument::cetera());
+
+        $this->loginControllerMock->_call(
+            'checkRedirect',
+            $this->prophesize(ServerRequest::class)->reveal(),
+            $pageRendererProphecy->reveal()
+        );
 
-        self::assertContains('window.opener.TYPO3.LoginRefresh.startTask();', $this->loginControllerMock->_get('documentTemplate')->JScode);
+        $inlineCodeProphecy->shouldHaveBeenCalledTimes(1);
     }
 
     /**
@@ -266,11 +280,18 @@ class LoginControllerTest extends UnitTestCase
         $GLOBALS['BE_USER']->user['uid'] = 1;
         $this->loginControllerMock->method('isLoginInProgress')->willReturn(true);
         $this->loginControllerMock->_set('loginRefresh', true);
-        $this->loginControllerMock->_set('documentTemplate', $this->prophesize(DocumentTemplate::class)->reveal());
-
-        $this->loginControllerMock->_call('checkRedirect', $this->prophesize(ServerRequest::class)->reveal());
+        /** @var ObjectProphecy|PageRenderer $pageRendererProphecy */
+        $pageRendererProphecy = $this->prophesize(PageRenderer::class);
+        /** @var MethodProphecy $inlineCodeProphecy */
+        $inlineCodeProphecy = $pageRendererProphecy->addJsInlineCode('loginRefresh', Argument::cetera());
+
+        $this->loginControllerMock->_call(
+            'checkRedirect',
+            $this->prophesize(ServerRequest::class)->reveal(),
+            $pageRendererProphecy->reveal()
+        );
 
-        self::assertContains('window.opener.TYPO3.LoginRefresh.startTask();', $this->loginControllerMock->_get('documentTemplate')->JScode);
+        $inlineCodeProphecy->shouldHaveBeenCalledTimes(1);
     }
 
     /**
@@ -290,7 +311,11 @@ class LoginControllerTest extends UnitTestCase
         $GLOBALS['BE_USER']->user['uid'] = null;
 
         $this->loginControllerMock->expects($this->never())->method('redirectToUrl');
-        $this->loginControllerMock->_call('checkRedirect', $this->prophesize(ServerRequest::class)->reveal());
+        $this->loginControllerMock->_call(
+            'checkRedirect',
+            $this->prophesize(ServerRequest::class)->reveal(),
+            $this->prophesize(PageRenderer::class)->reveal()
+        );
     }
 
     /**