Skip to content
Open
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
1 change: 0 additions & 1 deletion app/code/Magento/Wishlist/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ public function getItemCount()
);
if (!$this->_customerSession->hasWishlistItemCount() ||
$currentDisplayType != $storedDisplayType ||
$this->_customerSession->hasDisplayOutOfStockProducts() ||
$currentDisplayOutOfStockProducts != $storedDisplayOutOfStockProducts
) {
$this->calculate();
Expand Down
102 changes: 102 additions & 0 deletions app/code/Magento/Wishlist/Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use Magento\Catalog\Model\Product;
use Magento\Customer\Model\Session;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\Data\Helper\PostHelper;
use Magento\Framework\DataObject;
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\Url\EncoderInterface;
Expand All @@ -31,6 +33,7 @@

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields)
*/
class DataTest extends TestCase
{
Expand Down Expand Up @@ -78,6 +81,12 @@ class DataTest extends TestCase
/** @var Session|MockObject */
protected $customerSession;

/** @var ScopeConfigInterface|MockObject */
protected $scopeConfig;

/** @var EventManagerInterface|MockObject */
protected $eventManager;

/**
* Set up mock objects for tested class
*
Expand Down Expand Up @@ -112,6 +121,16 @@ protected function setUp(): void
->method('getRequest')
->willReturn($this->requestMock);

$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
$this->context->expects($this->once())
->method('getScopeConfig')
->willReturn($this->scopeConfig);

$this->eventManager = $this->createMock(EventManagerInterface::class);
$this->context->expects($this->once())
->method('getEventManager')
->willReturn($this->eventManager);

$this->wishlistProvider = $this->createMock(WishlistProviderInterface::class);

$this->coreRegistry = $this->createMock(Registry::class);
Expand Down Expand Up @@ -143,6 +162,89 @@ protected function setUp(): void
);
}

/**
* getItemCount() must not recalculate when nothing relevant changed since the last call,
* even though hasDisplayOutOfStockProducts() only reports whether the value was ever set.
*
* @return void
*/
public function testGetItemCountUsesCachedValueWhenNothingChanged()
{
$this->scopeConfig->method('getValue')
->willReturnMap(
[
['wishlist/wishlist_link/use_qty', 'store', false],
['cataloginventory/options/show_out_of_stock', 'store', true],
]
);

$session = $this->createPartialMockWithReflection(
Session::class,
[
'getWishlistDisplayType',
'getDisplayOutOfStockProducts',
'hasWishlistItemCount',
'hasDisplayOutOfStockProducts',
'getWishlistItemCount',
'isLoggedIn',
'setWishlistItemCount',
]
);
$session->method('getWishlistDisplayType')->willReturn(false);
$session->method('getDisplayOutOfStockProducts')->willReturn(true);
$session->method('hasWishlistItemCount')->willReturn(true);
$session->method('hasDisplayOutOfStockProducts')->willReturn(true);
$session->method('getWishlistItemCount')->willReturn(3);
$session->expects($this->never())->method('isLoggedIn');
$session->expects($this->never())->method('setWishlistItemCount');

$this->setPropertyValue($this->model, '_customerSession', $session);

$this->assertEquals(3, $this->model->getItemCount());
}

/**
* getItemCount() must still recalculate when the out-of-stock display setting changed,
* proving the remaining value comparison alone (without the removed existence check)
* correctly detects this case.
*
* @return void
*/
public function testGetItemCountRecalculatesWhenOutOfStockDisplaySettingChanged()
{
$this->scopeConfig->method('getValue')
->willReturnMap(
[
['wishlist/wishlist_link/use_qty', 'store', false],
['cataloginventory/options/show_out_of_stock', 'store', true],
]
);

$session = $this->createPartialMockWithReflection(
Session::class,
[
'getWishlistDisplayType',
'getDisplayOutOfStockProducts',
'hasWishlistItemCount',
'hasDisplayOutOfStockProducts',
'getWishlistItemCount',
'isLoggedIn',
'setWishlistItemCount',
]
);
$session->method('getWishlistDisplayType')->willReturn(false);
$session->method('getDisplayOutOfStockProducts')->willReturn(false);
$session->method('hasWishlistItemCount')->willReturn(true);
$session->method('hasDisplayOutOfStockProducts')->willReturn(false);
$session->method('getWishlistItemCount')->willReturn(5);
$session->expects($this->once())->method('isLoggedIn')->willReturn(false);
$session->expects($this->once())->method('setWishlistItemCount')->with(0);

$this->setPropertyValue($this->model, '_customerSession', $session);

$this->assertEquals(5, $this->model->getItemCount());
}

public function testGetAddToCartUrl()
{
$url = 'http://magento.com/wishlist/index/index/wishlist_id/1/?___store=default';
Expand Down