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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
use Piwik\Access;
use Piwik\Filesystem;
use Piwik\MetricsFormatter;
use Piwik\Piwik;
use Piwik\Plugins\SitesManager\API;
use Piwik\Translate;
/**
* Class Core_PiwikTest
*
* @group Core
*/
class Core_PiwikTest extends DatabaseTestCase
{
public function setUp()
{
parent::setUp();
}
/**
* Tests the generated JS code
* @group Core
*/
public function testJavascriptTrackingCode_withAllOptions()
{
$jsTag = Piwik::getJavascriptCode($idSite = 1, $piwikUrl = 'http://localhost/piwik',
$mergeSubdomains = true, $groupPageTitlesByDomain = true, $mergeAliasUrls = true,
$visitorCustomVariables = array( array("name", "value"), array("name 2", "value 2") ),
$pageCustomVariables = array( array("page cvar", "page cvar value") ),
$customCampaignNameQueryParam = "campaignKey", $customCampaignKeywordParam = "keywordKey",
$doNotTrack = true);
$expected = "<!-- Piwik -->
<script type="text/javascript">
var _paq = _paq || [];
_paq.push([\"setDocumentTitle\", document.domain + \"/\" + document.title]);
// you can set up to 5 custom variables for each visitor
_paq.push([\"setCustomVariable\", 1, \"name\", \"value\", \"visit\"]);
_paq.push([\"setCustomVariable\", 2, \"name 2\", \"value 2\", \"visit\"]);
// you can set up to 5 custom variables for each action (page view, download, click, site search)
_paq.push([\"setCustomVariable\", 1, \"page cvar\", \"page cvar value\", \"page\"]);
_paq.push([\"setCampaignNameKey\", \"campaignKey\"]);
_paq.push([\"setCampaignKeywordKey\", \"keywordKey\"]);
_paq.push([\"setDoNotTrack\", true]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//localhost/piwik/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', 1]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<noscript><p><img src="//localhost/piwik/piwik.php?idsite=1" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->
";
$this->assertEquals($expected, $jsTag);
}
/**
* Tests the generated JS code with protocol overwrite
* @group Core
*/
public function testJavascriptTrackingCode_withAllOptionsAndProtocolOverwrite()
{
Piwik::addAction('Piwik.getJavascriptCode', function (&$codeImpl) {
$codeImpl['protocol'] = 'https://';
});
$jsTag = Piwik::getJavascriptCode($idSite = 1, $piwikUrl = 'http://localhost/piwik',
$mergeSubdomains = true, $groupPageTitlesByDomain = true, $mergeAliasUrls = true,
$visitorCustomVariables = array( array("name", "value"), array("name 2", "value 2") ),
$pageCustomVariables = array( array("page cvar", "page cvar value") ),
$customCampaignNameQueryParam = "campaignKey", $customCampaignKeywordParam = "keywordKey",
$doNotTrack = true);
$expected = "<!-- Piwik -->
<script type="text/javascript">
var _paq = _paq || [];
_paq.push([\"setDocumentTitle\", document.domain + \"/\" + document.title]);
// you can set up to 5 custom variables for each visitor
_paq.push([\"setCustomVariable\", 1, \"name\", \"value\", \"visit\"]);
_paq.push([\"setCustomVariable\", 2, \"name 2\", \"value 2\", \"visit\"]);
// you can set up to 5 custom variables for each action (page view, download, click, site search)
_paq.push([\"setCustomVariable\", 1, \"page cvar\", \"page cvar value\", \"page\"]);
_paq.push([\"setCampaignNameKey\", \"campaignKey\"]);
_paq.push([\"setCampaignKeywordKey\", \"keywordKey\"]);
_paq.push([\"setDoNotTrack\", true]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://localhost/piwik/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', 1]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<noscript><p><img src="https://localhost/piwik/piwik.php?idsite=1" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->
";
$this->assertEquals($expected, $jsTag);
}
/**
* Tests the generated JS code with options before tracker url
* @group Core
*/
public function testJavascriptTrackingCode_withAllOptionsAndOptionsBeforeTrackerUrl()
{
Piwik::addAction('Piwik.getJavascriptCode', function (&$codeImpl) {
$codeImpl['optionsBeforeTrackerUrl'] .= "_paq.push(['setAPIUrl', 'http://localhost/statistics']);\n ";
});
$jsTag = Piwik::getJavascriptCode($idSite = 1, $piwikUrl = 'http://localhost/piwik',
$mergeSubdomains = true, $groupPageTitlesByDomain = true, $mergeAliasUrls = true,
$visitorCustomVariables = array( array("name", "value"), array("name 2", "value 2") ),
$pageCustomVariables = array( array("page cvar", "page cvar value") ),
$customCampaignNameQueryParam = "campaignKey", $customCampaignKeywordParam = "keywordKey",
$doNotTrack = true);
$expected = "<!-- Piwik -->
<script type="text/javascript">
var _paq = _paq || [];
_paq.push([\"setDocumentTitle\", document.domain + \"/\" + document.title]);
// you can set up to 5 custom variables for each visitor
_paq.push([\"setCustomVariable\", 1, \"name\", \"value\", \"visit\"]);
_paq.push([\"setCustomVariable\", 2, \"name 2\", \"value 2\", \"visit\"]);
// you can set up to 5 custom variables for each action (page view, download, click, site search)
_paq.push([\"setCustomVariable\", 1, \"page cvar\", \"page cvar value\", \"page\"]);
_paq.push([\"setCampaignNameKey\", \"campaignKey\"]);
_paq.push([\"setCampaignKeywordKey\", \"keywordKey\"]);
_paq.push([\"setDoNotTrack\", true]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//localhost/piwik/";
_paq.push(['setAPIUrl', 'http://localhost/statistics']);
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', 1]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<noscript><p><img src="//localhost/piwik/piwik.php?idsite=1" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->
";
$this->assertEquals($expected, $jsTag);
}
/**
* Dataprovider for testIsNumericValid
*/
public function getValidNumeric()
{
$valid = array(
-1, 0, 1, 1.5, -1.5, 21111, 89898, 99999999999, -4565656,
(float)-1, (float)0, (float)1, (float)1.5, (float)-1.5, (float)21111, (float)89898, (float)99999999999, (float)-4565656,
(int)-1, (int)0, (int)1, (int)1.5, (int)-1.5, (int)21111, (int)89898, (int)99999999999, (int)-4565656,
'-1', '0', '1', '1.5', '-1.5', '21111', '89898', '99999999999', '-4565656',
'1e3', '0x123', "-1e-2",
);
foreach ($valid as $key => $value) {
$valid[$key] = array($value);
}
return $valid;
}
/**
* @dataProvider getValidNumeric
*/
public function testIsNumericValid($toTest)
{
$this->assertTrue(is_numeric($toTest), $toTest . " not valid but should!");
}
/**
* Dataprovider for testIsNumericNotValid
*/
public function getInvalidNumeric()
{
$notValid = array(
'-1.0.0', '1,2', '--1', '-.', '- 1', '1-',
);
foreach ($notValid as $key => $value) {
$notValid[$key] = array($value);
}
return $notValid;
}
/**
* @dataProvider getInvalidNumeric
*/
public function testIsNumericNotValid($toTest)
{
$this->assertFalse(is_numeric($toTest), $toTest . " valid but shouldn't!");
}
public function testSecureDiv()
{
$this->assertSame(3, Piwik::secureDiv(9, 3));
$this->assertSame(0, Piwik::secureDiv(9, 0));
$this->assertSame(10, Piwik::secureDiv(10, 1));
$this->assertSame(10.0, Piwik::secureDiv(10.0, 1.0));
$this->assertSame(5.5, Piwik::secureDiv(11.0, 2));
$this->assertSame(0, Piwik::secureDiv(11.0, 'a'));
}
/**
* Dataprovider for testGetPrettyTimeFromSeconds
*/
public function getPrettyTimeFromSecondsData()
{
return array(
array(30, array('30s', '00:00:30')),
array(60, array('1 min 0s', '00:01:00')),
array(100, array('1 min 40s', '00:01:40')),
array(3600, array('1 hours 0 min', '01:00:00')),
array(3700, array('1 hours 1 min', '01:01:40')),
array(86400 + 3600 * 10, array('1 days 10 hours', '34:00:00')),
array(86400 * 365, array('365 days 0 hours', '8760:00:00')),
array((86400 * (365.25 + 10)), array('1 years 10 days', '9006:00:00')),
array(1.342, array('1.34s', '00:00:01.34')),
array(.342, array('0.34s', '00:00:00.34')),
array(.02, array('0.02s', '00:00:00.02')),
array(.002, array('0.002s', '00:00:00')),
array(1.002, array('1s', '00:00:01')),
array(1.02, array('1.02s', '00:00:01.02')),
array(1.2, array('1.2s', '00:00:01.20')),
array(122.1, array('2 min 2.1s', '00:02:02.10')),
array(-122.1, array('-2 min 2.1s', '-00:02:02.10')),
array(86400 * -365, array('-365 days 0 hours', '-8760:00:00'))
);
}
/**
* @dataProvider getPrettyTimeFromSecondsData
*/
public function testGetPrettyTimeFromSeconds($seconds, $expected)
{
if (($seconds * 100) > PHP_INT_MAX) {
$this->markTestSkipped("Will not pass on 32-bit machine.");
}
Translate::loadEnglishTranslation();
$sentenceExpected = str_replace(' ', ' ', $expected[0]);
$numericExpected = $expected[1];
$this->assertEquals($sentenceExpected, MetricsFormatter::getPrettyTimeFromSeconds($seconds, $sentence = true));
$this->assertEquals($numericExpected, MetricsFormatter::getPrettyTimeFromSeconds($seconds, $sentence = false));
Translate::unloadEnglishTranslation();
}
/**
* Dataprovider for testCheckValidLoginString
*/
public function getInvalidLoginStringData()
{
$notValid = array(
'',
' ',
'a',
'aa',
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'alpha/beta',
'alpha:beta',
'alpha;beta',
'alpha<beta',
'alpha=beta',
'alpha>beta',
'alpha?beta',
);
foreach ($notValid as $key => $value) {
$notValid[$key] = array($value);
}
return $notValid;
}
/**
* @dataProvider getInvalidLoginStringData
* @expectedException \Exception
*/
public function testCheckInvalidLoginString($toTest)
{
Piwik::checkValidLoginString($toTest);
}
/**
* Dataprovider for testCheckValidLoginString
*/
public function getValidLoginStringData()
{
$valid = array(
'aaa',
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'shoot_puck@the-goal.com',
);
foreach ($valid as $key => $value) {
$valid[$key] = array($value);
}
return $valid;
}
/**
* @dataProvider getValidLoginStringData
*/
public function testCheckValidLoginString($toTest)
{
$this->assertNull(Piwik::checkValidLoginString($toTest));
}
/**
* Dataprovider for testGetPrettyValue
*/
public function getGetPrettyValueTestCases()
{
return array(
array('revenue', 12, '$ 12'),
array('revenue_evolution', '100 %', '100 %'),
array('avg_time_generation', '3.333', '3.33s'),
array('avg_time_generation', '333.333', '5 min 33.33s'),
array('avg_time_on_page', '3', '00:00:03'),
array('avg_time_on_page', '333', '00:05:33'),
);
}
/**
* @dataProvider getGetPrettyValueTestCases
*/
public function testGetPrettyValue($columnName, $value, $expected)
{
Translate::loadEnglishTranslation();
$access = Access::getInstance();
$access->setSuperUserAccess(true);
$idsite = API::getInstance()->addSite("test", "http://test");
$this->assertEquals(
$expected,
MetricsFormatter::getPrettyValue($idsite, $columnName, $value, false, false)
);
Translate::unloadEnglishTranslation();
}
/**
* Data provider for testIsAssociativeArray.
*/
public function getIsAssociativeArrayTestCases()
{
return array(
array(array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd', 4 => 'e', 5 => 'f'), false),
array(array(-1 => 'a', 0 => 'a', 1 => 'a', 2 => 'a', 3 => 'a'), true),
array(array(4 => 'a', 5 => 'a', 6 => 'a', 7 => 'a', 8 => 'a'), true),
array(array(0 => 'a', 2 => 'a', 3 => 'a', 4 => 'a', 5 => 'a'), true),
array(array('abc' => 'a', 0 => 'b', 'sdfds' => 'd'), true),
array(array('abc' => 'def'), true)
);
}
/**
* @dataProvider getIsAssociativeArrayTestCases
*/
public function testIsAssociativeArray($array, $expected)
{
$this->assertEquals($expected, Piwik::isAssociativeArray($array));
}
public function testCheckIfFileSystemIsNFSOnNonNFS()
{
$this->assertFalse(Filesystem::checkIfFileSystemIsNFS());
}
}
|