Skip to content

Commit 1f0f75d

Browse files
authored
Merge pull request #1768 from algolia/release/3.16.0-dev
3.16.0-dev => 3.17.0-dev
2 parents d667852 + fd5680a commit 1f0f75d

File tree

4 files changed

+217
-5
lines changed

4 files changed

+217
-5
lines changed

Helper/Configuration/NoticeHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ protected function getPersonalizationNotice()
236236
switch ($personalizationStatus) {
237237
// Activated
238238
case 2: $warningContent = 'Personalization is based on actions a user has performed in the past. We help you collect some of the data automatically.</br>
239-
After you\'ve collected a reasonable amount of data, Personlization can be applied.';
239+
After you\'ve collected a reasonable amount of data, Personalization can be applied.';
240240
$icon = 'icon-warning';
241241

242242
break;
@@ -262,7 +262,7 @@ protected function getPersonalizationNotice()
262262
];
263263

264264
// Adding footer
265-
$footerContent = '<div class="algolia-perso-footer"><br/><h2>Personlization preferences</h2>
265+
$footerContent = '<div class="algolia-perso-footer"><br/><h2>Personalization preferences</h2>
266266
<p>Manage your Personalization further on the <a href="https://www.algolia.com/dashboard" target="_blank`">Algolia Dashboard</a></p></div>';
267267

