Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion app/code/Magento/Catalog/Block/Product/AbstractProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class AbstractProduct extends \Magento\Framework\View\Element\Template
protected $_cartHelper;

/**
* @var \Magento\Wishlist\Helper\Data
* @var \Magento\Catalog\Helper\Wishlist\AddToWishlistInterface
*/
protected $_wishlistHelper;

Expand Down Expand Up @@ -182,6 +182,18 @@ public function getAddToWishlistParams($product)
return $this->_wishlistHelper->getAddParams($product);
}

/**
* Check whether customers can add products to their wish list
*
* Always false when Magento_Wishlist is not installed.
*
* @return bool
*/
public function isAddToWishlistAllowed()
{
return (bool)$this->_wishlistHelper->isAllow();
}

/**
* Retrieve Add Product to Compare Products List URL
*
Expand Down
9 changes: 5 additions & 4 deletions app/code/Magento/Catalog/Block/Product/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Catalog\Block\Product;

use Magento\Catalog\Helper\Wishlist\AddToWishlistInterface;
use Magento\Framework\Cache\LockGuardedCacheLoader;

/**
Expand Down Expand Up @@ -35,7 +36,7 @@ class Context extends \Magento\Framework\View\Element\Template\Context
protected $compareProduct;

/**
* @var \Magento\Wishlist\Helper\Data
* @var AddToWishlistInterface
*/
protected $wishlistHelper;

