Skip to content

Commit 1ba23ce

Browse files
author
Yuri Kovsher
committed
MAGETWO-17189: Recently Compared / Viewed widgets aren't displayed in side columns
1 parent 6e74547 commit 1ba23ce

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

app/code/Magento/Reports/Model/Event/Observer.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,13 @@ public function catalogProductCompareClear(\Magento\Framework\Event\Observer $ob
215215
public function catalogProductCompareAddProduct(\Magento\Framework\Event\Observer $observer)
216216
{
217217
$productId = $observer->getEvent()->getProduct()->getId();
218-
219-
$this->_productCompFactory->create()->setProductId($productId)->save()->calculate();
220-
218+
$viewData = ['product_id' => $productId];
219+
if ($this->_customerSession->isLoggedIn()) {
220+
$viewData['customer_id'] = $this->_customerSession->getCustomerId();
221+
} else {
222+
$viewData['visitor_id'] = $this->_customerVisitor->getId();
223+
}
224+
$this->_productCompFactory->create()->setData($viewData)->save()->calculate();
221225
return $this->_event(\Magento\Reports\Model\Event::EVENT_PRODUCT_COMPARE, $productId);
222226
}
223227

dev/tests/unit/testsuite/Magento/Reports/Model/Event/ObserverTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ class ObserverTest extends \PHPUnit_Framework_TestCase
3636
*/
3737
protected $storeMock;
3838

39+
/**
40+
* @var \Magento\Reports\Model\Product\Index\ComparedFactory|\PHPUnit_Framework_MockObject_MockObject
41+
*/
42+
protected $productCompFactoryMock;
43+
44+
/**
45+
* @var \Magento\Reports\Model\Product\Index\Compared|\PHPUnit_Framework_MockObject_MockObject
46+
*/
47+
protected $productCompModelMock;
48+
3949
public function setUp()
4050
{
4151
$objectManager = new \Magento\TestFramework\Helper\ObjectManager($this);
@@ -73,13 +83,20 @@ public function setUp()
7383
$storeManager->expects($this->any())
7484
->method('getStore')
7585
->willReturn($this->storeMock);
86+
$this->productCompFactoryMock = $this->getMockBuilder('Magento\Reports\Model\Product\Index\ComparedFactory')
87+
->disableOriginalConstructor()
88+
->getMock();
89+
$this->productCompModelMock = $this->getMockBuilder('Magento\Reports\Model\Product\Index\Compared')
90+
->disableOriginalConstructor()
91+
->getMock();
7692

7793
$this->observer = $objectManager->getObject(
7894
'Magento\Reports\Model\Event\Observer',
7995
[
8096
'customerSession' => $this->customerSessionMock,
8197
'customerVisitor' => $this->customerVisitorMock,
8298
'productIndxFactory' => $productIndexFactoryMock,
99+
'productCompFactory' => $this->productCompFactoryMock,
83100
'storeManager' => $storeManager,
84101
'event' => $reportEventFactory
85102
]
@@ -158,6 +175,63 @@ public function testCatalogProductViewVisitor()
158175
$this->observer->catalogProductView($eventObserver);
159176
}
160177

178+
/**
179+
* @param bool $isLoggedIn
180+
* @param str $userKey
181+
* @param int $userId
182+
* @dataProvider catalogProductCompareAddProductDataProvider
183+
*/
184+
public function testCatalogProductCompareAddProduct($isLoggedIn, $userKey, $userId)
185+
{
186+
$productId = 111;
187+
$customerId = 222;
188+
$visitorId = 333;
189+
$viewData = [
190+
'product_id' => $productId,
191+
$userKey => $userId
192+
];
193+
$observerMock = $this->getObserverMock($productId);
194+
$this->customerSessionMock->expects($this->any())
195+
->method('isLoggedIn')
196+
->willReturn($isLoggedIn);
197+
$this->customerSessionMock->expects($this->any())
198+
->method('getCustomerId')
199+
->willReturn($customerId);
200+
$this->customerVisitorMock->expects($this->any())
201+
->method('getId')
202+
->willReturn($visitorId);
203+
$this->productCompFactoryMock->expects($this->any())
204+
->method('create')
205+
->willReturn($this->productCompModelMock);
206+
$this->productCompModelMock->expects($this->any())
207+
->method('setData')
208+
->with($viewData)
209+
->willReturnSelf();
210+
$this->productCompModelMock->expects($this->any())
211+
->method('save')
212+
->willReturnSelf();
213+
$this->productCompModelMock->expects($this->any())
214+
->method('calculate')
215+
->willReturnSelf();
216+
$this->assertEquals($this->observer, $this->observer->catalogProductCompareAddProduct($observerMock));
217+
}
218+
219+
public function catalogProductCompareAddProductDataProvider()
220+
{
221+
return [
222+
'logged in' => [
223+
'isLoggedIn' => true,
224+
'userKey' => 'customer_id',
225+
'userId' => 222
226+
],
227+
'not logged in' => [
228+
'isLoggedIn' => false,
229+
'userKey' => 'visitor_id',
230+
'userId' => 333
231+
]
232+
];
233+
}
234+
161235
/**
162236
* @param array $expectedViewedData
163237
* @return void

0 commit comments

Comments
 (0)