View source
<?php
namespace Drupal\Tests\google_analytics\Functional;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class GoogleAnalyticsBasicTest extends BrowserTestBase {
protected $noSnippetUser;
public static $modules = [
'block',
'google_analytics',
'help',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$permissions = [
'access administration pages',
'administer google analytics',
'administer modules',
'administer site configuration',
];
$this->noSnippetUser = $this
->drupalCreateUser($permissions);
$permissions[] = 'add JS snippets for google analytics';
$this->admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->admin_user);
$this
->drupalPlaceBlock('help_block', [
'region' => 'help',
]);
}
public function testGoogleAnalyticsConfiguration() {
$this
->drupalGet('admin/modules');
$this
->assertSession()
->responseContains('admin/config/system/google-analytics');
$this
->drupalGet('admin/reports/status');
$this
->assertSession()
->responseContains('admin/config/system/google-analytics');
$this
->drupalGet('admin/config/system/google-analytics');
$this
->assertSession()
->responseContains(t('Web Property ID'));
$edit['google_analytics_account'] = $this
->randomMachineName(2);
$this
->drupalPostForm('admin/config/system/google-analytics', $edit, t('Save configuration'));
$this
->assertSession()
->responseContains(t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'));
$this
->assertFieldByName('google_analytics_codesnippet_create');
$this
->assertFieldByName('google_analytics_codesnippet_before');
$this
->assertFieldByName('google_analytics_codesnippet_after');
$this
->assertNoFieldByXPath("//textarea[@name='google_analytics_codesnippet_create' and @disabled='disabled']", NULL, '"Create only fields" is enabled.');
$this
->assertNoFieldByXPath("//textarea[@name='google_analytics_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is enabled.');
$this
->assertNoFieldByXPath("//textarea[@name='google_analytics_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is enabled.');
$this
->drupalLogin($this->noSnippetUser);
$this
->drupalGet('admin/config/system/google-analytics');
$this
->assertFieldByName('google_analytics_codesnippet_create');
$this
->assertFieldByName('google_analytics_codesnippet_before');
$this
->assertFieldByName('google_analytics_codesnippet_after');
$this
->assertNoFieldByXPath("//textarea[@name='google_analytics_codesnippet_create' and @disabled='disabled']", NULL, '"Create only fields" is enabled.');
$this
->assertFieldByXPath("//textarea[@name='google_analytics_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is disabled.');
$this
->assertFieldByXPath("//textarea[@name='google_analytics_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is disabled.');
}
public function testGoogleAnalyticsHelp() {
$this
->drupalGet('admin/config/system/google-analytics');
$this
->assertText('Google Analytics is a free (registration required) website traffic and marketing effectiveness service.');
$this
->drupalGet('admin/help/google_analytics');
$this
->assertText('Google Analytics adds a web statistics tracking system to your website.');
}
public function testGoogleAnalyticsPageVisibility() {
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains('https://www.google-analytics.com/analytics.js');
$ua_code = 'UA-123456-1';
$this
->config('google_analytics.settings')
->set('account', $ua_code)
->save();
$this
->config('google_analytics.settings')
->set('visibility.request_path_mode', 0)
->save();
$this
->config('google_analytics.settings')
->set('visibility.request_path_pages', "/admin\n/admin/*")
->save();
$this
->config('google_analytics.settings')
->set('visibility.user_role_roles', [
AccountInterface::AUTHENTICATED_ROLE => AccountInterface::AUTHENTICATED_ROLE,
])
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains($ua_code);
$this
->drupalGet('admin');
$this
->assertSession()
->responseNotContains($ua_code);
$this
->drupalGet('admin/config/system/google-analytics');
$this
->assertSession()
->responseNotContains('https://www.google-analytics.com/analytics.js');
$this
->config('google_analytics.settings')
->set('visibility.request_path_mode', 1)
->save();
$this
->drupalGet('admin');
$this
->assertSession()
->responseContains($ua_code);
$this
->drupalGet('admin/config/system/google-analytics');
$this
->assertSession()
->responseContains('https://www.google-analytics.com/analytics.js');
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains($ua_code);
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains($ua_code);
$this
->config('google_analytics.settings')
->set('visibility.request_path_mode', 0)
->save();
$this
->config('google_analytics.settings')
->set('visibility.user_role_roles', [])
->save();
$base_path = base_path();
$this
->drupalGet('admin');
$this
->assertSession()
->statusCodeEquals(403);
$this
->assertSession()
->responseContains($base_path . '403.html');
$this
->drupalGet($this
->randomMachineName(64));
$this
->assertSession()
->statusCodeEquals(404);
$this
->assertSession()
->responseContains($base_path . '404.html');
}
public function testGoogleAnalyticsTrackingCode() {
$ua_code = 'UA-123456-2';
$this
->config('google_analytics.settings')
->set('account', $ua_code)
->save();
$this
->config('google_analytics.settings')
->set('visibility.request_path_mode', 0)
->save();
$this
->config('google_analytics.settings')
->set('visibility.user_role_roles', [])
->save();
$this
->config('google_analytics.settings')
->set('cache', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('https://www.google-analytics.com/analytics.js');
$this
->config('google_analytics.settings')
->set('privacy.anonymizeip', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains('ga("set", "anonymizeIp", true);');
$this
->config('google_analytics.settings')
->set('privacy.anonymizeip', 1)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("set", "anonymizeIp", true);');
$this
->config('google_analytics.settings')
->set('track.linkid', 1)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("require", "linkid", "linkid.js");');
$this
->config('google_analytics.settings')
->set('track.linkid', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains('ga("require", "linkid", "linkid.js");');
$this
->config('google_analytics.settings')
->set('track.urlfragments', 1)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("set", "page", location.pathname + location.search + location.hash);');
$this
->config('google_analytics.settings')
->set('track.urlfragments', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains('ga("set", "page", location.pathname + location.search + location.hash);');
$this
->config('google_analytics.settings')
->set('track.userid', 1)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains(', {"cookieDomain":"auto","userId":"');
$this
->config('google_analytics.settings')
->set('track.userid', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains(', {"cookieDomain":"auto","userId":"');
$this
->config('google_analytics.settings')
->set('track.displayfeatures', 1)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("require", "displayfeatures");');
$this
->config('google_analytics.settings')
->set('track.displayfeatures', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains('ga("require", "displayfeatures");');
$this
->drupalGet('');
$this
->assertSession()
->responseContains('{"cookieDomain":"auto"}');
$this
->config('google_analytics.settings')
->set('domain_mode', 1)
->save();
$this
->drupalGet('');
global $cookie_domain;
if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
$this
->assertSession()
->responseContains('{"cookieDomain":"' . $cookie_domain . '"}');
}
else {
$this
->assertSession()
->responseNotContains('{"cookieDomain":"' . $cookie_domain . '"}');
}
$this
->config('google_analytics.settings')
->set('domain_mode', 2)
->set('cross_domains', "www.example.com\nwww.example.net")
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("create", "' . $ua_code . '", {"cookieDomain":"auto","allowLinker":true');
$this
->assertSession()
->responseContains('ga("require", "linker");');
$this
->assertSession()
->responseContains('ga("linker:autoLink", ["www.example.com","www.example.net"]);');
$this
->assertSession()
->responseContains('"trackDomainMode":2,');
$this
->assertSession()
->responseContains('"trackCrossDomains":["www.example.com","www.example.net"]');
$this
->config('google_analytics.settings')
->set('domain_mode', 0)
->save();
$this
->config('google_analytics.settings')
->set('debug', 1)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('https://www.google-analytics.com/analytics_debug.js');
$this
->drupalGet('admin/reports/status');
$this
->assertSession()
->responseContains(t('Google Analytics module has debugging enabled. Please disable debugging setting in production sites from the <a href=":url">Google Analytics settings page</a>.', [
':url' => Url::fromRoute('google_analytics.admin_settings_form')
->toString(),
]));
$this
->config('google_analytics.settings')
->set('debug', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('https://www.google-analytics.com/analytics.js');
$codesnippet_create = [
'cookieDomain' => 'foo.example.com',
'cookieName' => 'myNewName',
'cookieExpires' => 20000,
'allowAnchor' => TRUE,
'sampleRate' => 4.3,
];
$this
->config('google_analytics.settings')
->set('codesnippet.create', $codesnippet_create)
->set('codesnippet.before', 'ga("set", "forceSSL", true);')
->set('codesnippet.after', 'ga("create", "UA-123456-3", {"name": "newTracker"});if(1 == 1 && 2 < 3 && 2 > 1){console.log("Google Analytics: Custom condition works.");}ga("newTracker.send", "pageview");')
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("create", "' . $ua_code . '", {"cookieDomain":"foo.example.com","cookieName":"myNewName","cookieExpires":20000,"allowAnchor":true,"sampleRate":4.3});');
$this
->assertSession()
->responseContains('ga("set", "forceSSL", true);');
$this
->assertSession()
->responseContains('ga("create", "UA-123456-3", {"name": "newTracker"});');
$this
->assertSession()
->responseContains('if(1 == 1 && 2 < 3 && 2 > 1){console.log("Google Analytics: Custom condition works.");}');
}
}