[TASK] Remove leftover FLOW3 code in Caching
[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\Core\Tests\UnitTestCase
23 {
24 /**
25 * @test
26 */
27 public function theConstructorAcceptsValidIdentifiers()
28 {
29 $mockBackend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', false);
30 foreach (array('x', 'someValue', '123fivesixseveneight', 'some&', 'ab_cd%', rawurlencode('resource://some/äöü$&% sadf'), str_repeat('x', 250)) as $identifier) {
31 $abstractCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class, array('__construct', 'get', 'set', 'has', 'remove', 'getByTag', 'flush', 'flushByTag', 'collectGarbage'), array($identifier, $mockBackend));
32 }
33 }
34
35 /**
36 * @test
37 */
38 public function theConstructorRejectsInvalidIdentifiers()
39 {
40 $mockBackend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', false);
41 foreach (array('', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#') as $identifier) {
42 try {
43 $abstractCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class, array('__construct', 'get', 'set', 'has', 'remove', 'getByTag', 'flush', 'flushByTag', 'collectGarbage'), array($identifier, $mockBackend));
44 $this->fail('Identifier "' . $identifier . '" was not rejected.');
45 } catch (\InvalidArgumentException $exception) {
46 }
47 }
48 }
49
50 /**
51 * @test
52 */
53 public function flushCallsBackend()
54 {
55 $identifier = 'someCacheIdentifier';
56 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', false);
57 $backend->expects($this->once())->method('flush');
58 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class, array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
59 $cache->flush();
60 }
61
62 /**
63 * @test
64 * @expectedException \InvalidArgumentException
65 */
66 public function flushByTagRejectsInvalidTags()
67 {
68 $identifier = 'someCacheIdentifier';
69 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\BackendInterface::class, array(), array(), '', false);
70 $backend->expects($this->never())->method('flushByTag');
71 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class, array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
72 $cache->flushByTag('SomeInvalid\\Tag');
73 }
74
75 /**
76 * @test
77 */
78 public function flushByTagCallsBackendIfItIsATaggableBackend()
79 {
80 $tag = 'sometag';
81 $identifier = 'someCacheIdentifier';
82 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface::class, array('setCache', 'get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', false);
83 $backend->expects($this->once())->method('flushByTag')->with($tag);
84 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class, array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
85 $cache->flushByTag($tag);
86 }
87
88 /**
89 * @test
90 */
91 public function collectGarbageCallsBackend()
92 {
93 $identifier = 'someCacheIdentifier';
94 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', false);
95 $backend->expects($this->once())->method('collectGarbage');
96 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class, array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
97 $cache->collectGarbage();
98 }
99
100 /**
101 * @test
102 */
103 public function invalidEntryIdentifiersAreRecognizedAsInvalid()
104 {
105 $identifier = 'someCacheIdentifier';
106 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, array(), array(), '', false);
107 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class, array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
108 foreach (array('', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#') as $entryIdentifier) {
109 $this->assertFalse($cache->isValidEntryIdentifier($entryIdentifier), 'Invalid identifier "' . $entryIdentifier . '" was not rejected.');
110 }
111 }
112
113 /**
114 * @test
115 */
116 public function validEntryIdentifiersAreRecognizedAsValid()
117 {
118 $identifier = 'someCacheIdentifier';
119 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, array(), array(), '', false);
120 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class, array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
121 foreach (array('_', 'abcdef', 'foo', 'bar123', '3some', '_bl_a', 'some&', 'one%TWO', str_repeat('x', 250)) as $entryIdentifier) {
122 $this->assertTrue($cache->isValidEntryIdentifier($entryIdentifier), 'Valid identifier "' . $entryIdentifier . '" was not accepted.');
123 }
124 }
125
126 /**
127 * @test
128 */
129 public function invalidTagsAreRecognizedAsInvalid()
130 {
131 $identifier = 'someCacheIdentifier';
132 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, array(), array(), '', false);
133 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class, array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
134 foreach (array('', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#') as $tag) {
135 $this->assertFalse($cache->isValidTag($tag), 'Invalid tag "' . $tag . '" was not rejected.');
136 }
137 }
138
139 /**
140 * @test
141 */
142 public function validTagsAreRecognizedAsValid()
143 {
144 $identifier = 'someCacheIdentifier';
145 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, array(), array(), '', false);
146 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class, array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
147 foreach (array('abcdef', 'foo-bar', 'foo_baar', 'bar123', '3some', 'file%Thing', 'some&', '%x%', str_repeat('x', 250)) as $tag) {
148 $this->assertTrue($cache->isValidTag($tag), 'Valid tag "' . $tag . '" was not accepted.');
149 }
150 }
151 }