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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\ScheduledReports\tests;
use Piwik\Access;
use Piwik\Plugins\MobileMessaging\API as APIMobileMessaging;
use Piwik\Plugins\MobileMessaging\MobileMessaging;
use Piwik\Plugins\ScheduledReports\API as APIScheduledReports;
use Piwik\Plugins\ScheduledReports\Menu;
use Piwik\Plugins\ScheduledReports\Tasks;
use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\ScheduledTask;
use Piwik\ScheduledTime\Monthly;
use Piwik\ScheduledTime;
use Piwik\Site;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use FakeAccess;
use Exception;
use ReflectionMethod;
require_once PIWIK_INCLUDE_PATH . '/plugins/ScheduledReports/ScheduledReports.php';
/**
* Class Plugins_ScheduledReportsTest
*
* @group Plugins
* @group ScheduledReportsTest
*/
class ApiTest extends IntegrationTestCase
{
private $idSite = 1;
public function setUp()
{
parent::setUp();
// setup the access layer
self::setSuperUser();
\Piwik\Plugin\Manager::getInstance()->loadPlugins(array('API', 'UserCountry', 'ScheduledReports', 'MobileMessaging'));
\Piwik\Plugin\Manager::getInstance()->installLoadedPlugins();
APISitesManager::getInstance()->addSite("Test", array("http://piwik.net"));
APISitesManager::getInstance()->addSite("Test", array("http://piwik.net"));
FakeAccess::setIdSitesView(array($this->idSite, 2));
APIScheduledReports::$cache = array();
}
/**
* @group Plugins
*/
public function testAddReportGetReports()
{
$data = array(
'idsite' => $this->idSite,
'description' => 'test description"',
'type' => 'email',
'period' => ScheduledTime::PERIOD_DAY,
'hour' => '4',
'format' => 'pdf',
'reports' => array('UserCountry_getCountry'),
'parameters' => array(
'displayFormat' => '1',
'emailMe' => true,
'additionalEmails' => array('test@test.com', 't2@test.com'),
'evolutionGraph' => true
)
);
$dataWebsiteTwo = $data;
$dataWebsiteTwo['idsite'] = 2;
$dataWebsiteTwo['period'] = ScheduledTime::PERIOD_MONTH;
self::addReport($dataWebsiteTwo);
// Testing getReports without parameters
$tmp = APIScheduledReports::getInstance()->getReports();
$report = reset($tmp);
$this->assertReportsEqual($report, $dataWebsiteTwo);
$idReport = self::addReport($data);
// Passing 3 parameters
$tmp = APIScheduledReports::getInstance()->getReports($this->idSite, $data['period'], $idReport);
$report = reset($tmp);
$this->assertReportsEqual($report, $data);
// Passing only idsite
$tmp = APIScheduledReports::getInstance()->getReports($this->idSite);
$report = reset($tmp);
$this->assertReportsEqual($report, $data);
// Passing only period
$tmp = APIScheduledReports::getInstance()->getReports($idSite = false, $data['period']);
$report = reset($tmp);
$this->assertReportsEqual($report, $data);
// Passing only idreport
$tmp = APIScheduledReports::getInstance()->getReports($idSite = false, $period = false, $idReport);
$report = reset($tmp);
$this->assertReportsEqual($report, $data);
}
/**
* @group Plugins
*/
public function testGetReportsIdReportNotFound()
{
try {
APIScheduledReports::getInstance()->getReports($idSite = false, $period = false, $idReport = 1);
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* @group Plugins
*/
public function testGetReportsInvalidPermission()
{
try {
APIScheduledReports::getInstance()->getReports(
$idSite = 44,
$period = false,
self::addReport(self::getDailyPDFReportData($this->idSite))
);
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* @group Plugins
*/
public function testAddReportInvalidWebsite()
{
try {
self::addReport(self::getDailyPDFReportData(33));
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* @group Plugins
*/
public function testAddReportInvalidPeriod()
{
try {
$data = self::getDailyPDFReportData($this->idSite);
$data['period'] = 'dx';
self::addReport($data);
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* @group Plugins
*/
public function testUpdateReport()
{
$idReport = self::addReport(self::getDailyPDFReportData($this->idSite));
$dataAfter = self::getMonthlyEmailReportData($this->idSite);
self::updateReport($idReport, $dataAfter);
$reports = APIScheduledReports::getInstance()->getReports($idSite = false, $period = false, $idReport);
$this->assertReportsEqual(
reset($reports),
$dataAfter
);
}
/**
* @group Plugins
*/
public function testDeleteReport()
{
// Deletes non existing report throws exception
try {
APIScheduledReports::getInstance()->deleteReport($idReport = 1);
$this->fail('Exception not raised');
} catch (Exception $e) {
}
$idReport = self::addReport(self::getMonthlyEmailReportData($this->idSite));
$this->assertEquals(1, count(APIScheduledReports::getInstance()->getReports()));
APIScheduledReports::getInstance()->deleteReport($idReport);
$this->assertEquals(0, count(APIScheduledReports::getInstance()->getReports()));
}
/**
* @group Plugins
*/
public function testGetTopMenuTranslationKeyMobileMessagingInactive()
{
// unload MobileMessaging plugin
\Piwik\Plugin\Manager::getInstance()->loadPlugins(array('ScheduledReports'));
$pdfReportPlugin = new Menu();
$this->assertEquals(
Menu::PDF_REPORTS_TOP_MENU_TRANSLATION_KEY,
$pdfReportPlugin->getTopMenuTranslationKey()
);
}
/**
* @group Plugins
*/
public function testGetTopMenuTranslationKeyUserIsAnonymous()
{
$anonymousAccess = new FakeAccess;
FakeAccess::$identity = 'anonymous';
Access::setSingletonInstance($anonymousAccess);
$pdfReportPlugin = new Menu();
$this->assertEquals(
Menu::MOBILE_MESSAGING_TOP_MENU_TRANSLATION_KEY,
$pdfReportPlugin->getTopMenuTranslationKey()
);
}
/**
* top menu should display 'Email & SMS reports' when the user has set-up a valid mobile provider account
* even though there is no sms reports configured
*
* @group Plugins
*/
public function testGetTopMenuTranslationKeyNoReportMobileAccountOK()
{
// set mobile provider account
self::setSuperUser();
APIMobileMessaging::getInstance()->setSMSAPICredential('StubbedProvider', '');
$pdfReportPlugin = new Menu();
$this->assertEquals(
Menu::MOBILE_MESSAGING_TOP_MENU_TRANSLATION_KEY,
$pdfReportPlugin->getTopMenuTranslationKey()
);
}
/**
* top menu should display 'Email reports' when the user has not set-up a valid mobile provider account
* and no reports at all have been configured
*
* @group Plugins
*/
public function testGetTopMenuTranslationKeyNoReportMobileAccountKO()
{
$pdfReportPlugin = new Menu();
$this->assertEquals(
Menu::PDF_REPORTS_TOP_MENU_TRANSLATION_KEY,
$pdfReportPlugin->getTopMenuTranslationKey()
);
}
/**
* top menu should display 'Email & SMS reports' if there is at least one sms report
* whatever the status of the mobile provider account
*
* @group Plugins
*/
public function testGetTopMenuTranslationKeyOneSMSReportMobileAccountKO()
{
APIScheduledReports::getInstance()->addReport(
1,
'',
ScheduledTime::PERIOD_DAY,
0,
MobileMessaging::MOBILE_TYPE,
MobileMessaging::SMS_FORMAT,
array(),
array(
MobileMessaging::PHONE_NUMBERS_PARAMETER => array()
)
);
$pdfReportPlugin = new Menu();
$this->assertEquals(
Menu::MOBILE_MESSAGING_TOP_MENU_TRANSLATION_KEY,
$pdfReportPlugin->getTopMenuTranslationKey()
);
}
/**
* top menu should display 'Email reports' if there are no SMS reports and at least one email report
* whatever the status of the mobile provider account
*
* @group Plugins
*/
public function testGetTopMenuTranslationKeyNoSMSReportAccountOK()
{
// set mobile provider account
self::setSuperUser();
APIMobileMessaging::getInstance()->setSMSAPICredential('StubbedProvider', '');
self::addReport(self::getMonthlyEmailReportData($this->idSite));
$pdfReportPlugin = new Menu();
$this->assertEquals(
Menu::PDF_REPORTS_TOP_MENU_TRANSLATION_KEY,
$pdfReportPlugin->getTopMenuTranslationKey()
);
}
/**
* @group Plugins
*/
public function testGetScheduledTasks()
{
// stub API to control getReports() return values
$report1 = self::getDailyPDFReportData($this->idSite);
$report1['idreport'] = 1;
$report1['hour'] = 0;
$report1['deleted'] = 0;
$report2 = self::getMonthlyEmailReportData($this->idSite);
$report2['idreport'] = 2;
$report2['idsite'] = 2;
$report2['hour'] = 0;
$report2['deleted'] = 0;
$report3 = self::getMonthlyEmailReportData($this->idSite);
$report3['idreport'] = 3;
$report3['deleted'] = 1; // should not be scheduled
$report4 = self::getMonthlyEmailReportData($this->idSite);
$report4['idreport'] = 4;
$report4['idsite'] = 1;
$report4['hour'] = 8;
$report4['deleted'] = 0;
$report5 = self::getMonthlyEmailReportData($this->idSite);
$report5['idreport'] = 5;
$report5['idsite'] = 2;
$report5['hour'] = 8;
$report5['deleted'] = 0;
// test no exception is raised when a scheduled report is set to never send
$report6 = self::getMonthlyEmailReportData($this->idSite);
$report6['idreport'] = 6;
$report6['period'] = ScheduledTime::PERIOD_NEVER;
$report6['deleted'] = 0;
$stubbedAPIScheduledReports = $this->getMock('\\Piwik\\Plugins\\ScheduledReports\\API', array('getReports', 'getInstance'), $arguments = array(), $mockClassName = '', $callOriginalConstructor = false);
$stubbedAPIScheduledReports->expects($this->any())->method('getReports')->will($this->returnValue(
array($report1, $report2, $report3, $report4, $report5, $report6))
);
\Piwik\Plugins\ScheduledReports\API::setSingletonInstance($stubbedAPIScheduledReports);
// initialize sites 1 and 2
Site::setSites( array(
1 => array('timezone' => 'Europe/Paris'),
2 => array('timezone' => 'UTC-6.5'),
));
// expected tasks
$scheduleTask1 = ScheduledTime::factory('daily');
$scheduleTask1->setHour(0); // paris is UTC-1, period ends at 23h UTC
$scheduleTask1->setTimezone('Europe/Paris');
$scheduleTask2 = new Monthly();
$scheduleTask2->setHour(0); // site is UTC-6.5, period ends at 6h30 UTC, smallest resolution is hour
$scheduleTask2->setTimezone('UTC-6.5');
$scheduleTask3 = new Monthly();
$scheduleTask3->setHour(8); // paris is UTC-1, configured to be sent at 8h
$scheduleTask3->setTimezone('Europe/Paris');
$scheduleTask4 = new Monthly();
$scheduleTask4->setHour(8); // site is UTC-6.5, configured to be sent at 8h
$scheduleTask4->setTimezone('UTC-6.5');
$expectedTasks = array(
new ScheduledTask (APIScheduledReports::getInstance(), 'sendReport', 1, $scheduleTask1),
new ScheduledTask (APIScheduledReports::getInstance(), 'sendReport', 2, $scheduleTask2),
new ScheduledTask (APIScheduledReports::getInstance(), 'sendReport', 4, $scheduleTask3),
new ScheduledTask (APIScheduledReports::getInstance(), 'sendReport', 5, $scheduleTask4),
);
$pdfReportPlugin = new Tasks();
$pdfReportPlugin->schedule();
$tasks = $pdfReportPlugin->getScheduledTasks();
$this->assertEquals($expectedTasks, $tasks);
\Piwik\Plugins\ScheduledReports\API::unsetInstance();
}
/**
* Dataprovider for testGetReportSubjectAndReportTitle
*/
public function getGetReportSubjectAndReportTitleTestCases()
{
return array(
array('<Piwik.org>', '<Piwik.org>', '<Piwik.org>', array('UserSettings_getBrowserType')),
array('Piwik.org', 'Piwik.org', 'Piwik.org', array('MultiSites_getAll', 'UserSettings_getBrowserType')),
array('General_MultiSitesSummary', 'General_MultiSitesSummary', 'Piwik.org', array('MultiSites_getAll')),
);
}
/**
* @group Plugins
*
* @dataProvider getGetReportSubjectAndReportTitleTestCases
*/
public function testGetReportSubjectAndReportTitle($expectedReportSubject, $expectedReportTitle, $websiteName, $reports)
{
$getReportSubjectAndReportTitle = new ReflectionMethod(
'\\Piwik\\Plugins\\ScheduledReports\\API', 'getReportSubjectAndReportTitle'
);
$getReportSubjectAndReportTitle->setAccessible(true);
list($reportSubject, $reportTitle) = $getReportSubjectAndReportTitle->invoke( APIScheduledReports::getInstance(), $websiteName, $reports);
$this->assertEquals($expectedReportSubject, $reportSubject);
$this->assertEquals($expectedReportTitle, $reportTitle);
}
private function assertReportsEqual($report, $data)
{
foreach ($data as $key => $value) {
if ($key == 'description') $value = substr($value, 0, 250);
$this->assertEquals($value, $report[$key], "Error for $key for report " . var_export($report, true) . " and data " . var_export($data, true));
}
}
private static function addReport($data)
{
$idReport = APIScheduledReports::getInstance()->addReport(
$data['idsite'],
$data['description'],
$data['period'],
$data['hour'],
$data['type'],
$data['format'],
$data['reports'],
$data['parameters']
);
return $idReport;
}
private static function getDailyPDFReportData($idSite)
{
return array(
'idsite' => $idSite,
'description' => 'test description"',
'period' => ScheduledTime::PERIOD_DAY,
'hour' => '7',
'type' => 'email',
'format' => 'pdf',
'reports' => array('UserCountry_getCountry'),
'parameters' => array(
'displayFormat' => '1',
'emailMe' => true,
'additionalEmails' => array('test@test.com', 't2@test.com'),
'evolutionGraph' => false
)
);
}
private static function getMonthlyEmailReportData($idSite)
{
return array(
'idsite' => $idSite,
'description' => 'very very long and possibly truncated description. very very long and possibly truncated description. very very long and possibly truncated description. very very long and possibly truncated description. very very long and possibly truncated description. ',
'period' => ScheduledTime::PERIOD_MONTH,
'hour' => '0',
'type' => 'email',
'format' => 'pdf',
'reports' => array('UserCountry_getContinent'),
'parameters' => array(
'displayFormat' => '1',
'emailMe' => false,
'additionalEmails' => array('blabla@ec.fr'),
'evolutionGraph' => false
)
);
}
private static function updateReport($idReport, $data)
{
$idReport = APIScheduledReports::getInstance()->updateReport(
$idReport,
$data['idsite'],
$data['description'],
$data['period'],
$data['hour'],
$data['type'],
$data['format'],
$data['reports'],
$data['parameters']);
return $idReport;
}
private static function setSuperUser()
{
$pseudoMockAccess = new FakeAccess;
FakeAccess::$superUser = true;
Access::setSingletonInstance($pseudoMockAccess);
}
}
|