268268
$this->notices[] = [

Service/Insights/EventProcessor.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
use Magento\Sales\Model\Order;
1212
use Magento\Sales\Model\Order\Item as OrderItem;
1313
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\Tax\Model\Config as TaxConfig;
1415

1516
class EventProcessor implements EventProcessorInterface
1617
{
1718
/** @var string */
1819
protected const NO_QUERY_ID_KEY = '__NO_QUERY_ID__';
1920

2021
public function __construct(
22+
protected TaxConfig $taxConfig,
2123
protected ?InsightsClient $client = null,
2224
protected ?string $userToken = null,
2325
protected ?string $authenticatedUserToken = null,
@@ -268,7 +270,9 @@ protected function getQuoteItemDiscount(Item $item): float
268270
*/
269271
protected function getOrderItemSalePrice(OrderItem $item): float
270272
{
271-
return floatval($item->getPrice()) - $this->getOrderItemCartDiscount($item);
273+
return $this->taxConfig->priceIncludesTax($this->storeManager->getStore()->getId()) ?
274+
floatval($item->getPriceInclTax()) - $this->getOrderItemCartDiscount($item):
275+
floatval($item->getPrice()) - $this->getOrderItemCartDiscount($item);
272276
}
273277

274278
/**
@@ -286,7 +290,9 @@ protected function getOrderItemCartDiscount(OrderItem $item): float
286290
*/
287291
protected function getOrderItemDiscount(OrderItem $item): float
288292
{
289-
$itemDiscount = floatval($item->getOriginalPrice()) - floatval($item->getPrice());
293+
$itemDiscount = $this->taxConfig->priceIncludesTax($this->storeManager->getStore()->getId()) ?
294+
floatval($item->getOriginalPrice()) - floatval($item->getPriceInclTax()) :
295+
floatval($item->getOriginalPrice()) - floatval($item->getPrice());
290296
return $itemDiscount + $this->getOrderItemCartDiscount($item);
291297
}
292298

@@ -303,7 +309,7 @@ protected function getObjectDataForPurchase(array $items): array
303309
return array_map(function($item) {
304310
return [
305311
'price' => $this->getOrderItemSalePrice($item),
306-
'discount' => $this->getOrderItemDiscount($item),
312+
'discount' => max(0, $this->getOrderItemDiscount($item)),
307313
'quantity' => intval($item->getQtyOrdered())
308314
];
309315
}, $items);
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Unit\Service;
4+
5+
use Algolia\AlgoliaSearch\Api\InsightsClient;
6+
use Algolia\AlgoliaSearch\Service\Insights\EventProcessor;
7+
use Magento\Sales\Model\Order\Item as OrderItem;
8+
use Magento\Store\Model\Store;
9+
use Magento\Store\Model\StoreManagerInterface;
10+
use Magento\Tax\Model\Config as TaxConfig;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class EventProcessorTest extends TestCase
14+
{
15+
protected ?TaxConfig $taxConfig;
16+
protected ?InsightsClient $client;
17+
protected ?string $userToken;
18+
protected ?string $authenticatedUserToken;
19+
protected ?StoreManagerInterface $storeManager;
20+
protected ?EventProcessor $eventProcessor;
21+
22+
protected function setUp(): void
23+
{
24+
$this->client = $this->createMock(InsightsClient::class);
25+
$this->userToken = 'foo';
26+
$this->authenticatedUserToken = 'authenticated-foo';
27+
$this->storeManager = $this->createMock(StoreManagerInterface::class);
28+
29+
$store = $this->createMock(Store::class);
30+
$store->method('getId')->willReturn(1);
31+
$this->storeManager->method('getStore')->willReturn($store);
32+
$this->taxConfig = $this->createMock(TaxConfig::class);
33+
34+
$this->eventProcessor = new EventProcessorTestable(
35+
$this->taxConfig,
36+
$this->client,
37+
$this->userToken,
38+
$this->authenticatedUserToken,
39+
$this->storeManager
40+
);
41+
}
42+
43+
/**
44+
* @dataProvider orderItemsProvider
45+
*/
46+
public function testObjectDataForPurchase($priceIncludesTax, $orderItemsData, $expectedResult, $expectedTotalRevenue): void
47+
{
48+
$this->taxConfig->method('priceIncludesTax')->willReturn($priceIncludesTax);
49+
50+
$orderItems = [];
51+
52+
foreach ($orderItemsData as $orderItemData) {
53+
$orderItem = $this->getMockBuilder(OrderItem::class)
54+
->disableOriginalConstructor()
55+
->getMock();
56+
57+
foreach ($orderItemData as $method => $value){
58+
$orderItem->method($method)->willReturn($value);
59+
}
60+
61+
$orderItems[] = $orderItem;
62+
}
63+
64+
$object = $this->eventProcessor->getObjectDataForPurchase($orderItems);
65+
$this->assertEquals($expectedResult, $object);
66+
67+
$totalRevenue = $this->eventProcessor->getTotalRevenueForEvent($object);
68+
$this->assertEquals($expectedTotalRevenue, $totalRevenue);
69+
}
70+
71+
public function orderItemsProvider(): array
72+
{
73+
return [
74+
[ // One item
75+
'priceIncludesTax' => true,
76+
'orderItemsData' => [
77+
[
78+
'getPrice' => 32.00,
79+
'getPriceInclTax' => 32.00,
80+
'getOriginalPrice' => 32.00,
81+
'getDiscountAmount' => 0.00,
82+
'getQtyOrdered' => 1,
83+
]
84+
],
85+
'expectedResult' => [
86+
[
87+
'price' => 32.00,
88+
'discount' => 0.00,
89+
'quantity' => 1,
90+
]
91+
],
92+
'expectedTotalRevenue' => 32.00
93+
],
94+
[ // One item (tax excluded)
95+
'priceIncludesTax' => false,
96+
'orderItemsData' => [
97+
[
98+
'getPrice' => 25.00,
99+
'getPriceInclTax' => 32.00,
100+
'getOriginalPrice' => 25.00,
101+
'getDiscountAmount' => 0.00,
102+
'getQtyOrdered' => 1,
103+
]
104+
],
105+
'expectedResult' => [
106+
[
107+
'price' => 25.00,
108+
'discount' => 0.00,
109+
'quantity' => 1,
110+
]
111+
],
112+
'expectedTotalRevenue' => 25.00
113+
],
114+
[ // One item with discount
115+
'priceIncludesTax' => true,
116+
'orderItemsData' => [
117+
[
118+
'getPrice' => 32.00,
119+
'getPriceInclTax' => 32.00,
120+
'getOriginalPrice' => 32.00,
121+
'getDiscountAmount' => 7.00,
122+
'getQtyOrdered' => 1,
123+
]
124+
],
125+
'expectedResult' => [
126+
[
127+
'price' => 25.00,
128+
'discount' => 7.00,
129+
'quantity' => 1,
130+
]
131+
],
132+
'expectedTotalRevenue' => 25.00
133+
],
134+
[ // One item with discount (tax excluded)
135+
'priceIncludesTax' => false,
136+
'orderItemsData' => [
137+
[
138+
'getPrice' => 25.00,
139+
'getPriceInclTax' => 32.00,
140+
'getOriginalPrice' => 25.00,
141+
'getDiscountAmount' => 7.00,
142+
'getQtyOrdered' => 1,
143+
]
144+
],
145+
'expectedResult' => [
146+
[
147+
'price' => 18.00,
148+
'discount' => 7.00,
149+
'quantity' => 1,
150+
]
151+
],
152+
'expectedTotalRevenue' => 18.00
153+
],
154+
[ // Two items
155+
'priceIncludesTax' => true,
156+
'orderItemsData' => [
157+
[
158+
'getPrice' => 32.00,
159+
'getPriceInclTax' => 32.00,
160+
'getOriginalPrice' => 32.00,
161+
'getDiscountAmount' => 7.00,
162+
'getQtyOrdered' => 1,
163+
],
164+
[
165+
'getPrice' => 32.00,
166+
'getPriceInclTax' => 32.00,
167+
'getOriginalPrice' => 32.00,
168+
'getDiscountAmount' => 0.00,
169+
'getQtyOrdered' => 2,
170+
],
171+
],
172+
'expectedResult' => [
173+
[
174+
'price' => 25.00,
175+
'discount' => 7.00,
176+
'quantity' => 1,
177+
],
178+
[
179+
'price' => 32.00,
180+
'discount' => 0.00,
181+
'quantity' => 2,
182+
]
183+
],
184+
'expectedTotalRevenue' => 89.00 // 25 + 32*2
185+
],
186+
];
187+
}
188+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Unit\Service;
4+
5+
use Algolia\AlgoliaSearch\Service\Insights\EventProcessor;
6+
7+
class EventProcessorTestable extends EventProcessor
8+
{
9+
public function getObjectDataForPurchase(...$params): array
10+
{
11+
return parent::getObjectDataForPurchase(...$params);
12+
}
13+
14+
public function getTotalRevenueForEvent(...$params): float
15+
{
16+
return parent::getTotalRevenueForEvent(...$params);
17+
}
18+
}

0 commit comments

Comments
 (0)