|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Block\Product; |
| 9 | + |
| 10 | +use Magento\Framework\View\LayoutInterface; |
| 11 | +use Magento\TestFramework\Helper\Bootstrap; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * Verifies escaping strategy for custom attributes in product image templates. |
| 16 | + * |
| 17 | + * @magentoAppArea frontend |
| 18 | + */ |
| 19 | +class ImageTemplateEscapingTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Ensure custom attribute values are escaped using escapeHtml in image_with_borders.phtml |
| 23 | + */ |
| 24 | + public function testCustomAttributeEscapingInImageWithBordersTemplate(): void |
| 25 | + { |
| 26 | + $layout = Bootstrap::getObjectManager()->get(LayoutInterface::class); |
| 27 | + |
| 28 | + $valueNeedingHtmlEscape = 'http://example.test/media/x.jpg'; |
| 29 | + |
| 30 | + $block = $layout->createBlock( |
| 31 | + Image::class, |
| 32 | + 'test_image_with_borders_escape', |
| 33 | + [ |
| 34 | + 'data' => [ |
| 35 | + 'template' => 'Magento_Catalog::product/image_with_borders.phtml', |
| 36 | + 'image_url' => 'http://example.test/media/x.jpg', |
| 37 | + 'width' => 100, |
| 38 | + 'height' => 80, |
| 39 | + 'label' => 'Test', |
| 40 | + 'ratio' => 0.8, |
| 41 | + 'custom_attributes' => [ |
| 42 | + 'data-src' => $valueNeedingHtmlEscape, |
| 43 | + ], |
| 44 | + 'class' => 'product-image-photo', |
| 45 | + 'product_id' => 123, |
| 46 | + ], |
| 47 | + ] |
| 48 | + ); |
| 49 | + |
| 50 | + $html = $block->toHtml(); |
| 51 | + |
| 52 | + $expectedEscaped = htmlspecialchars($valueNeedingHtmlEscape, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false); |
| 53 | + $this->assertStringContainsString('data-src="' . $expectedEscaped . '"', $html); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Ensure custom attribute values are escaped using escapeHtml in image.phtml |
| 58 | + */ |
| 59 | + public function testCustomAttributeEscapingInDeprecatedImageTemplate(): void |
| 60 | + { |
| 61 | + $layout = Bootstrap::getObjectManager()->get(LayoutInterface::class); |
| 62 | + |
| 63 | + $valueNeedingHtmlEscape = 'http://example.test/media/x.jpg'; |
| 64 | + |
| 65 | + $block = $layout->createBlock( |
| 66 | + Image::class, |
| 67 | + 'test_image_escape_deprecated', |
| 68 | + [ |
| 69 | + 'data' => [ |
| 70 | + 'template' => 'Magento_Catalog::product/image.phtml', |
| 71 | + 'image_url' => 'http://example.test/media/y.jpg', |
| 72 | + 'width' => 120, |
| 73 | + 'height' => 90, |
| 74 | + 'label' => 'Test', |
| 75 | + 'ratio' => 0.75, |
| 76 | + 'custom_attributes' => [ |
| 77 | + 'data-src' => $valueNeedingHtmlEscape, |
| 78 | + ], |
| 79 | + 'class' => 'photo image', |
| 80 | + 'product_id' => 456, |
| 81 | + ], |
| 82 | + ] |
| 83 | + ); |
| 84 | + |
| 85 | + $html = $block->toHtml(); |
| 86 | + |
| 87 | + $expectedEscaped = htmlspecialchars($valueNeedingHtmlEscape, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', false); |
| 88 | + $this->assertStringContainsString('data-src="' . $expectedEscaped . '"', $html); |
| 89 | + } |
| 90 | +} |
0 commit comments