1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class GoogleAnalyticsReportServiceTest |
5
|
|
|
* |
6
|
|
|
* Test the report service it's parts |
7
|
|
|
*/ |
8
|
|
|
class GoogleAnalyticsReportServiceTest extends SapphireTest |
9
|
|
|
{ |
10
|
|
|
const VIEWID = 12345; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var GoogleClientService |
14
|
|
|
*/ |
15
|
|
|
protected $client; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var GoogleAnalyticsReportService |
19
|
|
|
*/ |
20
|
|
|
protected $service; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create the Services |
24
|
|
|
*/ |
25
|
|
|
public function setUp() |
26
|
|
|
{ |
27
|
|
|
parent::setUp(); |
28
|
|
|
$this->client = new GoogleClientService(); |
29
|
|
|
$this->service = new GoogleAnalyticsReportService($this->client); |
30
|
|
|
$siteConfig = SiteConfig::current_site_config(); |
|
|
|
|
31
|
|
|
$siteConfig->Viewid = static::VIEWID; |
32
|
|
|
$siteConfig->write(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Check if we can set and get the client |
37
|
|
|
*/ |
38
|
|
|
public function testSetClient() |
39
|
|
|
{ |
40
|
|
|
$this->service->setClient($this->client); |
41
|
|
|
$this->assertInstanceOf(GoogleClientService::class, $this->service->getClient()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* See if we have a new Service registered |
46
|
|
|
*/ |
47
|
|
|
public function testGetAnalytics() |
48
|
|
|
{ |
49
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting::class, $this->service->getAnalytics()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Check if the Metric is set |
54
|
|
|
*/ |
55
|
|
|
public function testGetMetrics() |
56
|
|
|
{ |
57
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_Metric::class, $this->service->getMetrics()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check if setting the daterange updates the Analytics_DateRange |
62
|
|
|
*/ |
63
|
|
|
public function testDateRange() |
64
|
|
|
{ |
65
|
|
|
/* Set the daterange to 60 days */ |
66
|
|
|
$this->service->setDateRange(60); |
67
|
|
|
/** @var Google_Service_AnalyticsReporting_DateRange $dateRange */ |
68
|
|
|
$dateRange = $this->service->getDateRange(); |
69
|
|
|
|
70
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_DateRange::class, $dateRange); |
71
|
|
|
|
72
|
|
|
$this->assertEquals('60daysAgo', $dateRange->getStartDate()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Confirm the report request is correctly build |
77
|
|
|
*/ |
78
|
|
|
public function testGetReportRequest() |
79
|
|
|
{ |
80
|
|
|
$reportRequest = $this->service->getReportRequest(); |
81
|
|
|
|
82
|
|
|
$metrics = $reportRequest->getMetrics(); |
83
|
|
|
$dimensions = $reportRequest->getDimensions(); |
84
|
|
|
|
85
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_ReportRequest::class, $reportRequest); |
86
|
|
|
$this->assertTrue(is_array($metrics)); |
87
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_Metric::class, $metrics[0]); |
88
|
|
|
$this->assertTrue(is_array($dimensions)); |
89
|
|
|
$this->assertEquals('ga:pagePath', $dimensions[0]->getName()); |
|
|
|
|
90
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilterClause::class, $reportRequest->getDimensionFilterClauses()); |
91
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_DateRange::class, $reportRequest->getDateRanges()); |
92
|
|
|
$this->assertEquals(static::VIEWID, $reportRequest->getViewId()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testGetDimension() |
96
|
|
|
{ |
97
|
|
|
$dimensions = $this->service->getDimensions(); |
98
|
|
|
$this->assertTrue(is_array($dimensions)); |
99
|
|
|
foreach ($dimensions as $dimension) { |
100
|
|
|
$this->assertEquals('ga:pagePath', $dimension->getName()); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Validate start_with filter |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function testGetStartDimensionFilters() |
108
|
|
|
{ |
109
|
|
|
Config::inst()->update(GoogleAnalyticsReportService::class, 'starts_with', '/'); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$filters = $this->service->getDimensionFilters(); |
112
|
|
|
|
113
|
|
|
$this->assertTrue(is_array($filters)); |
114
|
|
|
/** @var $filter */ |
115
|
|
|
foreach ($filters as $filter) { |
116
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter); |
117
|
|
|
$expressions = $filter->getExpressions(); |
118
|
|
|
$this->assertEquals('/', $expressions[0]); |
119
|
|
|
$this->assertEquals('BEGINS_WITH', $filter->getOperator()); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* validate end_with filter |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function testGetEndDimensionFilters() |
127
|
|
|
{ |
128
|
|
|
Config::inst()->update(GoogleAnalyticsReportService::class, 'ends_with', '/'); |
129
|
|
|
|
130
|
|
|
$filters = $this->service->getDimensionFilters(); |
131
|
|
|
|
132
|
|
|
$this->assertTrue(is_array($filters)); |
133
|
|
|
foreach ($filters as $filter) { |
134
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter); |
135
|
|
|
$expressions = $filter->getExpressions(); |
136
|
|
|
$this->assertEquals('/', $expressions[0]); |
137
|
|
|
$this->assertEquals('ENDS_WITH', $filter->getOperator()); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Test page filters are created |
143
|
|
|
*/ |
144
|
|
|
public function testGetPageDimensionFilters() |
145
|
|
|
{ |
146
|
|
|
$filters = $this->service->getDimensionFilters(); |
147
|
|
|
|
148
|
|
|
$this->assertTrue(is_array($filters)); |
149
|
|
|
foreach ($filters as $filter) { |
150
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter); |
151
|
|
|
$this->assertTrue(in_array($filter->getOperator(), ['ENDS_WITH', 'EXACT'], true)); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Validate our DimensionClause is set with OR |
157
|
|
|
*/ |
158
|
|
|
public function testGetDimensionFilterClauses() |
159
|
|
|
{ |
160
|
|
|
$dimensionsClauses = $this->service->getDimensionFilterClauses(); |
161
|
|
|
$this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilterClause::class, $dimensionsClauses); |
162
|
|
|
$this->assertEquals('OR', $dimensionsClauses->getOperator()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Make sure the filter-list does not exceed 20 |
167
|
|
|
*/ |
168
|
|
|
public function testGetPages() |
169
|
|
|
{ |
170
|
|
|
$pages = $this->service->getPages(); |
171
|
|
|
$this->assertTrue(is_array($pages)); |
172
|
|
|
$this->assertLessThanOrEqual(20, count($pages)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Make sure we're not adding more than 20 filters, despite more pages being available |
177
|
|
|
*/ |
178
|
|
|
public function testFilterLength() |
179
|
|
|
{ |
180
|
|
|
for ($i = 1; $i <= 30; $i++) { |
181
|
|
|
/** @var Page` $page */ |
182
|
|
|
$page = Page::create(['Title' => $i . ' test']); |
183
|
|
|
$page->write(); |
184
|
|
|
$page->doPublish(); |
185
|
|
|
} |
186
|
|
|
$pages = $this->service->getPages(); |
187
|
|
|
$this->assertTrue(is_array($pages)); |
188
|
|
|
$this->assertCount(20, $pages); |
189
|
|
|
$this->assertTrue($this->service->batched); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Validate blacklisted pages don't show up |
194
|
|
|
*/ |
195
|
|
View Code Duplication |
public function testGetBlacklistedPages() |
196
|
|
|
{ |
197
|
|
|
Config::inst()->update(GoogleAnalyticsReportService::class, 'blacklist', [ErrorPage::class]); |
|
|
|
|
198
|
|
|
$pages = $this->service->getPages(); |
199
|
|
|
$this->assertTrue(is_array($pages)); |
200
|
|
|
$this->assertNotContains('page-not-found', $pages); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Make sure we only get whitelisted pages |
205
|
|
|
*/ |
206
|
|
View Code Duplication |
public function testGetWhitelistedPages() |
207
|
|
|
{ |
208
|
|
|
Config::inst()->update(GoogleAnalyticsReportService::class, 'whitelist', [Page::class]); |
209
|
|
|
$pages = $this->service->getPages(); |
210
|
|
|
$this->assertTrue(is_array($pages)); |
211
|
|
|
// There seems to be some chaining in effect, causing the array to be empty |
212
|
|
|
$this->assertEquals(20, count($pages)); |
213
|
|
|
// Error Pages are children of Page, so they should be included |
214
|
|
|
$this->assertContains('page-not-found', $pages); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
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