Commit aed44c1c authored by hass's avatar hass

Issue #159865 by hass, larowlan: add JS snippets for google analytics

permission
parent c8eb7f18
......@@ -8,3 +8,7 @@ use PHP for google analytics tracking visibility:
title: 'Use PHP for tracking visibility'
description: 'Enter PHP code in the field for tracking visibility settings.'
restrict access: true
add JS snippets for google analytics:
title: 'Add JavaScript snippets'
description: 'Enter JavaScript code snippets for advanced Google Analytics functionality.'
restrict access: true
......@@ -484,6 +484,8 @@ class GoogleAnalyticsAdminSettingsForm extends ConfigFormBase {
];
}
$user_access_add_js_snippets = !$this->currentUser()->hasPermission('add JS snippets for google analytics');
$user_access_add_js_snippets_permission_warning = $user_access_add_js_snippets ? ' <em>' . $this->t('This field has been disabled because you do not have sufficient permissions to edit it.') . '</em>' : '';
$form['advanced']['codesnippet'] = [
'#type' => 'details',
'#title' => $this->t('Custom JavaScript code'),
......@@ -502,15 +504,17 @@ class GoogleAnalyticsAdminSettingsForm extends ConfigFormBase {
'#type' => 'textarea',
'#title' => $this->t('Code snippet (before)'),
'#default_value' => $config->get('codesnippet.before'),
'#disabled' => $user_access_add_js_snippets,
'#rows' => 5,
'#description' => $this->t('Code in this textarea will be added <strong>before</strong> <code>ga("send", "pageview");</code>.'),
'#description' => $this->t('Code in this textarea will be added <strong>before</strong> <code>ga("send", "pageview");</code>.') . $user_access_add_js_snippets_permission_warning,
];
$form['advanced']['codesnippet']['google_analytics_codesnippet_after'] = [
'#type' => 'textarea',
'#title' => $this->t('Code snippet (after)'),
'#default_value' => $config->get('codesnippet.after'),
'#disabled' => $user_access_add_js_snippets,
'#rows' => 5,
'#description' => $this->t('Code in this textarea will be added <strong>after</strong> <code>ga("send", "pageview");</code>. This is useful if you\'d like to track a site in two accounts.'),
'#description' => $this->t('Code in this textarea will be added <strong>after</strong> <code>ga("send", "pageview");</code>. This is useful if you\'d like to track a site in two accounts.') . $user_access_add_js_snippets_permission_warning,
];
$form['advanced']['google_analytics_debug'] = [
......
......@@ -13,6 +13,13 @@ use Drupal\simpletest\WebTestBase;
*/
class GoogleAnalyticsBasicTest extends WebTestBase {
/**
* User without permissions to use snippets.
*
* @var \Drupal\user\UserInterface
*/
protected $noSnippetUser;
/**
* Modules to enable.
*
......@@ -38,6 +45,8 @@ class GoogleAnalyticsBasicTest extends WebTestBase {
];
// User to set up google_analytics.
$this->noSnippetUser = $this->drupalCreateUser($permissions);
$permissions[] = 'add JS snippets for google analytics';
$this->admin_user = $this->drupalCreateUser($permissions);
$this->drupalLogin($this->admin_user);
......@@ -68,6 +77,26 @@ class GoogleAnalyticsBasicTest extends WebTestBase {
$edit['google_analytics_account'] = $this->randomMachineName(2);
$this->drupalPostForm('admin/config/system/google-analytics', $edit, t('Save configuration'));
$this->assertRaw(t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'), '[testGoogleAnalyticsConfiguration]: Invalid Web Property ID number validated.');
// User should have access to code snippets.
$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.');
// Login as user without JS permissions.
$this->drupalLogin($this->noSnippetUser);
$this->drupalGet('admin/config/system/google-analytics');
// User should *not* have access to snippets, but create fields.
$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.');
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment