Skip to content

Commit e7db99e

Browse files
author
Vladyslav Shcherbyna
committed
MAGETWO-31967: Exception page instead of 404 when edit url for product with required configuration
1 parent 3bca5f4 commit e7db99e

File tree

2 files changed

+259
-6
lines changed

2 files changed

+259
-6
lines changed

app/code/Magento/Checkout/Controller/Cart/Configure.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,19 @@ public function execute()
4343
{
4444
// Extract item and product to configure
4545
$id = (int)$this->getRequest()->getParam('id');
46+
$productId = (int)$this->getRequest()->getParam('product_id');
4647
$quoteItem = null;
4748
if ($id) {
4849
$quoteItem = $this->cart->getQuote()->getItemById($id);
4950
}
5051

51-
if (!$quoteItem) {
52-
$this->messageManager->addError(__("We can't find the quote item."));
53-
$this->_redirect('checkout/cart');
54-
return;
55-
}
56-
5752
try {
53+
if (!$quoteItem || $productId != $quoteItem->getProduct()->getId()) {
54+
$this->messageManager->addError(__("We can't find the quote item."));
55+
$this->_redirect('checkout/cart');
56+
return;
57+
}
58+
5859
$params = new \Magento\Framework\Object();
5960
$params->setCategoryId(false);
6061
$params->setConfigureMode(true);
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
<?php
2+
/**
3+
* {license_notice}
4+
*
5+
* @copyright {copyright}
6+
* @license {license_link}
7+
*/
8+
namespace Magento\Checkout\Controller\Cart;
9+
10+
/**
11+
* Shopping cart edit tests
12+
*/
13+
class ConfigureTest extends \PHPUnit_Framework_TestCase
14+
{
15+
16+
/**
17+
* @var \Magento\Framework\ObjectManagerInterface | \PHPUnit_Framework_MockObject_MockObject
18+
*/
19+
protected $objectManagerMock;
20+
21+
/**
22+
* @var \Magento\Framework\View\Result\PageFactory | \PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
protected $resultPageFactoryMock;
25+
26+
/**
27+
* @var \Magento\Framework\App\ResponseInterface | \PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
protected $responseMock;
30+
31+
/**
32+
* @var \Magento\Framework\App\RequestInterface | \PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
protected $requestMock;
35+
36+
/**
37+
* @var \Magento\Framework\Message\ManagerInterface | \PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
protected $messageManagerMock;
40+
41+
/**
42+
* @var \Magento\Framework\App\Response\RedirectInterface | \PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
protected $redirectMock;
45+
46+
/**
47+
* @var \Magento\Checkout\Controller\Cart\Configure | \PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
protected $configureController;
50+
51+
/**
52+
* @var \Magento\Framework\App\Action\Context | \PHPUnit_Framework_MockObject_MockObject
53+
*/
54+
protected $contextMock;
55+
56+
/**
57+
* @var \Magento\Checkout\Model\Cart | \PHPUnit_Framework_MockObject_MockObject
58+
*/
59+
protected $cartMock;
60+
61+
public function setUp()
62+
{
63+
$eventManagerMock = $this->getMockBuilder('Magento\Framework\Event\ManagerInterface')
64+
->disableOriginalConstructor()
65+
->setMethods([])
66+
->getMockForAbstractClass();
67+
$urlMock = $this->getMockBuilder('Magento\Framework\UrlInterface')
68+
->disableOriginalConstructor()
69+
->setMethods([])
70+
->getMockForAbstractClass();
71+
$actionFlagMock = $this->getMockBuilder('Magento\Framework\App\ActionFlag')
72+
->disableOriginalConstructor()
73+
->setMethods([])
74+
->getMockForAbstractClass();
75+
$viewMock = $this->getMockBuilder('Magento\Framework\App\ViewInterface')
76+
->disableOriginalConstructor()
77+
->setMethods([])
78+
->getMockForAbstractClass();
79+
$this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface')
80+
->disableOriginalConstructor()
81+
->setMethods([])
82+
->getMockForAbstractClass();
83+
$this->responseMock = $this->getMockBuilder('Magento\Framework\App\ResponseInterface')
84+
->disableOriginalConstructor()
85+
->setMethods([])
86+
->getMockForAbstractClass();
87+
$this->requestMock = $this->getMockBuilder('Magento\Framework\App\RequestInterface')
88+
->disableOriginalConstructor()
89+
->setMethods(['getParam'])
90+
->getMockForAbstractClass();
91+
$this->messageManagerMock = $this->getMockBuilder('Magento\Framework\Message\ManagerInterface')
92+
->disableOriginalConstructor()
93+
->setMethods([])
94+
->getMockForAbstractClass();
95+
$this->redirectMock = $this->getMockBuilder('Magento\Framework\App\Response\RedirectInterface')
96+
->disableOriginalConstructor()
97+
->setMethods([])
98+
->getMock();
99+
100+
$this->contextMock = $this->getMockBuilder('Magento\Framework\App\Action\Context')
101+
->setConstructorArgs(
102+
[
103+
$this->requestMock,
104+
$this->responseMock,
105+
$this->objectManagerMock,
106+
$eventManagerMock,
107+
$urlMock,
108+
$this->redirectMock,
109+
$actionFlagMock,
110+
$viewMock,
111+
$this->messageManagerMock
112+
]
113+
)
114+
->setMethods([])
115+
->getMock();
116+
$this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
117+
$this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
118+
$this->contextMock->expects($this->any())->method('getResponse')->willReturn($this->responseMock);
119+
$this->contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock);
120+
$this->contextMock->expects($this->any())->method('getRedirect')->willReturn($this->redirectMock);
121+
$scopeConfig = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')
122+
->disableOriginalConstructor()
123+
->getMock();
124+
$session = $this->getMockBuilder('Magento\Checkout\Model\Session')
125+
->disableOriginalConstructor()
126+
->getMock();
127+
$storeManager = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface')
128+
->disableOriginalConstructor()
129+
->getMockForAbstractClass();
130+
$formKeyValidator = $this->getMockBuilder('Magento\Core\App\Action\FormKeyValidator')
131+
->disableOriginalConstructor()
132+
->getMockForAbstractClass();
133+
$this->cartMock = $this->getMockBuilder('Magento\Checkout\Model\Cart')
134+
->disableOriginalConstructor()
135+
->getMock();
136+
$this->resultPageFactoryMock = $this->getMockBuilder('Magento\Framework\View\Result\PageFactory')
137+
->disableOriginalConstructor()
138+
->getMock();
139+
140+
$this->configureController = new \Magento\Checkout\Controller\Cart\Configure(
141+
$this->contextMock,
142+
$scopeConfig,
143+
$session,
144+
$storeManager,
145+
$formKeyValidator,
146+
$this->cartMock,
147+
$this->resultPageFactoryMock
148+
);
149+
}
150+
151+
/**
152+
* Test checks controller call product view and send parameter to it
153+
*
154+
* @return void
155+
*/
156+
public function testPrepareAndRenderCall()
157+
{
158+
$quoteId = 1;
159+
$actualProductId = 1;
160+
$quoteMock = $this->getMockBuilder('Magento\Sales\Model\Quote')
161+
->disableOriginalConstructor()
162+
->getMock();
163+
$quoteItemMock = $this->getMockBuilder('Magento\Sales\Model\Quote\Item')
164+
->disableOriginalConstructor()
165+
->getMock();
166+
$productMock = $this->getMockBuilder('Magento\Catalog\Model\Product')
167+
->disableOriginalConstructor()
168+
->getMock();
169+
$viewMock = $this->getMockBuilder('Magento\Catalog\Helper\Product\View')
170+
->disableOriginalConstructor()
171+
->getMock();
172+
$pageMock = $this->getMockBuilder('Magento\Framework\View\Result\Page')
173+
->disableOriginalConstructor()
174+
->getMock();
175+
$buyRequestMock = $this->getMockBuilder('Magento\Framework\Object')
176+
->disableOriginalConstructor()
177+
->getMock();
178+
//expects
179+
$this->requestMock->expects($this->at(0))
180+
->method('getParam')
181+
->with('id')
182+
->willReturn($quoteId);
183+
$this->requestMock->expects($this->at(1))
184+
->method('getParam')
185+
->with('product_id')
186+
->willReturn($actualProductId);
187+
$this->cartMock->expects($this->any())->method('getQuote')->willReturn($quoteMock);
188+
189+
$quoteItemMock->expects($this->exactly(1))->method('getBuyRequest')->willReturn($buyRequestMock);
190+
191+
$this->resultPageFactoryMock->expects($this->once())->method('create')->willReturn($pageMock);
192+
$this->objectManagerMock->expects($this->at(0))
193+
->method('get')
194+
->with('Magento\Catalog\Helper\Product\View')
195+
->willReturn($viewMock);
196+
197+
$viewMock->expects($this->once())->method('prepareAndRender')->with(
198+
$pageMock,
199+
$actualProductId,
200+
$this->configureController,
201+
$this->callback(
202+
function ($subject) use ($buyRequestMock) {
203+
return $subject->getBuyRequest() === $buyRequestMock;
204+
}
205+
)
206+
)->willReturn($pageMock);
207+
208+
$quoteMock->expects($this->once())->method('getItemById')->willReturn($quoteItemMock);
209+
$quoteItemMock->expects($this->exactly(2))->method('getProduct')->willReturn($productMock);
210+
211+
$productMock->expects($this->exactly(2))->method('getId')->willReturn($actualProductId);
212+
213+
$this->assertSame($pageMock, $this->configureController->execute());
214+
}
215+
216+
/**
217+
* Test checks controller redirect user to cart
218+
* if user request product id in cart edit page is not same as quota product id
219+
*
220+
* @return void
221+
*/
222+
public function testRedirectWithWrongProductId()
223+
{
224+
$quotaId = 1;
225+
$productIdInQuota = 1;
226+
$productIdInRequest = null;
227+
$quoteItemMock = $this->getMockBuilder('Magento\Sales\Model\Quote\Item')
228+
->disableOriginalConstructor()
229+
->getMock();
230+
$quoteMock = $this->getMockBuilder('Magento\Sales\Model\Quote')
231+
->disableOriginalConstructor()
232+
->getMock();
233+
$productMock = $this->getMockBuilder('Magento\Catalog\Model\Product')
234+
->disableOriginalConstructor()
235+
->getMock();
236+
$this->requestMock->expects($this->at(0))
237+
->method('getParam')
238+
->with('id')
239+
->willReturn($quotaId);
240+
$this->requestMock->expects($this->at(1))
241+
->method('getParam')
242+
->with('product_id')
243+
->willReturn($productIdInRequest);
244+
$this->cartMock->expects($this->any())->method('getQuote')->willReturn($quoteMock);
245+
$quoteMock->expects($this->once())->method('getItemById')->willReturn($quoteItemMock);
246+
$quoteItemMock->expects($this->exactly(1))->method('getProduct')->willReturn($productMock);
247+
$productMock->expects($this->exactly(1))->method('getId')->willReturn($productIdInQuota);
248+
$this->messageManagerMock->expects($this->once())->method('addError');
249+
$this->redirectMock->expects($this->once())->method('redirect')->with($this->responseMock, 'checkout/cart', []);
250+
$this->configureController->execute();
251+
}
252+
}

0 commit comments

Comments
 (0)