Completed
Push — 6.13 ( 9c335d...8c8805 )
by
unknown
16:33
created

LocationAwareStoreTest::testSetFilesystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
namespace eZ\Publish\Core\MVC\Symfony\Cache\Tests\Http;
10
11
use eZ\Publish\Core\MVC\Symfony\Cache\Http\LocationAwareStore;
12
use Symfony\Component\Filesystem\Filesystem;
13
use Symfony\Component\HttpFoundation\Request;
14
use PHPUnit\Framework\TestCase;
15
16
class LocationAwareStoreTest extends TestCase
17
{
18
    /**
19
     * @var \eZ\Publish\Core\MVC\Symfony\Cache\Http\LocationAwareStore
20
     */
21
    private $store;
22
23
    protected function setUp()
24
    {
25
        parent::setUp();
26
        $this->store = new LocationAwareStore(__DIR__);
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\MVC\Symf...Http\LocationAwareStore has been deprecated with message: since 6.8. Replaced by TagAwareStore from the http-cache multi-tagging package.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
27
    }
28
29
    protected function tearDown()
30
    {
31
        array_map('unlink', glob(__DIR__ . '/*.purging'));
32
        parent::tearDown();
33
    }
34
35
    public function testGetFilesystem()
36
    {
37
        $this->assertInstanceOf('Symfony\\Component\\Filesystem\\Filesystem', $this->store->getFilesystem());
38
    }
39
40
    public function testSetFilesystem()
41
    {
42
        $fs = new Filesystem();
43
        $this->store->setFilesystem($fs);
44
        $this->assertSame($fs, $this->store->getFilesystem());
45
    }
46
47
    public function testGetPath()
48
    {
49
        $prefix = LocationAwareStore::LOCATION_CACHE_DIR . DIRECTORY_SEPARATOR . '123' . DIRECTORY_SEPARATOR;
50
        $path = $this->store->getPath($prefix . DIRECTORY_SEPARATOR . 'en' . sha1('someContent'));
51
        $this->assertTrue(strpos($path, __DIR__ . DIRECTORY_SEPARATOR . $prefix) === 0);
52
    }
53
54
    public function testGetStalePath()
55
    {
56
        // Generate the lock file to force using the stale cache dir
57
        $locationId = 123;
58
        $prefix = LocationAwareStore::LOCATION_CACHE_DIR . DIRECTORY_SEPARATOR . $locationId;
59
        $prefixStale = LocationAwareStore::LOCATION_STALE_CACHE_DIR . DIRECTORY_SEPARATOR . $locationId;
60
        $lockFile = $this->store->getLocationCacheLockName($locationId);
61
        file_put_contents($lockFile, getmypid());
62
63
        $path = $this->store->getPath($prefix . DIRECTORY_SEPARATOR . 'en' . sha1('someContent'));
64
        $this->assertTrue(strpos($path, __DIR__ . DIRECTORY_SEPARATOR . $prefixStale) === 0);
65
        @unlink($lockFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
66
    }
67
68
    public function testGetPathDeadProcess()
69
    {
70
        if (!function_exists('posix_kill')) {
71
            self::markTestSkipped('posix_kill() function is needed for this test');
72
        }
73
74
        $locationId = 123;
75
        $prefix = LocationAwareStore::LOCATION_CACHE_DIR . "/$locationId";
76
        $lockFile = $this->store->getLocationCacheLockName($locationId);
77
        file_put_contents($lockFile, '99999999999999999');
78
79
        $path = $this->store->getPath("$prefix/en" . sha1('someContent'));
80
        $this->assertTrue(strpos($path, __DIR__ . "/$prefix") === 0);
81
        $this->assertFileNotExists($lockFile);
82
    }
83
84
    /**
85
     * @return \PHPUnit_Framework_MockObject_MockObject
86
     */
87
    private function getFilesystemMock()
88
    {
89
        return $this->createMock(Filesystem::class);
90
    }
91
92
    public function testPurgeByRequestSingleLocation()
93
    {
94
        $fs = $this->getFilesystemMock();
95
        $this->store->setFilesystem($fs);
96
        $locationId = 123;
97
        $locationCacheDir = $this->store->getLocationCacheDir($locationId);
98
        $staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir);
99
100
        $fs
101
            ->expects($this->any())
102
            ->method('exists')
103
            ->with($locationCacheDir)
104
            ->will($this->returnValue(true));
105
        $fs
106
            ->expects($this->once())
107
            ->method('mkdir')
108
            ->with($staleCacheDir);
109
        $fs
110
            ->expects($this->once())
111
            ->method('mirror')
112
            ->with($locationCacheDir, $staleCacheDir);
113
        $fs
114
            ->expects($this->once())
115
            ->method('remove')
116
            ->with([$staleCacheDir, $this->store->getLocationCacheLockName($locationId), $locationCacheDir]);
117
118
        $request = Request::create('/', 'PURGE');
119
        $request->headers->set('X-Location-Id', "$locationId");
120
        $this->store->purgeByRequest($request);
121
    }
122
123 View Code Duplication
    public function testPurgeByRequestMultipleLocationsBC()
124
    {
125
        $fs = $this->getFilesystemMock();
126
        $this->store->setFilesystem($fs);
127
        $locationIds = [123, 456, 789];
128
        $i = 0;
129
        foreach ($locationIds as $locationId) {
130
            $locationCacheDir = $this->store->getLocationCacheDir($locationId);
131
            $staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir);
132
133
            $fs
134
                ->expects($this->at($i++))
135
                ->method('exists')
136
                ->with($locationCacheDir)
137
                ->will($this->returnValue(true));
138
            $fs
139
                ->expects($this->at($i++))
140
                ->method('mkdir')
141
                ->with($staleCacheDir);
142
            $fs
143
                ->expects($this->at($i++))
144
                ->method('mirror')
145
                ->with($locationCacheDir, $staleCacheDir);
146
            $fs
147
                ->expects($this->at($i++))
148
                ->method('remove')
149
                ->with([$staleCacheDir, $this->store->getLocationCacheLockName($locationId), $locationCacheDir]);
150
        }
151
152
        $request = Request::create('/', 'PURGE');
153
        $request->headers->set('X-Group-Location-Id', implode('; ', $locationIds));
154
        $this->store->purgeByRequest($request);
155
    }
