Passed
Push — master ( c81a5e...499d21 )
by Simon
01:29
created

tests/GoogleAnalyticsReportServiceTest.php (1 issue)

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
    /**
21
     * Create the Services
22
     */
23
    public function setUp()
24
    {
25
        parent::setUp();
26
        $this->client = new GoogleClientService();
27
        $this->service = new GoogleAnalyticsReportService($this->client);
28
    }
29
30
    /**
31
     * Check if we can set and get the client
32
     */
33
    public function testSetClient()
34
    {
35
        $this->service->setClient($this->client);
36
        $this->assertInstanceOf(GoogleClientService::class, $this->service->getClient());
37
    }
38
39
    /**
40
     * See if we have a new Service registered
41
     */
42
    public function testGetAnalytics()
43
    {
44
        $this->assertInstanceOf(Google_Service_AnalyticsReporting::class, $this->service->getAnalytics());
45
    }
46
47
    /**
48
     * Check if the Metric is set
49
     */
50
    public function testGetMetrics()
51
    {
52
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_Metric::class, $this->service->getMetrics());
53
    }
54
55
    /**
56
     * Check if setting the daterange updates the Analytics_DateRange
57
     */
58
    public function testDateRange()
59
    {
60
        /* Set the daterange to 60 days */
61
        $this->service->setDateRange(60);
62
        /** @var Google_Service_AnalyticsReporting_DateRange $dateRange */
63
        $dateRange = $this->service->getDateRange();
64
65
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_DateRange::class, $dateRange);
66
67
        $this->assertEquals('60daysAgo', $dateRange->getStartDate());
68
    }
69
70
    /**
71
     * Validate start_with filter
72
     */
73 View Code Duplication
    public function testGetStartDimensionFilters()
74
    {
75
        Config::inst()->update(GoogleAnalyticsReportService::class, 'starts_with', '/');
76
77
        $filters = $this->service->getDimensionFilters();
78
79
        $this->assertTrue(is_array($filters));
80
        /** @var  $filter */
81
        foreach ($filters as $filter) {
82
            $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter);
83
            $expressions = $filter->getExpressions();
84
            $this->assertEquals('/', $expressions[0]);
85
            $this->assertEquals('BEGINS_WITH', $filter->getOperator());
86
        }
87
    }
88
89
    /**
90
     * validate end_with filter
91
     */
92 View Code Duplication
    public function testGetEndDimensionFilters()
93
    {
94
        Config::inst()->update(GoogleAnalyticsReportService::class, 'ends_with', '/');
95
96
        $filters = $this->service->getDimensionFilters();
97
98
        $this->assertTrue(is_array($filters));
99
        foreach ($filters as $filter) {
100
            $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter);
101
            $expressions = $filter->getExpressions();
102
            $this->assertEquals('/', $expressions[0]);
103
            $this->assertEquals('ENDS_WITH', $filter->getOperator());
104
        }
105
    }
106
107
    /**
108
     * Test page filters are created
109
     */
110
    public function testGetPageDimensionFilters()
111
    {
112
        $filters = $this->service->getDimensionFilters();
113
114
        $this->assertTrue(is_array($filters));
115
        foreach ($filters as $filter) {
116
            $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter);
117
            $this->assertEquals('ENDS_WITH', $filter->getOperator());
118
        }
119
    }
120
121
    /**
122
     * Validate our DimensionClause is set with OR
123
     */
124
    public function testGetDimensionFilterClauses()
125
    {
126
        $dimensionsClauses = $this->service->getDimensionFilterClauses();
127
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilterClause::class, $dimensionsClauses);
128
        $this->assertEquals('OR', $dimensionsClauses->getOperator());
129
    }
130
131
    /**
132
     * Make sure the filter-list does not exceed 20
133
     */
134
    public function testGetPages()
135
    {
136
        $pages = $this->service->getPages();
137
        $this->assertTrue(is_array($pages));
138
        $this->assertLessThanOrEqual(20, count($pages));
139
    }
140
141
    /**
142
     * Make sure we're not adding more than 20 filters, despite more pages being available
143
     */
144
    public function testFilterLength()
145
    {
146
        for ($i = 1; $i <= 30; $i++) {
147
            /** @var Page` $page */
148
            $page = Page::create(['Title' => $i . ' test']);
0 ignored issues
show
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
149
            $page->write();
150
            $page->doPublish();
151
        }
152
        $pages = $this->service->getPages();
153
        $this->assertTrue(is_array($pages));
154
        $this->assertEquals(20, count($pages));
155
        $this->assertTrue($this->service->batched);
156
    }
