Skip to content

Commit 631d9d4

Browse files
committed
AC-15338::Added public function for image_file_name_pattern constant
1 parent ec9459d commit 631d9d4

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

app/code/Magento/MediaGalleryRenditions/Model/GenerateRenditions.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -26,6 +26,16 @@ class GenerateRenditions implements GenerateRenditionsInterface
2626
{
2727
private const IMAGE_FILE_NAME_PATTERN = '#\.(jpg|jpeg|gif|png)$# i';
2828

29+
/**
30+
* Get image file name pattern for validation
31+
*
32+
* @return string
33+
*/
34+
public function getImageFileNamePattern(): string
35+
{
36+
return self::IMAGE_FILE_NAME_PATTERN;
37+
}
38+
2939
/**
3040
* @var AdapterFactory
3141
*/
@@ -162,7 +172,7 @@ private function validateAsset(string $path): void
162172
);
163173
}
164174

165-
if (!preg_match(self::IMAGE_FILE_NAME_PATTERN, $path)) {
175+
if (!preg_match($this->getImageFileNamePattern(), $path)) {
166176
throw new LocalizedException(
167177
__('Could not create rendition for image, unsupported file type: %path.', ['path' => $path])
168178
);

app/code/Magento/MediaGalleryRenditions/Test/Integration/Model/GenerateRenditionsTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
55
*/
66

77
declare(strict_types=1);
@@ -145,6 +145,34 @@ private function copyImage(string $path): void
145145
);
146146
}
147147

148+
/**
149+
* Test getImageFileNamePattern method returns correct regex pattern
150+
*/
151+
public function testGetImageFileNamePattern(): void
152+
{
153+
$pattern = $this->generateRenditions->getImageFileNamePattern();
154+
// Assert the pattern is the expected string
155+
$this->assertEquals('#\.(jpg|jpeg|gif|png)$# i', $pattern);
156+
// Test that the pattern correctly validates supported file types
157+
$validExtensions = ['test.jpg', 'test.jpeg', 'test.gif', 'test.png', 'TEST.JPG', 'TEST.PNG'];
158+
foreach ($validExtensions as $filename) {
159+
$this->assertEquals(
160+
1,
161+
preg_match($pattern, $filename),
162+
"Pattern should match valid image file: $filename"
163+
);
164+
}
165+
// Test that the pattern correctly rejects unsupported file types
166+
$invalidExtensions = ['test.txt', 'test.pdf', 'test.webp', 'test.bmp', 'test'];
167+
foreach ($invalidExtensions as $filename) {
168+
$this->assertEquals(
169+
0,
170+
preg_match($pattern, $filename),
171+
"Pattern should not match invalid image file: $filename"
172+
);
173+
}
174+
}
175+
148176
/**
149177
* @return array
150178
*/

0 commit comments

Comments
 (0)