Source of file GoogleAnalyticsReportServiceTest.php
Size: 8,092 Bytes - Last Modified: 2021-12-24T06:47:25+00:00
/var/www/docs.ssmods.com/process/src/tests/GoogleAnalyticsReportServiceTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 | <?php /** * Class GoogleAnalyticsReportServiceTest * * Test the report service it's parts */ class GoogleAnalyticsReportServiceTest extends SapphireTest { const VIEWID = 12345; /** * @var GoogleClientService */ protected $client; /** * @var GoogleAnalyticsReportService */ protected $service; /** * Create the Services */ public function setUp() { parent::setUp(); if (!defined('SS_ANALYTICS_KEY')) { define('SS_ANALYTICS_KEY', Director::baseFolder() . DIRECTORY_SEPARATOR . 'google-api/tests/fixtures/test.json'); } $this->client = new GoogleClientService(); $this->service = new GoogleAnalyticsReportService($this->client); $siteConfig = SiteConfig::current_site_config(); $siteConfig->Viewid = static::VIEWID; $siteConfig->write(); } /** * Check if we can set and get the client */ public function testSetClient() { $this->service->setClient($this->client); $this->assertInstanceOf(GoogleClientService::class, $this->service->getClient()); } /** * See if we have a new Service registered */ public function testGetAnalytics() { $this->assertInstanceOf(Google_Service_AnalyticsReporting::class, $this->service->getAnalytics()); } /** * Check if the Metric is set */ public function testGetMetrics() { $this->assertInstanceOf(Google_Service_AnalyticsReporting_Metric::class, $this->service->getMetrics()); } /** * Check if setting the daterange updates the Analytics_DateRange */ public function testDateRange() { /* Set the daterange to 60 days */ $this->service->setDateRange(60); /** @var Google_Service_AnalyticsReporting_DateRange $dateRange */ $dateRange = $this->service->getDateRange(); $this->assertInstanceOf(Google_Service_AnalyticsReporting_DateRange::class, $dateRange); $this->assertEquals('60daysAgo', $dateRange->getStartDate()); } /** * Confirm the report request is correctly build */ public function testGetReportRequest() { $reportRequest = $this->service->getReportRequest(); /** @var array|Google_Service_AnalyticsReporting_Metric[] $metrics */ $metrics = $reportRequest->getMetrics(); /** @var array|Google_Service_AnalyticsReporting_Dimension[] $dimensions */ $dimensions = $reportRequest->getDimensions(); $this->assertInstanceOf(Google_Service_AnalyticsReporting_ReportRequest::class, $reportRequest); $this->assertTrue(is_array($metrics)); $this->assertInstanceOf(Google_Service_AnalyticsReporting_Metric::class, $metrics[0]); $this->assertTrue(is_array($dimensions)); $this->assertInstanceOf(Google_Service_AnalyticsReporting_Dimension::class, $dimensions[0]); $this->assertEquals('ga:pagePath', $dimensions[0]->getName()); $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilterClause::class, $reportRequest->getDimensionFilterClauses()); $this->assertInstanceOf(Google_Service_AnalyticsReporting_DateRange::class, $reportRequest->getDateRanges()); $this->assertEquals(static::VIEWID, $reportRequest->getViewId()); } public function testGetGetReportRequest() { $reportRequest = $this->service->getReportRequest(); $getReportRequest = $this->service->getGetReportRequest($reportRequest); $this->assertInstanceOf(Google_Service_AnalyticsReporting_GetReportsRequest::class, $getReportRequest); $reportRequest2 = $getReportRequest->getReportRequests(); $this->assertEquals($reportRequest, $reportRequest2[0]); } public function testGetDimension() { /** @var array|Google_Service_AnalyticsReporting_Dimension[] $dimensions */ $dimensions = $this->service->getDimensions(); $this->assertTrue(is_array($dimensions)); foreach ($dimensions as $dimension) { $this->assertEquals('ga:pagePath', $dimension->getName()); } } /** * Validate start_with filter */ public function testGetStartDimensionFilters() { Config::inst()->update(GoogleAnalyticsReportService::class, 'starts_with', '/'); $filters = $this->service->getDimensionFilters(); $this->assertTrue(is_array($filters)); /** @var $filter */ foreach ($filters as $filter) { $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter); $expressions = $filter->getExpressions(); $this->assertEquals('/', $expressions[0]); $this->assertEquals('BEGINS_WITH', $filter->getOperator()); } } /** * validate end_with filter */ public function testGetEndDimensionFilters() { Config::inst()->update(GoogleAnalyticsReportService::class, 'ends_with', '/'); $filters = $this->service->getDimensionFilters(); $this->assertTrue(is_array($filters)); foreach ($filters as $filter) { $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter); $expressions = $filter->getExpressions(); $this->assertEquals('/', $expressions[0]); $this->assertEquals('ENDS_WITH', $filter->getOperator()); } } /** * Test page filters are created */ public function testGetPageDimensionFilters() { $filters = $this->service->getDimensionFilters(); $this->assertTrue(is_array($filters)); foreach ($filters as $filter) { $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter); $this->assertTrue(in_array($filter->getOperator(), ['ENDS_WITH', 'EXACT'], true)); } } /** * Validate our DimensionClause is set with OR */ public function testGetDimensionFilterClauses() { $dimensionsClauses = $this->service->getDimensionFilterClauses(); $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilterClause::class, $dimensionsClauses); $this->assertEquals('OR', $dimensionsClauses->getOperator()); } /** * Make sure the filter-list does not exceed 20 */ public function testGetPages() { $pages = $this->service->getPages(); $this->assertTrue(is_array($pages)); $this->assertLessThanOrEqual(20, count($pages)); } /** * Make sure we're not adding more than 20 filters, despite more pages being available */ public function testFilterLength() { for ($i = 1; $i <= 30; $i++) { /** @var Page` $page */ $page = Page::create(['Title' => $i . ' test']); $page->write(); $page->doPublish(); } $pages = $this->service->getPages(); $this->assertTrue(is_array($pages)); $this->assertCount(20, $pages); $this->assertTrue($this->service->batched); } /** * Validate blacklisted pages don't show up */ public function testGetBlacklistedPages() { Config::inst()->update(GoogleAnalyticsReportService::class, 'blacklist', [ErrorPage::class]); $pages = $this->service->getPages(); $this->assertTrue(is_array($pages)); $this->assertNotContains('page-not-found', $pages); } /** * Make sure we only get whitelisted pages */ public function testGetWhitelistedPages() { Config::inst()->update(GoogleAnalyticsReportService::class, 'whitelist', [Page::class]); $pages = $this->service->getPages(); $this->assertTrue(is_array($pages)); // There seems to be some chaining in effect, causing the array to be empty $this->assertEquals(20, count($pages)); // Error Pages are children of Page, so they should be included $this->assertContains('page-not-found', $pages); } } |