TYPO3 CMS  TYPO3_7-6
FlashMessageQueueTest.php
Go to the documentation of this file.
1 <?php
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 
21 {
25  protected $flashMessageQueue;
26 
30  protected $frontendUser;
31 
32  protected function setUp()
33  {
34  $this->frontendUser = $this->getMock(\TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication::class, ['dummy']);
35  $this->flashMessageQueue = $this->getAccessibleMock(
36  \TYPO3\CMS\Core\Messaging\FlashMessageQueue::class,
37  ['getUserByContext'],
38  ['core.template.flashMessages']
39  );
40 
41  $this->flashMessageQueue->expects($this->any())->method('getUserByContext')->will($this->returnValue($this->frontendUser));
42  }
43 
47  public function userSessionInitiallyIsEmpty()
48  {
49  $this->assertSame([], $this->flashMessageQueue->_call('getFlashMessagesFromSession'));
50  }
51 
56  {
57  $this->flashMessageQueue->enqueue(new \TYPO3\CMS\Core\Messaging\FlashMessage('Foo', 'Bar', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false));
58 
59  $this->assertSame([], $this->flashMessageQueue->_call('getFlashMessagesFromSession'));
60  }
61 
66  {
67  $flashMessage = new \TYPO3\CMS\Core\Messaging\FlashMessage('Foo', 'Bar', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, true);
68  $this->flashMessageQueue->enqueue($flashMessage);
69 
70  $this->assertSame([$flashMessage], $this->flashMessageQueue->_call('getFlashMessagesFromSession'));
71  }
72 
77  {
78  $flashMessage1 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false);
79  $flashMessage2 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Session', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, true);
80  $this->flashMessageQueue->enqueue($flashMessage1);
81  $this->flashMessageQueue->enqueue($flashMessage2);
82 
83  $this->assertCount(2, $this->flashMessageQueue->getAllMessages());
84  }
85 
89  public function clearClearsTheQueue()
90  {
91  $flashMessage1 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false);
92  $flashMessage2 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false);
93  $this->flashMessageQueue->enqueue($flashMessage1);
94  $this->flashMessageQueue->enqueue($flashMessage2);
95  $this->flashMessageQueue->clear();
96 
97  $this->assertSame(0, $this->flashMessageQueue->count());
98  }
99 
104  {
105  $flashMessage1 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false);
106  $flashMessage2 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, true);
107  $this->flashMessageQueue->enqueue($flashMessage1);
108  $this->flashMessageQueue->enqueue($flashMessage2);
109 
110  $this->assertCount(1, $this->flashMessageQueue->toArray());
111  }
112 
117  {
118  $this->assertSame([], $this->flashMessageQueue->toArray());
119  }
120 
124  public function getAllMessagesAndFlushClearsSessionStack()
125  {
126  $flashMessage = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, true);
127  $this->flashMessageQueue->enqueue($flashMessage);
128  $this->flashMessageQueue->getAllMessagesAndFlush();
129 
131  $frontendUser = $this->flashMessageQueue->_call('getUserByContext');
132 
133  $this->assertNull($frontendUser->getSessionData('core.template.flashMessages'));
134  }
135 
140  {
141  $messages = [
142  0 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is a test message', 1, \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE),
143  1 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is another test message', 2, \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING)
144  ];
145  $this->flashMessageQueue->enqueue($messages[0]);
146  $this->flashMessageQueue->enqueue($messages[1]);
147 
148  $filteredFlashMessages = $this->flashMessageQueue->getAllMessages(\TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE);
149 
150  $this->assertEquals(count($filteredFlashMessages), 1);
151 
152  reset($filteredFlashMessages);
153  $flashMessage = current($filteredFlashMessages);
154  $this->assertEquals($messages[0], $flashMessage);
155  }
156 
161  {
162  $messages = [
163  0 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is a test message', 1, \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE),
164  1 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is another test message', 2, \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING)
165  ];
166  $this->flashMessageQueue->enqueue($messages[0]);
167  $this->flashMessageQueue->enqueue($messages[1]);
168 
169  $filteredFlashMessages = $this->flashMessageQueue->getAllMessagesAndFlush(\TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE);
170 
171  $this->assertEquals(count($filteredFlashMessages), 1);
172 
173  reset($filteredFlashMessages);
174  $flashMessage = current($filteredFlashMessages);
175  $this->assertEquals($messages[0], $flashMessage);
176 
177  $this->assertEquals([], $this->flashMessageQueue->getAllMessages(\TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE));
178  $this->assertEquals([$messages[1]], array_values($this->flashMessageQueue->getAllMessages()));
179  }
180 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)