From 43319c3aad613fc15093cb956196e3496c1418ae Fri, 5 Apr 2013 01:00:20 +0200 From: hass Date: Fri, 5 Apr 2013 00:59:48 +0200 Subject: [PATCH] D8 upgrade diff --git a/config/google_analytics.settings.yml b/config/google_analytics.settings.yml new file mode 100644 index 0000000..3be53e5 --- /dev/null +++ b/config/google_analytics.settings.yml @@ -0,0 +1,27 @@ +account: 'UA-' +domain_mode: '0' +cross_domains: '' +visibility: + pages_enabled: '0' + pages: 'admin\nadmin/*\nbatch\nnode/add*\nnode/*/*\nuser/*/*' + roles_enabled: '0' + roles: + custom: '0' +track: + outbound: '1' + mailto: '1' + files: '1' + files_extensions: '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls|xml|z|zip' + messages: + site_search: '0' + adsense: '0' + doubleclick: '0' +privacy: + anonymizeip: '1' + donottrack: '1' +cache: '1' +translation_set: '0' +codesnippet: + before: '' + after: '' +js_scope: 'header' diff --git a/google_analytics.module b/google_analytics.module index 1042c31..a9e832e 100644 --- a/google_analytics.module +++ b/google_analytics.module @@ -24,7 +24,7 @@ /** * Implements hook_help(). */ -function googleanalytics_help($path, $arg) { +function google_analytics_help($path, $arg) { switch ($path) { case 'admin/config/system/googleanalytics': return t('Google Analytics is a free (registration required) website traffic and marketing effectiveness service.', array('@ga_url' => 'http://www.google.com/analytics/')); @@ -34,7 +34,7 @@ /** * Implements hook_theme(). */ -function googleanalytics_theme() { +function google_analytics_theme() { return array( 'googleanalytics_admin_custom_var_table' => array( 'render element' => 'form', @@ -45,7 +45,7 @@ /** * Implements hook_permission(). */ -function googleanalytics_permission() { +function google_analytics_permission() { return array( 'administer google analytics' => array( 'title' => t('Administer Google Analytics'), @@ -66,15 +66,13 @@ /** * Implements hook_menu(). */ -function googleanalytics_menu() { - $items['admin/config/system/googleanalytics'] = array( +function google_analytics_menu() { + $items['admin/config/system/google_analytics'] = array( 'title' => 'Google Analytics', 'description' => 'Configure tracking behavior to get insights into your website traffic and marketing effectiveness.', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('googleanalytics_admin_settings_form'), + 'route_name' => 'google_analytics_settings', 'access arguments' => array('administer google analytics'), 'type' => MENU_NORMAL_ITEM, - 'file' => 'googleanalytics.admin.inc', ); return $items; @@ -83,7 +81,7 @@ /** * Implements hook_page_alter() to insert JavaScript to the appropriate scope/region of the page. */ -function googleanalytics_page_alter(&$page) { +function google_analytics_page_alter(&$page) { global $user; $id = variable_get('googleanalytics_account', ''); @@ -312,7 +310,7 @@ /** * Implements hook_field_extra_fields(). */ -function googleanalytics_field_extra_fields() { +function google_analytics_field_extra_fields() { $extra['user']['user']['form']['googleanalytics'] = array( 'label' => t('Google Analytics configuration'), 'description' => t('Google Analytics module form element.'), @@ -327,7 +325,7 @@ * * Allow users to decide if tracking code will be added to pages or not. */ -function googleanalytics_form_user_profile_form_alter(&$form, &$form_state) { +function google_analytics_form_user_profile_form_alter(&$form, &$form_state) { $account = $form['#user']; $category = $form['#user_category']; @@ -378,7 +376,7 @@ /** * Implements hook_user_presave(). */ -function googleanalytics_user_presave(&$edit, $account, $category) { +function google_analytics_user_presave(&$edit, $account, $category) { if (isset($edit['googleanalytics']['custom'])) { $edit['data']['googleanalytics']['custom'] = $edit['googleanalytics']['custom']; } @@ -387,7 +385,7 @@ /** * Implements hook_cron(). */ -function googleanalytics_cron() { +function google_analytics_cron() { // Regenerate the tracking code file every day. if (REQUEST_TIME - variable_get('googleanalytics_last_cache', 0) >= 86400 && variable_get('googleanalytics_cache', 0)) { _googleanalytics_cache('http://www.google-analytics.com/analytics.js', TRUE); @@ -400,7 +398,7 @@ * * Collects and adds the number of search results to the head. */ -function googleanalytics_preprocess_search_results(&$variables) { +function google_analytics_preprocess_search_results(&$variables) { // There is no search result $variable available that hold the number of items // found. But the pager item mumber can tell the number of search results. global $pager_total_items; @@ -413,7 +411,7 @@ * * http://api.drupal.org/api/function/search_get_keys/6 */ -function googleanalytics_search_get_keys() { +function google_analytics_search_get_keys() { static $return; if (!isset($return)) { // Extract keys as remainder of path @@ -435,7 +433,7 @@ * @return mixed * The path to the local javascript file on success, boolean FALSE on failure. */ -function _googleanalytics_cache($location, $sync_cached_file = FALSE) { +function _google_analytics_cache($location, $sync_cached_file = FALSE) { $path = 'public://googleanalytics'; $file_destination = $path . '/' . basename($location); @@ -481,7 +479,7 @@ /** * Delete cached files and directory. */ -function googleanalytics_clear_js_cache() { +function google_analytics_clear_js_cache() { $path = 'public://googleanalytics'; if (file_prepare_directory($path)) { file_scan_directory($path, '/.*/', array('callback' => 'file_unmanaged_delete')); @@ -502,7 +500,7 @@ * @return boolean * A decision on if the current user is being tracked by Google Analytics. */ -function _googleanalytics_visibility_user($account) { +function _google_analytics_visibility_user($account) { $enabled = FALSE; @@ -531,7 +529,7 @@ * Based on visibility setting this function returns TRUE if GA code should * be added for the current role and otherwise FALSE. */ -function _googleanalytics_visibility_roles($account) { +function _google_analytics_visibility_roles($account) { $visibility = variable_get('googleanalytics_visibility_roles', 0); $enabled = $visibility; @@ -560,7 +558,7 @@ * Based on visibility setting this function returns TRUE if GA code should * be added to the current page and otherwise FALSE. */ -function _googleanalytics_visibility_pages() { +function _google_analytics_visibility_pages() { static $page_match; // Cache visibility result if function is called more than once. @@ -606,7 +604,7 @@ * Based on headers send by clients this function returns TRUE if GA code should * be added to the current page and otherwise FALSE. */ -function _googleanalytics_visibility_header($account) { +function _google_analytics_visibility_header($account) { if (($account->uid || variable_get('cache', 0) == 0) && variable_get('googleanalytics_privacy_donottrack', 1) && !empty($_SERVER['HTTP_DNT'])) { // Disable tracking if caching is disabled or a visitors is logged in and diff --git a/google_analytics.routing.yml b/google_analytics.routing.yml new file mode 100644 index 0000000..5cc660e --- /dev/null +++ b/google_analytics.routing.yml @@ -0,0 +1,6 @@ +google_analytics_settings: + pattern: '/admin/config/system/google_analytics' + defaults: + _form: 'Drupal\google_analytics\Google_AnalyticsSettingsForm' + requirements: + _permission: 'administer google analytics' diff --git a/lib/Drupal/google_analytics/Google_AnalyticsSettingsForm.php b/lib/Drupal/google_analytics/Google_AnalyticsSettingsForm.php new file mode 100644 index 0000000..b540c18 --- /dev/null +++ b/lib/Drupal/google_analytics/Google_AnalyticsSettingsForm.php @@ -0,0 +1,76 @@ +configFactory->get('google_analytics.settings'); + + $form['general'] = array( + '#type' => 'fieldset', + '#title' => t('General settings'), + ); + + $form['general']['googleanalytics_account'] = array( + '#title' => t('Web Property ID'), + '#type' => 'textfield', + '#default_value' => $config->get('account', 'UA-'), + '#size' => 15, + '#maxlength' => 20, + '#required' => TRUE, + '#description' => t('This ID is unique to each site you want to track separately, and is in the form of UA-xxxxxxx-yy. To get a Web Property ID, register your site with Google Analytics, or if you already have registered your site, go to your Google Analytics Settings page to see the ID next to every site profile. Find more information in the documentation.', array('@analytics' => 'http://www.google.com/analytics/', '@webpropertyid' => url('https://developers.google.com/analytics/resources/concepts/gaConceptsAccounts', array('fragment' => 'webProperty')))), + ); + + + return parent::buildForm($form, $form_state); + } + + /** + * Implements \Drupal\Core\Form\FormInterface::validateForm(). + */ + public function validateForm(array &$form, array &$form_state) { + parent::validateForm($form, $form_state); + + $form_state['values']['googleanalytics_account'] = trim($form_state['values']['googleanalytics_account']); + + // Replace all type of dashes (n-dash, m-dash, minus) with the normal dashes. + $form_state['values']['googleanalytics_account'] = str_replace(array('–', '—', '?'), '-', $form_state['values']['googleanalytics_account']); + + if (!preg_match('/^UA-\d{4,}-\d+$/', $form_state['values']['googleanalytics_account'])) { + form_set_error('googleanalytics_account', t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.')); + } + } + + /** + * Implements \Drupal\Core\Form\FormInterface::submitForm(). + */ + public function submitForm(array &$form, array &$form_state) { + $values = $form_state['values']; + + $config = $this->configFactory->get('google_analytics.settings'); + $config->set('account', $values['account'])->save(); + + parent::submitForm($form, $form_state); + } + +} diff --git a/lib/Drupal/google_analytics/Tests/GoogleAnalyticsBasicTest.php b/lib/Drupal/google_analytics/Tests/GoogleAnalyticsBasicTest.php new file mode 100644 index 0000000..df6e6d7 --- /dev/null +++ b/lib/Drupal/google_analytics/Tests/GoogleAnalyticsBasicTest.php @@ -0,0 +1,438 @@ + t('Google Analytics basic tests'), + 'description' => t('Test basic functionality of Google Analytics module.'), + 'group' => 'Google Analytics', + ); + } + + function setUp() { + parent::setUp('googleanalytics'); + + $permissions = array( + 'access administration pages', + 'administer google analytics', + ); + + // User to set up google_analytics. + $this->admin_user = $this->drupalCreateUser($permissions); + $this->drupalLogin($this->admin_user); + } + + function testGoogleAnalyticsConfiguration() { + // Check for setting page's presence. + $this->drupalGet('admin/config/system/googleanalytics'); + $this->assertRaw(t('Web Property ID'), '[testGoogleAnalyticsConfiguration]: Settings page displayed.'); + + // Check for account code validation. + $edit['googleanalytics_account'] = $this->randomName(2); + $this->drupalPost('admin/config/system/googleanalytics', $edit, '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.'); + } + + function testGoogleAnalyticsPageVisibility() { + $ua_code = 'UA-123456-1'; + variable_set('googleanalytics_account', $ua_code); + + // Show tracking on "every page except the listed pages". + variable_set('googleanalytics_visibility_pages', 0); + // Disable tracking one "admin*" pages only. + variable_set('googleanalytics_pages', "admin\nadmin/*"); + // Enable tracking only for authenticated users only. + variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)); + + // Check tracking code visibility. + $this->drupalGet(''); + $this->assertRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed for authenticated users.'); + + // Test whether tracking code is not included on pages to omit. + $this->drupalGet('admin'); + $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin page.'); + $this->drupalGet('admin/config/system/googleanalytics'); + // Checking for tracking code URI here, as $ua_code is displayed in the form. + $this->assertNoRaw('google-analytics.com/analytics.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin subpage.'); + + // Test whether tracking code display is properly flipped. + variable_set('googleanalytics_visibility_pages', 1); + $this->drupalGet('admin'); + $this->assertRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin page.'); + $this->drupalGet('admin/config/system/googleanalytics'); + // Checking for tracking code URI here, as $ua_code is displayed in the form. + $this->assertRaw('google-analytics.com/analytics.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin subpage.'); + $this->drupalGet(''); + $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is NOT displayed on front page.'); + + // Test whether tracking code is not display for anonymous. + $this->drupalLogout(); + $this->drupalGet(''); + $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is NOT displayed for anonymous.'); + + // Switch back to every page except the listed pages. + variable_set('googleanalytics_visibility_pages', 0); + // Enable tracking code for all user roles. + variable_set('googleanalytics_roles', array()); + + // Test whether 403 forbidden tracking code is shown if user has no access. + $this->drupalGet('admin'); + $this->assertRaw('/403.html', '[testGoogleAnalyticsPageVisibility]: 403 Forbidden tracking code shown if user has no access.'); + + // Test whether 404 not found tracking code is shown on non-existent pages. + $this->drupalGet($this->randomName(64)); + $this->assertRaw('/404.html', '[testGoogleAnalyticsPageVisibility]: 404 Not Found tracking code shown on non-existent page.'); + + // DNT Tests: + // Enable caching of pages for anonymous users. + variable_set('cache', 1); + // Test whether DNT headers will fail to disable embedding of tracking code. + $this->drupalGet('', array(), array('DNT: 1')); + $this->assertRaw('ga("send", "pageview");', '[testGoogleAnalyticsDNTVisibility]: DNT header send from client, but page caching is enabled and tracker cannot removed.'); + // DNT works only with caching of pages for anonymous users disabled. + variable_set('cache', 0); + $this->drupalGet(''); + $this->assertRaw('ga("send", "pageview");', '[testGoogleAnalyticsDNTVisibility]: Tracking is enabled without DNT header.'); + // Test whether DNT header is able to remove the tracking code. + $this->drupalGet('', array(), array('DNT: 1')); + $this->assertNoRaw('ga("send", "pageview");', '[testGoogleAnalyticsDNTVisibility]: DNT header received from client. Tracking has been disabled by browser.'); + // Disable DNT feature and see if tracker is still embedded. + variable_set('googleanalytics_privacy_donottrack', 0); + $this->drupalGet('', array(), array('DNT: 1')); + $this->assertRaw('ga("send", "pageview");', '[testGoogleAnalyticsDNTVisibility]: DNT feature is disabled, DNT header from browser has been ignored.'); + } + + function testGoogleAnalyticsTrackingCode() { + $ua_code = 'UA-123456-2'; + variable_set('googleanalytics_account', $ua_code); + + // Show tracking code on every page except the listed pages. + variable_set('googleanalytics_visibility_pages', 0); + // Enable tracking code for all user roles. + variable_set('googleanalytics_roles', array()); + + /* Sample JS code as added to page: + + + + */ + + // Test whether tracking code uses latest JS. + variable_set('googleanalytics_cache', 0); + $this->drupalGet(''); + $this->assertRaw('google-analytics.com/analytics.js', '[testGoogleAnalyticsTrackingCode]: Latest tracking code used.'); + + // Test whether anonymize visitors IP address feature has been enabled. + $this->drupalGet(''); + $this->assertNoRaw('ga("set", "anonymizeIp", 1);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address not found on frontpage.'); + // Enable anonymizing of IP addresses. + variable_set('googleanalytics_tracker_anonymizeip', 1); + $this->drupalGet(''); + $this->assertRaw('ga("set", "anonymizeIp", 1);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address found on frontpage.'); + + // Test whether single domain tracking is active. + $this->drupalGet(''); + $this->assertNoRaw('ga("set", "cookieDomain",', '[testGoogleAnalyticsTrackingCode]: Single domain tracking is active.'); + + // Enable "One domain with multiple subdomains". + variable_set('googleanalytics_domain_mode', 1); + $this->drupalGet(''); + + // Test may run on localhost, an ipaddress or real domain name. + // TODO: Workaround to run tests successfully. This feature cannot tested reliable. + global $cookie_domain; + if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) { + $this->assertRaw('ga("set", "cookieDomain",', '[testGoogleAnalyticsTrackingCode]: One domain with multiple subdomains is active on real host.'); + } + else { + // Special cases, Localhost and IP addresses don't show '_setDomainName'. + $this->assertNoRaw('ga("set", "cookieDomain",', '[testGoogleAnalyticsTrackingCode]: One domain with multiple subdomains may be active on localhost (test result is not reliable).'); + } + + // Enable "Multiple top-level domains" tracking. + variable_set('googleanalytics_domain_mode', 2); + variable_set('googleanalytics_cross_domains', "www.example.com\nwww.example.net"); + $this->drupalGet(''); + $this->assertRaw('ga("set", "cookieDomain", "none");', '[testGoogleAnalyticsTrackingCode]: _setDomainName: "none" found. Cross domain tracking is active.'); + $this->assertRaw('ga("set", "allowLinker", true);', '[testGoogleAnalyticsTrackingCode]: _setAllowLinker: true found. Cross domain tracking is active.'); + $this->assertRaw('"trackCrossDomains":["www.example.com","www.example.net"]', '[testGoogleAnalyticsTrackingCode]: Cross domain tracking with www.example.com and www.example.net is active.'); + + // Test whether the BEFORE and AFTER code is added to the tracker. + // @todo: review detectFlash once API docs are available. + variable_set('googleanalytics_codesnippet_before', 'ga("set", "detectFlash", false);'); + variable_set('googleanalytics_codesnippet_after', 'ga("create", "UA-123456-3", {name: "newTracker"});ga("newTracker.send", "pageview");'); + $this->drupalGet(''); + $this->assertRaw('ga("set", "detectFlash", false);', '[testGoogleAnalyticsTrackingCode]: Before codesnippet has been found with "Flash" detection disabled.'); + $this->assertRaw('ga("create", "UA-123456-3", {name: "newTracker"});', '[testGoogleAnalyticsTrackingCode]: After codesnippet with "newTracker" tracker has been found.'); + } +} + +/* @todo: upgrade to custom dimensions +class GoogleAnalyticsCustomVariablesTest extends DrupalWebTestCase { + + public static function getInfo() { + return array( + 'name' => t('Google Analytics Custom Variables tests'), + 'description' => t('Test custom variables functionality of Google Analytics module.'), + 'group' => 'Google Analytics', + 'dependencies' => array('token'), + ); + } + + function setUp() { + parent::setUp('googleanalytics', 'token'); + + $permissions = array( + 'access administration pages', + 'administer google analytics', + ); + + // User to set up google_analytics. + $this->admin_user = $this->drupalCreateUser($permissions); + } + + function testGoogleAnalyticsCustomVariables() { + $ua_code = 'UA-123456-3'; + variable_set('googleanalytics_account', $ua_code); + + // Basic test if the feature works. + $custom_vars = array( + 'slots' => array( + 1 => array( + 'slot' => 1, + 'name' => 'Foo 1', + 'value' => 'Bar 1', + 'scope' => 3, + ), + 2 => array( + 'slot' => 2, + 'name' => 'Foo 2', + 'value' => 'Bar 2', + 'scope' => 2, + ), + 3 => array( + 'slot' => 3, + 'name' => 'Foo 3', + 'value' => 'Bar 3', + 'scope' => 3, + ), + 4 => array( + 'slot' => 4, + 'name' => 'Foo 4', + 'value' => 'Bar 4', + 'scope' => 2, + ), + 5 => array( + 'slot' => 5, + 'name' => 'Foo 5', + 'value' => 'Bar 5', + 'scope' => 1, + ), + ) + ); + variable_set('googleanalytics_custom_var', $custom_vars); + $this->drupalGet(''); + + foreach ($custom_vars['slots'] as $slot) { + $this->assertRaw("ga('set', 'customvar', " . $slot['slot'] . ", \"" . $slot['name'] . "\", \"" . $slot['value'] . "\", " . $slot['scope'] . ");", '[testGoogleAnalyticsCustomVariables]: _setCustomVar ' . $slot['slot'] . ' is shown.'); + } + + // Test whether tokens are replaced in custom variable names. + $site_slogan = $this->randomName(16); + variable_set('site_slogan', $site_slogan); + + $custom_vars = array( + 'slots' => array( + 1 => array( + 'slot' => 1, + 'name' => 'Name: [site:slogan]', + 'value' => 'Value: [site:slogan]', + 'scope' => 3, + ), + 2 => array( + 'slot' => 2, + 'name' => '', + 'value' => $this->randomName(16), + 'scope' => 1, + ), + 3 => array( + 'slot' => 3, + 'name' => $this->randomName(16), + 'value' => '', + 'scope' => 2, + ), + 4 => array( + 'slot' => 4, + 'name' => '', + 'value' => '', + 'scope' => 3, + ), + 5 => array( + 'slot' => 5, + 'name' => '', + 'value' => '', + 'scope' => 3, + ), + ) + ); + variable_set('googleanalytics_custom_var', $custom_vars); + $this->verbose('
' . print_r($custom_vars, TRUE) . '
'); + + $this->drupalGet(''); + $this->assertRaw("ga('set', 'customvar', 1, \"Name: $site_slogan\", \"Value: $site_slogan\", 3);", '[testGoogleAnalyticsCustomVariables]: Tokens have been replaced in custom variable.'); + $this->assertNoRaw("ga('set', 'customvar', 2,", '[testGoogleAnalyticsCustomVariables]: Value with empty name is not shown.'); + $this->assertNoRaw("ga('set', 'customvar', 3,", '[testGoogleAnalyticsCustomVariables]: Name with empty value is not shown.'); + $this->assertNoRaw("ga('set', 'customvar', 4,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.'); + $this->assertNoRaw("ga('set', 'customvar', 5,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.'); + } +} */ + +class GoogleAnalyticsStatusMessagesTest extends DrupalWebTestCase { + + public static function getInfo() { + return array( + 'name' => t('Google Analytics status messages tests'), + 'description' => t('Test status messages functionality of Google Analytics module.'), + 'group' => 'Google Analytics', + ); + } + + function setUp() { + parent::setUp('googleanalytics'); + + $permissions = array( + 'access administration pages', + 'administer google analytics', + ); + + // User to set up google_analytics. + $this->admin_user = $this->drupalCreateUser($permissions); + } + + function testGoogleAnalyticsStatusMessages() { + $ua_code = 'UA-123456-4'; + variable_set('googleanalytics_account', $ua_code); + + // Enable logging of errors only. + variable_set('googleanalytics_trackmessages', array('error' => 'error')); + + $this->drupalPost('user/login', array(), 'Log in'); + $this->assertRaw('ga("send", "event", "Messages", "Error message", "Username field is required.");', '[testGoogleAnalyticsStatusMessages]: Event message "Username field is required." is shown.'); + $this->assertRaw('ga("send", "event", "Messages", "Error message", "Password field is required.");', '[testGoogleAnalyticsStatusMessages]: Event message "Password field is required." is shown.'); + + // @todo: investigate why drupal_set_message() fails. + //drupal_set_message('Example status message.', 'status'); + //drupal_set_message('Example warning message.', 'warning'); + //drupal_set_message('Example error message.', 'error'); + //drupal_set_message('Example error message with html tags and link.', 'error'); + //$this->drupalGet(''); + //$this->assertNoRaw('ga("send", "event", "Messages", "Status message", "Example status message.");', '[testGoogleAnalyticsStatusMessages]: Example status message is not enabled for tracking.'); + //$this->assertNoRaw('ga("send", "event", "Messages", "Warning message", "Example warning message.");', '[testGoogleAnalyticsStatusMessages]: Example warning message is not enabled for tracking.'); + //$this->assertRaw('ga("send", "event", "Messages", "Error message", "Example error message.");', '[testGoogleAnalyticsStatusMessages]: Example error message is shown.'); + //$this->assertRaw('ga("send", "event", "Messages", "Error message", "Example error message with html tags and link.");', '[testGoogleAnalyticsStatusMessages]: HTML has been stripped successful from Example error message with html tags and link.'); + } +} + +class GoogleAnalyticsRolesTest extends DrupalWebTestCase { + + public static function getInfo() { + return array( + 'name' => t('Google Analytics role tests'), + 'description' => t('Test roles functionality of Google Analytics module.'), + 'group' => 'Google Analytics', + ); + } + + function setUp() { + parent::setUp('googleanalytics'); + + $permissions = array( + 'access administration pages', + 'administer google analytics', + ); + + // User to set up google_analytics. + $this->admin_user = $this->drupalCreateUser($permissions); + } + + function testGoogleAnalyticsRolesTracking() { + $ua_code = 'UA-123456-4'; + variable_set('googleanalytics_account', $ua_code); + + // Test if the default settings are working as expected. + + // Add to the selected roles only. + variable_set('googleanalytics_visibility_roles', 0); + // Enable tracking for all users. + variable_set('googleanalytics_roles', array()); + + // Check tracking code visibility. + $this->drupalGet(''); + $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for anonymous users on frontpage with default settings.'); + $this->drupalGet('admin'); + $this->assertRaw('/403.html', '[testGoogleAnalyticsRoleVisibility]: 403 Forbidden tracking code is displayed for anonymous users in admin section with default settings.'); + + $this->drupalLogin($this->admin_user); + + $this->drupalGet(''); + $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users on frontpage with default settings.'); + $this->drupalGet('admin'); + $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for authenticated users in admin section with default settings.'); + + // Test if the non-default settings are working as expected. + + // Enable tracking only for authenticated users. + variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)); + + $this->drupalGet(''); + $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users only on frontpage.'); + + $this->drupalLogout(); + $this->drupalGet(''); + $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for anonymous users on frontpage.'); + + // Add to every role except the selected ones. + variable_set('googleanalytics_visibility_roles', 1); + // Enable tracking for all users. + variable_set('googleanalytics_roles', array()); + + // Check tracking code visibility. + $this->drupalGet(''); + $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed for anonymous users.'); + $this->drupalGet('admin'); + $this->assertRaw('/403.html', '[testGoogleAnalyticsRoleVisibility]: 403 Forbidden tracking code is shown for anonymous users if every role except the selected ones is selected.'); + + $this->drupalLogin($this->admin_user); + + $this->drupalGet(''); + $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed on frontpage for authenticated users.'); + $this->drupalGet('admin'); + $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and NOT displayed in admin section for authenticated users.'); + + // Disable tracking for authenticated users. + variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)); + + $this->drupalGet(''); + $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed on frontpage for excluded authenticated users.'); + $this->drupalGet('admin'); + $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed in admin section for excluded authenticated users.'); + + $this->drupalLogout(); + $this->drupalGet(''); + $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.'); + } + +} diff --git a/lib/Drupal/google_analytics/Tests/GoogleAnalyticsCustomVariablesTest.php b/lib/Drupal/google_analytics/Tests/GoogleAnalyticsCustomVariablesTest.php new file mode 100644 index 0000000..1621863 --- /dev/null +++ b/lib/Drupal/google_analytics/Tests/GoogleAnalyticsCustomVariablesTest.php @@ -0,0 +1,125 @@ + t('Google Analytics Custom Variables tests'), + 'description' => t('Test custom variables functionality of Google Analytics module.'), + 'group' => 'Google Analytics', + 'dependencies' => array('token'), + ); + } + + function setUp() { + parent::setUp('googleanalytics', 'token'); + + $permissions = array( + 'access administration pages', + 'administer google analytics', + ); + + // User to set up google_analytics. + $this->admin_user = $this->drupalCreateUser($permissions); + } + + function testGoogleAnalyticsCustomVariables() { + $ua_code = 'UA-123456-3'; + variable_set('googleanalytics_account', $ua_code); + + // Basic test if the feature works. + $custom_vars = array( + 'slots' => array( + 1 => array( + 'slot' => 1, + 'name' => 'Foo 1', + 'value' => 'Bar 1', + 'scope' => 3, + ), + 2 => array( + 'slot' => 2, + 'name' => 'Foo 2', + 'value' => 'Bar 2', + 'scope' => 2, + ), + 3 => array( + 'slot' => 3, + 'name' => 'Foo 3', + 'value' => 'Bar 3', + 'scope' => 3, + ), + 4 => array( + 'slot' => 4, + 'name' => 'Foo 4', + 'value' => 'Bar 4', + 'scope' => 2, + ), + 5 => array( + 'slot' => 5, + 'name' => 'Foo 5', + 'value' => 'Bar 5', + 'scope' => 1, + ), + ) + ); + variable_set('googleanalytics_custom_var', $custom_vars); + $this->drupalGet(''); + + foreach ($custom_vars['slots'] as $slot) { + $this->assertRaw("ga('set', 'customvar', " . $slot['slot'] . ", \"" . $slot['name'] . "\", \"" . $slot['value'] . "\", " . $slot['scope'] . ");", '[testGoogleAnalyticsCustomVariables]: _setCustomVar ' . $slot['slot'] . ' is shown.'); + } + + // Test whether tokens are replaced in custom variable names. + $site_slogan = $this->randomName(16); + variable_set('site_slogan', $site_slogan); + + $custom_vars = array( + 'slots' => array( + 1 => array( + 'slot' => 1, + 'name' => 'Name: [site:slogan]', + 'value' => 'Value: [site:slogan]', + 'scope' => 3, + ), + 2 => array( + 'slot' => 2, + 'name' => '', + 'value' => $this->randomName(16), + 'scope' => 1, + ), + 3 => array( + 'slot' => 3, + 'name' => $this->randomName(16), + 'value' => '', + 'scope' => 2, + ), + 4 => array( + 'slot' => 4, + 'name' => '', + 'value' => '', + 'scope' => 3, + ), + 5 => array( + 'slot' => 5, + 'name' => '', + 'value' => '', + 'scope' => 3, + ), + ) + ); + variable_set('googleanalytics_custom_var', $custom_vars); + $this->verbose('
' . print_r($custom_vars, TRUE) . '
'); + + $this->drupalGet(''); + $this->assertRaw("ga('set', 'customvar', 1, \"Name: $site_slogan\", \"Value: $site_slogan\", 3);", '[testGoogleAnalyticsCustomVariables]: Tokens have been replaced in custom variable.'); + $this->assertNoRaw("ga('set', 'customvar', 2,", '[testGoogleAnalyticsCustomVariables]: Value with empty name is not shown.'); + $this->assertNoRaw("ga('set', 'customvar', 3,", '[testGoogleAnalyticsCustomVariables]: Name with empty value is not shown.'); + $this->assertNoRaw("ga('set', 'customvar', 4,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.'); + $this->assertNoRaw("ga('set', 'customvar', 5,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.'); + } +} */ diff --git a/lib/Drupal/google_analytics/Tests/GoogleAnalyticsRolesTest.php b/lib/Drupal/google_analytics/Tests/GoogleAnalyticsRolesTest.php new file mode 100644 index 0000000..2a01d4b --- /dev/null +++ b/lib/Drupal/google_analytics/Tests/GoogleAnalyticsRolesTest.php @@ -0,0 +1,96 @@ + t('Google Analytics role tests'), + 'description' => t('Test roles functionality of Google Analytics module.'), + 'group' => 'Google Analytics', + ); + } + + function setUp() { + parent::setUp('googleanalytics'); + + $permissions = array( + 'access administration pages', + 'administer google analytics', + ); + + // User to set up google_analytics. + $this->admin_user = $this->drupalCreateUser($permissions); + } + + function testGoogleAnalyticsRolesTracking() { + $ua_code = 'UA-123456-4'; + variable_set('googleanalytics_account', $ua_code); + + // Test if the default settings are working as expected. + + // Add to the selected roles only. + variable_set('googleanalytics_visibility_roles', 0); + // Enable tracking for all users. + variable_set('googleanalytics_roles', array()); + + // Check tracking code visibility. + $this->drupalGet(''); + $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for anonymous users on frontpage with default settings.'); + $this->drupalGet('admin'); + $this->assertRaw('/403.html', '[testGoogleAnalyticsRoleVisibility]: 403 Forbidden tracking code is displayed for anonymous users in admin section with default settings.'); + + $this->drupalLogin($this->admin_user); + + $this->drupalGet(''); + $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users on frontpage with default settings.'); + $this->drupalGet('admin'); + $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for authenticated users in admin section with default settings.'); + + // Test if the non-default settings are working as expected. + + // Enable tracking only for authenticated users. + variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)); + + $this->drupalGet(''); + $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users only on frontpage.'); + + $this->drupalLogout(); + $this->drupalGet(''); + $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for anonymous users on frontpage.'); + + // Add to every role except the selected ones. + variable_set('googleanalytics_visibility_roles', 1); + // Enable tracking for all users. + variable_set('googleanalytics_roles', array()); + + // Check tracking code visibility. + $this->drupalGet(''); + $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed for anonymous users.'); + $this->drupalGet('admin'); + $this->assertRaw('/403.html', '[testGoogleAnalyticsRoleVisibility]: 403 Forbidden tracking code is shown for anonymous users if every role except the selected ones is selected.'); + + $this->drupalLogin($this->admin_user); + + $this->drupalGet(''); + $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed on frontpage for authenticated users.'); + $this->drupalGet('admin'); + $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and NOT displayed in admin section for authenticated users.'); + + // Disable tracking for authenticated users. + variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)); + + $this->drupalGet(''); + $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed on frontpage for excluded authenticated users.'); + $this->drupalGet('admin'); + $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed in admin section for excluded authenticated users.'); + + $this->drupalLogout(); + $this->drupalGet(''); + $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.'); + } + +} diff --git a/lib/Drupal/google_analytics/Tests/GoogleAnalyticsStatusMessagesTest.php b/lib/Drupal/google_analytics/Tests/GoogleAnalyticsStatusMessagesTest.php new file mode 100644 index 0000000..fc14a98 --- /dev/null +++ b/lib/Drupal/google_analytics/Tests/GoogleAnalyticsStatusMessagesTest.php @@ -0,0 +1,51 @@ + t('Google Analytics status messages tests'), + 'description' => t('Test status messages functionality of Google Analytics module.'), + 'group' => 'Google Analytics', + ); + } + + function setUp() { + parent::setUp('googleanalytics'); + + $permissions = array( + 'access administration pages', + 'administer google analytics', + ); + + // User to set up google_analytics. + $this->admin_user = $this->drupalCreateUser($permissions); + } + + function testGoogleAnalyticsStatusMessages() { + $ua_code = 'UA-123456-4'; + variable_set('googleanalytics_account', $ua_code); + + // Enable logging of errors only. + variable_set('googleanalytics_trackmessages', array('error' => 'error')); + + $this->drupalPost('user/login', array(), 'Log in'); + $this->assertRaw('ga("send", "event", "Messages", "Error message", "Username field is required.");', '[testGoogleAnalyticsStatusMessages]: Event message "Username field is required." is shown.'); + $this->assertRaw('ga("send", "event", "Messages", "Error message", "Password field is required.");', '[testGoogleAnalyticsStatusMessages]: Event message "Password field is required." is shown.'); + + // @todo: investigate why drupal_set_message() fails. + //drupal_set_message('Example status message.', 'status'); + //drupal_set_message('Example warning message.', 'warning'); + //drupal_set_message('Example error message.', 'error'); + //drupal_set_message('Example error message with html tags and link.', 'error'); + //$this->drupalGet(''); + //$this->assertNoRaw('ga("send", "event", "Messages", "Status message", "Example status message.");', '[testGoogleAnalyticsStatusMessages]: Example status message is not enabled for tracking.'); + //$this->assertNoRaw('ga("send", "event", "Messages", "Warning message", "Example warning message.");', '[testGoogleAnalyticsStatusMessages]: Example warning message is not enabled for tracking.'); + //$this->assertRaw('ga("send", "event", "Messages", "Error message", "Example error message.");', '[testGoogleAnalyticsStatusMessages]: Example error message is shown.'); + //$this->assertRaw('ga("send", "event", "Messages", "Error message", "Example error message with html tags and link.");', '[testGoogleAnalyticsStatusMessages]: HTML has been stripped successful from Example error message with html tags and link.'); + } +}