Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow wishlist share when all items are out of stock #26185

Merged
Merged
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
18 changes: 11 additions & 7 deletions app/code/Magento/Wishlist/Controller/Index/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Magento\Framework\Controller\ResultFactory;

/**
* Class Update
* Controller for updating wishlists
*/
class Update extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPostActionInterface
{
Expand Down Expand Up @@ -70,7 +70,12 @@ public function execute()
}

$post = $this->getRequest()->getPostValue();
if ($post && isset($post['description']) && is_array($post['description'])) {
$resultRedirect->setPath('*', ['wishlist_id' => $wishlist->getId()]);
if (!$post) {
return $resultRedirect;
}

if (isset($post['description']) && is_array($post['description'])) {
$updatedItems = 0;

foreach ($post['description'] as $itemId => $description) {
Expand Down Expand Up @@ -136,13 +141,12 @@ public function execute()
$this->messageManager->addErrorMessage(__('Can\'t update wish list'));
}
}
}

if (isset($post['save_and_share'])) {
$resultRedirect->setPath('*/*/share', ['wishlist_id' => $wishlist->getId()]);
return $resultRedirect;
}
if (isset($post['save_and_share'])) {
$resultRedirect->setPath('*/*/share', ['wishlist_id' => $wishlist->getId()]);
}
$resultRedirect->setPath('*', ['wishlist_id' => $wishlist->getId()]);

return $resultRedirect;
}
}
290 changes: 290 additions & 0 deletions app/code/Magento/Wishlist/Test/Unit/Controller/Index/UpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Wishlist\Test\Unit\Controller\Index;

use Magento\Backend\Model\View\Result\Redirect;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Wishlist\Controller\Index\Update;
use Magento\Wishlist\Controller\WishlistProviderInterface;
use Magento\Wishlist\Helper\Data;
use Magento\Wishlist\Model\Item;
use Magento\Wishlist\Model\LocaleQuantityProcessor;
use Magento\Wishlist\Model\Wishlist;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
* Test for upate controller wishlist
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class UpdateTest extends TestCase
{
private const STUB_ITEM_ID = 1;

private const STUB_WISHLIST_PRODUCT_QTY = 21;

/**
* @var MockObject|Validator $formKeyValidatorMock
*/
private $formKeyValidatorMock;

/**
* @var MockObject|WishlistProviderInterface $wishlistProviderMock
*/
private $wishlistProviderMock;

/**
* @var MockObject|LocaleQuantityProcessor $quantityProcessorMock
*/
private $quantityProcessorMock;

/**
* @var Update $updateController
*/
private $updateController;

/**
* @var MockObject|Context$contextMock
*/
private $contextMock;

/**
* @var MockObject|Redirect $resultRedirectMock
*/
private $resultRedirectMock;

/**
* @var MockObject|ResultFactory $resultFatoryMock
*/
private $resultFactoryMock;

/**
* @var MockObject|RequestInterface $requestMock
*/
private $requestMock;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


/**
* @var MockObject|ObjectManagerInterface $objectManagerMock
*/
private $objectManagerMock;

/**
* @var MockObject|ManagerInterface $messageManagerMock
*/
private $messageManagerMock;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->formKeyValidatorMock = $this->createMock(Validator::class);
$this->wishlistProviderMock = $this->createMock(WishlistProviderInterface::class);
$this->quantityProcessorMock = $this->createMock(LocaleQuantityProcessor::class);
$this->contextMock = $this->createMock(Context::class);
$this->resultRedirectMock = $this->createMock(Redirect::class);
$this->resultFactoryMock = $this->createPartialMock(ResultFactory::class, ['create']);
$this->messageManagerMock = $this->createMock(ManagerInterface::class);
$this->objectManagerMock = $this->createMock(ObjectManagerInterface::class);
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
->setMethods(['getPostValue'])
->getMockForAbstractClass();

$this->resultFactoryMock->expects($this->any())
->method('create')
->willReturn($this->resultRedirectMock);
$this->contextMock->expects($this->once())
->method('getResultFactory')
->willReturn($this->resultFactoryMock);
$this->contextMock->expects($this->once())
->method('getObjectManager')
->willReturn($this->objectManagerMock);
$this->contextMock->expects($this->any())
->method('getRequest')
->willReturn($this->requestMock);
$this->contextMock->expects($this->any())
->method('getMessageManager')
->willReturn($this->messageManagerMock);

$objectManager = new ObjectManagerHelper($this);

$this->updateController = $objectManager->getObject(
Update::class,
[
'context' => $this->contextMock,
'_formKeyValidator' => $this->formKeyValidatorMock,
'wishlistProvider' => $this->wishlistProviderMock,
'quantityProcessor' => $this->quantityProcessorMock
]
);
}

