-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCacheKeyWithSubfolderTest.php
More file actions
115 lines (92 loc) · 3.69 KB
/
CacheKeyWithSubfolderTest.php
File metadata and controls
115 lines (92 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace JoliCode\MediaBundle\Tests\Storage;
use JoliCode\MediaBundle\Binary\MimeTypeGuesser;
use JoliCode\MediaBundle\Model\Format;
use JoliCode\MediaBundle\Storage\MediaPropertyAccessor;
use JoliCode\MediaBundle\Storage\MediaVariationPropertyAccessor;
use JoliCode\MediaBundle\Storage\Strategy\FolderStorageStrategy;
use JoliCode\MediaBundle\Transformer\TransformerChain;
use JoliCode\MediaBundle\Variation\Variation;
use League\Flysystem\Filesystem;
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Mime\FileBinaryMimeTypeGuesser;
use Symfony\Component\Mime\MimeTypes;
/**
* Tests that cache keys are properly sanitized when using paths with reserved PSR-6 characters.
*
* This test ensures that files in subfolders (containing forward slashes) don't cause
* cache key validation errors.
*/
class CacheKeyWithSubfolderTest extends TestCase
{
private ArrayAdapter $cache;
private Filesystem $filesystem;
private MimeTypeGuesser $mimeTypeGuesser;
protected function setUp(): void
{
$this->cache = new ArrayAdapter();
$this->filesystem = new Filesystem(new InMemoryFilesystemAdapter());
$this->mimeTypeGuesser = new MimeTypeGuesser(new MimeTypes(), new FileBinaryMimeTypeGuesser());
}
public function testSubfolderPath(): void
{
// Create a file in a subfolder (path contains '/' which is a reserved PSR-6 character)
$path = 'subfolder/test.jpg';
$this->filesystem->write($path, 'dummy image content');
$accessor = new MediaPropertyAccessor(
'default',
$this->filesystem,
$this->mimeTypeGuesser,
$this->cache,
);
$this->expectNotToPerformAssertions();
// This should NOT throw an exception about reserved characters
$accessor->getMimeType($path);
$accessor->getFormat($path);
$accessor->getFileSize($path);
// Test clearCache doesn't throw either
$accessor->clearCache($path);
}
public function testVariationSubfolderPath(): void
{
$strategy = new FolderStorageStrategy();
// Create a file in a nested subfolder
$path = 'folder/subfolder/image.png';
$this->filesystem->write($path, 'dummy image content');
$variation = new Variation('thumbnail', Format::PNG, new TransformerChain([]));
$accessor = new MediaVariationPropertyAccessor(
'my-library',
$strategy,
$this->filesystem,
$this->mimeTypeGuesser,
$this->cache,
);
$this->expectNotToPerformAssertions();
// This should NOT throw an exception about reserved characters
$accessor->getMimeType($path, $variation);
$accessor->getFormat($path, $variation);
$accessor->getFileSize($path, $variation);
// Test clearCache doesn't throw either
$accessor->clearCache($path, $variation);
}
public function testNestedSubfolderPath(): void
{
// Path with forward slash (subfolder)
$path = 'user-uploads/2024/document.pdf';
$this->filesystem->write($path, 'dummy pdf content');
$accessor = new MediaPropertyAccessor(
'default',
$this->filesystem,
$this->mimeTypeGuesser,
$this->cache,
);
// Should work without throwing PSR-6 cache key validation errors
$mimeType = $accessor->getMimeType($path);
// Verify the cache was actually used by calling again
$mimeType2 = $accessor->getMimeType($path);
$this->assertSame($mimeType, $mimeType2);
}
}