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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
|
<?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\SitesManager\tests\Integration;
use Piwik\Access;
use Piwik\Plugins\SitesManager\API;
use Piwik\Plugins\UsersManager\API as APIUsersManager;
use Piwik\Site;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use FakeAccess;
use Exception;
use PHPUnit_Framework_Constraint_IsType;
/**
* Class Plugins_SitesManagerTest
*
* @group Plugins
*/
class SitesManagerTest extends IntegrationTestCase
{
public function setUp()
{
parent::setUp();
// setup the access layer
$pseudoMockAccess = new FakeAccess;
FakeAccess::$superUser = true;
Access::setSingletonInstance($pseudoMockAccess);
}
/**
* empty name -> exception
*
* @group Plugins
*/
public function testAddSiteEmptyName()
{
try {
API::getInstance()->addSite("", array("http://piwik.net"));
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* DataProvider for testAddSiteWrongUrls
*/
public function getInvalidUrlData()
{
return array(
array(array()), // no urls
array(array("")),
array(""),
array("httpww://piwik.net"),
array("httpww://piwik.net/gqg~#"),
);
}
/**
* wrong urls -> exception
*
* @dataProvider getInvalidUrlData
* @group Plugins
*/
public function testAddSiteWrongUrls($url)
{
try {
API::getInstance()->addSite("name", $url);
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* Test with valid IPs
*
* @group Plugins
*/
public function testAddSiteExcludedIpsAndtimezoneAndCurrencyAndExcludedQueryParametersValid()
{
$ips = '1.2.3.4,1.1.1.*,1.2.*.*,1.*.*.*';
$timezone = 'Europe/Paris';
$currency = 'EUR';
$excludedQueryParameters = 'p1,P2, P33333';
$expectedExcludedQueryParameters = 'p1,P2,P33333';
$excludedUserAgents = " p1,P2, \nP3333 ";
$expectedExcludedUserAgents = "p1,P2,P3333";
$expectedWebsiteType = 'mobile-\'app';
$keepUrlFragment = 1;
$idsite = API::getInstance()->addSite("name", "http://piwik.net/", $ecommerce = 1,
$siteSearch = 1, $searchKeywordParameters = 'search,param', $searchCategoryParameters = 'cat,category',
$ips, $excludedQueryParameters, $timezone, $currency, $group = null, $startDate = null, $excludedUserAgents,
$keepUrlFragment, $expectedWebsiteType);
$siteInfo = API::getInstance()->getSiteFromId($idsite);
$this->assertEquals($ips, $siteInfo['excluded_ips']);
$this->assertEquals($timezone, $siteInfo['timezone']);
$this->assertEquals($currency, $siteInfo['currency']);
$this->assertEquals($ecommerce, $siteInfo['ecommerce']);
$this->assertTrue(Site::isEcommerceEnabledFor($idsite));
$this->assertEquals($siteSearch, $siteInfo['sitesearch']);
$this->assertTrue(Site::isSiteSearchEnabledFor($idsite));
$this->assertEquals($expectedWebsiteType, $siteInfo['type']);
$this->assertEquals($expectedWebsiteType, Site::getTypeFor($idsite));
$this->assertEquals($searchKeywordParameters, $siteInfo['sitesearch_keyword_parameters']);
$this->assertEquals($searchCategoryParameters, $siteInfo['sitesearch_category_parameters']);
$this->assertEquals($expectedExcludedQueryParameters, $siteInfo['excluded_parameters']);
$this->assertEquals($expectedExcludedUserAgents, $siteInfo['excluded_user_agents']);
}
/**
* dataProvider for testAddSiteExcludedIpsNotValid
*/
public function getInvalidIPsData()
{
return array(
array('35817587341'),
array('ieagieha'),
array('1.2.3'),
array('*.1.1.1'),
array('*.*.1.1'),
array('*.*.*.1'),
array('1.1.1.1.1'),
);
}
/**
* Test with invalid IPs
*
* @dataProvider getInvalidIPsData
* @group Plugins
*/
public function testAddSiteExcludedIpsNotValid($ip)
{
try {
API::getInstance()->addSite("name", "http://piwik.net/", $ecommerce = 0,
$siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null, $ip);
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* one url -> one main_url and nothing inserted as alias urls
*
* @group Plugins
*/
public function testAddSiteOneUrl()
{
$url = "http://piwik.net/";
$urlOK = "http://piwik.net";
$idsite = API::getInstance()->addSite("name", $url);
$this->assertInternalType(PHPUnit_Framework_Constraint_IsType::TYPE_INT, $idsite);
$siteInfo = API::getInstance()->getSiteFromId($idsite);
$this->assertEquals($urlOK, $siteInfo['main_url']);
$this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($siteInfo['ts_created'])));
$siteUrls = API::getInstance()->getSiteUrlsFromId($idsite);
$this->assertEquals(1, count($siteUrls));
}
/**
* several urls -> one main_url and others as alias urls
*
* @group Plugins
*/
public function testAddSiteSeveralUrls()
{
$urls = array("http://piwik.net/", "http://piwik.com", "https://piwik.net/test/");
$urlsOK = array("http://piwik.net", "http://piwik.com", "https://piwik.net/test");
$idsite = API::getInstance()->addSite("super website", $urls);
$this->assertInternalType(PHPUnit_Framework_Constraint_IsType::TYPE_INT, $idsite);
$siteInfo = API::getInstance()->getSiteFromId($idsite);
$this->assertEquals($urlsOK[0], $siteInfo['main_url']);
$siteUrls = API::getInstance()->getSiteUrlsFromId($idsite);
$this->assertEquals($urlsOK, $siteUrls);
}
/**
* strange name
*
* @group Plugins
*/
public function testAddSiteStrangeName()
{
$name = "supertest(); ~@@()''!£\$'%%^'!£ போ";
$idsite = API::getInstance()->addSite($name, "http://piwik.net");
$this->assertInternalType(PHPUnit_Framework_Constraint_IsType::TYPE_INT, $idsite);
$siteInfo = API::getInstance()->getSiteFromId($idsite);
$this->assertEquals($name, $siteInfo['name']);
}
/**
* adds a site
* use by several other unit tests
*/
protected function _addSite()
{
$name = "website ";
$idsite = API::getInstance()->addSite($name, array("http://piwik.net", "http://piwik.com/test/"));
$this->assertInternalType(PHPUnit_Framework_Constraint_IsType::TYPE_INT, $idsite);
$siteInfo = API::getInstance()->getSiteFromId($idsite);
$this->assertEquals($name, $siteInfo['name']);
$this->assertEquals("http://piwik.net", $siteInfo['main_url']);
$siteUrls = API::getInstance()->getSiteUrlsFromId($idsite);
$this->assertEquals(array("http://piwik.net", "http://piwik.com/test"), $siteUrls);
return $idsite;
}
/**
* no duplicate -> all the urls are saved
*
* @group Plugins
*/
public function testAddSiteUrlsnoDuplicate()
{
$idsite = $this->_addSite();
$siteUrlsBefore = API::getInstance()->getSiteUrlsFromId($idsite);
$toAdd = array("http://piwik1.net",
"http://piwik2.net",
"http://piwik3.net/test/",
"http://localhost/test",
"http://localho5.st/test",
"http://l42578gqege.f4",
"http://super.com/test/test/atqata675675/te"
);
$toAddValid = array("http://piwik1.net",
"http://piwik2.net",
"http://piwik3.net/test",
"http://localhost/test",
"http://localho5.st/test",
"http://l42578gqege.f4",
"http://super.com/test/test/atqata675675/te");
$insertedUrls = API::getInstance()->addSiteAliasUrls($idsite, $toAdd);
$this->assertEquals(count($toAdd), $insertedUrls);
$siteUrlsAfter = API::getInstance()->getSiteUrlsFromId($idsite);
$shouldHave = array_merge($siteUrlsBefore, $toAddValid);
sort($shouldHave);
sort($siteUrlsAfter);
$this->assertEquals($shouldHave, $siteUrlsAfter);
}
/**
* duplicate -> don't save the already existing URLs
*
* @group Plugins
*/
public function testAddSiteUrlsDuplicate()
{
$idsite = $this->_addSite();
$siteUrlsBefore = API::getInstance()->getSiteUrlsFromId($idsite);
$toAdd = array_merge($siteUrlsBefore, array("http://piwik1.net", "http://piwik2.net"));
$insertedUrls = API::getInstance()->addSiteAliasUrls($idsite, $toAdd);
$this->assertEquals(count($toAdd) - count($siteUrlsBefore), $insertedUrls);
$siteUrlsAfter = API::getInstance()->getSiteUrlsFromId($idsite);
$shouldHave = $toAdd;
sort($shouldHave);
sort($siteUrlsAfter);
$this->assertEquals($shouldHave, $siteUrlsAfter);
}
/**
* case empty array => nothing happens
*
* @group Plugins
*/
public function testAddSiteUrlsNoUrlsToAdd1()
{
$idsite = $this->_addSite();
$siteUrlsBefore = API::getInstance()->getSiteUrlsFromId($idsite);
$toAdd = array();
$insertedUrls = API::getInstance()->addSiteAliasUrls($idsite, $toAdd);
$this->assertEquals(count($toAdd), $insertedUrls);
$siteUrlsAfter = API::getInstance()->getSiteUrlsFromId($idsite);
$shouldHave = $siteUrlsBefore;
sort($shouldHave);
sort($siteUrlsAfter);
$this->assertEquals($shouldHave, $siteUrlsAfter);
}
/**
* case array only duplicate => nothing happens
*
* @group Plugins
*/
public function testAddSiteUrlsNoUrlsToAdd2()
{
$idsite = $this->_addSite();
$siteUrlsBefore = API::getInstance()->getSiteUrlsFromId($idsite);
$toAdd = $siteUrlsBefore;
$insertedUrls = API::getInstance()->addSiteAliasUrls($idsite, $toAdd);
$this->assertEquals(0, $insertedUrls);
$siteUrlsAfter = API::getInstance()->getSiteUrlsFromId($idsite);
$shouldHave = $siteUrlsBefore;
sort($shouldHave);
sort($siteUrlsAfter);
$this->assertEquals($shouldHave, $siteUrlsAfter);
}
/**
* wrong format urls => exception
*
* @group Plugins
*/
public function testAddSiteUrlsWrongUrlsFormat3()
{
try {
$idsite = $this->_addSite();
$toAdd = array("http:mpigeq");
API::getInstance()->addSiteAliasUrls($idsite, $toAdd);
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* wrong idsite => no exception because simply no access to this resource
*
* @group Plugins
*/
public function testAddSiteUrlsWrongIdSite1()
{
try {
$toAdd = array("http://pigeq.com/test");
API::getInstance()->addSiteAliasUrls(-1, $toAdd);
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* wrong idsite => exception
*
* @group Plugins
*/
public function testAddSiteUrlsWrongIdSite2()
{
try {
$toAdd = array("http://pigeq.com/test");
API::getInstance()->addSiteAliasUrls(155, $toAdd);
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* no Id -> empty array
*
* @group Plugins
*/
public function testGetAllSitesIdNoId()
{
$ids = API::getInstance()->getAllSitesId();
$this->assertEquals(array(), $ids);
}
/**
* several Id -> normal array
*
* @group Plugins
*/
public function testGetAllSitesIdSeveralId()
{
$name = "tetq";
$idsites = array(
API::getInstance()->addSite($name, array("http://piwik.net", "http://piwik.com/test/")),
API::getInstance()->addSite($name, array("http://piwik.net", "http://piwik.com/test/")),
API::getInstance()->addSite($name, array("http://piwik.net", "http://piwik.com/test/")),
API::getInstance()->addSite($name, array("http://piwik.net", "http://piwik.com/test/")),
API::getInstance()->addSite($name, array("http://piwik.net", "http://piwik.com/test/")),
);
$ids = API::getInstance()->getAllSitesId();
$this->assertEquals($idsites, $ids);
}
/**
* wrong id => exception
*
* @group Plugins
*/
public function testGetSiteFromIdWrongId1()
{
try {
API::getInstance()->getSiteFromId(0);
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* wrong id => exception
*
* @group Plugins
*/
public function testGetSiteFromIdWrongId2()
{
try {
API::getInstance()->getSiteFromId("x1");
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* wrong id : no access => exception
*
* @group Plugins
*/
public function testGetSiteFromIdWrongId3()
{
$idsite = API::getInstance()->addSite("site", array("http://piwik.net", "http://piwik.com/test/"));
$this->assertEquals(1, $idsite);
// set noaccess to site 1
FakeAccess::setIdSitesView(array(2));
FakeAccess::setIdSitesAdmin(array());
try {
API::getInstance()->getSiteFromId(1);
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* normal case
*
* @group Plugins
*/
public function testGetSiteFromIdNormalId()
{
$name = "website ''";
$idsite = API::getInstance()->addSite($name, array("http://piwik.net", "http://piwik.com/test/"));
$this->assertInternalType(PHPUnit_Framework_Constraint_IsType::TYPE_INT, $idsite);
$siteInfo = API::getInstance()->getSiteFromId($idsite);
$this->assertEquals($name, $siteInfo['name']);
$this->assertEquals("http://piwik.net", $siteInfo['main_url']);
}
/**
* there is no admin site available -> array()
*
* @group Plugins
*/
public function testGetSitesWithAdminAccessNoResult()
{
FakeAccess::setIdSitesAdmin(array());
$sites = API::getInstance()->getSitesWithAdminAccess();
$this->assertEquals(array(), $sites);
}
/**
* normal case, admin and view and noaccess website => return only admin
*
* @group Plugins
*/
public function testGetSitesWithAdminAccess()
{
API::getInstance()->addSite("site1", array("http://piwik.net", "http://piwik.com/test/"));
API::getInstance()->addSite("site2", array("http://piwik.com/test/"));
API::getInstance()->addSite("site3", array("http://piwik.org"));
$resultWanted = array(
0 => array("idsite" => 1, "name" => "site1", "main_url" => "http://piwik.net", "ecommerce" => 0, "excluded_ips" => "", 'sitesearch' => 1, 'sitesearch_keyword_parameters' => '', 'sitesearch_category_parameters' => '', 'excluded_parameters' => '', 'excluded_user_agents' => '', 'timezone' => 'UTC', 'currency' => 'USD', 'group' => '', 'keep_url_fragment' => 0, 'type' => 'website'),
1 => array("idsite" => 3, "name" => "site3", "main_url" => "http://piwik.org", "ecommerce" => 0, "excluded_ips" => "", 'sitesearch' => 1, 'sitesearch_keyword_parameters' => '', 'sitesearch_category_parameters' => '', 'excluded_parameters' => '', 'excluded_user_agents' => '', 'timezone' => 'UTC', 'currency' => 'USD', 'group' => '', 'keep_url_fragment' => 0, 'type' => 'website'),
);
FakeAccess::setIdSitesAdmin(array(1, 3));
$sites = API::getInstance()->getSitesWithAdminAccess();
// we dont test the ts_created
unset($sites[0]['ts_created']);
unset($sites[1]['ts_created']);
$this->assertEquals($resultWanted, $sites);
}
/**
* there is no admin site available -> array()
*
* @group Plugins
*/
public function testGetSitesWithViewAccessNoResult()
{
FakeAccess::setIdSitesView(array());
FakeAccess::setIdSitesAdmin(array());
$sites = API::getInstance()->getSitesWithViewAccess();
$this->assertEquals(array(), $sites);
}
/**
* normal case, admin and view and noaccess website => return only admin
*
* @group Plugins
*/
public function testGetSitesWithViewAccess()
{
API::getInstance()->addSite("site1", array("http://piwik.net", "http://piwik.com/test/"));
API::getInstance()->addSite("site2", array("http://piwik.com/test/"));
API::getInstance()->addSite("site3", array("http://piwik.org"));
$resultWanted = array(
0 => array("idsite" => 1, "name" => "site1", "main_url" => "http://piwik.net", "ecommerce" => 0, 'sitesearch' => 1, 'sitesearch_keyword_parameters' => '', 'sitesearch_category_parameters' => '', "excluded_ips" => "", 'excluded_parameters' => '', 'excluded_user_agents' => '', 'timezone' => 'UTC', 'currency' => 'USD', 'group' => '', 'keep_url_fragment' => 0, 'type' => 'website'),
1 => array("idsite" => 3, "name" => "site3", "main_url" => "http://piwik.org", "ecommerce" => 0, 'sitesearch' => 1, 'sitesearch_keyword_parameters' => '', 'sitesearch_category_parameters' => '', "excluded_ips" => "", 'excluded_parameters' => '', 'excluded_user_agents' => '', 'timezone' => 'UTC', 'currency' => 'USD', 'group' => '', 'keep_url_fragment' => 0, 'type' => 'website'),
);
FakeAccess::setIdSitesView(array(1, 3));
FakeAccess::setIdSitesAdmin(array());
$sites = API::getInstance()->getSitesWithViewAccess();
// we dont test the ts_created
unset($sites[0]['ts_created']);
unset($sites[1]['ts_created']);
$this->assertEquals($resultWanted, $sites);
}
/**
* there is no admin site available -> array()
*
* @group Plugins
*/
public function testGetSitesWithAtLeastViewAccessNoResult()
{
FakeAccess::setIdSitesView(array());
FakeAccess::setIdSitesAdmin(array());
$sites = API::getInstance()->getSitesWithAtLeastViewAccess();
$this->assertEquals(array(), $sites);
}
/**
* normal case, admin and view and noaccess website => return only admin
*
* @group Plugins
*/
public function testGetSitesWithAtLeastViewAccess()
{
API::getInstance()->addSite("site1", array("http://piwik.net", "http://piwik.com/test/"), $ecommerce = 1);
API::getInstance()->addSite("site2", array("http://piwik.com/test/"));
API::getInstance()->addSite("site3", array("http://piwik.org"));
$resultWanted = array(
0 => array("idsite" => 1, "name" => "site1", "main_url" => "http://piwik.net", "ecommerce" => 1, "excluded_ips" => "", 'sitesearch' => 1, 'sitesearch_keyword_parameters' => '', 'sitesearch_category_parameters' => '', 'excluded_parameters' => '', 'excluded_user_agents' => '', 'timezone' => 'UTC', 'currency' => 'USD', 'group' => '', 'keep_url_fragment' => 0, 'type' => 'website'),
1 => array("idsite" => 3, "name" => "site3", "main_url" => "http://piwik.org", "ecommerce" => 0, "excluded_ips" => "", 'sitesearch' => 1, 'sitesearch_keyword_parameters' => '', 'sitesearch_category_parameters' => '', 'excluded_parameters' => '', 'excluded_user_agents' => '', 'timezone' => 'UTC', 'currency' => 'USD', 'group' => '', 'keep_url_fragment' => 0, 'type' => 'website'),
);
FakeAccess::setIdSitesView(array(1, 3));
FakeAccess::setIdSitesAdmin(array());
$sites = API::getInstance()->getSitesWithAtLeastViewAccess();
// we dont test the ts_created
unset($sites[0]['ts_created']);
unset($sites[1]['ts_created']);
$this->assertEquals($resultWanted, $sites);
}
/**
* no urls for this site => array()
*
* @group Plugins
*/
public function testGetSiteUrlsFromIdNoUrls()
{
$idsite = API::getInstance()->addSite("site1", array("http://piwik.net"));
$urls = API::getInstance()->getSiteUrlsFromId($idsite);
$this->assertEquals(array("http://piwik.net"), $urls);
}
/**
* normal case
*
* @group Plugins
*/
public function testGetSiteUrlsFromIdManyUrls()
{
$site = array("http://piwik.net",
"http://piwik.org",
"http://piwik.org",
"http://piwik.com");
sort($site);
$idsite = API::getInstance()->addSite("site1", $site);
$siteWanted = array("http://piwik.net",
"http://piwik.org",
"http://piwik.com");
sort($siteWanted);
$urls = API::getInstance()->getSiteUrlsFromId($idsite);
$this->assertEquals($siteWanted, $urls);
}
/**
* wrongId => exception
*
* @group Plugins
*/
public function testGetSiteUrlsFromIdWrongId()
{
try {
FakeAccess::setIdSitesView(array(3));
FakeAccess::setIdSitesAdmin(array());
API::getInstance()->getSiteUrlsFromId(1);
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* one url => no change to alias urls
*
* @group Plugins
*/
public function testUpdateSiteOneUrl()
{
$urls = array("http://piwiknew.com",
"http://piwiknew.net",
"http://piwiknew.org",
"http://piwiknew.fr");
$idsite = API::getInstance()->addSite("site1", $urls);
$newMainUrl = "http://main.url";
// Also test that the group was set to empty, and is searchable
$websites = API::getInstance()->getSitesFromGroup('');
$this->assertEquals(1, count($websites));
// the Update doesn't change the group field
API::getInstance()->updateSite($idsite, "test toto@{}", $newMainUrl);
$websites = API::getInstance()->getSitesFromGroup('');
$this->assertEquals(1, count($websites));
// Updating the group to something
$group = 'something';
API::getInstance()->updateSite($idsite, "test toto@{}", $newMainUrl, $ecommerce = 0, $ss = true, $ss_kwd = null, $ss_cat = '', $ips = null, $parametersExclude = null, $timezone = null, $currency = null, $group);
$websites = API::getInstance()->getSitesFromGroup($group);
$this->assertEquals(1, count($websites));
$this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($websites[0]['ts_created'])));
// Updating the group to nothing
$group = '';
$type = 'mobileAppTest';
API::getInstance()->updateSite($idsite, "test toto@{}", $newMainUrl, $ecommerce = 0, $ss = false, $ss_kwd = '', $ss_cat = null, $ips = null, $parametersExclude = null, $timezone = null, $currency = null, $group, $startDate = '2010-01-01', $excludedUserAgent = null, $keepUrlFragment = 1, $type);
$websites = API::getInstance()->getSitesFromGroup($group);
$this->assertEquals(1, count($websites));
$this->assertEquals('2010-01-01', date('Y-m-d', strtotime($websites[0]['ts_created'])));
// Test setting the website type
$this->assertEquals($type, Site::getTypeFor($idsite));
// Check Alias URLs contain only main url
$allUrls = API::getInstance()->getSiteUrlsFromId($idsite);
$this->assertEquals($newMainUrl, $allUrls[0]);
$aliasUrls = array_slice($allUrls, 1);
$this->assertEquals(array(), $aliasUrls);
}
/**
* strange name and NO URL => name ok, main_url not updated
*
* @group Plugins
*/
public function testUpdateSiteStrangeNameNoUrl()
{
$idsite = API::getInstance()->addSite("site1", "http://main.url");
$newName = "test toto@{'786'}";
API::getInstance()->updateSite($idsite, $newName);
$site = API::getInstance()->getSiteFromId($idsite);
$this->assertEquals($newName, $site['name']);
// url didn't change because parameter url NULL in updateSite
$this->assertEquals("http://main.url", $site['main_url']);
}
/**
* several urls => both main and alias are updated
* also test the update of group field
*
* @group Plugins
*/
public function testUpdateSiteSeveralUrlsAndGroup()
{
$urls = array("http://piwiknew.com",
"http://piwiknew.net",
"http://piwiknew.org",
"http://piwiknew.fr");
$group = 'GROUP Before';
$idsite = API::getInstance()->addSite("site1", $urls, $ecommerce = 1,
$siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null,
$excludedIps = null, $excludedQueryParameters = null, $timezone = null, $currency = null, $group, $startDate = '2011-01-01');
$websites = API::getInstance()->getSitesFromGroup($group);
$this->assertEquals(1, count($websites));
$newurls = array("http://piwiknew2.com",
"http://piwiknew2.net",
"http://piwiknew2.org",
"http://piwiknew2.fr");
$groupAfter = ' GROUP After';
API::getInstance()->updateSite($idsite, "test toto@{}", $newurls, $ecommerce = 0,
$siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null,
$excludedIps = null, $excludedQueryParameters = null, $timezone = null, $currency = null, $groupAfter);
// no result for the group before update
$websites = API::getInstance()->getSitesFromGroup($group);
$this->assertEquals(0, count($websites));
// Testing that the group was updated properly (and testing that the group value is trimmed before inserted/searched)
$websites = API::getInstance()->getSitesFromGroup($groupAfter . ' ');
$this->assertEquals(1, count($websites));
$this->assertEquals('2011-01-01', date('Y-m-d', strtotime($websites[0]['ts_created'])));
// Test fetch website groups
$expectedGroups = array(trim($groupAfter));
$fetched = API::getInstance()->getSitesGroups();
$this->assertEquals($expectedGroups, $fetched);
$allUrls = API::getInstance()->getSiteUrlsFromId($idsite);
sort($allUrls);
sort($newurls);
$this->assertEquals($newurls, $allUrls);
}
/**
* @group Plugins
*/
public function testGetSitesGroups()
{
$groups = array('group1', ' group1 ', '', 'group2');
$expectedGroups = array('group1', '', 'group2');
foreach ($groups as $group) {
API::getInstance()->addSite("test toto@{}", 'http://example.org', $ecommerce = 1, $siteSearch = null, $searchKeywordParameters = null, $searchCategoryParameters = null, $excludedIps = null, $excludedQueryParameters = null, $timezone = null, $currency = null, $group);
}
$this->assertEquals($expectedGroups, API::getInstance()->getSitesGroups());
}
public function getInvalidTimezoneData()
{
return array(
array('UTC+15'),
array('Paris'),
);
}
/**
*
* @dataProvider getInvalidTimezoneData
* @group Plugins
*/
public function testAddSitesInvalidTimezone($timezone)
{
try {
API::getInstance()->addSite("site1", array('http://example.org'), $ecommerce = 0,
$siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null, $ip = '', $params = '', $timezone);
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* @group Plugins
*/
public function testAddSitesInvalidCurrency()
{
try {
$invalidCurrency = '€';
API::getInstance()->addSite("site1", array('http://example.org'), $ecommerce = 0,
$siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null, '', 'UTC', $invalidCurrency);
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');
}
/**
* @group Plugins
*/
public function testSetDefaultTimezoneAndCurrencyAndExcludedQueryParametersAndExcludedIps()
{
// test that they return default values
$defaultTimezone = API::getInstance()->getDefaultTimezone();
$this->assertEquals('UTC', $defaultTimezone);
$defaultCurrency = API::getInstance()->getDefaultCurrency();
$this->assertEquals('USD', $defaultCurrency);
$excludedIps = API::getInstance()->getExcludedIpsGlobal();
$this->assertEquals('', $excludedIps);
$excludedQueryParameters = API::getInstance()->getExcludedQueryParametersGlobal();
$this->assertEquals('', $excludedQueryParameters);
// test that when not specified, defaults are set as expected
$idsite = API::getInstance()->addSite("site1", array('http://example.org'));
$site = new Site($idsite);
$this->assertEquals('UTC', $site->getTimezone());
$this->assertEquals('USD', $site->getCurrency());
$this->assertEquals('', $site->getExcludedQueryParameters());
$this->assertEquals('', $site->getExcludedIps());
$this->assertEquals(false, $site->isEcommerceEnabled());
// set the global timezone and get it
$newDefaultTimezone = 'UTC+5.5';
API::getInstance()->setDefaultTimezone($newDefaultTimezone);
$defaultTimezone = API::getInstance()->getDefaultTimezone();
$this->assertEquals($newDefaultTimezone, $defaultTimezone);
// set the default currency and get it
$newDefaultCurrency = 'EUR';
API::getInstance()->setDefaultCurrency($newDefaultCurrency);
$defaultCurrency = API::getInstance()->getDefaultCurrency();
$this->assertEquals($newDefaultCurrency, $defaultCurrency);
// set the global IPs to exclude and get it
$newGlobalExcludedIps = '1.1.1.*,1.1.*.*,150.1.1.1';
API::getInstance()->setGlobalExcludedIps($newGlobalExcludedIps);
$globalExcludedIps = API::getInstance()->getExcludedIpsGlobal();
$this->assertEquals($newGlobalExcludedIps, $globalExcludedIps);
// set the global URL query params to exclude and get it
$newGlobalExcludedQueryParameters = 'PHPSESSID,blabla, TesT';
// removed the space
$expectedGlobalExcludedQueryParameters = 'PHPSESSID,blabla,TesT';
API::getInstance()->setGlobalExcludedQueryParameters($newGlobalExcludedQueryParameters);
$globalExcludedQueryParameters = API::getInstance()->getExcludedQueryParametersGlobal();
$this->assertEquals($expectedGlobalExcludedQueryParameters, $globalExcludedQueryParameters);
// create a website and check that default currency and default timezone are set
// however, excluded IPs and excluded query Params are not returned
$idsite = API::getInstance()->addSite("site1", array('http://example.org'), $ecommerce = 0,
$siteSearch = 0, $searchKeywordParameters = 'test1,test2', $searchCategoryParameters = 'test2,test1',
'', '', $newDefaultTimezone);
$site = new Site($idsite);
$this->assertEquals($newDefaultTimezone, $site->getTimezone());
$this->assertEquals(date('Y-m-d'), $site->getCreationDate()->toString());
$this->assertEquals($newDefaultCurrency, $site->getCurrency());
$this->assertEquals('', $site->getExcludedIps());
$this->assertEquals('', $site->getExcludedQueryParameters());
$this->assertEquals('test1,test2', $site->getSearchKeywordParameters());
$this->assertEquals('test2,test1', $site->getSearchCategoryParameters());
$this->assertFalse($site->isSiteSearchEnabled());
$this->assertFalse(Site::isSiteSearchEnabledFor($idsite));
$this->assertFalse($site->isEcommerceEnabled());
$this->assertFalse(Site::isEcommerceEnabledFor($idsite));
}
/**
* @group Plugins
*/
public function testGetSitesIdFromSiteUrlSuperUser()
{
API::getInstance()->addSite("site1", array("http://piwik.net", "http://piwik.com"));
API::getInstance()->addSite("site2", array("http://piwik.com", "http://piwik.net"));
API::getInstance()->addSite("site3", array("http://piwik.com", "http://piwik.org"));
$idsites = API::getInstance()->getSitesIdFromSiteUrl('http://piwik.org');
$this->assertTrue(count($idsites) == 1);
$idsites = API::getInstance()->getSitesIdFromSiteUrl('http://www.piwik.org');
$this->assertTrue(count($idsites) == 1);
$idsites = API::getInstance()->getSitesIdFromSiteUrl('http://piwik.net');
$this->assertTrue(count($idsites) == 2);
$idsites = API::getInstance()->getSitesIdFromSiteUrl('http://piwik.com');
$this->assertTrue(count($idsites) == 3);
}
/**
* @group Plugins
*/
public function testGetSitesIdFromSiteUrlUser()
{
API::getInstance()->addSite("site1", array("http://www.piwik.net", "http://piwik.com"));
API::getInstance()->addSite("site2", array("http://piwik.com", "http://piwik.net"));
API::getInstance()->addSite("site3", array("http://piwik.com", "http://piwik.org"));
$saveAccess = Access::getInstance();
APIUsersManager::getInstance()->addUser("user1", "geqgegagae", "tegst@tesgt.com", "alias");
APIUsersManager::getInstance()->setUserAccess("user1", "view", array(1));
APIUsersManager::getInstance()->addUser("user2", "geqgegagae", "tegst2@tesgt.com", "alias");
APIUsersManager::getInstance()->setUserAccess("user2", "view", array(1));
APIUsersManager::getInstance()->setUserAccess("user2", "admin", array(3));
APIUsersManager::getInstance()->addUser("user3", "geqgegagae", "tegst3@tesgt.com", "alias");
APIUsersManager::getInstance()->setUserAccess("user3", "view", array(1, 2));
APIUsersManager::getInstance()->setUserAccess("user3", "admin", array(3));
$pseudoMockAccess = new FakeAccess;
FakeAccess::$superUser = false;
FakeAccess::$identity = 'user1';
FakeAccess::setIdSitesView(array(1));
FakeAccess::setIdSitesAdmin(array());
Access::setSingletonInstance($pseudoMockAccess);
$idsites = API::getInstance()->getSitesIdFromSiteUrl('http://piwik.com');
$this->assertEquals(1, count($idsites));
// testing URL normalization
$idsites = API::getInstance()->getSitesIdFromSiteUrl('http://www.piwik.com');
$this->assertEquals(1, count($idsites));
$idsites = API::getInstance()->getSitesIdFromSiteUrl('http://piwik.net');
$this->assertEquals(1, count($idsites));
$pseudoMockAccess = new FakeAccess;
FakeAccess::$superUser = false;
FakeAccess::$identity = 'user2';
FakeAccess::setIdSitesView(array(1));
FakeAccess::setIdSitesAdmin(array(3));
Access::setSingletonInstance($pseudoMockAccess);
$idsites = API::getInstance()->getSitesIdFromSiteUrl('http://piwik.com');
$this->assertEquals(2, count($idsites));
$pseudoMockAccess = new FakeAccess;
FakeAccess::$superUser = false;
FakeAccess::$identity = 'user3';
FakeAccess::setIdSitesView(array(1, 2));
FakeAccess::setIdSitesAdmin(array(3));
Access::setSingletonInstance($pseudoMockAccess);
$idsites = API::getInstance()->getSitesIdFromSiteUrl('http://piwik.com');
$this->assertEquals(3, count($idsites));
Access::setSingletonInstance($saveAccess);
}
/**
* @group Plugins
*/
public function testGetSitesFromTimezones()
{
API::getInstance()->addSite("site3", array("http://piwik.org"), null, $siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null, null, null, 'UTC');
$idsite2 = API::getInstance()->addSite("site3", array("http://piwik.org"), null, $siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null, null, null, 'Pacific/Auckland');
$idsite3 = API::getInstance()->addSite("site3", array("http://piwik.org"), null, $siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null, null, null, 'Pacific/Auckland');
$idsite4 = API::getInstance()->addSite("site3", array("http://piwik.org"), null, $siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null, null, null, 'UTC+10');
$result = API::getInstance()->getSitesIdFromTimezones(array('UTC+10', 'Pacific/Auckland'));
$this->assertEquals(array($idsite2, $idsite3, $idsite4), $result);
}
}
|