[TASK] Add FlashMessage API to Extbase AbstractController 31/25731/6
authorNico de Haen <mail@ndh-websolutions.de>
Thu, 28 Nov 2013 16:56:49 +0000 (17:56 +0100)
committerSteffen Müller <typo3@t3node.com>
Thu, 16 Jan 2014 13:31:51 +0000 (14:31 +0100)
Flow comes with an API in AbstractController to add FlashMessages.
Introducing this API to Extbase provides a convenient way to
add new FlashMessages to the queue.
Usage in your controller: $this->addFlashMessage();

Resolves: #54036
Releases: 6.2
Change-Id: I3f6b69597d7f972054c5081d1f9e20d65b598848
Reviewed-on: https://review.typo3.org/25731
Reviewed-by: Benjamin Mack
Reviewed-by: Marcin S?gol
Tested-by: Marcin S?gol
Reviewed-by: Steffen Müller
Tested-by: Steffen Müller
typo3/sysext/extbase/Classes/Mvc/Controller/AbstractController.php
typo3/sysext/extbase/Classes/Mvc/Controller/FlashMessageContainer.php
typo3/sysext/extbase/Tests/Unit/Mvc/Controller/AbstractControllerTest.php

index 2d18b1b..f62f220 100644 (file)
@@ -192,6 +192,29 @@ abstract class AbstractController implements \TYPO3\CMS\Extbase\Mvc\Controller\C
        }
 
        /**
+        * Creates a Message object and adds it to the FlashMessageQueue.
+        *
+        * @param string $messageBody The message
+        * @param string $messageTitle Optional message title
+        * @param integer $severity Optional severity, must be one of \TYPO3\CMS\Core\Messaging\FlashMessage constants
+        * @param boolean $storeInSession Optional, defines whether the message should be stored in the session (default) or not
+        * @return void
+        * @throws \InvalidArgumentException if the message body is no string
+        * @see \TYPO3\CMS\Core\Messaging\FlashMessage
+        * @api
+        */
+       public function addFlashMessage($messageBody, $messageTitle = '', $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession = TRUE) {
+               if (!is_string($messageBody)) {
+                       throw new \InvalidArgumentException('The message body must be of type string, "' . gettype($messageBody) . '" given.', 1243258395);
+               }
+               /* @var \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage */
+               $flashMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
+                       'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $messageBody, $messageTitle, $severity, $storeInSession
+               );
+               $this->controllerContext->getFlashMessageQueue()->enqueue($flashMessage);
+       }
+
+       /**
         * Checks if the current request type is supported by the controller.
         *
         * If your controller only supports certain request types, either
index 92ffe1f..4984944 100644 (file)
@@ -73,7 +73,7 @@ class FlashMessageContainer implements \TYPO3\CMS\Core\SingletonInterface {
         * @param integer $severity optional severity code. One of the \TYPO3\CMS\Core\Messaging\FlashMessage constants
         * @throws \InvalidArgumentException
         * @return void
-        * @deprecated since 6.1, will be removed 2 versions later
+        * @deprecated since 6.1, will be removed 2 versions later use Mvc\Controller\AbstractController->addFlashMessage instead
         */
        public function add($message, $title = '', $severity = \TYPO3\CMS\Core\Messaging\FlashMessage::OK) {
                \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
index e5feece..00eb306 100644 (file)
@@ -5,6 +5,7 @@ namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller;
  *  Copyright notice
  *
  *  (c) 2012 Stefan Neufeind <info@speedpartner.de>
+ *  (c) 2013 Steffen Müller <typo3@t3node.com>
  *  All rights reserved
  *
  *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -52,4 +53,105 @@ class AbstractControllerTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase
                $actualResult = $mockController->getExtensionName();
                $this->assertEquals($expectedResult, $actualResult);
        }
+
+       /**
+        * @return array
+        */
+       public function addFlashMessageDataProvider() {
+               return array(
+                       array(
+                               new \TYPO3\CMS\Core\Messaging\FlashMessage('Simple Message'),
+                               'Simple Message',
+                               '',
+                               \TYPO3\CMS\Core\Messaging\FlashMessage::OK,
+                               FALSE
+                       ),
+                       array(
+                               new \TYPO3\CMS\Core\Messaging\FlashMessage('Some OK', 'Message Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, TRUE),
+                               'Some OK',
+                               'Message Title',
+                               \TYPO3\CMS\Core\Messaging\FlashMessage::OK,
+                               TRUE
+                       ),
+                       array(
+                               new \TYPO3\CMS\Core\Messaging\FlashMessage('Some Info', 'Message Title', \TYPO3\CMS\Core\Messaging\FlashMessage::INFO, TRUE),
+                               'Some Info',
+                               'Message Title',
+                               \TYPO3\CMS\Core\Messaging\FlashMessage::INFO,
+                               TRUE
+                       ),
+                       array(
+                               new \TYPO3\CMS\Core\Messaging\FlashMessage('Some Notice', 'Message Title', \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE, TRUE),
+                               'Some Notice',
+                               'Message Title',
+                               \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE,
+                               TRUE
+                       ),
+
+                       array(
+                               new \TYPO3\CMS\Core\Messaging\FlashMessage('Some Warning', 'Message Title', \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING, TRUE),
+                               'Some Warning',
+                               'Message Title',
+                               \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING,
+                               TRUE
+                       ),
+                       array(
+                               new \TYPO3\CMS\Core\Messaging\FlashMessage('Some Error', 'Message Title', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, TRUE),
+                               'Some Error',
+                               'Message Title',
+                               \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
+                               TRUE
+                       )
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider addFlashMessageDataProvider
+        */
+       public function addFlashMessageAddsFlashMessageObjectToFlashMessageQueue($expectedMessage, $messageBody, $messageTitle = '', $severity = \TYPO3\CMS\Core\Messaging\FlashMessage::OK, $storeInSession = TRUE) {
+               $flashMessageQueue = $this->getMock(
+                       'TYPO3\\CMS\\Core\\Messaging\\FlashMessageQueue',
+                       array('enqueue'),
+                       array(),
+                       '',
+                       FALSE
+               );
+               $flashMessageQueue->expects($this->once())->method('enqueue')->with($this->equalTo($expectedMessage));
+
+               $controllerContext = $this->getMock(
+                       '\\TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerContext',
+                       array('getFlashMessageQueue')
+               );
+               $controllerContext->expects($this->once())->method('getFlashMessageQueue')->will($this->returnValue($flashMessageQueue));
+
+               $controller = $this->getMockForAbstractClass('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\AbstractController',
+                       array(),
+                       '',
+                       FALSE,
+                       TRUE,
+                       TRUE,
+                       array('dummy')
+               );
+               $this->inject($controller, 'controllerContext', $controllerContext);
+
+               $controller->addFlashMessage($messageBody, $messageTitle, $severity, $storeInSession);
+       }
+
+       /**
+        * @test
+        * @expectedException \InvalidArgumentException
+        */
+       public function addFlashMessageThrowsExceptionOnInvalidMessageBody() {
+               $controller = $this->getMockForAbstractClass('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\AbstractController',
+                       array(),
+                       '',
+                       FALSE,
+                       TRUE,
+                       TRUE,
+                       array('dummy')
+               );
+
+               $controller->addFlashMessage(new \stdClass());
+       }
 }