From 09eb72d09c821834d785dd71b14c063044261063 Mon Sep 17 00:00:00 2001 From: KrasnoshchokBohdan Date: Thu, 26 Jun 2025 09:44:37 +0300 Subject: [PATCH 1/4] magento/magento2#39905 Product Removed on Mobile Still Appears in Web's Mini Compare Section Until Re-login Integrates the Product Compare helper across compare list resolvers to trigger updates after adding, removing, or creating compare lists. Also forces a reload of compare product data on the frontend to maintain accuracy. --- .../view/frontend/web/js/view/compare-products.js | 4 ++++ .../Model/Resolver/AddProductsToCompareList.php | 14 +++++++++++++- .../Model/Resolver/CreateCompareList.php | 14 +++++++++++++- .../Resolver/RemoveProductsFromCompareList.php | 14 +++++++++++++- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Catalog/view/frontend/web/js/view/compare-products.js b/app/code/Magento/Catalog/view/frontend/web/js/view/compare-products.js index 8e54742921ef7..7e63ac0fd81cc 100644 --- a/app/code/Magento/Catalog/view/frontend/web/js/view/compare-products.js +++ b/app/code/Magento/Catalog/view/frontend/web/js/view/compare-products.js @@ -46,6 +46,10 @@ define([ customerData.reload(['compare-products'], false); compareProductsReloaded = true; } + + // Force reload on page load + customerData.reload(['compare-products'], false); + initSidebar(); } }); diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php index 92c2bc1ab0551..f3ef6a0dccf08 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php @@ -7,10 +7,12 @@ namespace Magento\CompareListGraphQl\Model\Resolver; +use Magento\Catalog\Helper\Product\Compare; use Magento\Catalog\Model\MaskedListIdToCompareListId; use Magento\CompareListGraphQl\Model\Service\AddToCompareList; use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId; use Magento\CompareListGraphQl\Model\Service\GetCompareList; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; @@ -44,22 +46,31 @@ class AddProductsToCompareList implements ResolverInterface */ private $getListIdByCustomerId; + /** + * @var Compare + */ + private mixed $productCompareHelper; + /** * @param AddToCompareList $addProductToCompareList * @param GetCompareList $getCompareList * @param MaskedListIdToCompareListId $maskedListIdToCompareListId * @param GetListIdByCustomerId $getListIdByCustomerId + * @param Compare|null $productCompareHelper */ public function __construct( AddToCompareList $addProductToCompareList, GetCompareList $getCompareList, MaskedListIdToCompareListId $maskedListIdToCompareListId, - GetListIdByCustomerId $getListIdByCustomerId + GetListIdByCustomerId $getListIdByCustomerId, + ?Compare $productCompareHelper = null ) { $this->addProductToCompareList = $addProductToCompareList; $this->getCompareList = $getCompareList; $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; $this->getListIdByCustomerId = $getListIdByCustomerId; + $this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance() + ->get(Compare::class); } /** @@ -104,6 +115,7 @@ public function resolve( try { $this->addProductToCompareList->execute($listId, $args['input']['products'], $context); + $this->productCompareHelper->calculate(); } catch (\Exception $exception) { throw new GraphQlInputException(__($exception->getMessage())); } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php index 546bd3ee77096..1d50e46e5b505 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php @@ -7,10 +7,12 @@ namespace Magento\CompareListGraphQl\Model\Resolver; +use Magento\Catalog\Helper\Product\Compare; use Magento\CompareListGraphQl\Model\Service\AddToCompareList; use Magento\CompareListGraphQl\Model\Service\CreateCompareList as CreateCompareListService; use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId; use Magento\CompareListGraphQl\Model\Service\GetCompareList; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; @@ -50,25 +52,34 @@ class CreateCompareList implements ResolverInterface */ private $createCompareList; + /** + * @var Compare + */ + private mixed $productCompareHelper; + /** * @param Random $mathRandom * @param GetListIdByCustomerId $getListIdByCustomerId * @param AddToCompareList $addProductToCompareList * @param GetCompareList $getCompareList * @param CreateCompareListService $createCompareList + * @param Compare|null $productCompareHelper */ public function __construct( Random $mathRandom, GetListIdByCustomerId $getListIdByCustomerId, AddToCompareList $addProductToCompareList, GetCompareList $getCompareList, - CreateCompareListService $createCompareList + CreateCompareListService $createCompareList, + ?Compare $productCompareHelper = null ) { $this->mathRandom = $mathRandom; $this->getListIdByCustomerId = $getListIdByCustomerId; $this->addProductToCompareList = $addProductToCompareList; $this->getCompareList = $getCompareList; $this->createCompareList = $createCompareList; + $this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance() + ->get(Compare::class); } /** @@ -111,6 +122,7 @@ public function resolve( } else { $listId = $this->createCompareList->execute($generatedListId, $customerId); $this->addProductToCompareList->execute($listId, $products, $context); + $this->productCompareHelper->calculate(); } } } catch (LocalizedException $exception) { diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php index fcfde3408ae44..82c9d233cd9f0 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php @@ -7,10 +7,12 @@ namespace Magento\CompareListGraphQl\Model\Resolver; +use Magento\Catalog\Helper\Product\Compare; use Magento\Catalog\Model\MaskedListIdToCompareListId; use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId; use Magento\CompareListGraphQl\Model\Service\GetCompareList; use Magento\CompareListGraphQl\Model\Service\RemoveFromCompareList; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; @@ -44,22 +46,31 @@ class RemoveProductsFromCompareList implements ResolverInterface */ private $getListIdByCustomerId; + /** + * @var Compare + */ + private mixed $productCompareHelper; + /** * @param GetCompareList $getCompareList * @param RemoveFromCompareList $removeFromCompareList * @param MaskedListIdToCompareListId $maskedListIdToCompareListId * @param GetListIdByCustomerId $getListIdByCustomerId + * @param Compare|null $productCompareHelper */ public function __construct( GetCompareList $getCompareList, RemoveFromCompareList $removeFromCompareList, MaskedListIdToCompareListId $maskedListIdToCompareListId, - GetListIdByCustomerId $getListIdByCustomerId + GetListIdByCustomerId $getListIdByCustomerId, + ?Compare $productCompareHelper = null ) { $this->getCompareList = $getCompareList; $this->removeFromCompareList = $removeFromCompareList; $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; $this->getListIdByCustomerId = $getListIdByCustomerId; + $this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance() + ->get(Compare::class); } /** @@ -111,6 +122,7 @@ public function resolve( try { $this->removeFromCompareList->execute($listId, $args['input']['products']); + $this->productCompareHelper->calculate(); } catch (LocalizedException $exception) { throw new GraphQlInputException( __('Something was wrong during removing products from compare list') From 707be0de3955eed3e188e2e8779eed3adbe84491 Mon Sep 17 00:00:00 2001 From: KrasnoshchokBohdan Date: Fri, 27 Jun 2025 12:26:42 +0300 Subject: [PATCH 2/4] magento/magento2#39905 Product Removed on Mobile Still Appears in Web's Mini Compare Section Until Re-login Introduced CompareCookieManager to manage cookie invalidation for compare list products. Updated resolvers to use this service and ensure cookies are marked invalid after product compare list operations. Removed redundant client-side cookie reload logic in the frontend. --- .../frontend/web/js/view/compare-products.js | 4 - .../Resolver/AddProductsToCompareList.php | 17 ++- .../Model/Resolver/CreateCompareList.php | 16 ++- .../RemoveProductsFromCompareList.php | 17 ++- .../Model/Service/CompareCookieManager.php | 104 ++++++++++++++++++ 5 files changed, 145 insertions(+), 13 deletions(-) create mode 100644 app/code/Magento/CompareListGraphQl/Model/Service/CompareCookieManager.php diff --git a/app/code/Magento/Catalog/view/frontend/web/js/view/compare-products.js b/app/code/Magento/Catalog/view/frontend/web/js/view/compare-products.js index 7e63ac0fd81cc..8e54742921ef7 100644 --- a/app/code/Magento/Catalog/view/frontend/web/js/view/compare-products.js +++ b/app/code/Magento/Catalog/view/frontend/web/js/view/compare-products.js @@ -46,10 +46,6 @@ define([ customerData.reload(['compare-products'], false); compareProductsReloaded = true; } - - // Force reload on page load - customerData.reload(['compare-products'], false); - initSidebar(); } }); diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php index f3ef6a0dccf08..7f39bcd107697 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php @@ -1,12 +1,13 @@ addProductToCompareList = $addProductToCompareList; $this->getCompareList = $getCompareList; @@ -71,6 +79,8 @@ public function __construct( $this->getListIdByCustomerId = $getListIdByCustomerId; $this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance() ->get(Compare::class); + $this->compareCookieManager = $compareCookieManager ?: ObjectManager::getInstance() + ->get(CompareCookieManager::class); } /** @@ -116,6 +126,7 @@ public function resolve( try { $this->addProductToCompareList->execute($listId, $args['input']['products'], $context); $this->productCompareHelper->calculate(); + $this->compareCookieManager->invalidate(); } catch (\Exception $exception) { throw new GraphQlInputException(__($exception->getMessage())); } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php index 1d50e46e5b505..ea330ddd38d90 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php @@ -1,12 +1,13 @@ mathRandom = $mathRandom; $this->getListIdByCustomerId = $getListIdByCustomerId; @@ -80,6 +87,8 @@ public function __construct( $this->createCompareList = $createCompareList; $this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance() ->get(Compare::class); + $this->compareCookieManager = $compareCookieManager ?: ObjectManager::getInstance() + ->get(CompareCookieManager::class); } /** @@ -123,6 +132,7 @@ public function resolve( $listId = $this->createCompareList->execute($generatedListId, $customerId); $this->addProductToCompareList->execute($listId, $products, $context); $this->productCompareHelper->calculate(); + $this->compareCookieManager->invalidate(); } } } catch (LocalizedException $exception) { diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php index 82c9d233cd9f0..84fc2b2fd6689 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php @@ -1,12 +1,13 @@ getCompareList = $getCompareList; $this->removeFromCompareList = $removeFromCompareList; @@ -71,6 +79,8 @@ public function __construct( $this->getListIdByCustomerId = $getListIdByCustomerId; $this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance() ->get(Compare::class); + $this->compareCookieManager = $compareCookieManager ?: ObjectManager::getInstance() + ->get(CompareCookieManager::class); } /** @@ -123,6 +133,7 @@ public function resolve( try { $this->removeFromCompareList->execute($listId, $args['input']['products']); $this->productCompareHelper->calculate(); + $this->compareCookieManager->invalidate(); } catch (LocalizedException $exception) { throw new GraphQlInputException( __('Something was wrong during removing products from compare list') diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/CompareCookieManager.php b/app/code/Magento/CompareListGraphQl/Model/Service/CompareCookieManager.php new file mode 100644 index 0000000000000..ac37ec2c449ef --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Service/CompareCookieManager.php @@ -0,0 +1,104 @@ +cookieManager = $cookieManager; + $this->cookieMetadataFactory = $cookieMetadataFactory; + $this->logger = $logger; + } + + /** + * Marks compare products section as invalid by updating the cookie value + * + * @return void + */ + public function invalidate(): void + { + try { + $cookieValue = json_encode(['compare-products' => time()]); + $this->setCookie($cookieValue); + } catch (\Exception $e) { + $this->logger->error('Error invalidating compare products cookie: ' . $e->getMessage()); + } + } + + /** + * Set compare products cookie + * + * @param string $value + * @return void + * @throws InputException + * @throws CookieSizeLimitReachedException + * @throws FailureToSendException + */ + private function setCookie(string $value): void + { + $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata() + ->setDuration(self::COOKIE_LIFETIME) + ->setPath(self::COOKIE_PATH) + ->setHttpOnly(false); + + $this->cookieManager->setPublicCookie( + self::COOKIE_COMPARE_PRODUCTS, + $value, + $publicCookieMetadata + ); + } +} From c14fa3fee099462e45cb76b066d3fce484b56f02 Mon Sep 17 00:00:00 2001 From: KrasnoshchokBohdan Date: Sat, 28 Jun 2025 19:21:27 +0300 Subject: [PATCH 3/4] magento/magento2#39905 Product Removed on Mobile Still Appears in Web's Mini Compare Section Until Re-login Introduced `CompareCookieManager` enhancements with better cookie management through visibility modifiers and documentation changes. Suppressed warnings for PHPMD where relevant and added comprehensive unit tests for cookie handling, including edge cases like size limits and failures. --- .../Resolver/AddProductsToCompareList.php | 3 +- .../Model/Resolver/CreateCompareList.php | 3 + .../RemoveProductsFromCompareList.php | 2 + .../Model/Service/CompareCookieManager.php | 16 +- .../Service/CompareCookieManagerTest.php | 233 ++++++++++++++++++ 5 files changed, 249 insertions(+), 8 deletions(-) create mode 100644 app/code/Magento/CompareListGraphQl/Test/Unit/Model/Service/CompareCookieManagerTest.php diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php index 7f39bcd107697..b1d3baced670a 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php @@ -24,6 +24,8 @@ /** * Add products item to compare list + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class AddProductsToCompareList implements ResolverInterface { @@ -118,7 +120,6 @@ public function resolve( throw new GraphQlInputException(__($exception->getMessage())); } - if (!$listId) { throw new GraphQlInputException(__('"uid" value does not exist')); } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php index ea330ddd38d90..271cdd4e4083c 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php @@ -25,6 +25,8 @@ /** * Class for creating compare list + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class CreateCompareList implements ResolverInterface { @@ -70,6 +72,7 @@ class CreateCompareList implements ResolverInterface * @param GetCompareList $getCompareList * @param CreateCompareListService $createCompareList * @param Compare|null $productCompareHelper + * @param CompareCookieManager|null $compareCookieManager */ public function __construct( Random $mathRandom, diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php index 84fc2b2fd6689..bfb3e13f439cc 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php @@ -24,6 +24,8 @@ /** * Remove items from compare list + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class RemoveProductsFromCompareList implements ResolverInterface { diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/CompareCookieManager.php b/app/code/Magento/CompareListGraphQl/Model/Service/CompareCookieManager.php index ac37ec2c449ef..ef68682912ed0 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/CompareCookieManager.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/CompareCookieManager.php @@ -16,23 +16,25 @@ /** * Service for managing compare list cookies + * + * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) */ class CompareCookieManager { /** * Name of cookie that holds compare products section data */ - const COOKIE_COMPARE_PRODUCTS = 'section_data_ids'; + public const COOKIE_COMPARE_PRODUCTS = 'section_data_ids'; /** - * Cookie path + * The path for which the cookie will be available */ - const COOKIE_PATH = '/'; + public const COOKIE_PATH = '/'; /** - * Cookie lifetime value + * Cookie lifetime value in seconds (86400 = 24 hours) */ - const COOKIE_LIFETIME = 86400; + public const COOKIE_LIFETIME = 86400; /** * @var CookieManagerInterface @@ -56,8 +58,8 @@ class CompareCookieManager */ public function __construct( CookieManagerInterface $cookieManager, - CookieMetadataFactory $cookieMetadataFactory, - LoggerInterface $logger + CookieMetadataFactory $cookieMetadataFactory, + LoggerInterface $logger ) { $this->cookieManager = $cookieManager; $this->cookieMetadataFactory = $cookieMetadataFactory; diff --git a/app/code/Magento/CompareListGraphQl/Test/Unit/Model/Service/CompareCookieManagerTest.php b/app/code/Magento/CompareListGraphQl/Test/Unit/Model/Service/CompareCookieManagerTest.php new file mode 100644 index 0000000000000..487706462f04a --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Test/Unit/Model/Service/CompareCookieManagerTest.php @@ -0,0 +1,233 @@ +cookieManagerMock = $this->getMockForAbstractClass(CookieManagerInterface::class); + $this->cookieMetadataFactoryMock = $this->createMock(CookieMetadataFactory::class); + $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); + $this->publicCookieMetadataMock = $this->createMock(PublicCookieMetadata::class); + + $this->compareCookieManager = new CompareCookieManager( + $this->cookieManagerMock, + $this->cookieMetadataFactoryMock, + $this->loggerMock + ); + } + + /** + * Test invalidate method successfully sets cookie + * + * @return void + */ + public function testInvalidateSuccess(): void + { + $cookieValue = json_encode(['compare-products' => time()]); + + $this->cookieMetadataFactoryMock->expects($this->once()) + ->method('createPublicCookieMetadata') + ->willReturn($this->publicCookieMetadataMock); + + $this->publicCookieMetadataMock->expects($this->once()) + ->method('setDuration') + ->with(CompareCookieManager::COOKIE_LIFETIME) + ->willReturnSelf(); + + $this->publicCookieMetadataMock->expects($this->once()) + ->method('setPath') + ->with(CompareCookieManager::COOKIE_PATH) + ->willReturnSelf(); + + $this->publicCookieMetadataMock->expects($this->once()) + ->method('setHttpOnly') + ->with(false) + ->willReturnSelf(); + + $this->cookieManagerMock->expects($this->once()) + ->method('setPublicCookie') + ->with( + CompareCookieManager::COOKIE_COMPARE_PRODUCTS, + $this->callback(function ($value) { + $decodedValue = json_decode($value, true); + return isset($decodedValue['compare-products']) && is_int($decodedValue['compare-products']); + }), + $this->publicCookieMetadataMock + ); + + $this->compareCookieManager->invalidate(); + } + + /** + * Test invalidate method logs exception when cookie setting fails + * + * @return void + */ + public function testInvalidateWithException(): void + { + $exception = new InputException(__('Error setting cookie')); + + $this->cookieMetadataFactoryMock->expects($this->once()) + ->method('createPublicCookieMetadata') + ->willReturn($this->publicCookieMetadataMock); + + $this->publicCookieMetadataMock->expects($this->once()) + ->method('setDuration') + ->willReturnSelf(); + + $this->publicCookieMetadataMock->expects($this->once()) + ->method('setPath') + ->willReturnSelf(); + + $this->publicCookieMetadataMock->expects($this->once()) + ->method('setHttpOnly') + ->willReturnSelf(); + + $this->cookieManagerMock->expects($this->once()) + ->method('setPublicCookie') + ->willThrowException($exception); + + $this->loggerMock->expects($this->once()) + ->method('error') + ->with($this->stringContains('Error invalidating compare products cookie')); + + $this->compareCookieManager->invalidate(); + } + + /** + * Test cookie creation with CookieSizeLimitReachedException + * + * @return void + */ + public function testInvalidateWithCookieSizeLimitReachedException(): void + { + $exception = new CookieSizeLimitReachedException(__('Cookie size limit reached')); + + $this->cookieMetadataFactoryMock->expects($this->once()) + ->method('createPublicCookieMetadata') + ->willReturn($this->publicCookieMetadataMock); + + $this->publicCookieMetadataMock->expects($this->once()) + ->method('setDuration') + ->willReturnSelf(); + + $this->publicCookieMetadataMock->expects($this->once()) + ->method('setPath') + ->willReturnSelf(); + + $this->publicCookieMetadataMock->expects($this->once()) + ->method('setHttpOnly') + ->willReturnSelf(); + + $this->cookieManagerMock->expects($this->once()) + ->method('setPublicCookie') + ->willThrowException($exception); + + $this->loggerMock->expects($this->once()) + ->method('error') + ->with($this->stringContains('Error invalidating compare products cookie')); + + $this->compareCookieManager->invalidate(); + } + + /** + * Test cookie creation with FailureToSendException + * + * @return void + */ + public function testInvalidateWithFailureToSendException(): void + { + $exception = new FailureToSendException(__('Failed to send cookie')); + + $this->cookieMetadataFactoryMock->expects($this->once()) + ->method('createPublicCookieMetadata') + ->willReturn($this->publicCookieMetadataMock); + + $this->publicCookieMetadataMock->expects($this->once()) + ->method('setDuration') + ->willReturnSelf(); + + $this->publicCookieMetadataMock->expects($this->once()) + ->method('setPath') + ->willReturnSelf(); + + $this->publicCookieMetadataMock->expects($this->once()) + ->method('setHttpOnly') + ->willReturnSelf(); + + $this->cookieManagerMock->expects($this->once()) + ->method('setPublicCookie') + ->willThrowException($exception); + + $this->loggerMock->expects($this->once()) + ->method('error') + ->with($this->stringContains('Error invalidating compare products cookie')); + + $this->compareCookieManager->invalidate(); + } + + /** + * Test that constants have the expected values + * + * @return void + */ + public function testConstants(): void + { + $this->assertEquals('section_data_ids', CompareCookieManager::COOKIE_COMPARE_PRODUCTS); + $this->assertEquals('/', CompareCookieManager::COOKIE_PATH); + $this->assertEquals(86400, CompareCookieManager::COOKIE_LIFETIME); + } +} From 3220e13e2abd346b24fa4a07a1926e3eb2ba00b1 Mon Sep 17 00:00:00 2001 From: KrasnoshchokBohdan Date: Mon, 30 Jun 2025 09:44:55 +0300 Subject: [PATCH 4/4] magento/magento2#39905 Product Removed on Mobile Still Appears in Web's Mini Compare Section Until Re-login - Static tests fix --- .../Test/Unit/Model/Service/CompareCookieManagerTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/code/Magento/CompareListGraphQl/Test/Unit/Model/Service/CompareCookieManagerTest.php b/app/code/Magento/CompareListGraphQl/Test/Unit/Model/Service/CompareCookieManagerTest.php index 487706462f04a..2bb067a8c5c51 100644 --- a/app/code/Magento/CompareListGraphQl/Test/Unit/Model/Service/CompareCookieManagerTest.php +++ b/app/code/Magento/CompareListGraphQl/Test/Unit/Model/Service/CompareCookieManagerTest.php @@ -76,8 +76,6 @@ protected function setUp(): void */ public function testInvalidateSuccess(): void { - $cookieValue = json_encode(['compare-products' => time()]); - $this->cookieMetadataFactoryMock->expects($this->once()) ->method('createPublicCookieMetadata') ->willReturn($this->publicCookieMetadataMock);