/**
* Test for update method Wishlist controller.
*
* @dataProvider getWishlistDataProvider
* @param array $wishlistDataProvider
* @param array $postData
* @return void
*/
public function testUpdate(array $wishlistDataProvider, array $postData): void
{
$wishlist = $this->createMock(Wishlist::class);
$itemMock = $this->getMockBuilder(Item::class)
->disableOriginalConstructor()
->setMethods(
[
'load',
'getId',
'getWishlistId',
'setQty',
'save',
'getDescription',
'setDescription',
'getProduct',
'getName'
]
)->getMock();
$dataMock = $this->createMock(Data::class);
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->getMock();

$this->formKeyValidatorMock->expects($this->once())
->method('validate')
->with($this->requestMock)
->willReturn(true);
$this->wishlistProviderMock->expects($this->once())
->method('getWishlist')
->willReturn($wishlist);
$wishlist->expects($this->exactly(2))
->method('getId')
->willReturn($wishlistDataProvider['id']);
$this->requestMock->expects($this->once())
->method('getPostValue')
->willReturn($postData);
$this->resultRedirectMock->expects($this->once())
->method('setPath')
->with('*', ['wishlist_id' => $wishlistDataProvider['id']]);
$this->objectManagerMock->expects($this->once())
->method('create')
->with(Item::class)
->willReturn($itemMock);
$itemMock->expects($this->once())
->method('load')
->with(1)
->willReturnSelf();
$itemMock->expects($this->once())
->method('getWishLIstId')
->willReturn($wishlistDataProvider['id']);
$itemMock->expects($this->once())
->method('getDescription')
->willReturn('');
$itemMock->expects($this->once())
->method('setDescription')
->willReturnSelf();
$itemMock->expects($this->once())
->method('setQty')
->willReturnSelf();
$this->objectManagerMock->expects($this->exactly(2))
->method('get')
->with(Data::class)
->willReturn($dataMock);
$dataMock->expects($this->once())
->method('defaultCommentString')
->willReturn('');
$dataMock->expects($this->once())
->method('calculate');
$this->quantityProcessorMock->expects($this->once())
->method('process')
->willReturn($postData['qty']);
$itemMock->expects($this->once())
->method('getProduct')
->willReturn($productMock);
$productMock->expects($this->once())
->method('getName')
->willReturn('product');
$this->messageManagerMock->expects($this->once())
->method('addSuccessMessage');

$this->assertEquals($this->resultRedirectMock, $this->updateController->execute());
}

/**
* Verify update method if post data not available
*
* @dataProvider getWishlistDataProvider
* @param array $wishlistDataProvider
* @return void
*/
public function testUpdateRedirectWhenNoPostData(array $wishlistDataProvider): void
{
$wishlist = $this->createMock(Wishlist::class);

$this->formKeyValidatorMock->expects($this->once())
->method('validate')
->willReturn(true);
$this->wishlistProviderMock->expects($this->once())
->method('getWishlist')
->willReturn($wishlist);
$wishlist->expects($this->exactly(1))
->method('getId')
->willReturn($wishlistDataProvider['id']);
$this->resultRedirectMock->expects($this->once())
->method('setPath')
->with('*', ['wishlist_id' => $wishlistDataProvider['id']]);
$this->requestMock->expects($this->once())
->method('getPostValue')
->willReturn(null);

$this->assertEquals($this->resultRedirectMock, $this->updateController->execute());
}

/**
* Check if wishlist not availbale, and exception is shown
*
* @return void
*/
public function testUpdateThrowsNotFoundExceptionWhenWishlistDoNotExist(): void
{
$this->formKeyValidatorMock->expects($this->once())
->method('validate')
->willReturn(true);
$this->wishlistProviderMock->expects($this->once())
->method('getWishlist')
->willReturn(null);
$this->expectException(NotFoundException::class);
$this->updateController->execute();
}

/**
* Dataprovider for Update test
*
* @return array
*/
public function getWishlistDataProvider(): array
{
return
[
[
[
'id' => self::STUB_ITEM_ID
],
[
'qty' => [self::STUB_ITEM_ID => self::STUB_WISHLIST_PRODUCT_QTY],
'description' => [self::STUB_ITEM_ID => 'Description for item_id 1']
]
]
];
}
}