Skip to content

Commit 4be53e3

Browse files
committed
AC-15323: Wrong method called for Add To Compare button
Added Integration test coverage
1 parent 3ae7f68 commit 4be53e3

File tree

2 files changed

+218
-15
lines changed
  • app/code/Magento/Catalog/Ui/DataProvider/Product/Listing/Collector
  • dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Listing/Collector

2 files changed

+218
-15
lines changed

app/code/Magento/Catalog/Ui/DataProvider/Product/Listing/Collector/Url.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,6 @@
2020
*/
2121
class Url implements ProductRenderCollectorInterface
2222
{
23-
/** Compare Data key */
24-
const KEY_COMPARE_URL_POST_DATA = "compare_url_post_data";
25-
26-
/** Add to cart url key post data */
27-
const KEY_ADD_TO_CART_URL_POST_DATA = "add_to_cart_url_post_data";
28-
29-
/** Add to cart url key */
30-
const KEY_ADD_TO_CART_URL = "add_to_cart_url";
31-
32-
/** Product Url */
33-
const KEY_URL = "url";
34-
35-
/** Has Required options key */
36-
const KEY_HAS_REQUIRED_OPTIONS = "has_required_options";
37-
3823
/**
3924
* @var AbstractProduct
4025
*/
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Ui\DataProvider\Product\Listing\Collector;
9+
10+
use Magento\Catalog\Api\Data\ProductRender\ButtonInterface;
11+
use Magento\Catalog\Api\Data\ProductRender\ButtonInterfaceFactory;
12+
use Magento\Catalog\Api\Data\ProductRenderInterface;
13+
use Magento\Catalog\Model\ProductRender;
14+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
15+
use Magento\Catalog\Test\Fixture\ProductStock as ProductStockFixture;
16+
use Magento\Framework\ObjectManagerInterface;
17+
use Magento\TestFramework\Fixture\DataFixture;
18+
use Magento\TestFramework\Fixture\DataFixtureStorage;
19+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
20+
use Magento\TestFramework\Helper\Bootstrap;
21+
use PHPUnit\Framework\TestCase;
22+
23+
/**
24+
* Integration test for Url collector
25+
*/
26+
class UrlTest extends TestCase
27+
{
28+
/**
29+
* @var ObjectManagerInterface
30+
*/
31+
private $objectManager;
32+
33+
/**
34+
* @var Url
35+
*/
36+
private $urlCollector;
37+
38+
/**
39+
* @var ButtonInterfaceFactory
40+
*/
41+
private $buttonFactory;
42+
43+
/**
44+
* @var DataFixtureStorage
45+
*/
46+
private $fixtures;
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
protected function setUp(): void
52+
{
53+
$this->objectManager = Bootstrap::getObjectManager();
54+
$this->urlCollector = $this->objectManager->get(Url::class);
55+
$this->buttonFactory = $this->objectManager->get(ButtonInterfaceFactory::class);
56+
$this->fixtures = DataFixtureStorageManager::getStorage();
57+
}
58+
59+
/**
60+
* Test URL collector with simple product
61+
*/
62+
#[
63+
DataFixture(ProductFixture::class, as: 'product'),
64+
DataFixture(ProductStockFixture::class, ['prod_id' => '$product.id$']),
65+
]
66+
public function testCollectWithNullCompareButton()
67+
{
68+
$product = $this->fixtures->get('product');
69+
$productRender = $this->createProductRenderWithNullButtons();
70+
$this->urlCollector->collect($product, $productRender);
71+
72+
// Verify buttons were created and configured
73+
$addToCartButton = $productRender->getAddToCartButton();
74+
$addToCompareButton = $productRender->getAddToCompareButton();
75+
76+
$this->assertInstanceOf(ButtonInterface::class, $addToCartButton);
77+
$this->assertInstanceOf(ButtonInterface::class, $addToCompareButton);
78+
79+
// Verify add-to-cart button configuration
80+
$this->assertNotEmpty($addToCartButton->getPostData());
81+
$this->assertNotEmpty($addToCartButton->getUrl());
82+
$this->assertIsString($addToCartButton->getUrl());
83+
84+
// Verify add-to-compare button configuration
85+
$compareUrl = $addToCompareButton->getUrl();
86+
$this->assertNotEmpty($compareUrl);
87+
88+
// Verify product URL is set
89+
$this->assertNotEmpty($productRender->getUrl());
90+
$this->assertIsString($productRender->getUrl());
91+
}
92+
93+
/**
94+
* Test URL collector when ProductRender already has compare button
95+
*/
96+
#[
97+
DataFixture(ProductFixture::class, as: 'product'),
98+
DataFixture(ProductStockFixture::class, ['prod_id' => '$product.id$']),
99+
]
100+
public function testCollectWithExistingAddToCompareButton()
101+
{
102+
$product = $this->fixtures->get('product');
103+
$productRender = $this->createProductRenderWithExistingCompareButton();
104+
$originalCompareButton = $productRender->getAddToCompareButton();
105+
$this->urlCollector->collect($product, $productRender);
106+
107+
// Verify the same compare button instance is used (line 82 behavior)
108+
$this->assertSame($originalCompareButton, $productRender->getAddToCompareButton());
109+
110+
// Verify new cart button was created (since it was null)
111+
$addToCartButton = $productRender->getAddToCartButton();
112+
$this->assertInstanceOf(ButtonInterface::class, $addToCartButton);
113+
$this->assertNotEmpty($addToCartButton->getUrl());
114+
115+
// Verify compare button was properly configured
116+
$this->assertNotEmpty($originalCompareButton->getUrl());
117+
}
118+
119+
/**
120+
* Test URL collector integration with real Compare helper
121+
*/
122+
#[
123+
DataFixture(ProductFixture::class, as: 'product'),
124+
DataFixture(ProductStockFixture::class, ['prod_id' => '$product.id$']),
125+
]
126+
public function testCollectIntegrationWithCompareHelper()
127+
{
128+
$product = $this->fixtures->get('product');
129+
$productRender = $this->createProductRenderWithNullButtons();
130+
$this->urlCollector->collect($product, $productRender);
131+
132+
// Verify compare URL contains expected parameters
133+
$addToCompareButton = $productRender->getAddToCompareButton();
134+
$compareUrl = $addToCompareButton->getUrl();
135+
136+
// The compare helper returns JSON formatted data
137+
$this->assertIsString($compareUrl);
138+
$this->assertNotEmpty($compareUrl);
139+
140+
// Decode and verify structure
141+
$compareData = json_decode($compareUrl, true);
142+
$this->assertIsArray($compareData);
143+
$this->assertArrayHasKey('action', $compareData);
144+
$this->assertArrayHasKey('data', $compareData);
145+
$this->assertStringContainsString('catalog/product_compare/add', $compareData['action']);
146+
}
147+
148+
/**
149+
* Test URL collector with both buttons existing
150+
*/
151+
#[
152+
DataFixture(ProductFixture::class, as: 'product'),
153+
DataFixture(ProductStockFixture::class, ['prod_id' => '$product.id$']),
154+
]
155+
public function testCollectWithBothButtonsExisting()
156+
{
157+
$product = $this->fixtures->get('product');
158+
$productRender = $this->createProductRenderWithBothButtons();
159+
$originalCartButton = $productRender->getAddToCartButton();
160+
$originalCompareButton = $productRender->getAddToCompareButton();
161+
$this->urlCollector->collect($product, $productRender);
162+
163+
// Verify the same button instances are used (no new buttons created)
164+
$this->assertSame($originalCartButton, $productRender->getAddToCartButton());
165+
$this->assertSame($originalCompareButton, $productRender->getAddToCompareButton());
166+
167+
// Verify buttons were properly configured
168+
$this->assertNotEmpty($originalCartButton->getUrl());
169+
$this->assertNotEmpty($originalCompareButton->getUrl());
170+
}
171+
172+
/**
173+
* Create ProductRender object with null buttons
174+
*
175+
* @return ProductRenderInterface
176+
*/
177+
private function createProductRenderWithNullButtons(): ProductRenderInterface
178+
{
179+
return $this->objectManager->create(ProductRender::class);
180+
}
181+
182+
/**
183+
* Create ProductRender object with existing compare button
184+
*
185+
* @return ProductRenderInterface
186+
*/
187+
private function createProductRenderWithExistingCompareButton(): ProductRenderInterface
188+
{
189+
$productRender = $this->objectManager->create(ProductRender::class);
190+
191+
// Create an existing compare button
192+
$existingCompareButton = $this->buttonFactory->create();
193+
194+
// Only set compare button, leave cart button as null (not set)
195+
$productRender->setAddToCompareButton($existingCompareButton);
196+
197+
return $productRender;
198+
}
199+
200+
/**
201+
* Create ProductRender object with both buttons existing
202+
*
203+
* @return ProductRenderInterface
204+
*/
205+
private function createProductRenderWithBothButtons(): ProductRenderInterface
206+
{
207+
$productRender = $this->objectManager->create(ProductRender::class);
208+
209+
// Create both buttons to test existing button scenario
210+
$existingCartButton = $this->buttonFactory->create();
211+
$existingCompareButton = $this->buttonFactory->create();
212+
213+
$productRender->setAddToCartButton($existingCartButton);
214+
$productRender->setAddToCompareButton($existingCompareButton);
215+
216+
return $productRender;
217+
}
218+
}

0 commit comments

Comments
 (0)