Expand Down Expand Up @@ -121,7 +122,7 @@ class Context extends \Magento\Framework\View\Element\Template\Context
* @param \Magento\Catalog\Helper\Data $catalogHelper
* @param \Magento\Framework\Math\Random $mathRandom
* @param \Magento\Checkout\Helper\Cart $cartHelper
* @param \Magento\Wishlist\Helper\Data $wishlistHelper
* @param AddToWishlistInterface $wishlistHelper
* @param \Magento\Catalog\Helper\Product\Compare $compareProduct
* @param \Magento\Catalog\Helper\Image $imageHelper
* @param ImageBuilder $imageBuilder
Expand Down Expand Up @@ -163,7 +164,7 @@ public function __construct(
\Magento\Catalog\Helper\Data $catalogHelper,
\Magento\Framework\Math\Random $mathRandom,
\Magento\Checkout\Helper\Cart $cartHelper,
\Magento\Wishlist\Helper\Data $wishlistHelper,
AddToWishlistInterface $wishlistHelper,
\Magento\Catalog\Helper\Product\Compare $compareProduct,
\Magento\Catalog\Helper\Image $imageHelper,
\Magento\Catalog\Block\Product\ImageBuilder $imageBuilder,
Expand Down Expand Up @@ -316,7 +317,7 @@ public function getTaxData()
/**
* Get wishlist helper.
*
* @return \Magento\Wishlist\Helper\Data
* @return AddToWishlistInterface
*/
public function getWishlistHelper()
{
Expand Down
7 changes: 4 additions & 3 deletions app/code/Magento/Catalog/Helper/Product/Compare.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Catalog\Helper\Product;

use Magento\Catalog\Helper\Wishlist\AddToWishlistInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection;

Expand Down Expand Up @@ -88,7 +89,7 @@ class Compare extends \Magento\Framework\Url\Helper\Data
protected $_formKey;

/**
* @var \Magento\Wishlist\Helper\Data
* @var AddToWishlistInterface
*/
protected $_wishlistHelper;

Expand All @@ -111,7 +112,7 @@ class Compare extends \Magento\Framework\Url\Helper\Data
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Catalog\Model\Session $catalogSession
* @param \Magento\Framework\Data\Form\FormKey $formKey
* @param \Magento\Wishlist\Helper\Data $wishlistHelper
* @param AddToWishlistInterface $wishlistHelper
* @param \Magento\Framework\Data\Helper\PostHelper $postHelper
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -124,7 +125,7 @@ public function __construct(
\Magento\Customer\Model\Session $customerSession,
\Magento\Catalog\Model\Session $catalogSession,
\Magento\Framework\Data\Form\FormKey $formKey,
\Magento\Wishlist\Helper\Data $wishlistHelper,
AddToWishlistInterface $wishlistHelper,
\Magento\Framework\Data\Helper\PostHelper $postHelper
) {
$this->_itemCollectionFactory = $itemCollectionFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright © Mage-OS. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Helper\Wishlist;

/**
* The "add to wish list" operations that Catalog blocks and templates use.
*
* Magento_Catalog binds this to a null implementation. Magento_Wishlist
* binds it to Magento\Wishlist\Helper\Data, so with Wishlist installed
* callers receive the wishlist helper itself.
*
* @api
*/
interface AddToWishlistInterface
{
/**
* Check whether customers can add products to their wish list.
*
* @return bool
*/
public function isAllow();

/**
* Retrieve the post data for adding a product to the wish list.
*
* @param \Magento\Catalog\Model\Product|\Magento\Framework\DataObject $item
* @param array $params
* @return string
*/
public function getAddParams($item, array $params = []);
}
30 changes: 30 additions & 0 deletions app/code/Magento/Catalog/Helper/Wishlist/NullAddToWishlist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Copyright © Mage-OS. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Helper\Wishlist;

/**
* Used when Magento_Wishlist is not installed: adding to the wish list is never allowed.
*/
class NullAddToWishlist implements AddToWishlistInterface
{
/**
* @inheritdoc
*/
public function isAllow()
{
return false;
}

/**
* @inheritdoc
*/
public function getAddParams($item, array $params = [])
{
return '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Magento\Catalog\Block\Product\Image;
use Magento\Catalog\Block\Product\ImageBuilder;
use Magento\Catalog\Block\Product\View\Type\Simple;
use Magento\Catalog\Helper\Wishlist\AddToWishlistInterface;
use Magento\Catalog\Model\Product;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Api\StockRegistryInterface;
Expand Down Expand Up @@ -56,6 +57,11 @@ class AbstractProductTest extends TestCase
*/
protected $imageBuilder;

/**
* @var AddToWishlistInterface|MockObject
*/
private $addToWishlistMock;

/**
* Set up mocks and tested class
* Child class is used as the tested class is declared abstract
Expand All @@ -64,8 +70,11 @@ protected function setUp(): void
{
$this->productContextMock = $this->createPartialMock(
Context::class,
['getLayout', 'getStockRegistry', 'getImageBuilder']
['getLayout', 'getStockRegistry', 'getImageBuilder', 'getWishlistHelper']
);
$this->addToWishlistMock = $this->createMock(AddToWishlistInterface::class);
$this->productContextMock->method('getWishlistHelper')
->willReturn($this->addToWishlistMock);
$arrayUtilsMock = $this->createMock(ArrayUtils::class);
$this->layoutMock = $this->createPartialMock(Layout::class, ['getBlock']);
$this->stockRegistryMock = $this->createMock(StockRegistryInterface::class);
Expand All @@ -88,6 +97,50 @@ protected function setUp(): void
);
}

/**
* Adding to the wish list is allowed when the wish list implementation allows it
*
* @return void
*/
public function testIsAddToWishlistAllowed(): void
{
$this->addToWishlistMock->expects($this->once())
->method('isAllow')
->willReturn(true);

$this->assertTrue($this->block->isAddToWishlistAllowed());
}

/**
* Adding to the wish list is not allowed when the wish list implementation refuses it
*
* @return void
*/
public function testIsAddToWishlistNotAllowed(): void
{
$this->addToWishlistMock->expects($this->once())
->method('isAllow')
->willReturn(false);

$this->assertFalse($this->block->isAddToWishlistAllowed());
}

/**
* The add to wish list post data comes from the wish list implementation
*
* @return void
*/
public function testGetAddToWishlistParams(): void
{
$product = $this->createMock(Product::class);
$this->addToWishlistMock->expects($this->once())
->method('getAddParams')
->with($product)
->willReturn('{"action":"wishlist\\/index\\/add"}');

$this->assertSame('{"action":"wishlist\\/index\\/add"}', $this->block->getAddToWishlistParams($product));
}

/**
* Test for method getProductPrice
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Mage-OS. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Test\Unit\Helper\Wishlist;

use Magento\Catalog\Helper\Wishlist\NullAddToWishlist;
use Magento\Catalog\Model\Product;
use PHPUnit\Framework\TestCase;

class NullAddToWishlistTest extends TestCase
{
/**
* Without Magento_Wishlist, adding to the wish list is never allowed
*
* @return void
*/
public function testAddingToWishlistIsNeverAllowed(): void
{
$nullAddToWishlist = new NullAddToWishlist();

$this->assertFalse($nullAddToWishlist->isAllow());
$this->assertSame('', $nullAddToWishlist->getAddParams($this->createStub(Product::class), ['foo' => 'bar']));
}
}
1 change: 0 additions & 1 deletion app/code/Magento/Catalog/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"magento/module-ui": "*",
"magento/module-url-rewrite": "*",
"magento/module-widget": "*",
"magento/module-wishlist": "*",
"magento/module-aws-s3": "*"
},
"suggest": {
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Catalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Api\Data\ProductInterface" type="Magento\Catalog\Model\Product" />
<preference for="Magento\Catalog\Api\ProductRepositoryInterface" type="Magento\Catalog\Model\ProductRepository" />
<preference for="Magento\Catalog\Helper\Wishlist\AddToWishlistInterface" type="Magento\Catalog\Helper\Wishlist\NullAddToWishlist" />
<preference for="Magento\Catalog\Api\CategoryAttributeRepositoryInterface" type="Magento\Catalog\Model\Category\AttributeRepository" />
<preference for="Magento\Catalog\Api\Data\CategoryAttributeInterface" type="Magento\Catalog\Model\Category\Attribute" />
<preference for="Magento\Catalog\Api\CategoryAttributeOptionManagementInterface" type="Magento\Catalog\Model\Category\Attribute\OptionManagement" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<?php endif; ?>
<?php endif; ?>
</div>
<?php if ($this->helper(Magento\Wishlist\Helper\Data::class)->isAllow()) :?>
<?php if ($block->isAddToWishlistAllowed()):?>
<div class="secondary-addto-links actions-secondary" data-role="add-to-links">
<a href="#"
data-post='<?= /* @noEscape */ $block->getAddToWishlistParams($item) ?>'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@ $_helper = $this->helper(Magento\Catalog\Helper\Output::class);
'<div class="stock unavailable"><span>' . $block->escapeHtml(__('Out of stock')) . '</span></div>';
}

$info['links'] = '<div class="product links" data-role="add-to-links">'
. '<a href="#" data-post=\'' . $this->helper(Magento\Wishlist\Helper\Data::class)->getAddParams($_product) . '\' class="action towishlist" data-action="add-to-wishlist">'
. '<span>' . $block->escapeHtml(__('Add to Wish List')) . '</span></a>'
. '<a href="' . $block->escapeUrl($block->getAddToCompareUrl($_product)) . '" class="action tocompare">'
$info['links'] = '<div class="product links" data-role="add-to-links">';
if ($block->isAddToWishlistAllowed()) {
$info['links'] .= '<a href="#" data-post=\''
. $block->getAddToWishlistParams($_product)
. '\' class="action towishlist" data-action="add-to-wishlist">'
. '<span>' . $block->escapeHtml(__('Add to Wish List')) . '</span></a>';
}
$info['links'] .= '<a href="'
. $block->escapeUrl($block->getAddToCompareUrl($_product))
. '" class="action tocompare">'
. '<span>' . $block->escapeHtml(__('Add to Compare')) . '</span></a></div>';
$info['actions'] = '<div class="product action">' . $info['button'] . $info['links'] . '</div>';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ if ($exist = ($block->getProductCollection() && $block->getProductCollection()->
<?php endif; ?>
<?php if ($showWishlist || $showCompare) :?>
<div class="actions-secondary" data-role="add-to-links">
<?php if ($this->helper(Magento\Wishlist\Helper\Data::class)->isAllow() && $showWishlist) :?>
<?php if ($block->isAddToWishlistAllowed() && $showWishlist):?>
<a href="#"
data-post='<?= /* @noEscape */ $block->getAddToWishlistParams($_item) ?>'
class="action towishlist"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ if ($exist = ($block->getProductCollection() && $block->getProductCollection()->
<?php endif; ?>
<?php if ($showWishlist || $showCompare) :?>
<div class="actions-secondary" data-role="add-to-links">
<?php if ($this->helper(Magento\Wishlist\Helper\Data::class)->isAllow() && $showWishlist) :?>
<?php if ($block->isAddToWishlistAllowed() && $showWishlist):?>
<a href="#"
data-post='<?= /* @noEscape */ $block->getAddToWishlistParams($_item) ?>'
class="action towishlist" data-action="add-to-wishlist"
Expand Down
1 change: 0 additions & 1 deletion app/code/Magento/CatalogWidget/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"magento/module-rule": "*",
"magento/module-store": "*",
"magento/module-widget": "*",
"magento/module-wishlist": "*",
"magento/module-theme": "*"
},
"type": "magento2-module",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use Magento\Catalog\Helper\Product\Compare;
use Magento\CatalogWidget\Block\Product\ProductsList;
use Magento\Framework\App\Action\Action;
use Magento\Framework\Escaper;
use Magento\Wishlist\Helper\Data;

/** @var Escaper $escaper */
/** @var ProductsList $block */
Expand Down Expand Up @@ -111,7 +110,7 @@ use Magento\Wishlist\Helper\Data;
<?php endif; ?>
<?php if ($showWishlist || $showCompare): ?>
<div class="actions-secondary" data-role="add-to-links">
<?php if ($this->helper(Data::class)->isAllow() && $showWishlist): ?>
<?php if ($block->isAddToWishlistAllowed() && $showWishlist): ?>
<a href="#"
data-post='<?= /* @noEscape */ $block->getAddToWishlistParams($_item) ?>' class="action towishlist" data-action="add-to-wishlist" title="<?= $escaper->escapeHtmlAttr(__('Add to Wish List')) ?>">
<span><?= $escaper->escapeHtml(__('Add to Wish List')) ?></span>
Expand Down
Loading
Loading