|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\MediaGalleryRenditions\Test\Unit\Model; |
| 9 | + |
| 10 | +use Magento\Framework\Filesystem; |
| 11 | +use Magento\Framework\Filesystem\Driver\File; |
| 12 | +use Magento\Framework\Image\AdapterFactory; |
| 13 | +use Magento\MediaGalleryApi\Api\IsPathExcludedInterface; |
| 14 | +use Magento\MediaGalleryRenditions\Model\Config; |
| 15 | +use Magento\MediaGalleryRenditions\Model\GenerateRenditions; |
| 16 | +use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | +use Psr\Log\LoggerInterface; |
| 20 | + |
| 21 | +class GenerateRenditionsTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var GenerateRenditions |
| 25 | + */ |
| 26 | + private $model; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var AdapterFactory|MockObject |
| 30 | + */ |
| 31 | + private $imageFactoryMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var Config|MockObject |
| 35 | + */ |
| 36 | + private $configMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var GetRenditionPathInterface|MockObject |
| 40 | + */ |
| 41 | + private $getRenditionPathMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var Filesystem|MockObject |
| 45 | + */ |
| 46 | + private $filesystemMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var File|MockObject |
| 50 | + */ |
| 51 | + private $driverMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var IsPathExcludedInterface|MockObject |
| 55 | + */ |
| 56 | + private $isPathExcludedMock; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var LoggerInterface|MockObject |
| 60 | + */ |
| 61 | + private $loggerMock; |
| 62 | + |
| 63 | + protected function setUp(): void |
| 64 | + { |
| 65 | + $this->imageFactoryMock = $this->createMock(AdapterFactory::class); |
| 66 | + $this->configMock = $this->createMock(Config::class); |
| 67 | + $this->getRenditionPathMock = $this->createMock(GetRenditionPathInterface::class); |
| 68 | + $this->filesystemMock = $this->createMock(Filesystem::class); |
| 69 | + $this->driverMock = $this->createMock(File::class); |
| 70 | + $this->isPathExcludedMock = $this->createMock(IsPathExcludedInterface::class); |
| 71 | + $this->loggerMock = $this->createMock(LoggerInterface::class); |
| 72 | + |
| 73 | + $this->model = new GenerateRenditions( |
| 74 | + $this->imageFactoryMock, |
| 75 | + $this->configMock, |
| 76 | + $this->getRenditionPathMock, |
| 77 | + $this->filesystemMock, |
| 78 | + $this->driverMock, |
| 79 | + $this->isPathExcludedMock, |
| 80 | + $this->loggerMock |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Test getImageFileNamePattern method returns correct regex pattern |
| 86 | + */ |
| 87 | + public function testGetImageFileNamePattern(): void |
| 88 | + { |
| 89 | + $pattern = $this->model->getImageFileNamePattern(); |
| 90 | + |
| 91 | + // Assert the pattern is the expected string |
| 92 | + $this->assertEquals('#\.(jpg|jpeg|gif|png)$# i', $pattern); |
| 93 | + |
| 94 | + // Test that the pattern correctly validates supported file types |
| 95 | + $validExtensions = ['test.jpg', 'test.jpeg', 'test.gif', 'test.png', 'TEST.JPG', 'TEST.PNG']; |
| 96 | + foreach ($validExtensions as $filename) { |
| 97 | + $this->assertEquals( |
| 98 | + 1, |
| 99 | + preg_match($pattern, $filename), |
| 100 | + "Pattern should match valid image file: $filename" |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + // Test that the pattern correctly rejects unsupported file types |
| 105 | + $invalidExtensions = ['test.txt', 'test.pdf', 'test.webp', 'test.bmp', 'test']; |
| 106 | + foreach ($invalidExtensions as $filename) { |
| 107 | + $this->assertEquals( |
| 108 | + 0, |
| 109 | + preg_match($pattern, $filename), |
| 110 | + "Pattern should not match invalid image file: $filename" |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Test that pattern is case-insensitive |
| 117 | + */ |
| 118 | + public function testGetImageFileNamePatternCaseInsensitive(): void |
| 119 | + { |
| 120 | + $pattern = $this->model->getImageFileNamePattern(); |
| 121 | + $mixedCaseFiles = [ |
| 122 | + 'image.JPG', |
| 123 | + 'image.Jpg', |
| 124 | + 'image.JPEG', |
| 125 | + 'image.Jpeg', |
| 126 | + 'image.GIF', |
| 127 | + 'image.Gif', |
| 128 | + 'image.PNG', |
| 129 | + 'image.Png' |
| 130 | + ]; |
| 131 | + |
| 132 | + foreach ($mixedCaseFiles as $filename) { |
| 133 | + $this->assertEquals( |
| 134 | + 1, |
| 135 | + preg_match($pattern, $filename), |
| 136 | + "Pattern should match case-insensitive image file: $filename" |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments