1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class GoogleAnalyticsReportServiceTest |
5
|
|
|
* |
6
|
|
|
* Test the report service it's parts |
7
|
|
|
*/ |
8
|
|
|
class GoogleAnalyticsReportServiceTest extends SapphireTest |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var GoogleClientService |
12
|
|
|
*/ |
13
|
|
|
protected $client; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var GoogleAnalyticsReportService |
17
|
|
|
*/ |
18
|
|
|
protected $service; |
19
|
|
|
|
20
|
|
|
public function setUp() |
21
|
|
|
{ |
22
|
|
|
parent::setUp(); |
23
|
|
|
$this->client = new GoogleClientService(); |
24
|
|
|
$this->service = new GoogleAnalyticsReportService($this->client); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testSetClient() |
28
|
|
|
{ |
29
|
|
|
$this->service->setClient($this->client); |
30
|
|
|
$this->assertInstanceOf(GoogleClientService::class, $this->service->getClient()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testGetAnalytics() |
34
|
|
|
{ |
35
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting::class, $this->service->getAnalytics()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetMetrics() |
39
|
|
|
{ |
40
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_Metric::class, $this->service->getMetrics()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testDateRange() |
44
|
|
|
{ |
45
|
|
|
/* Set the daterange to 60 days */ |
46
|
|
|
$this->service->setDateRange(60); |
47
|
|
|
/** @var Google_Service_AnalyticsReporting_DateRange $dateRange */ |
48
|
|
|
$dateRange = $this->service->getDateRange(); |
49
|
|
|
|
50
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_DateRange::class, $dateRange); |
51
|
|
|
|
52
|
|
|
$this->assertEquals('60daysAgo', $dateRange->getStartDate()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function testGetStartDimensionFilters() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
Config::inst()->update(GoogleAnalyticsReportService::class, 'starts_with', '/home'); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$filters = $this->service->getDimensionFilters(); |
60
|
|
|
|
61
|
|
|
$this->assertTrue(is_array($filters)); |
62
|
|
|
foreach ($filters as $filter) { |
63
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
public function testGetEndDimensionFilters() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
Config::inst()->update(GoogleAnalyticsReportService::class, 'ends_with', '/home'); |
70
|
|
|
|
71
|
|
|
$filters = $this->service->getDimensionFilters(); |
72
|
|
|
|
73
|
|
|
$this->assertTrue(is_array($filters)); |
74
|
|
|
foreach ($filters as $filter) { |
75
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testGetPageDimensionFilters() |
80
|
|
|
{ |
81
|
|
|
$filters = $this->service->getDimensionFilters(); |
82
|
|
|
|
83
|
|
|
$this->assertTrue(is_array($filters)); |
84
|
|
|
foreach ($filters as $filter) { |
85
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testGetDimensionFilterClauses() |
90
|
|
|
{ |
91
|
|
|
$dimensionsClauses = $this->service->getDimensionFilterClauses(); |
92
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilterClause::class, $dimensionsClauses); |
93
|
|
|
$this->assertEquals('OR', $dimensionsClauses->getOperator()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testGetPages() |
97
|
|
|
{ |
98
|
|
|
$pages = $this->service->getPages(); |
99
|
|
|
$this->assertTrue(is_array($pages)); |
100
|
|
|
$this->assertLessThanOrEqual(20, count($pages)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testFilterLength() |
104
|
|
|
{ |
105
|
|
|
for ($i = 1; $i <= 30; $i++) { |
106
|
|
|
/** @var Page` $page */ |
107
|
|
|
$page = Page::create(['Title' => $i . ' test']); |
|
|
|
|
108
|
|
|
$page->write(); |
109
|
|
|
$page->doPublish(); |
110
|
|
|
} |
111
|
|
|
$pages = $this->service->getPages(); |
112
|
|
|
$this->assertTrue(is_array($pages)); |
113
|
|
|
$this->assertEquals(20, count($pages)); |
114
|
|
|
$this->assertTrue($this->service->batched); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testFindHomePage() |
118
|
|
|
{ |
119
|
|
|
$testArray = ['', '']; |
120
|
|
|
$page = $this->service->findPage($testArray); |
121
|
|
|
$this->assertInstanceOf(Page::class, $page); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
View Code Duplication |
public function testFindChildPage() |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
/** @var Page $homePage */ |
127
|
|
|
$homePage = Page::get()->filter(['URLSegment' => 'home'])->first(); |
128
|
|
|
/** @var Page $homePage */ |
129
|
|
|
$child = Page::create(['Title' => 'Test Page', 'ParentID' => $homePage->ID]); |
130
|
|
|
$child->write(); |
131
|
|
|
$child->doPublish(); |
132
|
|
|
$testArray = ['', '', 'test-page']; |
133
|
|
|
$page = $this->service->findPage($testArray); |
134
|
|
|
$this->assertInstanceOf(Page::class, $page); |
135
|
|
|
$this->assertEquals('/home/test-page/', $page->Link()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
View Code Duplication |
public function testFindChildPageWithQuery() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
/** @var Page $homePage */ |
141
|
|
|
$homePage = Page::get()->filter(['URLSegment' => 'home'])->first(); |
142
|
|
|
/** @var Page $homePage */ |
143
|
|
|
$child = Page::create(['Title' => 'Test Page', 'ParentID' => $homePage->ID]); |
144
|
|
|
$child->write(); |
145
|
|
|
$child->doPublish(); |
146
|
|
|
$testArray = ['', '', 'test-page', '?stage=Stage']; |
147
|
|
|
$page = $this->service->findPage($testArray); |
148
|
|
|
$this->assertInstanceOf(Page::class, $page); |
149
|
|
|
$this->assertEquals('/home/test-page/', $page->Link()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
View Code Duplication |
public function testFindPageWithEmptyLast() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
/** @var Page $homePage */ |
155
|
|
|
$homePage = Page::get()->filter(['URLSegment' => 'home'])->first(); |
156
|
|
|
/** @var Page $homePage */ |
157
|
|
|
$child = Page::create(['Title' => 'Test Page', 'ParentID' => $homePage->ID]); |
158
|
|
|
$child->write(); |
159
|
|
|
$child->doPublish(); |
160
|
|
|
$testArray = ['', '', 'test-page', '']; |
161
|
|
|
$page = $this->service->findPage($testArray); |
162
|
|
|
$this->assertInstanceOf(Page::class, $page); |
163
|
|
|
$this->assertEquals('/home/test-page/', $page->Link()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
View Code Duplication |
public function testGetBlacklistedPages() |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
Config::inst()->update(GoogleAnalyticsReportService::class, 'blacklist', [ErrorPage::class]); |
|
|
|
|
169
|
|
|
$pages = $this->service->getPages(); |
170
|
|
|
$this->assertTrue(is_array($pages)); |
171
|
|
|
$this->assertNotContains('page-not-found', $pages); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
View Code Duplication |
public function testGetWhitelistedPages() |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
Config::inst()->update(GoogleAnalyticsReportService::class, 'whitelist', [Page::class]); |
177
|
|
|
$pages = $this->service->getPages(); |
178
|
|
|
$this->assertTrue(is_array($pages)); |
179
|
|
|
// There seems to be some chaining in effect, causing the array to be empty |
180
|
|
|
$this->assertEquals(20, count($pages)); |
181
|
|
|
// Error Pages are children of Page, so they should be included |
182
|
|
|
$this->assertContains('page-not-found', $pages); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testUpdateVisits() |
186
|
|
|
{ |
187
|
|
|
$result = $this->service->updateVisits([new AnalyticsResponseHomePageMock()]); |
188
|
|
|
$this->assertEquals(1, $result); |
189
|
|
|
$page = Page::get()->filter(['URLSegment' => 'home'])->first(); |
190
|
|
|
$this->assertEquals(45477, $page->VisitCount); |
191
|
|
|
} |
192
|
|
|
public function testUpdateNoVisits() |
193
|
|
|
{ |
194
|
|
|
$result = $this->service->updateVisits([new AnalyticsResponseNoPageMock()]); |
195
|
|
|
$this->assertEquals(0, $result); |
196
|
|
|
$this->assertFalse($this->service->batched); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths