[TASK] Move render arguments to initializeArguments() in BytesVH
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Messaging / FlashMessageQueueTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\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 /**
18 * Test case
19 */
20 class FlashMessageQueueTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
21 {
22 /**
23 * @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
24 */
25 protected $flashMessageQueue;
26
27 /**
28 * @var \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject
29 */
30 protected $frontendUser;
31
32 protected function setUp()
33 {
34 $this->frontendUser = $this->getMockBuilder(\TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication::class)
35 ->setMethods(array('dummy'))
36 ->getMock();
37 $this->flashMessageQueue = $this->getAccessibleMock(
38 \TYPO3\CMS\Core\Messaging\FlashMessageQueue::class,
39 array('getUserByContext'),
40 array('core.template.flashMessages')
41 );
42
43 $this->flashMessageQueue->expects($this->any())->method('getUserByContext')->will($this->returnValue($this->frontendUser));
44 }
45
46 /**
47 * @test
48 */
49 public function userSessionInitiallyIsEmpty()
50 {
51 $this->assertSame(array(), $this->flashMessageQueue->_call('getFlashMessagesFromSession'));
52 }
53
54 /**
55 * @test
56 */
57 public function enqueueTransientFlashMessageKeepsSessionEmpty()
58 {
59 $this->flashMessageQueue->enqueue(new \TYPO3\CMS\Core\Messaging\FlashMessage('Foo', 'Bar', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false));
60
61 $this->assertSame(array(), $this->flashMessageQueue->_call('getFlashMessagesFromSession'));
62 }
63
64 /**
65 * @test
66 */
67 public function enqueueSessionFlashMessageWritesSessionEntry()
68 {
69 $flashMessage = new \TYPO3\CMS\Core\Messaging\FlashMessage('Foo', 'Bar', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, true);
70 $this->flashMessageQueue->enqueue($flashMessage);
71
72 $this->assertSame(array($flashMessage), $this->flashMessageQueue->_call('getFlashMessagesFromSession'));
73 }
74
75 /**
76 * @test
77 */
78 public function getAllMessagesReturnsSessionFlashMessageAndTransientFlashMessage()
79 {
80 $flashMessage1 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false);
81 $flashMessage2 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Session', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, true);
82 $this->flashMessageQueue->enqueue($flashMessage1);
83 $this->flashMessageQueue->enqueue($flashMessage2);
84
85 $this->assertCount(2, $this->flashMessageQueue->getAllMessages());
86 }
87
88 /**
89 * @test
90 */
91 public function clearClearsTheQueue()
92 {
93 $flashMessage1 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false);
94 $flashMessage2 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false);
95 $this->flashMessageQueue->enqueue($flashMessage1);
96 $this->flashMessageQueue->enqueue($flashMessage2);
97 $this->flashMessageQueue->clear();
98
99 $this->assertSame(0, $this->flashMessageQueue->count());
100 }
101
102 /**
103 * @test
104 */
105 public function toArrayOnlyRespectsTransientFlashMessages()
106 {
107 $flashMessage1 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false);
108 $flashMessage2 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, true);
109 $this->flashMessageQueue->enqueue($flashMessage1);
110 $this->flashMessageQueue->enqueue($flashMessage2);
111
112 $this->assertCount(1, $this->flashMessageQueue->toArray());
113 }
114
115 /**
116 * @test
117 */
118 public function toArrayReturnsEmptyArrayWithForEmptyQueue()
119 {
120 $this->assertSame(array(), $this->flashMessageQueue->toArray());
121 }
122
123 /**
124 * @test
125 */
126 public function getAllMessagesAndFlushClearsSessionStack()
127 {
128 $flashMessage = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, true);
129 $this->flashMessageQueue->enqueue($flashMessage);
130 $this->flashMessageQueue->getAllMessagesAndFlush();
131
132 /** @var $frontendUser \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication */
133 $frontendUser = $this->flashMessageQueue->_call('getUserByContext');
134
135 $this->assertNull($frontendUser->getSessionData('core.template.flashMessages'));
136 }
137
138 /**
139 * @test
140 */
141 public function messagesCanBeFilteredBySeverity()
142 {
143 $messages = array(
144 0 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is a test message', 1, \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE),
145 1 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is another test message', 2, \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING)
146 );
147 $this->flashMessageQueue->enqueue($messages[0]);
148 $this->flashMessageQueue->enqueue($messages[1]);
149
150 $filteredFlashMessages = $this->flashMessageQueue->getAllMessages(\TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE);
151
152 $this->assertEquals(count($filteredFlashMessages), 1);
153
154 reset($filteredFlashMessages);
155 $flashMessage = current($filteredFlashMessages);
156 $this->assertEquals($messages[0], $flashMessage);
157 }
158
159 /**
160 * @test
161 */
162 public function getMessagesAndFlushCanAlsoFilterBySeverity()
163 {
164 $messages = array(
165 0 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is a test message', 1, \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE),
166 1 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is another test message', 2, \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING)
167 );
168 $this->flashMessageQueue->enqueue($messages[0]);
169 $this->flashMessageQueue->enqueue($messages[1]);
170
171 $filteredFlashMessages = $this->flashMessageQueue->getAllMessagesAndFlush(\TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE);
172
173 $this->assertEquals(count($filteredFlashMessages), 1);
174
175 reset($filteredFlashMessages);
176 $flashMessage = current($filteredFlashMessages);
177 $this->assertEquals($messages[0], $flashMessage);
178
179 $this->assertEquals(array(), $this->flashMessageQueue->getAllMessages(\TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE));
180 $this->assertEquals(array($messages[1]), array_values($this->flashMessageQueue->getAllMessages()));
181 }
182 }