Skip to content

Commit 9cc9e63

Browse files
author
Anna Bukatar
committed
MC-42347: Recently Viewed : add to cart action not landing on product details page
1 parent a2d67dd commit 9cc9e63

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

app/code/Magento/Catalog/Controller/Product/View.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function execute()
162162
return;
163163
}
164164
$resultRedirect = $this->resultRedirectFactory->create();
165-
$resultRedirect->setRefererOrBaseUrl();
165+
$resultRedirect->setUrl($this->_url->getCurrentUrl());
166166
return $resultRedirect;
167167
}
168168

app/code/Magento/Catalog/Test/Unit/Controller/Product/ViewTest.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Catalog\Test\Unit\Controller\Product;
99

10+
use Magento\Backend\Model\View\Result\RedirectFactory;
1011
use Magento\Catalog\Api\Data\ProductInterface;
1112
use Magento\Catalog\Controller\Product\View;
1213
use Magento\Catalog\Helper\Product\View as ViewHelper;
@@ -16,6 +17,7 @@
1617
use Magento\Framework\App\RequestInterface;
1718
use Magento\Framework\Controller\Result\ForwardFactory;
1819
use Magento\Framework\DataObject;
20+
use Magento\Framework\ObjectManager\ObjectManager;
1921
use Magento\Framework\View\Result\Page;
2022
use Magento\Framework\View\Result\PageFactory;
2123
use Magento\Store\Model\Store;
@@ -63,6 +65,21 @@ class ViewTest extends TestCase
6365
*/
6466
protected $storeManagerMock;
6567

68+
/**
69+
* @var \Magento\Catalog\Helper\Product|MockObject
70+
*/
71+
protected $helperProduct;
72+
73+
/**
74+
* @var Magento\Framework\Controller\Result\Redirect|MockObject
75+
*/
76+
protected $redirectMock;
77+
78+
/**
79+
* @var Magento\Framework\UrlInterface|MockObject
80+
*/
81+
protected $urlBuilder;
82+
6683
/**
6784
* @inheritDoc
6885
*/
@@ -73,11 +90,36 @@ protected function setUp(): void
7390
->getMock();
7491
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
7592
->disableOriginalConstructor()
76-
->addMethods(['isPost'])
93+
->setMethods(['isAjax', 'isPost', 'getParam'])
7794
->getMockForAbstractClass();
7895
$contextMock->expects($this->any())
7996
->method('getRequest')
8097
->willReturn($this->requestMock);
98+
$objectManagerMock = $this->createMock(ObjectManager::class);
99+
$this->helperProduct = $this->createMock(\Magento\Catalog\Helper\Product::class);
100+
$objectManagerMock->expects($this->any())
101+
->method('get')
102+
->with(\Magento\Catalog\Helper\Product::class)
103+
->willReturn($this->helperProduct);
104+
$contextMock->expects($this->any())
105+
->method('getObjectManager')
106+
->willReturn($objectManagerMock);
107+
$resultRedirectFactoryMock = $this->createPartialMock(
108+
RedirectFactory::class,
109+
['create']
110+
);
111+
$this->redirectMock = $this->createMock(\Magento\Framework\Controller\Result\Redirect::class);
112+
$resultRedirectFactoryMock->method('create')->willReturn($this->redirectMock);
113+
$contextMock->expects($this->any())
114+
->method('getResultRedirectFactory')
115+
->willReturn($resultRedirectFactoryMock);
116+
$this->urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
117+
->setMethods(['getUrl'])
118+
->disableOriginalConstructor()
119+
->getMockForAbstractClass();
120+
$contextMock->expects($this->any())
121+
->method('getUrl')
122+
->willReturn($this->urlBuilder);
81123
$viewHelperMock = $this->getMockBuilder(ViewHelper::class)
82124
->disableOriginalConstructor()
83125
->getMock();
@@ -144,4 +186,30 @@ public function testExecute(): void
144186
->willReturn($viewResultPageMock);
145187
$this->view->execute();
146188
}
189+
190+
public function testExecuteRecentlyViewed(): void
191+
{
192+
$post = [
193+
'category' => '1',
194+
'id' => 1,
195+
'options' => false,
196+
View::PARAM_NAME_URL_ENCODED => 'some_param_url_encoded'
197+
];
198+
199+
// _initProduct
200+
$this->helperProduct->method('initProduct')
201+
->willReturn('true');
202+
$this->redirectMock->method('setUrl')->with('productUrl')->willReturnSelf();
203+
204+
$this->requestMock->method('isPost')
205+
->willReturn(true);
206+
$this->requestMock->method('getParam')->willReturnCallback(
207+
function ($key) use ($post) {
208+
return $post[$key];
209+
}
210+
);
211+
212+
$this->urlBuilder->expects($this->any())->method('getCurrentUrl')->willReturn('productUrl');
213+
$this->view->execute();
214+
}
147215
}

0 commit comments

Comments
 (0)