[BUGFIX] User settings: Removed call to deprecated method render() 11/46811/3
authorMichael Oehlhof <typo3@oehlhof.de>
Sat, 20 Feb 2016 22:42:41 +0000 (23:42 +0100)
committerFrank Naegler <frank.naegler@typo3.org>
Mon, 22 Feb 2016 15:27:03 +0000 (16:27 +0100)
Call of FlashMessage->render() was replaced with appropriate code when
* setup was saved
* temporary data was cleared
* Notice for activated changes
* password is updated
* selected language is not available

Resolves: #73581
Releases: master, 7.6
Change-Id: I68432729334eb905e3aae112d42ae45177a70809
Reviewed-on: https://review.typo3.org/46811
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
typo3/sysext/setup/Classes/Controller/SetupModuleController.php
typo3/sysext/setup/Tests/Unit/SetupModuleControllerTest.php [new file with mode: 0644]

index 76947cf..8d55910 100644 (file)
@@ -26,6 +26,7 @@ use TYPO3\CMS\Core\DataHandling\DataHandler;
 use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Messaging\FlashMessage;
+use TYPO3\CMS\Core\Messaging\FlashMessageService;
 use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
 use TYPO3\CMS\Core\Resource\ResourceFactory;
 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
@@ -384,42 +385,7 @@ class SetupModuleController extends AbstractModule
         $this->loadModules->observeWorkspaces = true;
         $this->loadModules->load($GLOBALS['TBE_MODULES']);
         $this->content .= $this->doc->header($this->getLanguageService()->getLL('UserSettings'));
-        // Show if setup was saved
-        if ($this->setupIsUpdated && !$this->settingsAreResetToDefault) {
-            $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('setupWasUpdated'), $this->getLanguageService()->getLL('UserSettings'));
-            $this->content .= $flashMessage->render();
-        }
-
-        // Show if temporary data was cleared
-        if ($this->settingsAreResetToDefault) {
-            $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('settingsAreReset'), $this->getLanguageService()->getLL('resetConfiguration'));
-            $this->content .= $flashMessage->render();
-        }
-
-        // Notice
-        if ($this->setupIsUpdated || $this->settingsAreResetToDefault) {
-            $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('activateChanges'), '', FlashMessage::INFO);
-            $this->content .= $flashMessage->render();
-        }
-
-        // If password is updated, output whether it failed or was OK.
-        if ($this->passwordIsSubmitted) {
-            $flashMessage = null;
-            switch ($this->passwordIsUpdated) {
-                case self::PASSWORD_OLD_WRONG:
-                    $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('oldPassword_failed'), $this->getLanguageService()->getLL('newPassword'), FlashMessage::ERROR);
-                    break;
-                case self::PASSWORD_NOT_THE_SAME:
-                    $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('newPassword_failed'), $this->getLanguageService()->getLL('newPassword'), FlashMessage::ERROR);
-                    break;
-                case self::PASSWORD_UPDATED:
-                    $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('newPassword_ok'), $this->getLanguageService()->getLL('newPassword'));
-                    break;
-            }
-            if ($flashMessage) {
-                $this->content .= $flashMessage->render();
-            }
-        }
+        $this->addFlashMessages();
 
         // Render user switch
         $this->content .= $this->renderSimulateUserSelectAndLabel();
@@ -731,9 +697,9 @@ class SetupModuleController extends AbstractModule
             <select id="field_lang" name="data[lang]" class="form-control">' . implode('', $languageOptions) . '
             </select>';
         if ($this->getBackendUser()->uc['lang'] && !@is_dir((PATH_typo3conf . 'l10n/' . $this->getBackendUser()->uc['lang']))) {
-            $languageUnavailableWarning = 'The selected language "' . $this->getLanguageService()->getLL(('lang_' . $this->getBackendUser()->uc['lang']), true) . '" is not available before the language files are installed.<br />' . ($this->getBackendUser()->isAdmin() ? 'You can use the Language module to easily download new language files.' : 'Please ask your system administrator to do this.');
-            $languageUnavailableMessage = GeneralUtility::makeInstance(FlashMessage::class, $languageUnavailableWarning, '', FlashMessage::WARNING);
-            $languageCode = $languageUnavailableMessage->render() . $languageCode;
+            // TODO: The text constants have to be moved into language files
+            $languageUnavailableWarning = 'The selected language "' . $this->getLanguageService()->getLL(('lang_' . $this->getBackendUser()->uc['lang']), true) . '" is not available before the language files are installed.&nbsp;&nbsp;<br />&nbsp;&nbsp;' . ($this->getBackendUser()->isAdmin() ? 'You can use the Language module to easily download new language files.' : 'Please ask your system administrator to do this.');
+            $languageCode = '<br /><span class="label label-danger">' . $languageUnavailableWarning . '</span><br /><br />' . $languageCode;
         }
         return $languageCode;
     }
