[!!!][FEATURE] Refactoring of FlashMessage rendering
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / Messaging / FlashMessageQueueTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\Messaging;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Messaging\FlashMessage;
18 use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
19
20 /**
21 * Test case
22 */
23 class FlashMessageQueueTest extends \TYPO3\CMS\Components\TestingFramework\Core\FunctionalTestCase
24 {
25 /**
26 * @test
27 */
28 public function getAllMessagesContainsEnqueuedMessage()
29 {
30 $this->setUpBackendUserFromFixture(1);
31 $flashMessage = new FlashMessage('Foo', 'Bar', FlashMessage::OK, true);
32 $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
33 $flashMessageQueue->addMessage($flashMessage);
34 $this->assertEquals([$flashMessage], $flashMessageQueue->getAllMessages());
35 }
36
37 /**
38 * @test
39 */
40 public function messagesCanBeFilteredBySeverity()
41 {
42 $this->setUpBackendUserFromFixture(1);
43 $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
44 $messages = [
45 0 => new FlashMessage('This is a test message', '1', FlashMessage::NOTICE),
46 1 => new FlashMessage('This is another test message', '2', FlashMessage::WARNING)
47 ];
48 $flashMessageQueue->enqueue($messages[0]);
49 $flashMessageQueue->enqueue($messages[1]);
50
51 $filteredFlashMessages = $flashMessageQueue->getAllMessages(FlashMessage::NOTICE);
52
53 $this->assertEquals(count($filteredFlashMessages), 1);
54
55 reset($filteredFlashMessages);
56 $flashMessage = current($filteredFlashMessages);
57 $this->assertEquals($messages[0], $flashMessage);
58 }
59
60 /**
61 * @test
62 */
63 public function getAllMessagesAndFlushContainsEnqueuedMessage()
64 {
65 $this->setUpBackendUserFromFixture(1);
66 $flashMessage = new FlashMessage('Foo', 'Bar', FlashMessage::OK, true);
67 $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
68 $flashMessageQueue->addMessage($flashMessage);
69 $this->assertEquals([$flashMessage], $flashMessageQueue->getAllMessagesAndFlush());
70 }
71
72 /**
73 * @test
74 */
75 public function getAllMessagesAndFlushClearsSessionStack()
76 {
77 $this->setUpBackendUserFromFixture(1);
78 $flashMessage = new FlashMessage('Foo', 'Bar', FlashMessage::OK, true);
79 $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
80 $flashMessageQueue->addMessage($flashMessage);
81 $flashMessageQueue->getAllMessagesAndFlush();
82 $this->assertEquals([], $flashMessageQueue->getAllMessagesAndFlush());
83 }
84
85 /**
86 * @test
87 */
88 public function getMessagesAndFlushCanFilterBySeverity()
89 {
90 $this->setUpBackendUserFromFixture(1);
91 $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
92 $messages = [
93 0 => new FlashMessage('This is a test message', '1', FlashMessage::NOTICE),
94 1 => new FlashMessage('This is another test message', '2', FlashMessage::WARNING)
95 ];
96 $flashMessageQueue->addMessage($messages[0]);
97 $flashMessageQueue->addMessage($messages[1]);
98
99 $filteredFlashMessages = $flashMessageQueue->getAllMessagesAndFlush(FlashMessage::NOTICE);
100
101 $this->assertEquals(count($filteredFlashMessages), 1);
102
103 reset($filteredFlashMessages);
104 $flashMessage = current($filteredFlashMessages);
105 $this->assertEquals($messages[0], $flashMessage);
106
107 $this->assertEquals([], $flashMessageQueue->getAllMessages(FlashMessage::NOTICE));
108 $this->assertEquals([$messages[1]], array_values($flashMessageQueue->getAllMessages()));
109 }
110
111 /**
112 * @test
113 */
114 public function getAllMessagesReturnsSessionFlashMessageAndTransientFlashMessage()
115 {
116 $this->setUpBackendUserFromFixture(1);
117 $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
118 $flashMessage1 = new FlashMessage('Transient', 'Title', FlashMessage::OK, false);
119 $flashMessage2 = new FlashMessage('Session', 'Title', FlashMessage::OK, true);
120 $flashMessageQueue->addMessage($flashMessage1);
121 $flashMessageQueue->addMessage($flashMessage2);
122
123 $this->assertCount(2, $flashMessageQueue->getAllMessages());
124 }
125
126 /**
127 * @test
128 */
129 public function clearClearsTheQueue()
130 {
131 $this->setUpBackendUserFromFixture(1);
132 $flashMessage = new FlashMessage('Foo', 'Bar', FlashMessage::OK, true);
133 $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
134 $flashMessageQueue->addMessage($flashMessage);
135 $flashMessageQueue->clear();
136 $this->assertCount(0, $flashMessageQueue);
137 }
138
139 /**
140 * @test
141 */
142 public function toArrayOnlyRespectsTransientFlashMessages()
143 {
144 $this->setUpBackendUserFromFixture(1);
145 $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
146 $flashMessage1 = new FlashMessage('Transient', 'Title', FlashMessage::OK, false);
147 $flashMessage2 = new FlashMessage('Session', 'Title', FlashMessage::OK, true);
148 $flashMessageQueue->addMessage($flashMessage1);
149 $flashMessageQueue->addMessage($flashMessage2);
150
151 $this->assertCount(1, $flashMessageQueue);
152 }
153 }