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