1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the LocationAwareStoreTest class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @version //autogentag// |
10
|
|
|
*/ |
11
|
|
|
namespace eZ\Publish\Core\MVC\Symfony\CacheTests\Http; |
12
|
|
|
|
13
|
|
|
use eZ\Publish\Core\MVC\Symfony\Cache\Http\LocationAwareStore; |
14
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use PHPUnit_Framework_TestCase; |
17
|
|
|
|
18
|
|
|
class LocationAwareStoreTest extends PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \eZ\Publish\Core\MVC\Symfony\Cache\Http\LocationAwareStore |
22
|
|
|
*/ |
23
|
|
|
private $store; |
24
|
|
|
|
25
|
|
|
protected function setUp() |
26
|
|
|
{ |
27
|
|
|
parent::setUp(); |
28
|
|
|
$this->store = new LocationAwareStore(__DIR__); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function tearDown() |
32
|
|
|
{ |
33
|
|
|
array_map('unlink', glob(__DIR__ . '/*.purging')); |
34
|
|
|
parent::tearDown(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testGetFilesystem() |
38
|
|
|
{ |
39
|
|
|
$this->assertInstanceOf('Symfony\\Component\\Filesystem\\Filesystem', $this->store->getFilesystem()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testSetFilesystem() |
43
|
|
|
{ |
44
|
|
|
$fs = new Filesystem(); |
45
|
|
|
$this->store->setFilesystem($fs); |
46
|
|
|
$this->assertSame($fs, $this->store->getFilesystem()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testGetPath() |
50
|
|
|
{ |
51
|
|
|
$prefix = LocationAwareStore::LOCATION_CACHE_DIR . DIRECTORY_SEPARATOR . '123' . DIRECTORY_SEPARATOR; |
52
|
|
|
$path = $this->store->getPath($prefix . DIRECTORY_SEPARATOR . 'en' . sha1('someContent')); |
53
|
|
|
$this->assertTrue(strpos($path, __DIR__ . DIRECTORY_SEPARATOR . $prefix) === 0); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testGetStalePath() |
57
|
|
|
{ |
58
|
|
|
// Generate the lock file to force using the stale cache dir |
59
|
|
|
$locationId = 123; |
60
|
|
|
$prefix = LocationAwareStore::LOCATION_CACHE_DIR . DIRECTORY_SEPARATOR . $locationId; |
61
|
|
|
$prefixStale = LocationAwareStore::LOCATION_STALE_CACHE_DIR . DIRECTORY_SEPARATOR . $locationId; |
62
|
|
|
$lockFile = $this->store->getLocationCacheLockName($locationId); |
63
|
|
|
file_put_contents($lockFile, getmypid()); |
64
|
|
|
|
65
|
|
|
$path = $this->store->getPath($prefix . DIRECTORY_SEPARATOR . 'en' . sha1('someContent')); |
66
|
|
|
$this->assertTrue(strpos($path, __DIR__ . DIRECTORY_SEPARATOR . $prefixStale) === 0); |
67
|
|
|
@unlink($lockFile); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testGetPathDeadProcess() |
71
|
|
|
{ |
72
|
|
|
if (!function_exists('posix_kill')) { |
73
|
|
|
self::markTestSkipped('posix_kill() function is needed for this test'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$locationId = 123; |
77
|
|
|
$prefix = LocationAwareStore::LOCATION_CACHE_DIR . "/$locationId"; |
78
|
|
|
$lockFile = $this->store->getLocationCacheLockName($locationId); |
79
|
|
|
file_put_contents($lockFile, '99999999999999999'); |
80
|
|
|
|
81
|
|
|
$path = $this->store->getPath("$prefix/en" . sha1('someContent')); |
82
|
|
|
$this->assertTrue(strpos($path, __DIR__ . "/$prefix") === 0); |
83
|
|
|
$this->assertFalse(file_exists($lockFile)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject |
88
|
|
|
*/ |
89
|
|
|
private function getFilesystemMock() |
90
|
|
|
{ |
91
|
|
|
return $this->getMock('Symfony\\Component\\Filesystem\\Filesystem'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testPurgeByRequestSingleLocation() |
95
|
|
|
{ |
96
|
|
|
$fs = $this->getFilesystemMock(); |
97
|
|
|
$this->store->setFilesystem($fs); |
98
|
|
|
$locationId = 123; |
99
|
|
|
$locationCacheDir = $this->store->getLocationCacheDir($locationId); |
100
|
|
|
$staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir); |
101
|
|
|
|
102
|
|
|
$fs |
103
|
|
|
->expects($this->any()) |
104
|
|
|
->method('exists') |
105
|
|
|
->with($locationCacheDir) |
106
|
|
|
->will($this->returnValue(true)); |
107
|
|
|
$fs |
108
|
|
|
->expects($this->once()) |
109
|
|
|
->method('mkdir') |
110
|
|
|
->with($staleCacheDir); |
111
|
|
|
$fs |
112
|
|
|
->expects($this->once()) |
113
|
|
|
->method('mirror') |
114
|
|
|
->with($locationCacheDir, $staleCacheDir); |
115
|
|
|
$fs |
116
|
|
|
->expects($this->once()) |
117
|
|
|
->method('remove') |
118
|
|
|
->with(array($staleCacheDir, $this->store->getLocationCacheLockName($locationId), $locationCacheDir)); |
119
|
|
|
|
120
|
|
|
$request = Request::create('/', 'PURGE'); |
121
|
|
|
$request->headers->set('X-Location-Id', "$locationId"); |
122
|
|
|
$this->store->purgeByRequest($request); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testPurgeByRequestMultipleLocations() |
126
|
|
|
{ |
127
|
|
|
$fs = $this->getFilesystemMock(); |
128
|
|
|
$this->store->setFilesystem($fs); |
129
|
|
|
$locationIds = array(123, 456, 789); |
130
|
|
|
$i = 0; |
131
|
|
|
foreach ($locationIds as $locationId) { |
132
|
|
|
$locationCacheDir = $this->store->getLocationCacheDir($locationId); |
133
|
|
|
$staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir); |
134
|
|
|
|
135
|
|
|
$fs |
136
|
|
|
->expects($this->at($i++)) |
137
|
|
|
->method('exists') |
138
|
|
|
->with($locationCacheDir) |
139
|
|
|
->will($this->returnValue(true)); |
140
|
|
|
$fs |
141
|
|
|
->expects($this->at($i++)) |
142
|
|
|
->method('mkdir') |
143
|
|
|
->with($staleCacheDir); |
144
|
|
|
$fs |
145
|
|
|
->expects($this->at($i++)) |
146
|
|
|
->method('mirror') |
147
|
|
|
->with($locationCacheDir, $staleCacheDir); |
148
|
|
|
$fs |
149
|
|
|
->expects($this->at($i++)) |
150
|
|
|
->method('remove') |
151
|
|
|
->with(array($staleCacheDir, $this->store->getLocationCacheLockName($locationId), $locationCacheDir)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$request = Request::create('/', 'BAN'); |
155
|
|
|
$request->headers->set('X-Location-Id', '(' . implode('|', $locationIds) . ')'); |
156
|
|
|
$this->store->purgeByRequest($request); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testPurgeAllContent() |
160
|
|
|
{ |
161
|
|
|
$fs = $this->getFilesystemMock(); |
162
|
|
|
$this->store->setFilesystem($fs); |
163
|
|
|
$locationCacheDir = $this->store->getLocationCacheDir(); |
164
|
|
|
$staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir); |
165
|
|
|
|
166
|
|
|
$fs |
167
|
|
|
->expects($this->any()) |
168
|
|
|
->method('exists') |
169
|
|
|
->with($locationCacheDir) |
170
|
|
|
->will($this->returnValue(true)); |
171
|
|
|
$fs |
172
|
|
|
->expects($this->once()) |
173
|
|
|
->method('mkdir') |
174
|
|
|
->with($staleCacheDir); |
175
|
|
|
$fs |
176
|
|
|
->expects($this->once()) |
177
|
|
|
->method('mirror') |
178
|
|
|
->with($locationCacheDir, $staleCacheDir); |
179
|
|
|
$fs |
180
|
|
|
->expects($this->once()) |
181
|
|
|
->method('remove') |
182
|
|
|
->with(array($staleCacheDir, $this->store->getLocationCacheLockName(), $locationCacheDir)); |
183
|
|
|
|
184
|
|
|
$this->store->purgeAllContent(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
View Code Duplication |
public function testPurgeAllContentByRequest() |
188
|
|
|
{ |
189
|
|
|
$fs = $this->getFilesystemMock(); |
190
|
|
|
$this->store->setFilesystem($fs); |
191
|
|
|
$locationCacheDir = $this->store->getLocationCacheDir(); |
192
|
|
|
$staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir); |
193
|
|
|
|
194
|
|
|
$fs |
195
|
|
|
->expects($this->any()) |
196
|
|
|
->method('exists') |
197
|
|
|
->with($locationCacheDir) |
198
|
|
|
->will($this->returnValue(true)); |
199
|
|
|
$fs |
200
|
|
|
->expects($this->once()) |
201
|
|
|
->method('mkdir') |
202
|
|
|
->with($staleCacheDir); |
203
|
|
|
$fs |
204
|
|
|
->expects($this->once()) |
205
|
|
|
->method('mirror') |
206
|
|
|
->with($locationCacheDir, $staleCacheDir); |
207
|
|
|
$fs |
208
|
|
|
->expects($this->once()) |
209
|
|
|
->method('remove') |
210
|
|
|
->with(array($staleCacheDir, $this->store->getLocationCacheLockName(), $locationCacheDir)); |
211
|
|
|
|
212
|
|
|
$request = Request::create('/', 'BAN'); |
213
|
|
|
$request->headers->set('X-Location-Id', '.*'); |
214
|
|
|
$this->store->purgeByRequest($request); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
View Code Duplication |
public function testPurgeAllContentByRequestBC() |
218
|
|
|
{ |
219
|
|
|
$fs = $this->getFilesystemMock(); |
220
|
|
|
$this->store->setFilesystem($fs); |
221
|
|
|
$locationCacheDir = $this->store->getLocationCacheDir(); |
222
|
|
|
$staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir); |
223
|
|
|
|
224
|
|
|
$fs |
225
|
|
|
->expects($this->any()) |
226
|
|
|
->method('exists') |
227
|
|
|
->with($locationCacheDir) |
228
|
|
|
->will($this->returnValue(true)); |
229
|
|
|
$fs |
230
|
|
|
->expects($this->once()) |
231
|
|
|
->method('mkdir') |
232
|
|
|
->with($staleCacheDir); |
233
|
|
|
$fs |
234
|
|
|
->expects($this->once()) |
235
|
|
|
->method('mirror') |
236
|
|
|
->with($locationCacheDir, $staleCacheDir); |
237
|
|
|
$fs |
238
|
|
|
->expects($this->once()) |
239
|
|
|
->method('remove') |
240
|
|
|
->with(array($staleCacheDir, $this->store->getLocationCacheLockName(), $locationCacheDir)); |
241
|
|
|
|
242
|
|
|
$request = Request::create('/', 'PURGE'); |
243
|
|
|
$request->headers->set('X-Location-Id', '*'); |
244
|
|
|
$this->store->purgeByRequest($request); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: