summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhass2016-08-09 21:26:37 (GMT)
committerhass2016-08-09 21:26:37 (GMT)
commit501f845fa17a4398e324bff7db3f0af765a8850b (patch)
tree95dc57513ffb7639915d2cf5f907f95d0c7a6c59
parent467df27a1cdb0eef1fedb7562ddf5c52f2139dcf (diff)
Issue #159865 by hass, larowlan: add JS snippets for google analytics7.x-2.3
permission
-rw-r--r--googleanalytics.admin.inc8
-rw-r--r--googleanalytics.module5
-rw-r--r--googleanalytics.test29
3 files changed, 40 insertions, 2 deletions
diff --git a/googleanalytics.admin.inc b/googleanalytics.admin.inc
index 9c9a821..d32939a 100644
--- a/googleanalytics.admin.inc
+++ b/googleanalytics.admin.inc
@@ -440,6 +440,8 @@ function googleanalytics_admin_settings_form($form_state) {
);
}
+ $user_access_add_js_snippets = !user_access('add JS snippets for google analytics');
+ $user_access_add_js_snippets_permission_warning = $user_access_add_js_snippets ? ' <em>' . t('This field has been disabled because you do not have sufficient permissions to edit it.') . '</em>' : '';
$form['advanced']['codesnippet'] = array(
'#type' => 'fieldset',
'#title' => t('Custom JavaScript code'),
@@ -459,15 +461,17 @@ function googleanalytics_admin_settings_form($form_state) {
'#type' => 'textarea',
'#title' => t('Code snippet (before)'),
'#default_value' => variable_get('googleanalytics_codesnippet_before', ''),
+ '#disabled' => $user_access_add_js_snippets,
'#rows' => 5,
- '#description' => t('Code in this textarea will be added <strong>before</strong> <code>ga("send", "pageview");</code>.'),
+ '#description' => 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']['googleanalytics_codesnippet_after'] = array(
'#type' => 'textarea',
'#title' => t('Code snippet (after)'),
'#default_value' => variable_get('googleanalytics_codesnippet_after', ''),
+ '#disabled' => $user_access_add_js_snippets,
'#rows' => 5,
- '#description' => 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' => 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']['googleanalytics_debug'] = array(
diff --git a/googleanalytics.module b/googleanalytics.module
index 8f65171..b45ee10 100644
--- a/googleanalytics.module
+++ b/googleanalytics.module
@@ -69,6 +69,11 @@ function googleanalytics_permission() {
'description' => t('Enter PHP code in the field for tracking visibility settings.'),
'restrict access' => TRUE,
),
+ 'add JS snippets for google analytics' => array(
+ 'title' => t('Add JavaScript snippets'),
+ 'description' => 'Enter JavaScript code snippets for advanced Google Analytics functionality.',
+ 'restrict access' => TRUE,
+ ),
);
}
diff --git a/googleanalytics.test b/googleanalytics.test
index 4f39096..745047a 100644
--- a/googleanalytics.test
+++ b/googleanalytics.test
@@ -6,6 +6,13 @@
*/
class GoogleAnalyticsBasicTest extends DrupalWebTestCase {
+ /**
+ * User without permissions to edit snippets.
+ *
+ * @var \StdClass
+ */
+ protected $noSnippetUser;
+
public static function getInfo() {
return array(
'name' => 'Google Analytics basic tests',
@@ -25,6 +32,8 @@ class GoogleAnalyticsBasicTest extends DrupalWebTestCase {
);
// 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);
}
@@ -48,6 +57,26 @@ class GoogleAnalyticsBasicTest extends DrupalWebTestCase {
$edit['googleanalytics_account'] = $this->randomName(2);
$this->drupalPost('admin/config/system/googleanalytics', $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('googleanalytics_codesnippet_create');
+ $this->assertFieldByName('googleanalytics_codesnippet_before');
+ $this->assertFieldByName('googleanalytics_codesnippet_after');
+ $this->assertNoFieldByXPath("//textarea[@name='googleanalytics_codesnippet_create' and @disabled='disabled']", NULL, '"Create only fields" is enabled.');
+ $this->assertNoFieldByXPath("//textarea[@name='googleanalytics_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is enabled.');
+ $this->assertNoFieldByXPath("//textarea[@name='googleanalytics_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/googleanalytics');
+
+ // User should *not* have access to snippets, but create fields.
+ $this->assertFieldByName('googleanalytics_codesnippet_create');
+ $this->assertFieldByName('googleanalytics_codesnippet_before');
+ $this->assertFieldByName('googleanalytics_codesnippet_after');
+ $this->assertNoFieldByXPath("//textarea[@name='googleanalytics_codesnippet_create' and @disabled='disabled']", NULL, '"Create only fields" is enabled.');
+ $this->assertFieldByXPath("//textarea[@name='googleanalytics_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is disabled.');
+ $this->assertFieldByXPath("//textarea[@name='googleanalytics_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is disabled.');
}
function testGoogleAnalyticsPageVisibility() {