1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GoogleAnalytics\Test\Unit\Block;
use Magento\Framework\Escaper;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\View\Element\Template\Context;
use Magento\GoogleAnalytics\Block\Ga;
use Magento\GoogleAnalytics\Helper\Data;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\ResourceModel\Order\Collection;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use PHPUnit\Framework\TestCase;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class GaTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Ga | \PHPUnit_Framework_MockObject_MockObject
*/
protected $gaBlock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $cookieHelperMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $salesOrderCollectionMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $storeManagerMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $storeMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $googleAnalyticsDataMock;
protected function setUp()
{
$objectManager = new ObjectManager($this);
$contextMock = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
$contextMock->expects($this->once())
->method('getEscaper')
->willReturn($objectManager->getObject(Escaper::class));
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->storeMock = $this->getMockBuilder(Store::class)->disableOriginalConstructor()->getMock();
$contextMock->expects($this->once())->method('getStoreManager')->willReturn($this->storeManagerMock);
$this->salesOrderCollectionMock = $this->getMockBuilder(CollectionFactory::class)
->disableOriginalConstructor()
->getMock();
$this->googleAnalyticsDataMock = $this->getMockBuilder(Data::class)
->disableOriginalConstructor()
->getMock();
$this->cookieHelperMock = $this->getMockBuilder(\Magento\Cookie\Helper\Cookie::class)
->disableOriginalConstructor()
->getMock();
$this->gaBlock = $objectManager->getObject(
Ga::class,
[
'context' => $contextMock,
'salesOrderCollection' => $this->salesOrderCollectionMock,
'googleAnalyticsData' => $this->googleAnalyticsDataMock,
'cookieHelper' => $this->cookieHelperMock
]
);
}
public function testOrderTrackingCode()
{
$this->salesOrderCollectionMock->expects($this->once())
->method('create')
->willReturn($this->createCollectionMock());
$this->storeMock->expects($this->once())->method('getFrontendName')->willReturn('test');
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
$expectedCode = "ga('require', 'ec', 'ec.js');
ga('set', 'currencyCode', 'USD');
ga('ec:addProduct', {
'id': 'sku0',
'name': 'testName0',
'price': '0.00',
'quantity': 1
});
ga('ec:setAction', 'purchase', {
'id': '100',
'affiliation': 'test',
'revenue': '10',
'tax': '2',
'shipping': '1'
});
ga('send', 'pageview');";
$this->gaBlock->setOrderIds([1, 2]);
$this->assertEquals(
$this->packString($expectedCode),
$this->packString($this->gaBlock->getOrdersTrackingCode())
);
}
public function testIsCookieRestrictionModeEnabled()
{
$this->cookieHelperMock->expects($this->once())->method('isCookieRestrictionModeEnabled')->willReturn(false);
$this->assertFalse($this->gaBlock->isCookieRestrictionModeEnabled());
}
public function testGetCurrentWebsiteId()
{
$websiteId = 100;
$websiteMock = $this->getMockBuilder(\Magento\Store\Api\Data\WebsiteInterface::class)->getMock();
$websiteMock->expects($this->once())->method('getId')->willReturn($websiteId);
$this->storeManagerMock->expects($this->once())->method('getWebsite')->willReturn($websiteMock);
$this->assertEquals($websiteId, $this->gaBlock->getCurrentWebsiteId());
}
public function testOrderTrackingData()
{
$this->salesOrderCollectionMock->expects($this->once())
->method('create')
->willReturn($this->createCollectionMock());
$this->storeMock->expects($this->once())->method('getFrontendName')->willReturn('test');
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
$expectedResult = [
'orders' => [
[
'id' => 100,
'affiliation' => 'test',
'revenue' => 10,
'tax' => 2,
'shipping' => 1
]
],
'products' => [
[
'id' => 'sku0',
'name' => 'testName0',
'price' => 0.00,
'quantity' => 1
]
],
'currency' => 'USD'
];
$this->gaBlock->setOrderIds([1, 2]);
$this->assertEquals($expectedResult, $this->gaBlock->getOrdersTrackingData());
}
public function testGetPageTrackingData()
{
$pageName = '/page/name';
$accountId = 100;
$expectedResult = [
'optPageUrl' => ", '" . $pageName . "'",
'isAnonymizedIpActive' => true,
'accountId' => $accountId
];
$this->gaBlock->setData('page_name', $pageName);
$this->googleAnalyticsDataMock->expects($this->once())->method('isAnonymizedIpActive')->willReturn(true);
$this->assertEquals($expectedResult, $this->gaBlock->getPageTrackingData($accountId));
}
/**
* Create Order mock with $orderItemCount items
*
* @param int $orderItemCount
* @return Order|\PHPUnit_Framework_MockObject_MockObject
*/
protected function createOrderMock($orderItemCount = 1)
{
$orderItems = [];
for ($i = 0; $i < $orderItemCount; $i++) {
$orderItemMock = $this->getMockBuilder(OrderItemInterface::class)
->disableOriginalConstructor()
->getMock();
$orderItemMock->expects($this->once())->method('getSku')->willReturn('sku' . $i);
$orderItemMock->expects($this->once())->method('getName')->willReturn('testName' . $i);
$orderItemMock->expects($this->once())->method('getPrice')->willReturn($i . '.00');
$orderItemMock->expects($this->once())->method('getQtyOrdered')->willReturn($i + 1);
$orderItems[] = $orderItemMock;
}
$orderMock = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
$orderMock->expects($this->once())->method('getIncrementId')->willReturn(100);
$orderMock->expects($this->once())->method('getAllVisibleItems')->willReturn($orderItems);
$orderMock->expects($this->once())->method('getGrandTotal')->willReturn(10);
$orderMock->expects($this->once())->method('getTaxAmount')->willReturn(2);
$orderMock->expects($this->once())->method('getShippingAmount')->willReturn($orderItemCount);
$orderMock->expects($this->once())->method('getOrderCurrencyCode')->willReturn('USD');
return $orderMock;
}
/**
* @return Collection | \PHPUnit_Framework_MockObject_MockObject
*/
protected function createCollectionMock()
{
$collectionMock = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->getMock();
$collectionMock->expects($this->any())
->method('getIterator')
->willReturn(new \ArrayIterator([$this->createOrderMock(1)]));
return $collectionMock;
}
/**
* Removes from $string whitespace characters
*
* @param string $string
* @return string
*/
protected function packString($string)
{
return preg_replace('/\s/', '', $string);
}
}