[BUGFIX] Ensure manually updated slug is saved correctly
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / Backend / Typo3DatabaseBackendTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Cache\Backend;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Prophecy\Argument;
19 use TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend;
20 use TYPO3\CMS\Core\Cache\Exception;
21 use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
22 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
23 use TYPO3\CMS\Core\Database\Connection;
24 use TYPO3\CMS\Core\Database\ConnectionPool;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27
28 /**
29 * Test case
30 */
31 class Typo3DatabaseBackendTest extends UnitTestCase
32 {
33 protected $resetSingletonInstances = true;
34
35 /**
36 * @test
37 */
38 public function setCacheCalculatesCacheTableName()
39 {
40 $frontendProphecy = $this->prophesize(FrontendInterface::class);
41 $frontendProphecy->getIdentifier()->willReturn('test');
42
43 $subject = new Typo3DatabaseBackend('Testing');
44 $subject->setCache($frontendProphecy->reveal());
45
46 $this->assertEquals('cache_test', $subject->getCacheTable());
47 }
48
49 /**
50 * @test
51 */
52 public function setCacheCalculatesTagsTableName()
53 {
54 $frontendProphecy = $this->prophesize(FrontendInterface::class);
55 $frontendProphecy->getIdentifier()->willReturn('test');
56
57 $subject = new Typo3DatabaseBackend('Testing');
58 $subject->setCache($frontendProphecy->reveal());
59
60 $this->assertEquals('cache_test_tags', $subject->getTagsTable());
61 }
62
63 /**
64 * @test
65 */
66 public function setThrowsExceptionIfFrontendWasNotSet()
67 {
68 $subject = new Typo3DatabaseBackend('Testing');
69 $this->expectException(Exception::class);
70 $this->expectExceptionCode(1236518288);
71 $subject->set('identifier', 'data');
72 }
73
74 /**
75 * @test
76 */
77 public function setThrowsExceptionIfDataIsNotAString()
78 {
79 $frontendProphecy = $this->prophesize(FrontendInterface::class);
80 $frontendProphecy->getIdentifier()->willReturn('test');
81
82 $subject = new Typo3DatabaseBackend('Testing');
83 $subject->setCache($frontendProphecy->reveal());
84
85 $this->expectException(InvalidDataException::class);
86 $this->expectExceptionCode(1236518298);
87
88 $subject->set('identifier', ['iAmAnArray']);
89 }
90
91 /**
92 * @test
93 */
94 public function getThrowsExceptionIfFrontendWasNotSet()
95 {
96 $subject = new Typo3DatabaseBackend('Testing');
97 $this->expectException(Exception::class);
98 $this->expectExceptionCode(1236518288);
99 $subject->get('identifier');
100 }
101
102 /**
103 * @test
104 */
105 public function hasThrowsExceptionIfFrontendWasNotSet()
106 {
107 $subject = new Typo3DatabaseBackend('Testing');
108 $this->expectException(Exception::class);
109 $this->expectExceptionCode(1236518288);
110 $subject->has('identifier');
111 }
112
113 /**
114 * @test
115 */
116 public function removeThrowsExceptionIfFrontendWasNotSet()
117 {
118 $subject = new Typo3DatabaseBackend('Testing');
119 $this->expectException(Exception::class);
120 $this->expectExceptionCode(1236518288);
121 $subject->remove('identifier');
122 }
123
124 /**
125 * @test
126 */
127 public function collectGarbageThrowsExceptionIfFrontendWasNotSet()
128 {
129 $subject = new Typo3DatabaseBackend('Testing');
130 $this->expectException(Exception::class);
131 $this->expectExceptionCode(1236518288);
132 $subject->collectGarbage();
133 }
134
135 /**
136 * @test
137 */
138 public function findIdentifiersByTagThrowsExceptionIfFrontendWasNotSet()
139 {
140 $subject = new Typo3DatabaseBackend('Testing');
141 $this->expectException(Exception::class);
142 $this->expectExceptionCode(1236518288);
143 $subject->findIdentifiersByTag('identifier');
144 }
145
146 /**
147 * @test
148 */
149 public function flushThrowsExceptionIfFrontendWasNotSet()
150 {
151 $subject = new Typo3DatabaseBackend('Testing');
152 $this->expectException(Exception::class);
153 $this->expectExceptionCode(1236518288);
154 $subject->flush();
155 }
156
157 /**
158 * @test
159 */
160 public function flushRemovesAllCacheEntries()
161 {
162 $frontendProphecy = $this->prophesize(FrontendInterface::class);
163 $frontendProphecy->getIdentifier()->willReturn('test');
164
165 $subject = new Typo3DatabaseBackend('Testing');
166 $subject->setCache($frontendProphecy->reveal());
167
168 $connectionProphet = $this->prophesize(Connection::class);
169 $connectionProphet->truncate('cache_test')->shouldBeCalled()->willReturn(0);
170 $connectionProphet->truncate('cache_test_tags')->shouldBeCalled()->willReturn(0);
171
172 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
173 $connectionPoolProphet->getConnectionForTable(Argument::cetera())->willReturn($connectionProphet->reveal());
174
175 // Two instances are required as there are different tables being cleared
176 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
177 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
178
179 $subject->flush();
180 }
181
182 public function flushByTagCallsDeleteOnConnection()
183 {
184 $frontendProphecy = $this->prophesize(FrontendInterface::class);
185 $frontendProphecy->getIdentifier()->willReturn('test');
186
187 $subject = new Typo3DatabaseBackend('Testing');
188 $subject->setCache($frontendProphecy->reveal());
189
190 $connectionProphet = $this->prophesize(Connection::class);
191 $connectionProphet->delete('cache_test')->shouldBeCalled()->willReturn(0);
192 $connectionProphet->delete('cache_test_tags')->shouldBeCalled()->willReturn(0);
193
194 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
195 $connectionPoolProphet->getConnectionForTable(Argument::cetera())->willReturn($connectionProphet->reveal());
196
197 // Two instances are required as there are different tables being cleared
198 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
199 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
200
201 $subject->flushByTag('Tag');
202 }
203
204 public function flushByTagsCallsDeleteOnConnection()
205 {
206 $frontendProphecy = $this->prophesize(FrontendInterface::class);
207 $frontendProphecy->getIdentifier()->willReturn('test');
208
209 $subject = new Typo3DatabaseBackend('Testing');
210 $subject->setCache($frontendProphecy->reveal());
211
212 $connectionProphet = $this->prophesize(Connection::class);
213 $connectionProphet->delete('cache_test')->shouldBeCalled()->willReturn(0);
214 $connectionProphet->delete('cache_test_tags')->shouldBeCalled()->willReturn(0);
215
216 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
217 $connectionPoolProphet->getConnectionForTable(Argument::cetera())->willReturn($connectionProphet->reveal());
218
219 // Two instances are required as there are different tables being cleared
220 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
221 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
222
223 $subject->flushByTag(['Tag1', 'Tag2']);
224 }
225
226 /**
227 * @test
228 */
229 public function flushByTagThrowsExceptionIfFrontendWasNotSet()
230 {
231 $subject = new Typo3DatabaseBackend('Testing');
232 $this->expectException(Exception::class);
233 $this->expectExceptionCode(1236518288);
234 $subject->flushByTag('Tag');
235 }
236 /**
237 * @test
238 */
239 public function flushByTagsThrowsExceptionIfFrontendWasNotSet()
240 {
241 $subject = new Typo3DatabaseBackend('Testing');
242 $this->expectException(Exception::class);
243 $this->expectExceptionCode(1236518288);
244 $subject->flushByTags([]);
245 }
246 }