|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Wishlist\Test\Unit\Model; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Framework\Api\SearchCriteria; |
| 13 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 14 | +use Magento\Framework\Api\SearchResultsInterface; |
| 15 | +use Magento\Wishlist\Model\ResourceModel\Item\Collection; |
| 16 | +use Magento\Wishlist\Model\WishlistItemPermissionsCollectionProcessor; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +class WishlistItemPermissionsCollectionProcessorTest extends TestCase |
| 21 | +{ |
| 22 | + /** @var ProductRepositoryInterface|MockObject */ |
| 23 | + private $productRepository; |
| 24 | + |
| 25 | + /** @var SearchCriteriaBuilder|MockObject */ |
| 26 | + private $searchCriteriaBuilder; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var WishlistItemPermissionsCollectionProcessor |
| 30 | + */ |
| 31 | + private WishlistItemPermissionsCollectionProcessor $processor; |
| 32 | + |
| 33 | + /** |
| 34 | + * @inheritDoc |
| 35 | + */ |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + $this->productRepository = $this->createMock(ProductRepositoryInterface::class); |
| 39 | + $this->searchCriteriaBuilder = $this->createMock(SearchCriteriaBuilder::class); |
| 40 | + |
| 41 | + $this->processor = new WishlistItemPermissionsCollectionProcessor( |
| 42 | + $this->productRepository, |
| 43 | + $this->searchCriteriaBuilder |
| 44 | + ); |
| 45 | + parent::setUp(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return void |
| 50 | + * @throws \Exception |
| 51 | + */ |
| 52 | + public function testExecuteReturnsCollectionWhenEmpty(): void |
| 53 | + { |
| 54 | + $collection = $this->getMockBuilder(Collection::class) |
| 55 | + ->disableOriginalConstructor() |
| 56 | + ->onlyMethods(['getItems']) |
| 57 | + ->getMock(); |
| 58 | + |
| 59 | + $collection->expects($this->once()) |
| 60 | + ->method('getItems') |
| 61 | + ->willReturn([]); |
| 62 | + |
| 63 | + $this->productRepository->expects($this->never())->method('getList'); |
| 64 | + $this->searchCriteriaBuilder->expects($this->never())->method('addFilter'); |
| 65 | + |
| 66 | + $result = $this->processor->execute($collection); |
| 67 | + |
| 68 | + $this->assertSame($collection, $result); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return void |
| 73 | + * @throws \PHPUnit\Framework\MockObject\Exception |
| 74 | + */ |
| 75 | + public function testExecuteFiltersCollectionWithValidProducts(): void |
| 76 | + { |
| 77 | + $wishlistItem1 = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class) |
| 78 | + ->disableOriginalConstructor() |
| 79 | + ->addMethods(['getProductId']) |
| 80 | + ->getMock(); |
| 81 | + $wishlistItem1->method('getProductId')->willReturn(10); |
| 82 | + |
| 83 | + $wishlistItem2 = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class) |
| 84 | + ->disableOriginalConstructor() |
| 85 | + ->addMethods(['getProductId']) |
| 86 | + ->getMock(); |
| 87 | + $wishlistItem2->method('getProductId')->willReturn(11); |
| 88 | + |
| 89 | + $collection = $this->getMockBuilder(Collection::class) |
| 90 | + ->disableOriginalConstructor() |
| 91 | + ->onlyMethods(['getItems', 'getColumnValues', 'addFieldToFilter']) |
| 92 | + ->getMock(); |
| 93 | + |
| 94 | + /** @var Collection|MockObject $clonedCollection */ |
| 95 | + $clonedCollection = clone $collection; |
| 96 | + |
| 97 | + $collection->expects($this->once()) |
| 98 | + ->method('getItems') |
| 99 | + ->willReturn([$wishlistItem1, $wishlistItem2]); |
| 100 | + |
| 101 | + $collection->expects($this->once()) |
| 102 | + ->method('getColumnValues') |
| 103 | + ->with('product_id') |
| 104 | + ->willReturn([10, 11]); |
| 105 | + |
| 106 | + $clonedCollection->expects($this->once()) |
| 107 | + ->method('addFieldToFilter') |
| 108 | + ->with( |
| 109 | + 'main_table.product_id', |
| 110 | + ['in' => [10]] |
| 111 | + ) |
| 112 | + ->willReturnSelf(); |
| 113 | + |
| 114 | + $searchCriteria = $this->createMock(SearchCriteria::class); |
| 115 | + |
| 116 | + $this->searchCriteriaBuilder->expects($this->once()) |
| 117 | + ->method('addFilter') |
| 118 | + ->with('entity_id', [10, 11], 'in') |
| 119 | + ->willReturnSelf(); |
| 120 | + |
| 121 | + $this->searchCriteriaBuilder->expects($this->once()) |
| 122 | + ->method('create') |
| 123 | + ->willReturn($searchCriteria); |
| 124 | + |
| 125 | + $product10 = $this->getMockBuilder(ProductInterface::class) |
| 126 | + ->disableOriginalConstructor() |
| 127 | + ->addMethods(['getIsHidden']) |
| 128 | + ->getMockForAbstractClass(); |
| 129 | + $product10->method('getId')->willReturn(10); |
| 130 | + $product10->method('getIsHidden')->willReturn(false); |
| 131 | + |
| 132 | + $product11 = $this->getMockBuilder(ProductInterface::class) |
| 133 | + ->disableOriginalConstructor() |
| 134 | + ->addMethods(['getIsHidden']) |
| 135 | + ->getMockForAbstractClass(); |
| 136 | + $product11->method('getId')->willReturn(11); |
| 137 | + $product11->method('getIsHidden')->willReturn(true); |
| 138 | + |
| 139 | + $searchResults = $this->createMock(SearchResultsInterface::class); |
| 140 | + $searchResults->method('getItems')->willReturn([$product10, $product11]); |
| 141 | + |
| 142 | + $this->productRepository->expects($this->once()) |
| 143 | + ->method('getList') |
| 144 | + ->with($searchCriteria) |
| 145 | + ->willReturn($searchResults); |
| 146 | + |
| 147 | + $result = $this->processor->execute($collection); |
| 148 | + |
| 149 | + $this->assertNotSame($clonedCollection, $result); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @return void |
| 154 | + * @throws \PHPUnit\Framework\MockObject\Exception |
| 155 | + */ |
| 156 | + public function testExecuteUsesCacheOnSecondCall(): void |
| 157 | + { |
| 158 | + $wishlistItem1 = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class) |
| 159 | + ->disableOriginalConstructor() |
| 160 | + ->addMethods(['getProductId']) |
| 161 | + ->getMock(); |
| 162 | + $wishlistItem1->method('getProductId')->willReturn(10); |
| 163 | + |
| 164 | + $wishlistItem2 = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class) |
| 165 | + ->disableOriginalConstructor() |
| 166 | + ->addMethods(['getProductId']) |
| 167 | + ->getMock(); |
| 168 | + $wishlistItem2->method('getProductId')->willReturn(11); |
| 169 | + |
| 170 | + /** @var Collection|MockObject $collection1 */ |
| 171 | + $collection1 = $this->getMockBuilder(Collection::class) |
| 172 | + ->disableOriginalConstructor() |
| 173 | + ->onlyMethods(['getItems', 'getColumnValues', 'addFieldToFilter']) |
| 174 | + ->getMock(); |
| 175 | + |
| 176 | + /** @var Collection|MockObject $cloned1 */ |
| 177 | + $cloned1 = clone $collection1; |
| 178 | + |
| 179 | + $collection1->expects($this->once()) |
| 180 | + ->method('getItems') |
| 181 | + ->willReturn([$wishlistItem1, $wishlistItem2]); |
| 182 | + |
| 183 | + $collection1->expects($this->once()) |
| 184 | + ->method('getColumnValues') |
| 185 | + ->with('product_id') |
| 186 | + ->willReturn([21, 22]); |
| 187 | + |
| 188 | + $cloned1->expects($this->once()) |
| 189 | + ->method('addFieldToFilter') |
| 190 | + ->with('main_table.product_id', ['in' => [21, 22]]) |
| 191 | + ->willReturnSelf(); |
| 192 | + |
| 193 | + $searchCriteria = $this->createMock(SearchCriteria::class); |
| 194 | + |
| 195 | + $this->searchCriteriaBuilder->expects($this->once()) |
| 196 | + ->method('addFilter') |
| 197 | + ->with('entity_id', [21, 22], 'in') |
| 198 | + ->willReturnSelf(); |
| 199 | + |
| 200 | + $this->searchCriteriaBuilder->expects($this->once()) |
| 201 | + ->method('create') |
| 202 | + ->willReturn($searchCriteria); |
| 203 | + |
| 204 | + $product21 = $this->getMockBuilder(ProductInterface::class) |
| 205 | + ->disableOriginalConstructor() |
| 206 | + ->addMethods(['getIsHidden']) |
| 207 | + ->getMockForAbstractClass(); |
| 208 | + $product21->method('getId')->willReturn(21); |
| 209 | + $product21->method('getIsHidden')->willReturn(false); |
| 210 | + |
| 211 | + $product22 = $this->getMockBuilder(ProductInterface::class) |
| 212 | + ->disableOriginalConstructor() |
| 213 | + ->addMethods(['getIsHidden']) |
| 214 | + ->getMockForAbstractClass(); |
| 215 | + $product22->method('getId')->willReturn(22); |
| 216 | + $product22->method('getIsHidden')->willReturn(false); |
| 217 | + |
| 218 | + $searchResults = $this->createMock(SearchResultsInterface::class); |
| 219 | + $searchResults->method('getItems')->willReturn([$product21, $product22]); |
| 220 | + |
| 221 | + $this->productRepository->expects($this->once()) |
| 222 | + ->method('getList') |
| 223 | + ->with($searchCriteria) |
| 224 | + ->willReturn($searchResults); |
| 225 | + |
| 226 | + $this->processor->execute($collection1); |
| 227 | + |
| 228 | + $collection2 = $this->getMockBuilder(Collection::class) |
| 229 | + ->disableOriginalConstructor() |
| 230 | + ->onlyMethods(['getItems', 'getColumnValues', 'addFieldToFilter']) |
| 231 | + ->getMock(); |
| 232 | + |
| 233 | + /** @var Collection|MockObject $cloned2 */ |
| 234 | + $cloned2 = clone $collection2; |
| 235 | + |
| 236 | + $collection2->expects($this->once()) |
| 237 | + ->method('getItems') |
| 238 | + ->willReturn([$wishlistItem1, $wishlistItem2]); |
| 239 | + |
| 240 | + $collection2->expects($this->once()) |
| 241 | + ->method('getColumnValues') |
| 242 | + ->with('product_id') |
| 243 | + ->willReturn([21, 22]); |
| 244 | + |
| 245 | + $cloned2->expects($this->once()) |
| 246 | + ->method('addFieldToFilter') |
| 247 | + ->with('main_table.product_id', ['in' => [21, 22]]) |
| 248 | + ->willReturnSelf(); |
| 249 | + |
| 250 | + $this->processor->execute($collection2); |
| 251 | + } |
| 252 | +} |
0 commit comments