@@ -756,7 +722,7 @@ class SetupModuleController extends AbstractModule
                     $modName = $subData['name'];
                     $modules .= '<option value="' . htmlspecialchars($modName) . '"';
                     $modules .= $this->getBackendUser()->uc['startModule'] === $modName ? ' selected="selected"' : '';
-                    $modules .=  '>' . $this->getLanguageService()->moduleLabels['tabs'][$modName . '_tab'] . '</option>';
+                    $modules .= '>' . $this->getLanguageService()->moduleLabels['tabs'][$modName . '_tab'] . '</option>';
                 }
                 $groupLabel = $this->getLanguageService()->moduleLabels['tabs'][$mainMod . '_tab'];
                 $startModuleSelect .= '<optgroup label="' . htmlspecialchars($groupLabel) . '">' . $modules . '</optgroup>';
@@ -1041,4 +1007,79 @@ class SetupModuleController extends AbstractModule
     {
         return $GLOBALS['TYPO3_DB'];
     }
+
+    /**
+     * Add FlashMessages for various actions
+     *
+     * @return void
+     */
+    protected function addFlashMessages()
+    {
+        $flashMessages = [];
+
+        // Show if setup was saved
+        if ($this->setupIsUpdated && !$this->settingsAreResetToDefault) {
+            $flashMessages[] = $this->getFlashMessage('setupWasUpdated', 'UserSettings');
+        }
+
+        // Show if temporary data was cleared
+        if ($this->settingsAreResetToDefault) {
+            $flashMessages[] = $this->getFlashMessage('settingsAreReset', 'resetConfiguration');
+        }
+
+        // Notice
+        if ($this->setupIsUpdated || $this->settingsAreResetToDefault) {
+            $flashMessages[] = $this->getFlashMessage('activateChanges', '', FlashMessage::INFO);
+        }
+
+        // If password is updated, output whether it failed or was OK.
+        if ($this->passwordIsSubmitted) {
+            $flashMessage = null;
+            switch ($this->passwordIsUpdated) {
+                case self::PASSWORD_OLD_WRONG:
+                    $flashMessages[] = $this->getFlashMessage('oldPassword_failed', 'newPassword', FlashMessage::ERROR);
+                    break;
+                case self::PASSWORD_NOT_THE_SAME:
+                    $flashMessages[] = $this->getFlashMessage('newPassword_failed', 'newPassword', FlashMessage::ERROR);
+                    break;
+                case self::PASSWORD_UPDATED:
+                    $flashMessages[] = $this->getFlashMessage('newPassword_ok', 'newPassword');
+                    break;
+            }
+        }
+        if (!empty($flashMessages)) {
+            $this->enqueueFlashMessages($flashMessages);
+        }
+    }
+
+    /**
+     * @param array $flashMessages
+     * @throws \TYPO3\CMS\Core\Exception
+     * @return void
+     */
+    protected function enqueueFlashMessages(array $flashMessages)
+    {
+        $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
+        $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
+        foreach ($flashMessages as $flashMessage) {
+            $defaultFlashMessageQueue->enqueue($flashMessage);
+        }
+    }
+
+    /**
+     * @param string $message
+     * @param string $title
+     * @param int $severity
+     * @return FlashMessage
+     */
+    protected function getFlashMessage($message, $title, $severity = FlashMessage::OK)
+    {
+        $title = !empty($title) ? $this->getLanguageService()->getLL($title) : ' ';
+        return GeneralUtility::makeInstance(
+            FlashMessage::class,
+            $this->getLanguageService()->getLL($message),
+            $title,
+            $severity
+        );
+    }
 }
