[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / Frontend / AbstractFrontendTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Cache\Frontend;
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 * Testcase for the abstract cache frontend
19 *
20 * This file is a backport from FLOW3
21 */
22 class AbstractFrontendTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
23 {
24 /**
25 * @test
26 */
27 public function theConstructorAcceptsValidIdentifiers()
28 {
29 $mockBackend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
30 ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
31 ->disableOriginalConstructor()
32 ->getMock();
33 foreach (['x', 'someValue', '123fivesixseveneight', 'some&', 'ab_cd%', rawurlencode('resource://some/äöü$&% sadf'), str_repeat('x', 250)] as $identifier) {
34 $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
35 ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag', 'flush', 'flushByTag', 'collectGarbage'])
36 ->setConstructorArgs([$identifier, $mockBackend])
37 ->getMock();
38 }
39 }
40
41 /**
42 * @test
43 */
44 public function theConstructorRejectsInvalidIdentifiers()
45 {
46 $mockBackend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
47 ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
48 ->disableOriginalConstructor()
49 ->getMock();
50 foreach (['', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#'] as $identifier) {
51 try {
52 $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
53 ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag', 'flush', 'flushByTag', 'collectGarbage'])
54 ->setConstructorArgs([$identifier, $mockBackend])
55 ->getMock();
56 $this->fail('Identifier "' . $identifier . '" was not rejected.');
57 } catch (\InvalidArgumentException $exception) {
58 }
59 }
60 }
61
62 /**
63 * @test
64 */
65 public function flushCallsBackend()
66 {
67 $identifier = 'someCacheIdentifier';
68 $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
69 ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
70 ->disableOriginalConstructor()
71 ->getMock();
72 $backend->expects($this->once())->method('flush');
73 $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
74 ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
75 ->setConstructorArgs([$identifier, $backend])
76 ->getMock();
77 $cache->flush();
78 }
79
80 /**
81 * @test
82 */
83 public function flushByTagRejectsInvalidTags()
84 {
85 $this->expectException(\InvalidArgumentException::class);
86 $this->expectExceptionCode(1233057359);
87
88 $identifier = 'someCacheIdentifier';
89 $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface::class);
90 $backend->expects($this->never())->method('flushByTag');
91 $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
92 ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
93 ->setConstructorArgs([$identifier, $backend])
94 ->getMock();
95 $cache->flushByTag('SomeInvalid\\Tag');
96 }
97
98 /**
99 * @test
100 */
101 public function flushByTagCallsBackendIfItIsATaggableBackend()
102 {
103 $tag = 'sometag';
104 $identifier = 'someCacheIdentifier';
105 $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface::class)
106 ->setMethods(['setCache', 'get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'])
107 ->disableOriginalConstructor()
108 ->getMock();
109 $backend->expects($this->once())->method('flushByTag')->with($tag);
110 $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
111 ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
112 ->setConstructorArgs([$identifier, $backend])
113 ->getMock();
114 $cache->flushByTag($tag);
115 }
116
117 /**
118 * @test
119 */
120 public function flushByTagsCallsBackendIfItIsATaggableBackend()
121 {
122 $tag = 'sometag';
123 $identifier = 'someCacheIdentifier';
124 $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface::class)
125 ->setMethods(['setCache', 'get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'])
126 ->disableOriginalConstructor()
127 ->getMock();
128 $backend->expects($this->once())->method('flushByTags')->with([$tag]);
129 $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
130 ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
131 ->setConstructorArgs([$identifier, $backend])
132 ->getMock();
133 $cache->flushByTags([$tag]);
134 }
135
136 /**
137 * @test
138 */
139 public function collectGarbageCallsBackend()
140 {
141 $identifier = 'someCacheIdentifier';
142 $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
143 ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
144 ->disableOriginalConstructor()
145 ->getMock();
146 $backend->expects($this->once())->method('collectGarbage');
147 $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
148 ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
149 ->setConstructorArgs([$identifier, $backend])
150 ->getMock();
151 $cache->collectGarbage();
152 }
153
154 /**
155 * @test
156 */
157 public function invalidEntryIdentifiersAreRecognizedAsInvalid()
158 {
159 $identifier = 'someCacheIdentifier';
160 $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class);
161 $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
162 ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
163 ->setConstructorArgs([$identifier, $backend])
164 ->getMock();
165 foreach (['', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#'] as $entryIdentifier) {
166 $this->assertFalse($cache->isValidEntryIdentifier($entryIdentifier), 'Invalid identifier "' . $entryIdentifier . '" was not rejected.');
167 }
168 }
169
170 /**
171 * @test
172 */
173 public function validEntryIdentifiersAreRecognizedAsValid()
174 {
175 $identifier = 'someCacheIdentifier';
176 $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class);
177 $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
178 ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
179 ->setConstructorArgs([$identifier, $backend])
180 ->getMock();
181 foreach (['_', 'abcdef', 'foo', 'bar123', '3some', '_bl_a', 'some&', 'one%TWO', str_repeat('x', 250)] as $entryIdentifier) {
182 $this->assertTrue($cache->isValidEntryIdentifier($entryIdentifier), 'Valid identifier "' . $entryIdentifier . '" was not accepted.');
183 }
184 }
185
186 /**
187 * @test
188 */
189 public function invalidTagsAreRecognizedAsInvalid()
190 {
191 $identifier = 'someCacheIdentifier';
192 $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class);
193 $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
194 ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
195 ->setConstructorArgs([$identifier, $backend])
196 ->getMock();
197 foreach (['', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#'] as $tag) {
198 $this->assertFalse($cache->isValidTag($tag), 'Invalid tag "' . $tag . '" was not rejected.');
199 }
200 }
201
202 /**
203 * @test
204 */
205 public function validTagsAreRecognizedAsValid()
206 {
207 $identifier = 'someCacheIdentifier';
208 $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class);
209 $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
210 ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
211 ->setConstructorArgs([$identifier, $backend])
212 ->getMock();
213 foreach (['abcdef', 'foo-bar', 'foo_baar', 'bar123', '3some', 'file%Thing', 'some&', '%x%', str_repeat('x', 250)] as $tag) {
214 $this->assertTrue($cache->isValidTag($tag), 'Valid tag "' . $tag . '" was not accepted.');
215 }
216 }
217 }