diff --git a/app/bundles/CoreBundle/Service/FlashBag.php b/app/bundles/CoreBundle/Service/FlashBag.php index dc0c9ad7f62..00c93e5c26c 100644 --- a/app/bundles/CoreBundle/Service/FlashBag.php +++ b/app/bundles/CoreBundle/Service/FlashBag.php @@ -12,7 +12,6 @@ namespace Mautic\CoreBundle\Service; use Mautic\CoreBundle\Model\NotificationModel; -use Mautic\CoreBundle\Translation\Translator; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\Translation\TranslatorInterface; @@ -22,13 +21,17 @@ */ class FlashBag { + const LEVEL_ERROR = 'error'; + const LEVEL_WARNING = 'warning'; + const LEVEL_NOTICE = 'notice'; + /** * @var Session */ private $session; /** - * @var Translator + * @var TranslatorInterface */ private $translator; diff --git a/app/bundles/CoreBundle/Test/Service/FlashBagTest.php b/app/bundles/CoreBundle/Test/Service/FlashBagTest.php new file mode 100644 index 00000000000..df82003543a --- /dev/null +++ b/app/bundles/CoreBundle/Test/Service/FlashBagTest.php @@ -0,0 +1,205 @@ +createMock(SymfonyFlashBag::class); + $symfonyFlashBag + ->expects($this->once()) + ->method('add') + ->with($type, $message); + $session = $this->createMock(Session::class); + $session + ->expects($this->once()) + ->method('getFlashBag') + ->willReturn($symfonyFlashBag); + $translator = $this->createMock(TranslatorInterface::class); + $requestStack = $this->createMock(RequestStack::class); + $notificationModel = $this->createMock(NotificationModel::class); + $flashBag = new FlashBag($session, $translator, $requestStack, $notificationModel); + + $flashBag->add($message, $messageVars, $type, $domain, $addNotification); + + $message = 'message'; + $messageVars['pluralCount'] = 2; + $translatedMessage = 'translatedMessage'; + $type = FlashBag::LEVEL_NOTICE; + $domain = 'flashes'; + $addNotification = false; + $symfonyFlashBag = $this->createMock(SymfonyFlashBag::class); + $symfonyFlashBag + ->expects($this->once()) + ->method('add') + ->with($type, $translatedMessage); + $session = $this->createMock(Session::class); + $session + ->expects($this->once()) + ->method('getFlashBag') + ->willReturn($symfonyFlashBag); + $translator = $this->createMock(TranslatorInterface::class); + $translator + ->expects($this->once()) + ->method('transChoice') + ->with($message, $messageVars['pluralCount'], $messageVars, $domain) + ->willReturn($translatedMessage); + $requestStack = $this->createMock(RequestStack::class); + $notificationModel = $this->createMock(NotificationModel::class); + $flashBag = new FlashBag($session, $translator, $requestStack, $notificationModel); + + $flashBag->add($message, $messageVars, $type, $domain, $addNotification); + + $message = 'message'; + $messageVars = []; + $translatedMessage = 'translatedMessage'; + $type = FlashBag::LEVEL_NOTICE; + $domain = 'flashes'; + $addNotification = false; + $symfonyFlashBag = $this->createMock(SymfonyFlashBag::class); + $symfonyFlashBag + ->expects($this->once()) + ->method('add') + ->with($type, $translatedMessage); + $session = $this->createMock(Session::class); + $session + ->expects($this->once()) + ->method('getFlashBag') + ->willReturn($symfonyFlashBag); + $translator = $this->createMock(TranslatorInterface::class); + $translator + ->expects($this->once()) + ->method('trans') + ->with($message, $messageVars, $domain) + ->willReturn($translatedMessage); + $requestStack = $this->createMock(RequestStack::class); + $notificationModel = $this->createMock(NotificationModel::class); + $flashBag = new FlashBag($session, $translator, $requestStack, $notificationModel); + + $flashBag->add($message, $messageVars, $type, $domain, $addNotification); + + $this->testReadStatus(1, true); + $this->testReadStatus(31, false); + + $this->testAddTypeCases(FlashBag::LEVEL_ERROR, 'text-danger fa-exclamation-circle'); + $this->testAddTypeCases(FlashBag::LEVEL_WARNING, 'text-warning fa-exclamation-triangle'); + $this->testAddTypeCases(FlashBag::LEVEL_NOTICE, 'fa-info-circle'); + $this->testAddTypeCases('default', 'fa-info-circle'); + } + + private function testReadStatus($mauticUserLastActive, $isRead) + { + $message = 'message'; + $messageVars = []; + $type = FlashBag::LEVEL_NOTICE; + $translatedMessage = 'translatedMessage'; + $domain = 'flashes'; + $addNotification = true; + $isRead = $mauticUserLastActive > 30 ? 0 : 1; + $symfonyFlashBag = $this->createMock(SymfonyFlashBag::class); + $symfonyFlashBag + ->expects($this->once()) + ->method('add') + ->with($type, $translatedMessage); + $session = $this->createMock(Session::class); + $session + ->expects($this->once()) + ->method('getFlashBag') + ->willReturn($symfonyFlashBag); + $translator = $this->createMock(TranslatorInterface::class); + $translator + ->expects($this->once()) + ->method('trans') + ->with($message, $messageVars, $domain) + ->willReturn($translatedMessage); + $request = $this->createMock(Request::class); + $request + ->expects($this->once()) + ->method('get') + ->with('mauticUserLastActive', 0) + ->willReturn($mauticUserLastActive); + $requestStack = $this->createMock(RequestStack::class); + $requestStack + ->expects($this->once()) + ->method('getCurrentRequest') + ->willReturn($request); + $notificationModel = $this->createMock(NotificationModel::class); + $notificationModel + ->expects($this->once()) + ->method('addNotification') + ->with($message, $type, $isRead, null, 'fa-info-circle'); + $flashBag = new FlashBag($session, $translator, $requestStack, $notificationModel); + + $flashBag->add($message, $messageVars, $type, $domain, $addNotification); + } + + private function testAddTypeCases($type, $expectedIcon) + { + $message = 'message'; + $messageVars = []; + $translatedMessage = 'translatedMessage'; + $domain = 'flashes'; + $addNotification = true; // <--- + $mauticUserLastActive = 1; // <--- + $symfonyFlashBag = $this->createMock(SymfonyFlashBag::class); + $symfonyFlashBag + ->expects($this->once()) + ->method('add') + ->with($type, $translatedMessage); + $session = $this->createMock(Session::class); + $session + ->expects($this->once()) + ->method('getFlashBag') + ->willReturn($symfonyFlashBag); + $translator = $this->createMock(TranslatorInterface::class); + $translator + ->expects($this->once()) + ->method('trans') + ->with($message, $messageVars, $domain) + ->willReturn($translatedMessage); + $request = $this->createMock(Request::class); + $request + ->expects($this->once()) + ->method('get') + ->with('mauticUserLastActive', 0) + ->willReturn($mauticUserLastActive); + $requestStack = $this->createMock(RequestStack::class); + $requestStack + ->expects($this->once()) + ->method('getCurrentRequest') + ->willReturn($request); + $notificationModel = $this->createMock(NotificationModel::class); + $notificationModel + ->expects($this->once()) + ->method('addNotification') + ->with($message, $type, 1, null, $expectedIcon); + $flashBag = new FlashBag($session, $translator, $requestStack, $notificationModel); + + $flashBag->add($message, $messageVars, $type, $domain, $addNotification); + } +}