|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\InventoryCatalog\Test\Unit\Plugin\Catalog\Ui\Component\Listing\Columns; |
| 9 | + |
| 10 | +use Magento\Catalog\Helper\Image; |
| 11 | +use Magento\Catalog\Ui\Component\Listing\Columns\Thumbnail; |
| 12 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 13 | +use Magento\Framework\DataObject; |
| 14 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 15 | +use Magento\Framework\UrlInterface; |
| 16 | +use Magento\Framework\View\Element\UiComponent\ContextInterface; |
| 17 | +use Magento\InventoryCatalog\Plugin\Catalog\Ui\Component\Listing\Columns\ThumbnailPlugin; |
| 18 | +use Magento\Store\Api\Data\WebsiteInterface; |
| 19 | +use Magento\Store\Api\StoreWebsiteRelationInterface; |
| 20 | +use Magento\Store\Model\ScopeInterface; |
| 21 | +use Magento\Store\Model\Store; |
| 22 | +use Magento\Store\Model\StoreManagerInterface; |
| 23 | +use Magento\Store\Model\Website; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | + |
| 26 | +/** |
| 27 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 28 | + */ |
| 29 | +class ThumbnailPluginTest extends TestCase |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @var ThumbnailPlugin |
| 33 | + */ |
| 34 | + private $plugin; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var ContextInterface |
| 38 | + */ |
| 39 | + private $context; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var Image |
| 43 | + */ |
| 44 | + private $imageHelper; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var UrlInterface |
| 48 | + */ |
| 49 | + private $urlBuilder; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var StoreManagerInterface |
| 53 | + */ |
| 54 | + private $storeManager; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var ScopeConfigInterface |
| 58 | + */ |
| 59 | + private $scopeConfig; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var StoreWebsiteRelationInterface |
| 63 | + */ |
| 64 | + private $storeWebsiteRelation; |
| 65 | + |
| 66 | + protected function setUp(): void |
| 67 | + { |
| 68 | + $this->context = $this->getMockBuilder(ContextInterface::class) |
| 69 | + ->getMockForAbstractClass(); |
| 70 | + $this->imageHelper = $this->getMockBuilder(Image::class) |
| 71 | + ->disableOriginalConstructor() |
| 72 | + ->getMock(); |
| 73 | + $this->urlBuilder = $this->getMockBuilder(UrlInterface::class) |
| 74 | + ->getMockForAbstractClass(); |
| 75 | + $this->storeManager = $this->getMockBuilder(StoreManagerInterface::class) |
| 76 | + ->getMockForAbstractClass(); |
| 77 | + $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class) |
| 78 | + ->getMockForAbstractClass(); |
| 79 | + $this->storeWebsiteRelation = $this->getMockBuilder(StoreWebsiteRelationInterface::class) |
| 80 | + ->getMockForAbstractClass(); |
| 81 | + |
| 82 | + $this->plugin = new ThumbnailPlugin( |
| 83 | + $this->context, |
| 84 | + $this->imageHelper, |
| 85 | + $this->urlBuilder, |
| 86 | + $this->storeManager, |
| 87 | + $this->scopeConfig, |
| 88 | + $this->storeWebsiteRelation |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test a thumbnail placeholder image for the admin catalog products grid in a multi-website scenario. |
| 94 | + * |
| 95 | + * @return void |
| 96 | + * @throws NoSuchEntityException |
| 97 | + */ |
| 98 | + public function testAroundPrepareDataSource(): void |
| 99 | + { |
| 100 | + $storeId = 2; |
| 101 | + $fieldName = 'thumbnail'; |
| 102 | + $dataSource = [ |
| 103 | + 'data' => [ |
| 104 | + 'totalRecords' => 1, |
| 105 | + 'items' => [ |
| 106 | + [ |
| 107 | + 'entity_id' => '1', |
| 108 | + 'attribute_set_id' => '4', |
| 109 | + 'type_id' => 'simple', |
| 110 | + 'sku' => 'P1', |
| 111 | + 'status' => '1', |
| 112 | + 'websites' => [ |
| 113 | + '2' |
| 114 | + ], |
| 115 | + 'website_ids' => [ |
| 116 | + '2' |
| 117 | + ] |
| 118 | + ], |
| 119 | + ], |
| 120 | + 'showTotalRecords' => true |
| 121 | + ] |
| 122 | + ]; |
| 123 | + $subject = $this->createMock(Thumbnail::class); |
| 124 | + $proceed = function () use ($dataSource) { |
| 125 | + return $dataSource; |
| 126 | + }; |
| 127 | + |
| 128 | + $websiteIds = [1, 2]; |
| 129 | + $websites = []; |
| 130 | + foreach ($websiteIds as $websiteId) { |
| 131 | + $websites[] = $this->createConfiguredMock( |
| 132 | + Website::class, |
| 133 | + [ |
| 134 | + 'getId' => $websiteId |
| 135 | + ] |
| 136 | + ); |
| 137 | + } |
| 138 | + $this->storeWebsiteRelation->expects($this->atLeastOnce()) |
| 139 | + ->method('getStoreByWebsiteId') |
| 140 | + ->withConsecutive([1], [2]) |
| 141 | + ->willReturnOnConsecutiveCalls([1], [2, 3]); |
| 142 | + |
| 143 | + $this->storeManager->expects($this->exactly(2)) |
| 144 | + ->method('getWebsites') |
| 145 | + ->willReturn($websites); |
| 146 | + |
| 147 | + $product = new DataObject($dataSource['data']['items'][0]); |
| 148 | + |
| 149 | + $subject->expects($this->atLeastOnce())->method('getData')->willReturn($fieldName); |
| 150 | + |
| 151 | + $this->scopeConfig->expects($this->atLeastOnce())->method('getValue') |
| 152 | + ->with("catalog/placeholder/{$fieldName}_placeholder", ScopeInterface::SCOPE_STORE, $storeId) |
| 153 | + ->willReturn("stores/{$storeId}/test.jpg"); |
| 154 | + |
| 155 | + $storeMock = $this->createMock(Store::class); |
| 156 | + $storeMock->method('getId')->willReturn($storeId); |
| 157 | + $this->storeManager->expects($this->atLeastOnce())->method('setCurrentStore')->with($storeId); |
| 158 | + |
| 159 | + $this->imageHelper->expects($this->atLeastOnce())->method('init') |
| 160 | + ->withConsecutive([$product, 'product_listing_thumbnail'], [$product, 'product_listing_thumbnail_preview']) |
| 161 | + ->willReturnOnConsecutiveCalls($this->imageHelper, $this->imageHelper); |
| 162 | + $this->imageHelper->expects($this->atLeastOnce())->method('getUrl') |
| 163 | + ->willReturnMap([["http://magento.local/media/catalog/product/placeholder/stores/{$storeId}/test.jpg"]]); |
| 164 | + $this->imageHelper->expects($this->atLeastOnce())->method('getLabel')->willReturn('Image Label'); |
| 165 | + |
| 166 | + $this->context->expects($this->atLeastOnce())->method('getRequestParam')->with('store')->willReturn(null); |
| 167 | + $this->urlBuilder->expects($this->atLeastOnce()) |
| 168 | + ->method('getUrl') |
| 169 | + ->with('catalog/product/edit', ['id' => $product['entity_id'], 'store' => null]) |
| 170 | + ->willReturn("http://magento.local/admin/catalog/product/edit/id/{$product['entity_id']}/"); |
| 171 | + |
| 172 | + $preparedDataSource = $this->plugin->aroundPrepareDataSource($subject, $proceed, $dataSource); |
| 173 | + |
| 174 | + foreach ($preparedDataSource['data']['items'] as $item) { |
| 175 | + $this->assertEquals( |
| 176 | + "http://magento.local/media/catalog/product/placeholder/stores/{$storeId}/test.jpg", |
| 177 | + $item['thumbnail_src'] |
| 178 | + ); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Test for product datasource in single (default) website scenario |
| 184 | + * |
| 185 | + * @return void |
| 186 | + * @throws NoSuchEntityException |
| 187 | + */ |
| 188 | + public function testAroundPrepareDataSourceForDefaultWebsiteOnly(): void |
| 189 | + { |
| 190 | + $dataSource = [ |
| 191 | + 'data' => [ |
| 192 | + 'totalRecords' => 1, |
| 193 | + 'items' => [ |
| 194 | + [ |
| 195 | + 'entity_id' => '1', |
| 196 | + 'attribute_set_id' => '4', |
| 197 | + 'type_id' => 'simple', |
| 198 | + 'sku' => 'P1', |
| 199 | + 'status' => '1', |
| 200 | + 'websites' => [ |
| 201 | + '1' |
| 202 | + ], |
| 203 | + 'website_ids' => [ |
| 204 | + '1' |
| 205 | + ] |
| 206 | + ] |
| 207 | + ], |
| 208 | + 'showTotalRecords' => true |
| 209 | + ] |
| 210 | + ]; |
| 211 | + $subject = $this->createMock(Thumbnail::class); |
| 212 | + $proceed = function () use ($dataSource) { |
| 213 | + return $dataSource; |
| 214 | + }; |
| 215 | + |
| 216 | + $websiteMock = $this->getMockForAbstractClass(WebsiteInterface::class); |
| 217 | + $this->storeManager->expects($this->once()) |
| 218 | + ->method('getWebsites') |
| 219 | + ->willReturn([$websiteMock]); |
| 220 | + $this->assertEquals($dataSource, $this->plugin->aroundPrepareDataSource($subject, $proceed, $dataSource)); |
| 221 | + } |
| 222 | +} |
0 commit comments