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