156
157 View Code Duplication
    public function testPurgeByRequestMultipleLocations()
158
    {
159
        $fs = $this->getFilesystemMock();
160
        $this->store->setFilesystem($fs);
161
        $locationIds = [123, 456, 789];
162
        $i = 0;
163
        foreach ($locationIds as $locationId) {
164
            $locationCacheDir = $this->store->getLocationCacheDir($locationId);
165
            $staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir);
166
167
            $fs
168
                ->expects($this->at($i++))
169
                ->method('exists')
170
                ->with($locationCacheDir)
171
                ->will($this->returnValue(true));
172
            $fs
173
                ->expects($this->at($i++))
174
                ->method('mkdir')
175
                ->with($staleCacheDir);
176
            $fs
177
                ->expects($this->at($i++))
178
                ->method('mirror')
179
                ->with($locationCacheDir, $staleCacheDir);
180
            $fs
181
                ->expects($this->at($i++))
182
                ->method('remove')
183
                ->with([$staleCacheDir, $this->store->getLocationCacheLockName($locationId), $locationCacheDir]);
184
        }
185
186
        $request = Request::create('/', 'BAN');
187
        $request->headers->set('X-Location-Id', '(' . implode('|', $locationIds) . ')');
188
        $this->store->purgeByRequest($request);
189
    }
190
191
    public function testPurgeAllContent()
192
    {
193
        $fs = $this->getFilesystemMock();
194
        $this->store->setFilesystem($fs);
195
        $locationCacheDir = $this->store->getLocationCacheDir();
196
        $staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir);
197
198
        $fs
199
            ->expects($this->any())
200
            ->method('exists')
201
            ->with($locationCacheDir)
202
            ->will($this->returnValue(true));
203
        $fs
204
            ->expects($this->once())
205
            ->method('mkdir')
206
            ->with($staleCacheDir);
207
        $fs
208
            ->expects($this->once())
209
            ->method('mirror')
210
            ->with($locationCacheDir, $staleCacheDir);
211
        $fs
212
            ->expects($this->once())
213
            ->method('remove')
214
            ->with([$staleCacheDir, $this->store->getLocationCacheLockName(), $locationCacheDir]);
215
216
        $this->store->purgeAllContent();
217
    }
218
219 View Code Duplication
    public function testPurgeAllContentByRequest()
220
    {
221
        $fs = $this->getFilesystemMock();
222
        $this->store->setFilesystem($fs);
223
        $locationCacheDir = $this->store->getLocationCacheDir();
224
        $staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir);
225
226
        $fs
227
            ->expects($this->any())
228
            ->method('exists')
229
            ->with($locationCacheDir)
230
            ->will($this->returnValue(true));
231
        $fs
232
            ->expects($this->once())
233
            ->method('mkdir')
234
            ->with($staleCacheDir);
235
        $fs
236
            ->expects($this->once())
237
            ->method('mirror')
238
            ->with($locationCacheDir, $staleCacheDir);
239
        $fs
240
            ->expects($this->once())
241
            ->method('remove')
242
            ->with([$staleCacheDir, $this->store->getLocationCacheLockName(), $locationCacheDir]);
243
244
        $request = Request::create('/', 'BAN');
245
        $request->headers->set('X-Location-Id', '.*');
246
        $this->store->purgeByRequest($request);
247
    }
248
249 View Code Duplication
    public function testPurgeAllContentByRequestBC()
250
    {
251
        $fs = $this->getFilesystemMock();
252
        $this->store->setFilesystem($fs);
253
        $locationCacheDir = $this->store->getLocationCacheDir();
254
        $staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir);
255
256
        $fs
257
            ->expects($this->any())
258
            ->method('exists')
259
            ->with($locationCacheDir)
260
            ->will($this->returnValue(true));
261
        $fs
262
            ->expects($this->once())
263
            ->method('mkdir')
264
            ->with($staleCacheDir);
265
        $fs
266
            ->expects($this->once())
267
            ->method('mirror')
268
            ->with($locationCacheDir, $staleCacheDir);
269
        $fs
270
            ->expects($this->once())
271
            ->method('remove')
272
            ->with([$staleCacheDir, $this->store->getLocationCacheLockName(), $locationCacheDir]);
273
274
        $request = Request::create('/', 'PURGE');
275
        $request->headers->set('X-Location-Id', '*');
276
        $this->store->purgeByRequest($request);
277
    }
278
}
279