diff --git a/app/code/Magento/Wishlist/Controller/Index/Fromcart.php b/app/code/Magento/Wishlist/Controller/Index/Fromcart.php index c17fe8022295bee6efe7ac24e41c40aad374d9b0..bcf8b278e2d03811fe8b7c5092b299acd7769f9b 100644 --- a/app/code/Magento/Wishlist/Controller/Index/Fromcart.php +++ b/app/code/Magento/Wishlist/Controller/Index/Fromcart.php @@ -5,28 +5,69 @@ */ namespace Magento\Wishlist\Controller\Index; +use Magento\Checkout\Helper\Cart as CartHelper; +use Magento\Checkout\Model\Cart as CheckoutCart; +use Magento\Customer\Model\Session; use Magento\Framework\App\Action; +use Magento\Framework\Escaper; use Magento\Framework\Exception\NotFoundException; use Magento\Framework\Exception\LocalizedException; use Magento\Wishlist\Controller\IndexInterface; use Magento\Framework\Controller\ResultFactory; +use Magento\Wishlist\Controller\WishlistProviderInterface; +use Magento\Wishlist\Helper\Data as WishlistHelper; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class Fromcart extends Action\Action implements IndexInterface { /** - * @var \Magento\Wishlist\Controller\WishlistProviderInterface + * @var WishlistProviderInterface */ protected $wishlistProvider; + /** + * @var WishlistHelper + */ + protected $wishlistHelper; + + /** + * @var CheckoutCart + */ + protected $cart; + + /** + * @var CartHelper + */ + protected $cartHelper; + + /** + * @var Escaper + */ + protected $escaper; + /** * @param Action\Context $context - * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider + * @param WishlistProviderInterface $wishlistProvider + * @param WishlistHelper $wishlistHelper + * @param CheckoutCart $cart + * @param CartHelper $cartHelper + * @param Escaper $escaper */ public function __construct( Action\Context $context, - \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider + WishlistProviderInterface $wishlistProvider, + WishlistHelper $wishlistHelper, + CheckoutCart $cart, + CartHelper $cartHelper, + Escaper $escaper ) { $this->wishlistProvider = $wishlistProvider; + $this->wishlistHelper = $wishlistHelper; + $this->cart = $cart; + $this->cartHelper = $cartHelper; + $this->escaper = $escaper; parent::__construct($context); } @@ -43,14 +84,10 @@ class Fromcart extends Action\Action implements IndexInterface if (!$wishlist) { throw new NotFoundException(__('Page not found.')); } - $itemId = (int)$this->getRequest()->getParam('item'); - - /* @var \Magento\Checkout\Model\Cart $cart */ - $cart = $this->_objectManager->get('Magento\Checkout\Model\Cart'); - $session = $this->_objectManager->get('Magento\Checkout\Model\Session'); try { - $item = $cart->getQuote()->getItemById($itemId); + $itemId = (int)$this->getRequest()->getParam('item'); + $item = $this->cart->getQuote()->getItemById($itemId); if (!$item) { throw new LocalizedException( __('The requested cart item doesn\'t exist.') @@ -59,27 +96,26 @@ class Fromcart extends Action\Action implements IndexInterface $productId = $item->getProductId(); $buyRequest = $item->getBuyRequest(); - $wishlist->addNewItem($productId, $buyRequest); - $productIds[] = $productId; - $cart->getQuote()->removeItem($itemId); - $cart->save(); - $this->_objectManager->get('Magento\Wishlist\Helper\Data')->calculate(); - $productName = $this->_objectManager->get('Magento\Framework\Escaper') - ->escapeHtml($item->getProduct()->getName()); - $wishlistName = $this->_objectManager->get('Magento\Framework\Escaper') - ->escapeHtml($wishlist->getName()); - $this->messageManager->addSuccess(__("%1 has been moved to wish list %2", $productName, $wishlistName)); + $this->cart->getQuote()->removeItem($itemId); + $this->cart->save(); + + $this->wishlistHelper->calculate(); $wishlist->save(); + + $this->messageManager->addSuccessMessage(__( + "%1 has been moved to your wishlist.", + $this->escaper->escapeHtml($item->getProduct()->getName()) + )); } catch (LocalizedException $e) { - $this->messageManager->addError($e->getMessage()); + $this->messageManager->addErrorMessage($e->getMessage()); } catch (\Exception $e) { - $this->messageManager->addException($e, __('We can\'t move the item to the wish list.')); + $this->messageManager->addExceptionMessage($e, __('We can\'t move the item to the wish list.')); } /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); - return $resultRedirect->setUrl($this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl()); + return $resultRedirect->setUrl($this->cartHelper->getCartUrl()); } } diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Index/FromcartTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Index/FromcartTest.php new file mode 100644 index 0000000000000000000000000000000000000000..d492dcced8ddc863b966d8068399c810d0ccd9b9 --- /dev/null +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Index/FromcartTest.php @@ -0,0 +1,362 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Wishlist\Test\Unit\Controller\Index; + +use Magento\Checkout\Helper\Cart as CartHelper; +use Magento\Checkout\Model\Cart as CheckoutCart; +use Magento\Framework\App\Action\Context; +use Magento\Framework\App\Request\Http; +use Magento\Framework\Controller\Result\Redirect as ResultRedirect; +use Magento\Framework\Controller\ResultFactory; +use Magento\Framework\DataObject; +use Magento\Framework\Escaper; +use Magento\Framework\Message\Manager as MessageManager; +use Magento\Wishlist\Controller\Index\Fromcart; +use Magento\Wishlist\Controller\WishlistProviderInterface; +use Magento\Wishlist\Helper\Data as WishlistHelper; + +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ +class FromcartTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Fromcart + */ + protected $controller; + + /** + * @var Context | \PHPUnit_Framework_MockObject_MockObject + */ + protected $context; + + /** + * @var WishlistProviderInterface | \PHPUnit_Framework_MockObject_MockObject + */ + protected $wishlistProvider; + + /** + * @var WishlistHelper | \PHPUnit_Framework_MockObject_MockObject + */ + protected $wishlistHelper; + + /** + * @var CheckoutCart | \PHPUnit_Framework_MockObject_MockObject + */ + protected $cart; + + /** + * @var CartHelper | \PHPUnit_Framework_MockObject_MockObject + */ + protected $cartHelper; + + /** + * @var Escaper | \PHPUnit_Framework_MockObject_MockObject + */ + protected $escaper; + + /** + * @var Http | \PHPUnit_Framework_MockObject_MockObject + */ + protected $request; + + /** + * @var MessageManager | \PHPUnit_Framework_MockObject_MockObject + */ + protected $messageManager; + + /** + * @var ResultFactory | \PHPUnit_Framework_MockObject_MockObject + */ + protected $resultFactory; + + /** + * @var ResultRedirect | \PHPUnit_Framework_MockObject_MockObject + */ + protected $resultRedirect; + + protected function setUp() + { + $this->prepareContext(); + + $this->wishlistProvider = $this->getMockBuilder('Magento\Wishlist\Controller\WishlistProviderInterface') + ->getMockForAbstractClass(); + + $this->wishlistHelper = $this->getMockBuilder('Magento\Wishlist\Helper\Data') + ->disableOriginalConstructor() + ->getMock(); + + $this->cart = $this->getMockBuilder('Magento\Checkout\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + $this->cartHelper = $this->getMockBuilder('Magento\Checkout\Helper\Cart') + ->disableOriginalConstructor() + ->getMock(); + + $this->escaper = $this->getMockBuilder('Magento\Framework\Escaper') + ->disableOriginalConstructor() + ->getMock(); + + $this->controller = new Fromcart( + $this->context, + $this->wishlistProvider, + $this->wishlistHelper, + $this->cart, + $this->cartHelper, + $this->escaper + ); + } + + /** + * @expectedException \Magento\Framework\Exception\NotFoundException + * @expectedExceptionMessage Page not found + */ + public function testExecutePageNotFound() + { + $this->wishlistProvider->expects($this->once()) + ->method('getWishlist') + ->willReturn(null); + + $this->controller->execute(); + } + + public function testExecuteNoCartItem() + { + $itemId = 1; + $cartUrl = 'cart_url'; + + $wishlistMock = $this->getMockBuilder('Magento\Wishlist\Model\Wishlist') + ->disableOriginalConstructor() + ->getMock(); + + $this->wishlistProvider->expects($this->once()) + ->method('getWishlist') + ->willReturn($wishlistMock); + + $this->request->expects($this->once()) + ->method('getParam') + ->with('item') + ->willReturn($itemId); + + $quoteMock = $this->getMockBuilder('Magento\Quote\Model\Quote') + ->disableOriginalConstructor() + ->getMock(); + + $quoteMock->expects($this->once()) + ->method('getItemById') + ->with($itemId) + ->willReturn(null); + + $this->cart->expects($this->once()) + ->method('getQuote') + ->willReturn($quoteMock); + + $this->cartHelper->expects($this->once()) + ->method('getCartUrl') + ->willReturn($cartUrl); + + $this->messageManager->expects($this->once()) + ->method('addErrorMessage') + ->with(__('The requested cart item doesn\'t exist.')) + ->willReturnSelf(); + + $this->resultRedirect->expects($this->once()) + ->method('setUrl') + ->with($cartUrl) + ->willReturnSelf(); + + $this->assertSame($this->resultRedirect, $this->controller->execute()); + } + + public function testExecute() + { + $itemId = 1; + $cartUrl = 'cart_url'; + $productId = 1; + $productName = 'product_name'; + + $dataObjectMock = $this->getMockBuilder('Magento\Framework\DataObject') + ->disableOriginalConstructor() + ->getMock(); + + $wishlistMock = $this->getMockBuilder('Magento\Wishlist\Model\Wishlist') + ->disableOriginalConstructor() + ->getMock(); + $wishlistMock->expects($this->once()) + ->method('addNewItem') + ->with($productId, $dataObjectMock) + ->willReturnSelf(); + $wishlistMock->expects($this->once()) + ->method('save') + ->willReturnSelf(); + + $this->wishlistProvider->expects($this->once()) + ->method('getWishlist') + ->willReturn($wishlistMock); + + $this->wishlistHelper->expects($this->once()) + ->method('calculate') + ->willReturnSelf(); + + $this->request->expects($this->once()) + ->method('getParam') + ->with('item') + ->willReturn($itemId); + + $quoteMock = $this->createQuoteMock($productId, $productName, $dataObjectMock, $itemId); + + $this->cart->expects($this->exactly(2)) + ->method('getQuote') + ->willReturn($quoteMock); + $this->cart->expects($this->once()) + ->method('save') + ->willReturnSelf(); + + $this->cartHelper->expects($this->once()) + ->method('getCartUrl') + ->willReturn($cartUrl); + + $this->escaper->expects($this->once()) + ->method('escapeHtml') + ->with($productName) + ->willReturn($productName); + + $this->messageManager->expects($this->once()) + ->method('addSuccessMessage') + ->with(__("%1 has been moved to your wishlist.", $productName)) + ->willReturnSelf(); + + $this->resultRedirect->expects($this->once()) + ->method('setUrl') + ->with($cartUrl) + ->willReturnSelf(); + + $this->assertSame($this->resultRedirect, $this->controller->execute()); + } + + public function testExecuteWithException() + { + $cartUrl = 'cart_url'; + $exceptionMessage = 'exception_message'; + $exception = new \Exception($exceptionMessage); + + $wishlistMock = $this->getMockBuilder('Magento\Wishlist\Model\Wishlist') + ->disableOriginalConstructor() + ->getMock(); + + $this->wishlistProvider->expects($this->once()) + ->method('getWishlist') + ->willReturn($wishlistMock); + + $this->request->expects($this->once()) + ->method('getParam') + ->with('item') + ->willThrowException($exception); + + $this->messageManager->expects($this->once()) + ->method('addExceptionMessage') + ->with($exception, __('We can\'t move the item to the wish list.')) + ->willReturnSelf(); + + $this->cartHelper->expects($this->once()) + ->method('getCartUrl') + ->willReturn($cartUrl); + + $this->resultRedirect->expects($this->once()) + ->method('setUrl') + ->with($cartUrl) + ->willReturnSelf(); + + $this->assertSame($this->resultRedirect, $this->controller->execute()); + } + + protected function prepareContext() + { + $this->request = $this->getMockBuilder('Magento\Framework\App\Request\Http') + ->disableOriginalConstructor() + ->getMock(); + + $this->messageManager = $this->getMockBuilder('Magento\Framework\Message\Manager') + ->disableOriginalConstructor() + ->getMock(); + + $this->resultRedirect = $this->getMockBuilder('Magento\Framework\Controller\Result\Redirect') + ->disableOriginalConstructor() + ->getMock(); + + $this->resultFactory = $this->getMockBuilder('Magento\Framework\Controller\ResultFactory') + ->disableOriginalConstructor() + ->getMock(); + $this->resultFactory->expects($this->any()) + ->method('create') + ->with(ResultFactory::TYPE_REDIRECT) + ->willReturn($this->resultRedirect); + + $this->context = $this->getMockBuilder('Magento\Framework\App\Action\Context') + ->disableOriginalConstructor() + ->getMock(); + + $this->context->expects($this->any()) + ->method('getRequest') + ->willReturn($this->request); + $this->context->expects($this->any()) + ->method('getMessageManager') + ->willReturn($this->messageManager); + $this->context->expects($this->any()) + ->method('getResultFactory') + ->willReturn($this->resultFactory); + } + + /** + * @param int $productId + * @param string $productName + * @param DataObject $dataObjectMock + * @param int $itemId + * @return \PHPUnit_Framework_MockObject_MockObject + */ + protected function createQuoteMock($productId, $productName, $dataObjectMock, $itemId) + { + $productMock = $this->getMockBuilder('Magento\Catalog\Model\Product') + ->disableOriginalConstructor() + ->getMock(); + $productMock->expects($this->once()) + ->method('getName') + ->willReturn($productName); + + $quoteItemMock = $this->getMockBuilder('Magento\Quote\Model\Quote\Item') + ->disableOriginalConstructor() + ->setMethods([ + 'getProductId', + 'getBuyRequest', + 'getProduct', + ]) + ->getMock(); + $quoteItemMock->expects($this->once()) + ->method('getProductId') + ->willReturn($productId); + $quoteItemMock->expects($this->once()) + ->method('getBuyRequest') + ->willReturn($dataObjectMock); + $quoteItemMock->expects($this->once()) + ->method('getProduct') + ->willReturn($productMock); + + $quoteMock = $this->getMockBuilder('Magento\Quote\Model\Quote') + ->disableOriginalConstructor() + ->getMock(); + $quoteMock->expects($this->once()) + ->method('getItemById') + ->with($itemId) + ->willReturn($quoteItemMock); + $quoteMock->expects($this->once()) + ->method('removeItem') + ->with($itemId) + ->willReturnSelf(); + + return $quoteMock; + } +} diff --git a/app/code/Magento/Wishlist/i18n/de_DE.csv b/app/code/Magento/Wishlist/i18n/de_DE.csv index 7d170fb8ee8f5702e701cfb9e889fbfdd62e6c8b..85d040f5d29ced1fab29104215bd450df9e96653 100644 --- a/app/code/Magento/Wishlist/i18n/de_DE.csv +++ b/app/code/Magento/Wishlist/i18n/de_DE.csv @@ -65,7 +65,7 @@ Message,Nachricht "An error occurred while deleting the item from wish list.","An error occurred while deleting the item from wish list." "Cannot add item to shopping cart","Kann Objekt nicht zum Warenkorb hinzufügen" "The requested cart item doesn't exist.","The requested cart item doesn't exist." -"%1 has been moved to wish list %2","%1 has been moved to wish list %2" +"%1 has been moved to your wishlist.","%1 has been moved to your wishlist." "We can't move the item to the wish list.","We can't move the item to the wish list." "Message length must not exceed %1 symbols","Message length must not exceed %1 symbols" "Email address can't be empty.","E-Mail-Adressfeld darf nicht leer sein." diff --git a/app/code/Magento/Wishlist/i18n/en_US.csv b/app/code/Magento/Wishlist/i18n/en_US.csv index 032419a9e0618024eb892ffb4f2478c25bcb0e12..fc5976e474022352c3751f4dbc5bf6f56f107efc 100644 --- a/app/code/Magento/Wishlist/i18n/en_US.csv +++ b/app/code/Magento/Wishlist/i18n/en_US.csv @@ -65,7 +65,7 @@ Message,Message "An error occurred while deleting the item from wish list.","An error occurred while deleting the item from wish list." "Cannot add item to shopping cart","Cannot add item to shopping cart" "The requested cart item doesn't exist.","The requested cart item doesn't exist." -"%1 has been moved to wish list %2","%1 has been moved to wish list %2" +"%1 has been moved to your wishlist.","%1 has been moved to your wishlist." "We can't move the item to the wish list.","We can't move the item to the wish list." "Message length must not exceed %1 symbols","Message length must not exceed %1 symbols" "Email address can't be empty.","Email address can't be empty." diff --git a/app/code/Magento/Wishlist/i18n/es_ES.csv b/app/code/Magento/Wishlist/i18n/es_ES.csv index 0c3d383c19eb5b00384fb9278a46f6c6a864dc28..90aa8b117a67b597ada05c6623f09f09d19dd458 100644 --- a/app/code/Magento/Wishlist/i18n/es_ES.csv +++ b/app/code/Magento/Wishlist/i18n/es_ES.csv @@ -65,7 +65,7 @@ Message,Mensaje "An error occurred while deleting the item from wish list.","An error occurred while deleting the item from wish list." "Cannot add item to shopping cart","No se pudo añadir el artÃculo al carrito de compra" "The requested cart item doesn't exist.","The requested cart item doesn't exist." -"%1 has been moved to wish list %2","%1 has been moved to wish list %2" +"%1 has been moved to your wishlist.","%1 has been moved to your wishlist." "We can't move the item to the wish list.","We can't move the item to the wish list." "Message length must not exceed %1 symbols","Message length must not exceed %1 symbols" "Email address can't be empty.","El campo Dirección de correo electrónico no puede estar vacÃo." diff --git a/app/code/Magento/Wishlist/i18n/fr_FR.csv b/app/code/Magento/Wishlist/i18n/fr_FR.csv index 393bd7ad11c2455ddb548f10a17cb532ea4c10d1..a1f5b4c198f61255af30d1b91322fd05d78c6a72 100644 --- a/app/code/Magento/Wishlist/i18n/fr_FR.csv +++ b/app/code/Magento/Wishlist/i18n/fr_FR.csv @@ -65,7 +65,7 @@ Message,Message "An error occurred while deleting the item from wish list.","An error occurred while deleting the item from wish list." "Cannot add item to shopping cart","Ne peut ajouter l'article au caddy" "The requested cart item doesn't exist.","The requested cart item doesn't exist." -"%1 has been moved to wish list %2","%1 has been moved to wish list %2" +"%1 has been moved to your wishlist.","%1 has been moved to your wishlist." "We can't move the item to the wish list.","We can't move the item to the wish list." "Message length must not exceed %1 symbols","Message length must not exceed %1 symbols" "Email address can't be empty.","Adresse courriel ne peut être vide." diff --git a/app/code/Magento/Wishlist/i18n/nl_NL.csv b/app/code/Magento/Wishlist/i18n/nl_NL.csv index 8742b17c50cbdcfb6999186862a5f55e8c634d66..6cd576421cb400056e0922687c566c9a62cb27f8 100644 --- a/app/code/Magento/Wishlist/i18n/nl_NL.csv +++ b/app/code/Magento/Wishlist/i18n/nl_NL.csv @@ -65,7 +65,7 @@ Message,Boodschap "An error occurred while deleting the item from wish list.","An error occurred while deleting the item from wish list." "Cannot add item to shopping cart","Kan het item niet toevoegen aan het winkelwagentje" "The requested cart item doesn't exist.","The requested cart item doesn't exist." -"%1 has been moved to wish list %2","%1 has been moved to wish list %2" +"%1 has been moved to your wishlist.","%1 has been moved to your wishlist." "We can't move the item to the wish list.","We can't move the item to the wish list." "Message length must not exceed %1 symbols","Message length must not exceed %1 symbols" "Email address can't be empty.","E-mailadres mag niet leeg zijn." diff --git a/app/code/Magento/Wishlist/i18n/pt_BR.csv b/app/code/Magento/Wishlist/i18n/pt_BR.csv index 21f4da865aca77be54eddc52c8e5761342522e44..94f8126a7f847a40130d838dd447812814b8b5e6 100644 --- a/app/code/Magento/Wishlist/i18n/pt_BR.csv +++ b/app/code/Magento/Wishlist/i18n/pt_BR.csv @@ -65,7 +65,7 @@ Message,Mensagem "An error occurred while deleting the item from wish list.","An error occurred while deleting the item from wish list." "Cannot add item to shopping cart","Não é possÃvel adicionar item ao carrinho de compras" "The requested cart item doesn't exist.","The requested cart item doesn't exist." -"%1 has been moved to wish list %2","%1 has been moved to wish list %2" +"%1 has been moved to your wishlist.","%1 has been moved to your wishlist." "We can't move the item to the wish list.","We can't move the item to the wish list." "Message length must not exceed %1 symbols","Message length must not exceed %1 symbols" "Email address can't be empty.","O endereço de email não pode ficar vazio." diff --git a/app/code/Magento/Wishlist/i18n/zh_CN.csv b/app/code/Magento/Wishlist/i18n/zh_CN.csv index 5ad8d2e6066973040f76226ddae69ca8b2733964..5279ab788f79154b4702044ccab02046b62369bb 100644 --- a/app/code/Magento/Wishlist/i18n/zh_CN.csv +++ b/app/code/Magento/Wishlist/i18n/zh_CN.csv @@ -65,7 +65,7 @@ Message,ä¿¡æ¯ "An error occurred while deleting the item from wish list.","An error occurred while deleting the item from wish list." "Cannot add item to shopping cart",æ— æ³•æ·»åŠ å†…å®¹åˆ°è´ç‰©è½¦ "The requested cart item doesn't exist.","The requested cart item doesn't exist." -"%1 has been moved to wish list %2","%1 has been moved to wish list %2" +"%1 has been moved to your wishlist.","%1 has been moved to your wishlist." "We can't move the item to the wish list.","We can't move the item to the wish list." "Message length must not exceed %1 symbols","Message length must not exceed %1 symbols" "Email address can't be empty.",邮件地å€ä¸èƒ½ä¸ºç©ºã€‚ diff --git a/dev/tests/functional/tests/app/Magento/Wishlist/Test/Constraint/AssertMoveProductToWishlistSuccessMessage.php b/dev/tests/functional/tests/app/Magento/Wishlist/Test/Constraint/AssertMoveProductToWishlistSuccessMessage.php index 29ef84e8349a6a0f2a998a011db4a6a95a525c40..af6d6ebf9a16728148c258e8a164d576f49535fb 100644 --- a/dev/tests/functional/tests/app/Magento/Wishlist/Test/Constraint/AssertMoveProductToWishlistSuccessMessage.php +++ b/dev/tests/functional/tests/app/Magento/Wishlist/Test/Constraint/AssertMoveProductToWishlistSuccessMessage.php @@ -19,7 +19,7 @@ class AssertMoveProductToWishlistSuccessMessage extends AbstractConstraint /** * Success add message */ - const SUCCESS_MESSAGE = "%s has been moved to wish list Wish List"; + const SUCCESS_MESSAGE = "%s has been moved to your wishlist."; /** * Assert that success message appears on My Wish List page after moving product to wishlist.