Commit 501f845f authored by hass's avatar hass

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

permission
parent 467df27a
......@@ -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(
......
......@@ -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,
),
);
}
......
......@@ -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() {
......
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