|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Test\Unit\Block\Adminhtml\Category\Helper; |
| 9 | + |
| 10 | +use Magento\Catalog\Block\Adminhtml\Category\Helper\Image; |
| 11 | +use Magento\Framework\UrlInterface; |
| 12 | +use Magento\Store\Model\Store; |
| 13 | +use Magento\Store\Model\StoreManagerInterface; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Unit test for Image helper |
| 19 | + * |
| 20 | + * @covers \Magento\Catalog\Block\Adminhtml\Category\Helper\Image |
| 21 | + */ |
| 22 | +class ImageTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var Image |
| 26 | + */ |
| 27 | + private Image $model; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var StoreManagerInterface|MockObject |
| 31 | + */ |
| 32 | + private $storeManagerMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var Store|MockObject |
| 36 | + */ |
| 37 | + private $storeMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @inheritdoc |
| 41 | + */ |
| 42 | + protected function setUp(): void |
| 43 | + { |
| 44 | + $this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class); |
| 45 | + $this->storeMock = $this->getMockBuilder(Store::class) |
| 46 | + ->disableOriginalConstructor() |
| 47 | + ->getMock(); |
| 48 | + |
| 49 | + // Using getMockBuilder to avoid parent constructor ObjectManager::getInstance() calls |
| 50 | + $this->model = $this->getMockBuilder(Image::class) |
| 51 | + ->disableOriginalConstructor() |
| 52 | + ->onlyMethods([]) |
| 53 | + ->getMock(); |
| 54 | + |
| 55 | + // Inject the storeManager dependency using reflection |
| 56 | + $reflection = new \ReflectionClass($this->model); |
| 57 | + $property = $reflection->getProperty('_storeManager'); |
| 58 | + $property->setAccessible(true); |
| 59 | + $property->setValue($this->model, $this->storeManagerMock); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Test _getUrl method returns false when no value is set |
| 64 | + * |
| 65 | + * @return void |
| 66 | + */ |
| 67 | + public function testGetUrlWithoutValue(): void |
| 68 | + { |
| 69 | + $this->model->setValue(null); |
| 70 | + |
| 71 | + $reflection = new \ReflectionClass($this->model); |
| 72 | + $method = $reflection->getMethod('_getUrl'); |
| 73 | + $method->setAccessible(true); |
| 74 | + |
| 75 | + $result = $method->invoke($this->model); |
| 76 | + |
| 77 | + $this->assertFalse($result); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Test _getUrl method returns correct URL when value is set |
| 82 | + * |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + public function testGetUrlWithValue(): void |
| 86 | + { |
| 87 | + $imageName = 'test_image.jpg'; |
| 88 | + $baseUrl = 'http://example.com/media/'; |
| 89 | + $expectedUrl = $baseUrl . 'catalog/category/' . $imageName; |
| 90 | + |
| 91 | + $this->model->setValue($imageName); |
| 92 | + |
| 93 | + $this->storeManagerMock->expects($this->once()) |
| 94 | + ->method('getStore') |
| 95 | + ->willReturn($this->storeMock); |
| 96 | + |
| 97 | + $this->storeMock->expects($this->once()) |
| 98 | + ->method('getBaseUrl') |
| 99 | + ->with(UrlInterface::URL_TYPE_MEDIA) |
| 100 | + ->willReturn($baseUrl); |
| 101 | + |
| 102 | + $reflection = new \ReflectionClass($this->model); |
| 103 | + $method = $reflection->getMethod('_getUrl'); |
| 104 | + $method->setAccessible(true); |
| 105 | + |
| 106 | + $result = $method->invoke($this->model); |
| 107 | + |
| 108 | + $this->assertEquals($expectedUrl, $result); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Test _getUrl method with different image names |
| 113 | + * |
| 114 | + * @dataProvider imageNameDataProvider |
| 115 | + * @param string $imageName |
| 116 | + * @return void |
| 117 | + */ |
| 118 | + public function testGetUrlWithDifferentImageNames(string $imageName): void |
| 119 | + { |
| 120 | + $baseUrl = 'http://example.com/media/'; |
| 121 | + $expectedUrl = $baseUrl . 'catalog/category/' . $imageName; |
| 122 | + |
| 123 | + $this->model->setValue($imageName); |
| 124 | + |
| 125 | + $this->storeManagerMock->expects($this->once()) |
| 126 | + ->method('getStore') |
| 127 | + ->willReturn($this->storeMock); |
| 128 | + |
| 129 | + $this->storeMock->expects($this->once()) |
| 130 | + ->method('getBaseUrl') |
| 131 | + ->with(UrlInterface::URL_TYPE_MEDIA) |
| 132 | + ->willReturn($baseUrl); |
| 133 | + |
| 134 | + $reflection = new \ReflectionClass($this->model); |
| 135 | + $method = $reflection->getMethod('_getUrl'); |
| 136 | + $method->setAccessible(true); |
| 137 | + |
| 138 | + $result = $method->invoke($this->model); |
| 139 | + |
| 140 | + $this->assertEquals($expectedUrl, $result); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Test _getUrl method with empty string value |
| 145 | + * |
| 146 | + * @return void |
| 147 | + */ |
| 148 | + public function testGetUrlWithEmptyString(): void |
| 149 | + { |
| 150 | + $this->model->setValue(''); |
| 151 | + |
| 152 | + $reflection = new \ReflectionClass($this->model); |
| 153 | + $method = $reflection->getMethod('_getUrl'); |
| 154 | + $method->setAccessible(true); |
| 155 | + |
| 156 | + $result = $method->invoke($this->model); |
| 157 | + |
| 158 | + $this->assertFalse($result); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Test that URL_TYPE_MEDIA constant has correct value |
| 163 | + * |
| 164 | + * @return void |
| 165 | + */ |
| 166 | + public function testUrlTypeMediaConstant(): void |
| 167 | + { |
| 168 | + // Verify the constant value is 'media' as expected by the implementation |
| 169 | + $this->assertEquals('media', UrlInterface::URL_TYPE_MEDIA); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Data provider for image names |
| 174 | + * |
| 175 | + * @return array |
| 176 | + */ |
| 177 | + public static function imageNameDataProvider(): array |
| 178 | + { |
| 179 | + return [ |
| 180 | + 'jpg image' => ['category_image.jpg'], |
| 181 | + 'png image' => ['category_image.png'], |
| 182 | + 'gif image' => ['category_image.gif'], |
| 183 | + 'image with path' => ['subfolder/category_image.jpg'], |
| 184 | + 'image with special chars' => ['category-image_01.jpg'] |
| 185 | + ]; |
| 186 | + } |
| 187 | +} |
0 commit comments