Skip to content

Commit ffee111

Browse files
authored
[11.x] Test for forgetting non-flexible keys for file driver (#53018)
* Test for forgetting non-flexible keys for file driver * Check file existence before forgetting * only forget when parent key is forgotten * lint
1 parent 65b6a40 commit ffee111

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/Illuminate/Cache/FileStore.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,11 @@ public function restoreLock($name, $owner)
248248
public function forget($key)
249249
{
250250
if ($this->files->exists($file = $this->path($key))) {
251-
$this->files->delete($this->path("illuminate:cache:flexible:created:{$key}"));
252-
253-
return $this->files->delete($file);
251+
return tap($this->files->delete($file), function ($forgotten) use ($key) {
252+
if ($forgotten && $this->files->exists($file = $this->path("illuminate:cache:flexible:created:{$key}"))) {
253+
$this->files->delete($file);
254+
}
255+
});
254256
}
255257

256258
return false;

tests/Cache/CacheFileStoreTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Contracts\Filesystem\FileNotFoundException;
88
use Illuminate\Filesystem\Filesystem;
99
use Illuminate\Support\Carbon;
10+
use Illuminate\Support\Str;
1011
use Mockery as m;
1112
use PHPUnit\Framework\TestCase;
1213

@@ -330,6 +331,49 @@ public function testFlushIgnoreNonExistingDirectory()
330331
$this->assertFalse($result, 'Flush should not clean directory');
331332
}
332333

334+
public function testItHandlesForgettingNonFlexibleKeys()
335+
{
336+
$store = new FileStore(new Filesystem, __DIR__);
337+
338+
$key = Str::random();
339+
$path = $store->path($key);
340+
$flexiblePath = "illuminate:cache:flexible:created:{$key}";
341+
342+
$store->put($key, 'value', 5);
343+
344+
$this->assertFileExists($path);
345+
$this->assertFileDoesNotExist($flexiblePath);
346+
347+
$store->forget($key);
348+
349+
$this->assertFileDoesNotExist($path);
350+
$this->assertFileDoesNotExist($flexiblePath);
351+
}
352+
353+
public function itOnlyForgetsFlexibleKeysIfParentIsForgotten()
354+
{
355+
$store = new FileStore(new Filesystem, __DIR__);
356+
357+
$key = Str::random();
358+
$path = $store->path($key);
359+
$flexiblePath = "illuminate:cache:flexible:created:{$key}";
360+
361+
touch($flexiblePath);
362+
363+
$this->assertFileDoesNotExist($path);
364+
$this->assertFileExists($flexiblePath);
365+
366+
$store->forget($key);
367+
368+
$this->assertFileDoesNotExist($path);
369+
$this->assertFileExists($flexiblePath);
370+
371+
$store->put($key, 'value', 5);
372+
373+
$this->assertFileDoesNotExist($path);
374+
$this->assertFileDoesNotExist($flexiblePath);
375+
}
376+
333377
protected function mockFilesystem()
334378
{
335379
return $this->createMock(Filesystem::class);

0 commit comments

Comments
 (0)