Skip to content

Commit 4381aef

Browse files
author
Yuri Kovsher
committed
Merge remote-tracking branch 'tango-ce/MAGETWO-17189' into Tango_Pull_Request_Bug_Fixing
2 parents 41c7cd2 + 56dc186 commit 4381aef

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-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: 75 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,21 @@ 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+
->setMethods(['create'])
89+
->getMock();
90+
$this->productCompModelMock = $this->getMockBuilder('Magento\Reports\Model\Product\Index\Compared')
91+
->disableOriginalConstructor()
92+
->getMock();
7693

7794
$this->observer = $objectManager->getObject(
7895
'Magento\Reports\Model\Event\Observer',
7996
[
8097
'customerSession' => $this->customerSessionMock,
8198
'customerVisitor' => $this->customerVisitorMock,
8299
'productIndxFactory' => $productIndexFactoryMock,
100+
'productCompFactory' => $this->productCompFactoryMock,
83101
'storeManager' => $storeManager,
84102
'event' => $reportEventFactory
85103
]
@@ -158,6 +176,63 @@ public function testCatalogProductViewVisitor()
158176
$this->observer->catalogProductView($eventObserver);
159177
}
160178

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

0 commit comments

Comments
 (0)