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
|
<?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;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
/**
* Class Core_PiwikTest
*
* @group Core
*/
class Core_PiwikTest extends IntegrationTestCase
{
/**
* 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));
}
}
|