diff --git a/typo3/sysext/setup/Tests/Unit/SetupModuleControllerTest.php b/typo3/sysext/setup/Tests/Unit/SetupModuleControllerTest.php
new file mode 100644 (file)
index 0000000..0d6090b
--- /dev/null
@@ -0,0 +1,190 @@
+<?php
+namespace TYPO3\CMS\Setup\Tests\Unit\Controller;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Messaging\FlashMessage;
+use TYPO3\CMS\Setup\Controller\SetupModuleController;
+
+/**
+ * Class SetupModuleControllerTest
+ */
+class SetupModuleControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
+{
+
+    /**
+     * @test
+     * @return void
+     */
+    public function addFlashMessagesAddsMessagesIfSetupIsUpdated()
+    {
+        $setupModuleControllerMock = $this->getAccessibleMock(
+            SetupModuleController::class,
+            ['getFlashMessage', 'enqueueFlashMessages'],
+            [],
+            '',
+            false
+        );
+        $setupModuleControllerMock->_set('setupIsUpdated', true);
+
+        $setupModuleControllerMock
+            ->expects($this->atMost(2))
+            ->method('getFlashMessage')
+            ->withConsecutive(
+                ['setupWasUpdated', 'UserSettings'],
+                ['activateChanges', '']
+            );
+
+        $setupModuleControllerMock->_call('addFlashMessages');
+    }
+
+    /**
+     * @test
+     * @return void
+     */
+    public function addFlashMessagesAddsMessageIfSettingsAreResetToDefault()
+    {
+        $setupModuleControllerMock = $this->getAccessibleMock(
+            SetupModuleController::class,
+            ['getFlashMessage', 'enqueueFlashMessages'],
+            [],
+            '',
+            false
+        );
+        $setupModuleControllerMock->_set('settingsAreResetToDefault', true);
+
+        $setupModuleControllerMock
+            ->expects($this->atMost(2))
+            ->method('getFlashMessage')
+            ->withConsecutive(
+                ['settingsAreReset', 'resetConfiguration'],
+                ['activateChanges', '']
+            );
+
+        $setupModuleControllerMock->_call('addFlashMessages');
+    }
+
+    /**
+     * @test
+     * @return void
+     */
+    public function addFlashMessagesAddsMessageIfPasswordWasSuccessfullyUpdated()
+    {
+        $setupModuleControllerMock = $this->getAccessibleMock(
+            SetupModuleController::class,
+            ['getFlashMessage', 'enqueueFlashMessages'],
+            [],
+            '',
+            false
+        );
+        $setupModuleControllerMock->_set('passwordIsSubmitted', true);
+        $setupModuleControllerMock->_set('passwordIsUpdated', SetupModuleController::PASSWORD_UPDATED);
+
+        $setupModuleControllerMock
+            ->expects($this->once())
+            ->method('getFlashMessage')
+            ->withConsecutive(
+                ['newPassword_ok', 'newPassword']
+            );
+
+        $setupModuleControllerMock->_call('addFlashMessages');
+    }
+
+    /**
+     * @test
+     * @return void
+     */
+    public function addFlashMessagesAddsMessageIfOldPasswordWasWrong()
+    {
+        $setupModuleControllerMock = $this->getAccessibleMock(
+            SetupModuleController::class,
+            ['getFlashMessage', 'enqueueFlashMessages'],
+            [],
+            '',
+            false
+        );
+        $setupModuleControllerMock->_set('passwordIsSubmitted', true);
+        $setupModuleControllerMock->_set('passwordIsUpdated', SetupModuleController::PASSWORD_OLD_WRONG);
+
+        $setupModuleControllerMock
+            ->expects($this->once())
+            ->method('getFlashMessage')
+            ->withConsecutive(
+                ['oldPassword_failed', 'newPassword']
+            );
+
+        $setupModuleControllerMock->_call('addFlashMessages');
+    }
+
+    /**
+     * @test
+     * @return void
+     */
+    public function addFlashMessagesAddsMessageIfPasswordsNotTheSame()
+    {
+        $setupModuleControllerMock = $this->getAccessibleMock(
+            SetupModuleController::class,
+            ['getFlashMessage', 'enqueueFlashMessages'],
+            [],
+            '',
+            false
+        );
+        $setupModuleControllerMock->_set('passwordIsSubmitted', true);
+        $setupModuleControllerMock->_set('passwordIsUpdated', SetupModuleController::PASSWORD_NOT_THE_SAME);
+
+        $setupModuleControllerMock
+            ->expects($this->once())
+            ->method('getFlashMessage')
+            ->withConsecutive(
+                ['newPassword_failed', 'newPassword']
+            );
+
+        $setupModuleControllerMock->_call('addFlashMessages');
+    }
+
+    /**
+     * @test
+     * @return void
+     */
+    public function addFlashMessagesAddsMessagesToQueue()
+    {
+        $setupModuleControllerMock = $this->getAccessibleMock(
+            SetupModuleController::class,
+            ['getFlashMessage', 'enqueueFlashMessages'],
+            [],
+            '',
+            false
+        );
+
+        $setupModuleControllerMock->_set('settingsAreResetToDefault', true);
+        $flashMessage1 = new FlashMessage('foo', 'bar');
+        $flashMessage2 = new FlashMessage('bar', 'foo');
+        $setupModuleControllerMock
+            ->expects($this->atMost(2))
+            ->method('getFlashMessage')
+            ->withConsecutive(
+                ['settingsAreReset', 'resetConfiguration'],
+                ['activateChanges', '']
+            )->willReturnOnConsecutiveCalls(
+                $flashMessage1,
+                $flashMessage2
+            );
+
+        $setupModuleControllerMock
+            ->expects($this->once())
+            ->method('enqueueFlashMessages')
+            ->with([$flashMessage1, $flashMessage2]);
+        $setupModuleControllerMock->_call('addFlashMessages');
+    }
+}