157
158
    /**
159
     * Validate an array with two empty values finds the homepage
160
     */
161
    public function testFindHomePage()
162
    {
163
        $testArray = ['', ''];
164
        $page = $this->service->findPage($testArray);
165
        $this->assertInstanceOf(Page::class, $page);
166
        $this->assertTrue('home', $page->URLSegment);
167
    }
168
169
    /**
170
     * Validate we're traversing correctly down in to the child pages
171
     */
172 View Code Duplication
    public function testFindChildPage()
173
    {
174
        /** @var Page $homePage */
175
        $homePage = Page::get()->filter(['URLSegment' => 'home'])->first();
176
        /** @var Page $homePage */
177
        $child = Page::create(['Title' => 'Test Page', 'ParentID' => $homePage->ID]);
178
        $child->write();
179
        $child->doPublish();
180
        $testArray = ['', '', 'test-page'];
181
        $page = $this->service->findPage($testArray);
182
        $this->assertInstanceOf(Page::class, $page);
183
        $this->assertEquals('/home/test-page/', $page->Link());
184
    }
185
186
    /**
187
     * Make sure the get params are ignored
188
     */
189 View Code Duplication
    public function testFindChildPageWithQuery()
190
    {
191
        /** @var Page $homePage */
192
        $homePage = Page::get()->filter(['URLSegment' => 'home'])->first();
193
        /** @var Page $homePage */
194
        $child = Page::create(['Title' => 'Test Page', 'ParentID' => $homePage->ID]);
195
        $child->write();
196
        $child->doPublish();
197
        $testArray = ['', '', 'test-page', '?stage=Stage'];
198
        $page = $this->service->findPage($testArray);
199
        $this->assertInstanceOf(Page::class, $page);
200
        $this->assertEquals('/home/test-page/', $page->Link());
201
    }
202
203
    /**
204
     * Make sure an empty last value is skipped
205
     */
206 View Code Duplication
    public function testFindPageWithEmptyLast()
207
    {
208
        /** @var Page $homePage */
209
        $homePage = Page::get()->filter(['URLSegment' => 'home'])->first();
210
        /** @var Page $homePage */
211
        $child = Page::create(['Title' => 'Test Page', 'ParentID' => $homePage->ID]);
212
        $child->write();
213
        $child->doPublish();
214
        $testArray = ['', '', 'test-page', ''];
215
        $page = $this->service->findPage($testArray);
216
        $this->assertInstanceOf(Page::class, $page);
217
        $this->assertEquals('/home/test-page/', $page->Link());
218
    }
219
220
    /**
221
     * Validate blacklisted pages don't show up
222
     */
223 View Code Duplication
    public function testGetBlacklistedPages()
224
    {
225
        Config::inst()->update(GoogleAnalyticsReportService::class, 'blacklist', [ErrorPage::class]);
226
        $pages = $this->service->getPages();
227
        $this->assertTrue(is_array($pages));
228
        $this->assertNotContains('page-not-found', $pages);
229
    }
230
231
    /**
232
     * Make sure we only get whitelisted pages
233
     */
234 View Code Duplication
    public function testGetWhitelistedPages()
235
    {
236
        Config::inst()->update(GoogleAnalyticsReportService::class, 'whitelist', [Page::class]);
237
        $pages = $this->service->getPages();
238
        $this->assertTrue(is_array($pages));
239
        // There seems to be some chaining in effect, causing the array to be empty
240
        $this->assertEquals(20, count($pages));
241
        // Error Pages are children of Page, so they should be included
242
        $this->assertContains('page-not-found', $pages);
243
    }
244
245
    /**
246
     * Validate updating the pages works
247
     */
248
    public function testUpdateVisits()
249
    {
250
        $result = $this->service->updateVisits([new AnalyticsResponseHomePageMock()]);
251
        $this->assertEquals(1, $result);
252
        $page = Page::get()->filter(['URLSegment' => 'home'])->first();
253
        $this->assertEquals(45477, $page->VisitCount);
254
    }
255
256
    /**
257
     * Validate that if no pages is found, nothing is done.
258
     */
259
    public function testUpdateNoVisits()
260
    {
261
        $result = $this->service->updateVisits([new AnalyticsResponseNoPageMock()]);
262
        $this->assertEquals(0, $result);
263
        $this->assertFalse($this->service->batched);
264
    }
265
}
266