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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SitesManager\tests\Integration;
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Plugin;
use Piwik\Plugins\MobileAppMeasurable;
use Piwik\Plugins\WebsiteMeasurable\Type as WebsiteType;
use Piwik\Plugins\SitesManager\API;
use Piwik\Plugins\SitesManager\Model;
use Piwik\Plugins\UsersManager\API as APIUsersManager;
use Piwik\Measurable\Measurable;
use Piwik\Site;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Exception;
/**
* Class Plugins_SitesManagerTest
*
* @group Plugins
* @group ApiTest
* @group SitesManager
*/
class ApiTest extends IntegrationTestCase
{
public function setUp(): void
{
parent::setUp();
Plugin\Manager::getInstance()->activatePlugin('MobileAppMeasurable');
// setup the access layer
FakeAccess::$superUser = true;
}
public function tearDown(): void
{
parent::tearDown();
Fixture::resetTranslations();
}
/**
* empty name -> exception
*/
public function test_addSite_WithEmptyName_ThrowsException()
{
$this->expectException(\Exception::class);
API::getInstance()->addSite("", array("http://piwik.net"));
}
/**
* DataProvider for testAddSiteWrongUrls
*/
public function getInvalidUrlData()
{
return array(
array(array()), // no urls
array(array("")),
array(""),
array("5http://piwik.net"),
array("???://piwik.net"),
);
}
/**
* wrong urls -> exception
*
* @dataProvider getInvalidUrlData
*/
public function test_addSite_WithWrongUrls_ThrowsException($url)
{
$this->expectException(\Exception::class);
API::getInstance()->addSite("name", $url);
}
/**
* Test with valid IPs
*/
public function test_addSite_WithExcludedIps_AndTimezone_AndCurrency_AndExcludedQueryParameters_SucceedsWhenParamsAreValid()
{
$this->addSiteTest($expectedWebsiteType = 'mobile-\'app');
}
/**
* @dataProvider getDifferentTypesDataProvider
*/
public function test_addSite_WhenTypeIsKnown($expectedWebsiteType)
{
$this->addSiteTest($expectedWebsiteType);
}
private function addSiteTest($expectedWebsiteType, $settingValues = null)
{
$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";
$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, $settingValues);
$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('superUserLogin', $siteInfo['creator_login']);
$this->assertEquals('superUserLogin', Site::getCreatorLoginFor($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']);
return $siteInfo;
}
/**
* 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
*/
public function test_addSite_WithInvalidExcludedIps_ThrowsException($ip)
{
$this->expectException(\Exception::class);
API::getInstance()->addSite("name", "http://piwik.net/", $ecommerce = 0,
$siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null, $ip);
}
/**
* one url -> one main_url and nothing inserted as alias urls
*/
public function test_addSite_WithOneUrl_Succeeds_AndCreatesNoAliasUrls()
{
$url = "http://piwik.net/";
$urlOK = "http://piwik.net";
$idsite = API::getInstance()->addSite("name", $url);
self::assertIsInt($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
*/
public function test_addSite_WithSeveralUrls_Succeeds_AndCreatesAliasUrls()
{
$urls = array("http://piwik.net/", "http://piwik.com", "https://piwik.net/test/", "piwik.net/another/test");
$urlsOK = array("http://piwik.net", "http://piwik.com", "http://piwik.net/another/test", "https://piwik.net/test");
$idsite = API::getInstance()->addSite("super website", $urls);
self::assertIsInt($idsite);
$siteInfo = API::getInstance()->getSiteFromId($idsite);
$this->assertEquals($urlsOK[0], $siteInfo['main_url']);
$siteUrls = API::getInstance()->getSiteUrlsFromId($idsite);
$this->assertEquals($urlsOK, $siteUrls);
}
/**
* strange name
*/
public function test_addSite_WithStrangeName_Succeeds()
{
$name = "supertest(); ~@@()''!£\$'%%^'!£ போ";
$idsite = API::getInstance()->addSite($name, "http://piwik.net");
self::assertIsInt($idsite);
$siteInfo = API::getInstance()->getSiteFromId($idsite);
$this->assertEquals($name, $siteInfo['name']);
}
/**
* @dataProvider getDifferentTypesDataProvider
*/
public function test_addSite_ShouldFailAndNotCreatedASite_IfASettingIsInvalid($type)
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('SitesManager_OnlyMatchedUrlsAllowed');
try {
$settings = array('WebsiteMeasurable' => array(array('name' => 'exclude_unknown_urls', 'value' => 'fooBar')));
$this->addSiteWithType($type, $settings);
} catch (Exception $e) {
// make sure no site created
$ids = API::getInstance()->getAllSitesId();
$this->assertEquals(array(), $ids);
throw $e;
}
}
public function test_addSite_ShouldSavePassedMeasurableSettings_IfSettingsAreValid()
{
$type = WebsiteType::ID;
$settings = array('WebsiteMeasurable' => array(array('name' => 'urls', 'value' => array('http://www.piwik.org'))));
$idSite = $this->addSiteWithType($type, $settings);
$this->assertSame(1, $idSite);
$settings = $this->getWebsiteMeasurable($idSite);
$urls = $settings->urls->getValue();
$this->assertSame(array('http://www.piwik.org'), $urls);
}
/**
* @return \Piwik\Plugins\WebsiteMeasurable\MeasurableSettings
*/
private function getWebsiteMeasurable($idSite)
{
$settings = StaticContainer::get('Piwik\Plugin\SettingsProvider');
return $settings->getMeasurableSettings('WebsiteMeasurable', $idSite, null);
}
/**
* 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/"));
self::assertIsInt($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;
}
private function addSiteWithType($type, $settings)
{
return API::getInstance()->addSite("name", "http://piwik.net/", $ecommerce = 0,
$siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null,
$ip = null,
$excludedQueryParameters = null,
$timezone = null,
$currency = null,
$group = null,
$startDate = null,
$excludedUserAgents = null,
$keepURLFragments = null,
$type, $settings);
}
private function updateSiteSettings($idSite, $newSiteName, $settings)
{
return API::getInstance()->updateSite($idSite,
$newSiteName,
$urls = null,
$ecommerce = null,
$siteSearch = null,
$searchKeywordParameters = null,
$searchCategoryParameters = null,
$excludedIps = null,
$excludedQueryParameters = null,
$timezone = null,
$currency = null,
$group = null,
$startDate = null,
$excludedUserAgents = null,
$keepURLFragments = null,
$type = null,
$settings);
}
/**
* no duplicate -> all the urls are saved
*/
public function test_addSiteAliasUrls_WithUniqueUrls_SavesAllUrls()
{
$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
*/
public function test_addSiteAliasUrls_WithDuplicateUrls_RemovesDuplicatesBeforeSaving()
{
$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
*/
public function test_addSiteAliasUrls_WithNoUrls_DoesNothing()
{
$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
*/
public function test_addSiteAliasUrls_WithAlreadyPersistedUrls_DoesNothing()
{
$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
*/
public function test_addSiteAliasUrls_WithIncorrectFormat_ThrowsException_3()
{
$this->expectException(\Exception::class);
$idsite = $this->_addSite();
$toAdd = array("http:mpigeq");
API::getInstance()->addSiteAliasUrls($idsite, $toAdd);
}
/**
* wrong idsite => no exception because simply no access to this resource
*/
public function test_addSiteAliasUrls_WithWrongIdSite_ThrowsException()
{
$this->expectException(\Exception::class);
$toAdd = array("http://pigeq.com/test");
API::getInstance()->addSiteAliasUrls(-1, $toAdd);
}
/**
* wrong idsite => exception
*/
public function test_addSiteAliasUrls_WithWrongIdSite_ThrowsException2()
{
$this->expectException(\Exception::class);
$toAdd = array("http://pigeq.com/test");
API::getInstance()->addSiteAliasUrls(155, $toAdd);
}
public function test_addSite_CorrectlySavesExcludeUnknownUrlsSetting()
{
$idSite = API::getInstance()->addSite("site", array("http://piwik.net"), $ecommerce = null, $siteSearch = null,
$searchKeywordParams = null, $searchCategoryParams = null, $excludedIps = null, $excludedQueryParams = null,
$timezone = null, $currency = null, $group = null, $startDate = null, $excludedUserAgents = null,
$keepUrlFragments = null, $type = null, $settings = null, $excludeUnknownUrls = true);
$site = API::getInstance()->getSiteFromId($idSite);
$this->assertEquals(1, $site['exclude_unknown_urls']);
}
/**
* no Id -> empty array
*/
public function test_getAllSitesId_ReturnsNothing_WhenNoSitesSaved()
{
$ids = API::getInstance()->getAllSitesId();
$this->assertEquals(array(), $ids);
}
/**
* several Id -> normal array
*/
public function test_getAllSitesId_ReturnsAllIds_WhenMultipleSitesPersisted()
{
$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
*/
public function test_getSiteFromId_WithWrongId_ThrowsException1()
{
$this->expectException(\Exception::class);
API::getInstance()->getSiteFromId(0);
}
/**
* wrong id => exception
*/
public function test_getSiteFromId_WithWrongId_ThrowsException2()
{
$this->expectException(\Exception::class);
API::getInstance()->getSiteFromId("x1");
}
/**
* wrong id : no access => exception
*/
public function test_getSiteFromId_ThrowsException_WhenTheUserDoesNotHavaAcessToTheSite()
{
$this->expectException(\Exception::class);
$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());
API::getInstance()->getSiteFromId(1);
}
/**
* normal case
*/
public function test_getSiteFromId_WithNormalId_ReturnsTheCorrectSite()
{
$name = "website ''";
$idsite = API::getInstance()->addSite($name, array("http://piwik.net", "http://piwik.com/test/"));
self::assertIsInt($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()
*/
public function test_getSitesWithAdminAccess_ReturnsNothing_WhenUserHasNoAdminAccess()
{
FakeAccess::setIdSitesAdmin(array());
$sites = API::getInstance()->getSitesWithAdminAccess();
$this->assertEquals(array(), $sites);
}
/**
* normal case, admin and view and noaccess website => return only admin
*/
public function test_getSitesWithAdminAccess_shouldOnlyReturnSitesHavingActuallyAdminAccess()
{
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"), null, null, null, null, null, null, 'Asia/Tokyo');
Fixture::loadAllTranslations();
$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', 'timezone_name' => 'UTC', 'currency' => 'USD', 'group' => '', 'keep_url_fragment' => 0, 'type' => 'website', 'exclude_unknown_urls' => 0, 'currency_name' => 'US Dollar'),
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' => 'Asia/Tokyo', 'timezone_name' => 'Japan', 'currency' => 'USD', 'group' => '', 'keep_url_fragment' => 0, 'type' => 'website', 'exclude_unknown_urls' => 0, 'currency_name' => 'US Dollar'),
);
FakeAccess::setIdSitesAdmin(array(1, 3));
$sites = API::getInstance()->getSitesWithAdminAccess();
// we don't test the ts_created
unset($sites[0]['ts_created']);
unset($sites[1]['ts_created']);
$this->assertEquals($resultWanted, $sites);
}
public function test_getSitesWithAdminAccess_shouldApplyLimit_IfSet()
{
$this->createManySitesWithAdminAccess(40);
// should return all sites by default
$sites = API::getInstance()->getSitesWithAdminAccess();
$this->assertReturnedSitesContainsSiteIds(range(1, 40), $sites);
// return only 5 sites
$sites = API::getInstance()->getSitesWithAdminAccess(false, false, 5);
$this->assertReturnedSitesContainsSiteIds(array(1, 2, 3, 4, 5), $sites);
// return only 10 sites
$sites = API::getInstance()->getSitesWithAdminAccess(false, false, 10);
$this->assertReturnedSitesContainsSiteIds(range(1, 10), $sites);
}
public function test_getSitesWithAdminAccess_shouldApplyPattern_IfSetAndFindBySiteName()
{
$this->createManySitesWithAdminAccess(40);
// by site name
$sites = API::getInstance()->getSitesWithAdminAccess(false, 'site38');
$this->assertReturnedSitesContainsSiteIds(array(38), $sites);
}
public function test_getSitesWithAdminAccess_shouldApplyPattern_IfSetAndFindByUrl()
{
$this->createManySitesWithAdminAccess(40);
$sites = API::getInstance()->getSitesWithAdminAccess(false, 'piwik38.o');
$this->assertReturnedSitesContainsSiteIds(array(38), $sites);
}
public function test_getSitesWithAdminAccess_shouldApplyPattern_AndFindMany()
{
$this->createManySitesWithAdminAccess(40);
$sites = API::getInstance()->getSitesWithAdminAccess(false, '5');
$this->assertReturnedSitesContainsSiteIds(array(5, 15, 25, 35), $sites);
}
public function test_getSitesWithAdminAccess_shouldApplyPatternAndLimit()
{
$this->createManySitesWithAdminAccess(40);
$sites = API::getInstance()->getSitesWithAdminAccess(false, '5', 2);
$this->assertReturnedSitesContainsSiteIds(array(5, 15), $sites);
}
private function createManySitesWithAdminAccess($numSites)
{
for ($i = 1; $i <= $numSites; $i++) {
API::getInstance()->addSite("site" . $i, array("http://piwik$i.org"));
}
FakeAccess::setIdSitesAdmin(range(1, $numSites));
}
private function assertReturnedSitesContainsSiteIds($expectedSiteIds, $sites)
{
$this->assertCount(count($expectedSiteIds), $sites);
foreach ($sites as $site) {
$key = array_search($site['idsite'], $expectedSiteIds);
$this->assertNotFalse($key, 'Did not find expected siteId "' . $site['idsite'] . '" in the expected siteIds');
unset($expectedSiteIds[$key]);
}
$siteIds = var_export($expectedSiteIds, 1);
$this->assertEmpty($expectedSiteIds, 'Not all expected sites were found, remaining site ids: ' . $siteIds);
}
/**
* there is no admin site available -> array()
*/
public function test_getSitesWithViewAccess_ReturnsNothing_IfUserHasNoViewOrAdminAccess()
{
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
*/
public function test_getSitesWithViewAccess_ReturnsSitesWithViewAccess()
{
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', 'exclude_unknown_urls' => 0, 'timezone_name' => 'SitesManager_Format_Utc', 'currency_name' => 'USD'),
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', 'exclude_unknown_urls' => 0, 'timezone_name' => 'SitesManager_Format_Utc', 'currency_name' => 'USD'),
);
FakeAccess::setIdSitesView(array(1, 3));
FakeAccess::setIdSitesAdmin(array());
$sites = API::getInstance()->getSitesWithViewAccess();
// we don't 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()
*/
public function test_getSitesWithAtLeastViewAccess_ReturnsNothing_WhenUserHasNoAccess()
{
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
*/
public function test_getSitesWithAtLeastViewAccess_ReturnsSitesWithViewAccess()
{
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', 'exclude_unknown_urls' => 0, 'timezone_name' => 'SitesManager_Format_Utc', 'currency_name' => 'USD'),
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', 'exclude_unknown_urls' => 0, 'timezone_name' => 'SitesManager_Format_Utc', 'currency_name' => 'USD'),
);
FakeAccess::setIdSitesView(array(1, 3));
FakeAccess::setIdSitesAdmin(array());
$sites = API::getInstance()->getSitesWithAtLeastViewAccess();
// we don't test the ts_created
unset($sites[0]['ts_created']);
unset($sites[1]['ts_created']);
$this->assertEquals($resultWanted, $sites);
}
/**
* no urls for this site => array()
*/
public function test_getSiteUrlsFromId_ReturnsMainUrlOnly_WhenNoAliasUrls()
{
$idsite = API::getInstance()->addSite("site1", array("http://piwik.net"));
$urls = API::getInstance()->getSiteUrlsFromId($idsite);
$this->assertEquals(array("http://piwik.net"), $urls);
}
/**
* normal case
*/
public function test_getSiteUrlsFromId_ReturnsMainAndAliasUrls()
{
$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
*/
public function test_getSiteUrlsFromId_ThrowsException_WhenSiteIdIsIncorrect()
{
$this->expectException(\Exception::class);
FakeAccess::setIdSitesView(array(3));
FakeAccess::setIdSitesAdmin(array());
API::getInstance()->getSiteUrlsFromId(1);
}
/**
* one url => no change to alias urls
*/
public function test_updateSite_WithOneUrl_RemovesAliasUrls_AndUpdatesTheSiteCorrectly()
{
$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
*/
public function test_updateSite_WithStrangeName_AndNoAliasUrls_UpdatesTheName_ButNoUrls()
{
$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
*/
public function test_updateSite_WithSeveralUrlsAndGroup_UpdatesGroupAndUrls()
{
$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);
}
public function test_updateSite_ShouldFailAndNotUpdateSite_IfASettingIsInvalid()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('SitesManager_OnlyMatchedUrlsAllowed');
$type = MobileAppMeasurable\Type::ID;
$idSite = $this->addSiteWithType($type, array());
try {
$settings = array('MobileAppMeasurable' => array(array('name' => 'exclude_unknown_urls', 'value' => 'fooBar')));
$this->updateSiteSettings($idSite, 'newSiteName', $settings);
} catch (Exception $e) {
// verify nothing was updated (not even the name)
$measurable = new Measurable($idSite);
$this->assertNotEquals('newSiteName', $measurable->getName());
throw $e;
}
}
public function test_updateSite_ShouldSavePassedMeasurableSettings_IfSettingsAreValid()
{
$type = WebsiteType::ID;
$idSite = $this->addSiteWithType($type, array());
$this->assertSame(1, $idSite);
$settings = array('WebsiteMeasurable' => array(array('name' => 'urls', 'value' => array('http://www.piwik.org'))));
$this->updateSiteSettings($idSite, 'newSiteName', $settings);
$settings = $this->getWebsiteMeasurable($idSite);
// verify it was updated
$measurable = new Measurable($idSite);
$this->assertSame('newSiteName', $measurable->getName());
$this->assertSame(array('http://www.piwik.org'), $settings->urls->getValue());
}
public function test_updateSite_CorreclySavesExcludedUnknownUrlSettings()
{
$idSite = API::getInstance()->addSite("site1", array("http://piwik.net"));
$site = API::getInstance()->getSiteFromId($idSite);
$this->assertEquals(0, $site['exclude_unknown_urls']);
API::getInstance()->updateSite($idSite, $siteName = null, $urls = null, $ecommerce = null, $siteSearch = null,
$searchKeywordParams = null, $searchCategoryParams = null, $excludedIps = null, $excludedQueryParameters = null,
$timzeone = null, $currency = null, $group = null, $startDate = null, $excludedUserAgents = null,
$keepUrlFragments = null, $type = null, $settings = null, $excludeUnknownUrls = true);
$site = API::getInstance()->getSiteFromId($idSite);
$this->assertEquals(1, $site['exclude_unknown_urls']);
}
/**
* @dataProvider getDifferentTypesDataProvider
*/
public function test_updateSite_WithDifferentTypes($type)
{
$idSite = $this->addSiteWithType('website', array());
$site = API::getInstance()->getSiteFromId($idSite);
$this->assertEquals(0, $site['exclude_unknown_urls']);
API::getInstance()->updateSite($idSite, $siteName = 'new site name', $urls = null, $ecommerce = true, $siteSearch = false,
$searchKeywordParams = null, $searchCategoryParams = null, $excludedIps = null, $excludedQueryParameters = null,
$timzeone = null, $currency = 'NZD', $group = null, $startDate = null, $excludedUserAgents = null,
$keepUrlFragments = null, $type, $settings = null, $excludeUnknownUrls = true);
$site = API::getInstance()->getSiteFromId($idSite);
$this->assertEquals('new site name', $site['name']);
$this->assertEquals(1, $site['exclude_unknown_urls']);
$this->assertEquals(1, $site['ecommerce']);
$this->assertEquals(0, $site['sitesearch']);
$this->assertEquals('NZD', $site['currency']);
}
public function getDifferentTypesDataProvider()
{
return array(
array('website'),
array('mobileapp'),
array('notexistingtype'),
);
}
public function test_delete_ShouldNotDeleteASiteInCaseThereIsOnlyOneSite()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('SitesManager_ExceptionDeleteSite');
$siteId1 = $this->_addSite();
$this->assertHasSite($siteId1);
try {
API::getInstance()->deleteSite($siteId1);
$this->fail('an expected exception was not raised');
} catch (Exception $e) {
$this->assertHasSite($siteId1);
throw $e;
}
}
public function test_delete_ShouldTriggerException_IfGivenSiteDoesNotExist()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('website id = 99999498 not found');
API::getInstance()->deleteSite(99999498);
}
public function test_delete_ShouldActuallyRemoveAnExistingSiteButOnlyTheGivenSite()
{
$this->_addSite();
$siteId1 = $this->_addSite();
$siteId2 = $this->_addSite();
$this->assertHasSite($siteId1);
$this->assertHasSite($siteId2);
API::getInstance()->deleteSite($siteId1);
$this->assertHasNotSite($siteId1);
$this->assertHasSite($siteId2);
}
public function test_delete_ShouldTriggerAnEventOnceSiteWasActuallyDeleted()
{
$called = 0;
$deletedSiteId = null;
Piwik::addAction('SitesManager.deleteSite.end', function ($param) use (&$called, &$deletedSiteId) {
$called++;
$deletedSiteId = $param;
});
$this->_addSite();
$siteId1 = $this->_addSite();
API::getInstance()->deleteSite($siteId1);
$this->assertSame(1, $called);
$this->assertSame($siteId1, $deletedSiteId);
}
private function assertHasSite($idSite)
{
$model = new Model();
$siteInfo = $model->getSiteFromId($idSite);
$this->assertNotEmpty($siteInfo);
}
private function assertHasNotSite($idSite)
{
$model = new Model();
$siteInfo = $model->getSiteFromId($idSite);
$this->assertEmpty($siteInfo);
}
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
*/
public function test_addSite_WithInvalidTimezone_ThrowsException($timezone)
{
$this->expectException(\Exception::class);
API::getInstance()->addSite("site1", array('http://example.org'), $ecommerce = 0,
$siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null, $ip = '', $params = '', $timezone);
}
public function test_addSite_WithInvalidCurrency_ThrowsException()
{
$this->expectException(\Exception::class);
$invalidCurrency = '€';
API::getInstance()->addSite("site1", array('http://example.org'), $ecommerce = 0,
$siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null, '', 'UTC', $invalidCurrency);
}
public function test_setDefaultTimezone_AndCurrency_AndExcludedQueryParameters_AndExcludedIps_UpdatesDefaultsCorreclty()
{
// 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));
}
public function test_getSitesIdFromSiteUrl_AsSuperUser_ReturnsTheRequestedSiteIds()
{
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);
}
public function test_getSitesIdFromSiteUrl_MatchesBothHttpAndHttpsUrls_AsSuperUser()
{
API::getInstance()->addSite("site1", array("https://piwik.org", "http://example.com", "fb://special-url"));
$this->assert_getSitesIdFromSiteUrl_matchesBothHttpAndHttpsUrls();
}
public function test_getSitesIdFromSiteUrl_MatchesBothHttpAndHttpsUrls_AsUserWithViewPermission()
{
API::getInstance()->addSite("site1", array("https://piwik.org", "http://example.com", "fb://special-url"));
APIUsersManager::getInstance()->addUser("user1", "geqgegagae", "tegst@tesgt.com");
APIUsersManager::getInstance()->setUserAccess("user1", "view", array(1));
// Make sure we're not Super user
FakeAccess::$superUser = false;
FakeAccess::$identity = 'user1';
$this->assertFalse(Piwik::hasUserSuperUserAccess());
$this->assert_getSitesIdFromSiteUrl_matchesBothHttpAndHttpsUrls();
}
private function assert_getSitesIdFromSiteUrl_matchesBothHttpAndHttpsUrls()
{
$idsites = API::getInstance()->getSitesIdFromSiteUrl('http://piwik.org');
$this->assertTrue(count($idsites) == 1);
$idsites = API::getInstance()->getSitesIdFromSiteUrl('piwik.org');
$this->assertTrue(count($idsites) == 1);
$idsites = API::getInstance()->getSitesIdFromSiteUrl('https://www.piwik.org');
$this->assertTrue(count($idsites) == 1);
$idsites = API::getInstance()->getSitesIdFromSiteUrl('https://example.com');
$this->assertTrue(count($idsites) == 1);
$idsites = API::getInstance()->getSitesIdFromSiteUrl("fb://special-url");
$this->assertTrue(count($idsites) == 1);
$idsites = API::getInstance()->getSitesIdFromSiteUrl('https://random-example.com');
$this->assertTrue(count($idsites) == 0);
$idsites = API::getInstance()->getSitesIdFromSiteUrl('not-found.piwik.org');
$this->assertTrue(count($idsites) == 0);
$idsites = API::getInstance()->getSitesIdFromSiteUrl('piwik.org/not-found/');
$this->assertTrue(count($idsites) == 0);
}
public function test_getSitesIdFromSiteUrl_AsUser()
{
API::getInstance()->addSite("site1", array("http://www.piwik.net", "https://piwik.com"));
API::getInstance()->addSite("site2", array("http://piwik.com", "http://piwik.net"));
API::getInstance()->addSite("site3", array("http://piwik.com", "http://piwik.org"));
APIUsersManager::getInstance()->addUser("user1", "geqgegagae", "tegst@tesgt.com");
APIUsersManager::getInstance()->setUserAccess("user1", "view", array(1));
APIUsersManager::getInstance()->addUser("user2", "geqgegagae", "tegst2@tesgt.com");
APIUsersManager::getInstance()->setUserAccess("user2", "view", array(1));
APIUsersManager::getInstance()->setUserAccess("user2", "admin", array(3));
APIUsersManager::getInstance()->addUser("user3", "geqgegagae", "tegst3@tesgt.com");
APIUsersManager::getInstance()->setUserAccess("user3", "view", array(1, 2));
APIUsersManager::getInstance()->setUserAccess("user3", "admin", array(3));
FakeAccess::$superUser = false;
FakeAccess::$identity = 'user1';
FakeAccess::setIdSitesView(array(1));
FakeAccess::setIdSitesAdmin(array());
$this->assertFalse(Piwik::hasUserSuperUserAccess());
$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));
FakeAccess::$superUser = false;
FakeAccess::$identity = 'user2';
FakeAccess::setIdSitesView(array(1));
FakeAccess::setIdSitesAdmin(array(3));
$idsites = API::getInstance()->getSitesIdFromSiteUrl('http://piwik.com');
$this->assertEquals(2, count($idsites));
FakeAccess::$superUser = false;
FakeAccess::$identity = 'user3';
FakeAccess::setIdSitesView(array(1, 2));
FakeAccess::setIdSitesAdmin(array(3));
$idsites = API::getInstance()->getSitesIdFromSiteUrl('http://piwik.com');
$this->assertEquals(3, count($idsites));
$idsites = API::getInstance()->getSitesIdFromSiteUrl('https://www.piwik.com');
$this->assertEquals(3, count($idsites));
}
public function test_getSitesFromTimezones_ReturnsCorrectIdSites()
{
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);
}
public function provideContainerConfig()
{
return array(
'Piwik\Access' => new FakeAccess(),
);
}
}
|