annotate sites/all/modules/google_analytics/googleanalytics.test @ 11:b0ee71395280

deleted .DS_Store files
author danieleb <danielebarchiesi@me.com>
date Mon, 28 Oct 2013 16:12:13 +0000
parents b74b41bb73f0
children 134d4b2e75f6
rev   line source
danielebarchiesi@2 1 <?php
danielebarchiesi@2 2
danielebarchiesi@2 3 /**
danielebarchiesi@2 4 * @file
danielebarchiesi@2 5 * Test file for Google Analytics module.
danielebarchiesi@2 6 */
danielebarchiesi@2 7 class GoogleAnalyticsBasicTest extends DrupalWebTestCase {
danielebarchiesi@2 8
danielebarchiesi@2 9 public static function getInfo() {
danielebarchiesi@2 10 return array(
danielebarchiesi@2 11 'name' => t('Google Analytics basic tests'),
danielebarchiesi@2 12 'description' => t('Test basic functionality of Google Analytics module.'),
danielebarchiesi@2 13 'group' => 'Google Analytics',
danielebarchiesi@2 14 );
danielebarchiesi@2 15 }
danielebarchiesi@2 16
danielebarchiesi@2 17 function setUp() {
danielebarchiesi@2 18 parent::setUp('googleanalytics');
danielebarchiesi@2 19
danielebarchiesi@2 20 $permissions = array(
danielebarchiesi@2 21 'access administration pages',
danielebarchiesi@2 22 'administer google analytics',
danielebarchiesi@2 23 );
danielebarchiesi@2 24
danielebarchiesi@2 25 // User to set up google_analytics.
danielebarchiesi@2 26 $this->admin_user = $this->drupalCreateUser($permissions);
danielebarchiesi@2 27 $this->drupalLogin($this->admin_user);
danielebarchiesi@2 28 }
danielebarchiesi@2 29
danielebarchiesi@2 30 function testGoogleAnalyticsConfiguration() {
danielebarchiesi@2 31 // Check for setting page's presence.
danielebarchiesi@2 32 $this->drupalGet('admin/config/system/googleanalytics');
danielebarchiesi@2 33 $this->assertRaw(t('Web Property ID'), '[testGoogleAnalyticsConfiguration]: Settings page displayed.');
danielebarchiesi@2 34
danielebarchiesi@2 35 // Check for account code validation.
danielebarchiesi@2 36 $edit['googleanalytics_account'] = $this->randomName(2);
danielebarchiesi@2 37 $this->drupalPost('admin/config/system/googleanalytics', $edit, 'Save configuration');
danielebarchiesi@2 38 $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.');
danielebarchiesi@2 39 }
danielebarchiesi@2 40
danielebarchiesi@2 41 function testGoogleAnalyticsPageVisibility() {
danielebarchiesi@2 42 $ua_code = 'UA-123456-1';
danielebarchiesi@2 43 variable_set('googleanalytics_account', $ua_code);
danielebarchiesi@2 44
danielebarchiesi@2 45 // Show tracking on "every page except the listed pages".
danielebarchiesi@2 46 variable_set('googleanalytics_visibility_pages', 0);
danielebarchiesi@2 47 // Disable tracking one "admin*" pages only.
danielebarchiesi@2 48 variable_set('googleanalytics_pages', "admin\nadmin/*");
danielebarchiesi@2 49 // Enable tracking only for authenticated users only.
danielebarchiesi@2 50 variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
danielebarchiesi@2 51
danielebarchiesi@2 52 // Check tracking code visibility.
danielebarchiesi@2 53 $this->drupalGet('');
danielebarchiesi@2 54 $this->assertRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed for authenticated users.');
danielebarchiesi@2 55
danielebarchiesi@2 56 // Test whether tracking code is not included on pages to omit.
danielebarchiesi@2 57 $this->drupalGet('admin');
danielebarchiesi@2 58 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin page.');
danielebarchiesi@2 59 $this->drupalGet('admin/config/system/googleanalytics');
danielebarchiesi@2 60 // Checking for tracking code URI here, as $ua_code is displayed in the form.
danielebarchiesi@2 61 $this->assertNoRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin subpage.');
danielebarchiesi@2 62
danielebarchiesi@2 63 // Test whether tracking code display is properly flipped.
danielebarchiesi@2 64 variable_set('googleanalytics_visibility_pages', 1);
danielebarchiesi@2 65 $this->drupalGet('admin');
danielebarchiesi@2 66 $this->assertRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin page.');
danielebarchiesi@2 67 $this->drupalGet('admin/config/system/googleanalytics');
danielebarchiesi@2 68 // Checking for tracking code URI here, as $ua_code is displayed in the form.
danielebarchiesi@2 69 $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin subpage.');
danielebarchiesi@2 70 $this->drupalGet('');
danielebarchiesi@2 71 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is NOT displayed on front page.');
danielebarchiesi@2 72
danielebarchiesi@2 73 // Test whether tracking code is not display for anonymous.
danielebarchiesi@2 74 $this->drupalLogout();
danielebarchiesi@2 75 $this->drupalGet('');
danielebarchiesi@2 76 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is NOT displayed for anonymous.');
danielebarchiesi@2 77
danielebarchiesi@2 78 // Switch back to every page except the listed pages.
danielebarchiesi@2 79 variable_set('googleanalytics_visibility_pages', 0);
danielebarchiesi@2 80 // Enable tracking code for all user roles.
danielebarchiesi@2 81 variable_set('googleanalytics_roles', array());
danielebarchiesi@2 82
danielebarchiesi@2 83 // Test whether 403 forbidden tracking code is shown if user has no access.
danielebarchiesi@2 84 $this->drupalGet('admin');
danielebarchiesi@2 85 $this->assertRaw('/403.html', '[testGoogleAnalyticsPageVisibility]: 403 Forbidden tracking code shown if user has no access.');
danielebarchiesi@2 86
danielebarchiesi@2 87 // Test whether 404 not found tracking code is shown on non-existent pages.
danielebarchiesi@2 88 $this->drupalGet($this->randomName(64));
danielebarchiesi@2 89 $this->assertRaw('/404.html', '[testGoogleAnalyticsPageVisibility]: 404 Not Found tracking code shown on non-existent page.');
danielebarchiesi@2 90
danielebarchiesi@2 91 // DNT Tests:
danielebarchiesi@2 92 // Enable caching of pages for anonymous users.
danielebarchiesi@2 93 variable_set('cache', 1);
danielebarchiesi@2 94 // Test whether DNT headers will fail to disable embedding of tracking code.
danielebarchiesi@2 95 $this->drupalGet('', array(), array('DNT: 1'));
danielebarchiesi@2 96 $this->assertRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT header send from client, but page caching is enabled and tracker cannot removed.');
danielebarchiesi@2 97 // DNT works only with caching of pages for anonymous users disabled.
danielebarchiesi@2 98 variable_set('cache', 0);
danielebarchiesi@2 99 $this->drupalGet('');
danielebarchiesi@2 100 $this->assertRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: Tracking is enabled without DNT header.');
danielebarchiesi@2 101 // Test whether DNT header is able to remove the tracking code.
danielebarchiesi@2 102 $this->drupalGet('', array(), array('DNT: 1'));
danielebarchiesi@2 103 $this->assertNoRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT header received from client. Tracking has been disabled by browser.');
danielebarchiesi@2 104 // Disable DNT feature and see if tracker is still embedded.
danielebarchiesi@2 105 variable_set('googleanalytics_privacy_donottrack', 0);
danielebarchiesi@2 106 $this->drupalGet('', array(), array('DNT: 1'));
danielebarchiesi@2 107 $this->assertRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT feature is disabled, DNT header from browser has been ignored.');
danielebarchiesi@2 108 }
danielebarchiesi@2 109
danielebarchiesi@2 110 function testGoogleAnalyticsTrackingCode() {
danielebarchiesi@2 111 $ua_code = 'UA-123456-2';
danielebarchiesi@2 112 variable_set('googleanalytics_account', $ua_code);
danielebarchiesi@2 113
danielebarchiesi@2 114 // Show tracking code on every page except the listed pages.
danielebarchiesi@2 115 variable_set('googleanalytics_visibility_pages', 0);
danielebarchiesi@2 116 // Enable tracking code for all user roles.
danielebarchiesi@2 117 variable_set('googleanalytics_roles', array());
danielebarchiesi@2 118
danielebarchiesi@2 119 /* Sample JS code as added to page:
danielebarchiesi@2 120 <script type="text/javascript" src="/sites/all/modules/google_analytics/googleanalytics.js?w"></script>
danielebarchiesi@2 121 <script type="text/javascript">
danielebarchiesi@2 122 var _gaq = _gaq || [];
danielebarchiesi@2 123 _gaq.push(['_setAccount', 'UA-123456-7']);
danielebarchiesi@2 124 _gaq.push(['_trackPageview']);
danielebarchiesi@2 125
danielebarchiesi@2 126 (function() {
danielebarchiesi@2 127 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
danielebarchiesi@2 128 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
danielebarchiesi@2 129 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
danielebarchiesi@2 130 })();
danielebarchiesi@2 131 </script>
danielebarchiesi@2 132 */
danielebarchiesi@2 133
danielebarchiesi@2 134 // Test whether tracking code uses latest JS.
danielebarchiesi@2 135 variable_set('googleanalytics_cache', 0);
danielebarchiesi@2 136 $this->drupalGet('');
danielebarchiesi@2 137 $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTrackingCode]: Latest tracking code used.');
danielebarchiesi@2 138
danielebarchiesi@2 139 // Test whether the alternate doubleclick library is used
danielebarchiesi@2 140 variable_set('googleanalytics_trackdoubleclick', 1);
danielebarchiesi@2 141 $this->drupalGet('');
danielebarchiesi@2 142 $this->assertRaw('stats.g.doubleclick.net/dc.js', '[testGoogleAnalyticsTrackingCode]: Doubleclick tracking code used.');
danielebarchiesi@2 143
danielebarchiesi@2 144 // Test whether anonymize visitors IP address feature has been enabled.
danielebarchiesi@2 145 $this->drupalGet('');
danielebarchiesi@2 146 $this->assertNoRaw('_gaq.push(["_gat._anonymizeIp"]);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address not found on frontpage.');
danielebarchiesi@2 147 // Enable anonymizing of IP addresses.
danielebarchiesi@2 148 variable_set('googleanalytics_tracker_anonymizeip', 1);
danielebarchiesi@2 149 $this->drupalGet('');
danielebarchiesi@2 150 $this->assertRaw('_gaq.push(["_gat._anonymizeIp"]);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address found on frontpage.');
danielebarchiesi@2 151
danielebarchiesi@2 152 // Test whether single domain tracking is active.
danielebarchiesi@2 153 $this->drupalGet('');
danielebarchiesi@2 154 $this->assertNoRaw('_gaq.push(["_setDomainName"', '[testGoogleAnalyticsTrackingCode]: Single domain tracking is active.');
danielebarchiesi@2 155
danielebarchiesi@2 156 // Enable "One domain with multiple subdomains".
danielebarchiesi@2 157 variable_set('googleanalytics_domain_mode', 1);
danielebarchiesi@2 158 $this->drupalGet('');
danielebarchiesi@2 159
danielebarchiesi@2 160 // Test may run on localhost, an ipaddress or real domain name.
danielebarchiesi@2 161 // TODO: Workaround to run tests successfully. This feature cannot tested reliable.
danielebarchiesi@2 162 global $cookie_domain;
danielebarchiesi@2 163 if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
danielebarchiesi@2 164 $this->assertRaw('_gaq.push(["_setDomainName",', '[testGoogleAnalyticsTrackingCode]: One domain with multiple subdomains is active on real host.');
danielebarchiesi@2 165 }
danielebarchiesi@2 166 else {
danielebarchiesi@2 167 // Special cases, Localhost and IP addresses don't show '_setDomainName'.
danielebarchiesi@2 168 $this->assertNoRaw('_gaq.push(["_setDomainName",', '[testGoogleAnalyticsTrackingCode]: One domain with multiple subdomains may be active on localhost (test result is not reliable).');
danielebarchiesi@2 169 }
danielebarchiesi@2 170
danielebarchiesi@2 171 // Enable "Multiple top-level domains" tracking.
danielebarchiesi@2 172 variable_set('googleanalytics_domain_mode', 2);
danielebarchiesi@2 173 variable_set('googleanalytics_cross_domains', "www.example.com\nwww.example.net");
danielebarchiesi@2 174 $this->drupalGet('');
danielebarchiesi@2 175 $this->assertRaw('_gaq.push(["_setDomainName", "none"]);', '[testGoogleAnalyticsTrackingCode]: _setDomainName: "none" found. Cross domain tracking is active.');
danielebarchiesi@2 176 $this->assertRaw('_gaq.push(["_setAllowLinker", true]);', '[testGoogleAnalyticsTrackingCode]: _setAllowLinker: true found. Cross domain tracking is active.');
danielebarchiesi@2 177 $this->assertRaw('"trackCrossDomains":["www.example.com","www.example.net"]', '[testGoogleAnalyticsTrackingCode]: Cross domain tracking with www.example.com and www.example.net is active.');
danielebarchiesi@2 178
danielebarchiesi@2 179 // Test whether the BEFORE and AFTER code is added to the tracker.
danielebarchiesi@2 180 variable_set('googleanalytics_codesnippet_before', '_setDetectFlash(false);');
danielebarchiesi@2 181 variable_set('googleanalytics_codesnippet_after', '_gaq.push(["t2._setAccount", "UA-123456-3"]);_gaq.push(["t2._trackPageview"]);');
danielebarchiesi@2 182 $this->drupalGet('');
danielebarchiesi@2 183 $this->assertRaw('_setDetectFlash(false);', '[testGoogleAnalyticsTrackingCode]: Before codesnippet has been found with "Flash" detection disabled.');
danielebarchiesi@2 184 $this->assertRaw('t2._setAccount', '[testGoogleAnalyticsTrackingCode]: After codesnippet with "t2" tracker has been found.');
danielebarchiesi@2 185 }
danielebarchiesi@2 186 }
danielebarchiesi@2 187
danielebarchiesi@2 188 class GoogleAnalyticsCustomVariablesTest extends DrupalWebTestCase {
danielebarchiesi@2 189
danielebarchiesi@2 190 public static function getInfo() {
danielebarchiesi@2 191 return array(
danielebarchiesi@2 192 'name' => t('Google Analytics Custom Variables tests'),
danielebarchiesi@2 193 'description' => t('Test custom variables functionality of Google Analytics module.'),
danielebarchiesi@2 194 'group' => 'Google Analytics',
danielebarchiesi@2 195 'dependencies' => array('token'),
danielebarchiesi@2 196 );
danielebarchiesi@2 197 }
danielebarchiesi@2 198
danielebarchiesi@2 199 function setUp() {
danielebarchiesi@2 200 parent::setUp('googleanalytics', 'token');
danielebarchiesi@2 201
danielebarchiesi@2 202 $permissions = array(
danielebarchiesi@2 203 'access administration pages',
danielebarchiesi@2 204 'administer google analytics',
danielebarchiesi@2 205 );
danielebarchiesi@2 206
danielebarchiesi@2 207 // User to set up google_analytics.
danielebarchiesi@2 208 $this->admin_user = $this->drupalCreateUser($permissions);
danielebarchiesi@2 209 }
danielebarchiesi@2 210
danielebarchiesi@2 211 function testGoogleAnalyticsCustomVariables() {
danielebarchiesi@2 212 $ua_code = 'UA-123456-3';
danielebarchiesi@2 213 variable_set('googleanalytics_account', $ua_code);
danielebarchiesi@2 214
danielebarchiesi@2 215 // Basic test if the feature works.
danielebarchiesi@2 216 $custom_vars = array(
danielebarchiesi@2 217 'slots' => array(
danielebarchiesi@2 218 1 => array(
danielebarchiesi@2 219 'slot' => 1,
danielebarchiesi@2 220 'name' => 'Foo 1',
danielebarchiesi@2 221 'value' => 'Bar 1',
danielebarchiesi@2 222 'scope' => 3,
danielebarchiesi@2 223 ),
danielebarchiesi@2 224 2 => array(
danielebarchiesi@2 225 'slot' => 2,
danielebarchiesi@2 226 'name' => 'Foo 2',
danielebarchiesi@2 227 'value' => 'Bar 2',
danielebarchiesi@2 228 'scope' => 2,
danielebarchiesi@2 229 ),
danielebarchiesi@2 230 3 => array(
danielebarchiesi@2 231 'slot' => 3,
danielebarchiesi@2 232 'name' => 'Foo 3',
danielebarchiesi@2 233 'value' => 'Bar 3',
danielebarchiesi@2 234 'scope' => 3,
danielebarchiesi@2 235 ),
danielebarchiesi@2 236 4 => array(
danielebarchiesi@2 237 'slot' => 4,
danielebarchiesi@2 238 'name' => 'Foo 4',
danielebarchiesi@2 239 'value' => 'Bar 4',
danielebarchiesi@2 240 'scope' => 2,
danielebarchiesi@2 241 ),
danielebarchiesi@2 242 5 => array(
danielebarchiesi@2 243 'slot' => 5,
danielebarchiesi@2 244 'name' => 'Foo 5',
danielebarchiesi@2 245 'value' => 'Bar 5',
danielebarchiesi@2 246 'scope' => 1,
danielebarchiesi@2 247 ),
danielebarchiesi@2 248 )
danielebarchiesi@2 249 );
danielebarchiesi@2 250 variable_set('googleanalytics_custom_var', $custom_vars);
danielebarchiesi@2 251 $this->drupalGet('');
danielebarchiesi@2 252
danielebarchiesi@2 253 foreach ($custom_vars['slots'] as $slot) {
danielebarchiesi@2 254 $this->assertRaw("_gaq.push(['_setCustomVar', " . $slot['slot'] . ", \"" . $slot['name'] . "\", \"" . $slot['value'] . "\", " . $slot['scope'] . "]);", '[testGoogleAnalyticsCustomVariables]: _setCustomVar ' . $slot['slot'] . ' is shown.');
danielebarchiesi@2 255 }
danielebarchiesi@2 256
danielebarchiesi@2 257 // Test whether tokens are replaced in custom variable names.
danielebarchiesi@2 258 $site_slogan = $this->randomName(16);
danielebarchiesi@2 259 variable_set('site_slogan', $site_slogan);
danielebarchiesi@2 260
danielebarchiesi@2 261 $custom_vars = array(
danielebarchiesi@2 262 'slots' => array(
danielebarchiesi@2 263 1 => array(
danielebarchiesi@2 264 'slot' => 1,
danielebarchiesi@2 265 'name' => 'Name: [site:slogan]',
danielebarchiesi@2 266 'value' => 'Value: [site:slogan]',
danielebarchiesi@2 267 'scope' => 3,
danielebarchiesi@2 268 ),
danielebarchiesi@2 269 2 => array(
danielebarchiesi@2 270 'slot' => 2,
danielebarchiesi@2 271 'name' => '',
danielebarchiesi@2 272 'value' => $this->randomName(16),
danielebarchiesi@2 273 'scope' => 1,
danielebarchiesi@2 274 ),
danielebarchiesi@2 275 3 => array(
danielebarchiesi@2 276 'slot' => 3,
danielebarchiesi@2 277 'name' => $this->randomName(16),
danielebarchiesi@2 278 'value' => '',
danielebarchiesi@2 279 'scope' => 2,
danielebarchiesi@2 280 ),
danielebarchiesi@2 281 4 => array(
danielebarchiesi@2 282 'slot' => 4,
danielebarchiesi@2 283 'name' => '',
danielebarchiesi@2 284 'value' => '',
danielebarchiesi@2 285 'scope' => 3,
danielebarchiesi@2 286 ),
danielebarchiesi@2 287 5 => array(
danielebarchiesi@2 288 'slot' => 5,
danielebarchiesi@2 289 'name' => '',
danielebarchiesi@2 290 'value' => '',
danielebarchiesi@2 291 'scope' => 3,
danielebarchiesi@2 292 ),
danielebarchiesi@2 293 )
danielebarchiesi@2 294 );
danielebarchiesi@2 295 variable_set('googleanalytics_custom_var', $custom_vars);
danielebarchiesi@2 296 $this->verbose('<pre>' . print_r($custom_vars, TRUE) . '</pre>');
danielebarchiesi@2 297
danielebarchiesi@2 298 $this->drupalGet('');
danielebarchiesi@2 299 $this->assertRaw("_gaq.push(['_setCustomVar', 1, \"Name: $site_slogan\", \"Value: $site_slogan\", 3]", '[testGoogleAnalyticsCustomVariables]: Tokens have been replaced in custom variable.');
danielebarchiesi@2 300 $this->assertNoRaw("_gaq.push(['_setCustomVar', 2,", '[testGoogleAnalyticsCustomVariables]: Value with empty name is not shown.');
danielebarchiesi@2 301 $this->assertNoRaw("_gaq.push(['_setCustomVar', 3,", '[testGoogleAnalyticsCustomVariables]: Name with empty value is not shown.');
danielebarchiesi@2 302 $this->assertNoRaw("_gaq.push(['_setCustomVar', 4,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.');
danielebarchiesi@2 303 $this->assertNoRaw("_gaq.push(['_setCustomVar', 5,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.');
danielebarchiesi@2 304 }
danielebarchiesi@2 305 }
danielebarchiesi@2 306
danielebarchiesi@2 307 class GoogleAnalyticsStatusMessagesTest extends DrupalWebTestCase {
danielebarchiesi@2 308
danielebarchiesi@2 309 public static function getInfo() {
danielebarchiesi@2 310 return array(
danielebarchiesi@2 311 'name' => t('Google Analytics status messages tests'),
danielebarchiesi@2 312 'description' => t('Test status messages functionality of Google Analytics module.'),
danielebarchiesi@2 313 'group' => 'Google Analytics',
danielebarchiesi@2 314 );
danielebarchiesi@2 315 }
danielebarchiesi@2 316
danielebarchiesi@2 317 function setUp() {
danielebarchiesi@2 318 parent::setUp('googleanalytics');
danielebarchiesi@2 319
danielebarchiesi@2 320 $permissions = array(
danielebarchiesi@2 321 'access administration pages',
danielebarchiesi@2 322 'administer google analytics',
danielebarchiesi@2 323 );
danielebarchiesi@2 324
danielebarchiesi@2 325 // User to set up google_analytics.
danielebarchiesi@2 326 $this->admin_user = $this->drupalCreateUser($permissions);
danielebarchiesi@2 327 }
danielebarchiesi@2 328
danielebarchiesi@2 329 function testGoogleAnalyticsStatusMessages() {
danielebarchiesi@2 330 $ua_code = 'UA-123456-4';
danielebarchiesi@2 331 variable_set('googleanalytics_account', $ua_code);
danielebarchiesi@2 332
danielebarchiesi@2 333 // Enable logging of errors only.
danielebarchiesi@2 334 variable_set('googleanalytics_trackmessages', array('error' => 'error'));
danielebarchiesi@2 335
danielebarchiesi@2 336 $this->drupalPost('user/login', array(), 'Log in');
danielebarchiesi@2 337 $this->assertRaw('_gaq.push(["_trackEvent", "Messages", "Error message", "Username field is required."]);', '[testGoogleAnalyticsStatusMessages]: _trackEvent "Username field is required." is shown.');
danielebarchiesi@2 338 $this->assertRaw('_gaq.push(["_trackEvent", "Messages", "Error message", "Password field is required."]);', '[testGoogleAnalyticsStatusMessages]: _trackEvent "Password field is required." is shown.');
danielebarchiesi@2 339
danielebarchiesi@2 340 // @todo: investigate why drupal_set_message() fails.
danielebarchiesi@2 341 //drupal_set_message('Example status message.', 'status');
danielebarchiesi@2 342 //drupal_set_message('Example warning message.', 'warning');
danielebarchiesi@2 343 //drupal_set_message('Example error message.', 'error');
danielebarchiesi@2 344 //drupal_set_message('Example error <em>message</em> with html tags and <a href="http://example.com/">link</a>.', 'error');
danielebarchiesi@2 345 //$this->drupalGet('');
danielebarchiesi@2 346 //$this->assertNoRaw('_gaq.push(["_trackEvent", "Messages", "Status message", "Example status message."]);', '[testGoogleAnalyticsStatusMessages]: Example status message is not enabled for tracking.');
danielebarchiesi@2 347 //$this->assertNoRaw('_gaq.push(["_trackEvent", "Messages", "Warning message", "Example warning message."]);', '[testGoogleAnalyticsStatusMessages]: Example warning message is not enabled for tracking.');
danielebarchiesi@2 348 //$this->assertRaw('_gaq.push(["_trackEvent", "Messages", "Error message", "Example error message."]);', '[testGoogleAnalyticsStatusMessages]: Example error message is shown.');
danielebarchiesi@2 349 //$this->assertRaw('_gaq.push(["_trackEvent", "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.');
danielebarchiesi@2 350 }
danielebarchiesi@2 351 }
danielebarchiesi@2 352
danielebarchiesi@2 353 class GoogleAnalyticsRolesTest extends DrupalWebTestCase {
danielebarchiesi@2 354
danielebarchiesi@2 355 public static function getInfo() {
danielebarchiesi@2 356 return array(
danielebarchiesi@2 357 'name' => t('Google Analytics role tests'),
danielebarchiesi@2 358 'description' => t('Test roles functionality of Google Analytics module.'),
danielebarchiesi@2 359 'group' => 'Google Analytics',
danielebarchiesi@2 360 );
danielebarchiesi@2 361 }
danielebarchiesi@2 362
danielebarchiesi@2 363 function setUp() {
danielebarchiesi@2 364 parent::setUp('googleanalytics');
danielebarchiesi@2 365
danielebarchiesi@2 366 $permissions = array(
danielebarchiesi@2 367 'access administration pages',
danielebarchiesi@2 368 'administer google analytics',
danielebarchiesi@2 369 );
danielebarchiesi@2 370
danielebarchiesi@2 371 // User to set up google_analytics.
danielebarchiesi@2 372 $this->admin_user = $this->drupalCreateUser($permissions);
danielebarchiesi@2 373 }
danielebarchiesi@2 374
danielebarchiesi@2 375 function testGoogleAnalyticsRolesTracking() {
danielebarchiesi@2 376 $ua_code = 'UA-123456-4';
danielebarchiesi@2 377 variable_set('googleanalytics_account', $ua_code);
danielebarchiesi@2 378
danielebarchiesi@2 379 // Test if the default settings are working as expected.
danielebarchiesi@2 380
danielebarchiesi@2 381 // Add to the selected roles only.
danielebarchiesi@2 382 variable_set('googleanalytics_visibility_roles', 0);
danielebarchiesi@2 383 // Enable tracking for all users.
danielebarchiesi@2 384 variable_set('googleanalytics_roles', array());
danielebarchiesi@2 385
danielebarchiesi@2 386 // Check tracking code visibility.
danielebarchiesi@2 387 $this->drupalGet('');
danielebarchiesi@2 388 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for anonymous users on frontpage with default settings.');
danielebarchiesi@2 389 $this->drupalGet('admin');
danielebarchiesi@2 390 $this->assertRaw('/403.html', '[testGoogleAnalyticsRoleVisibility]: 403 Forbidden tracking code is displayed for anonymous users in admin section with default settings.');
danielebarchiesi@2 391
danielebarchiesi@2 392 $this->drupalLogin($this->admin_user);
danielebarchiesi@2 393
danielebarchiesi@2 394 $this->drupalGet('');
danielebarchiesi@2 395 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users on frontpage with default settings.');
danielebarchiesi@2 396 $this->drupalGet('admin');
danielebarchiesi@2 397 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for authenticated users in admin section with default settings.');
danielebarchiesi@2 398
danielebarchiesi@2 399 // Test if the non-default settings are working as expected.
danielebarchiesi@2 400
danielebarchiesi@2 401 // Enable tracking only for authenticated users.
danielebarchiesi@2 402 variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
danielebarchiesi@2 403
danielebarchiesi@2 404 $this->drupalGet('');
danielebarchiesi@2 405 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users only on frontpage.');
danielebarchiesi@2 406
danielebarchiesi@2 407 $this->drupalLogout();
danielebarchiesi@2 408 $this->drupalGet('');
danielebarchiesi@2 409 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for anonymous users on frontpage.');
danielebarchiesi@2 410
danielebarchiesi@2 411 // Add to every role except the selected ones.
danielebarchiesi@2 412 variable_set('googleanalytics_visibility_roles', 1);
danielebarchiesi@2 413 // Enable tracking for all users.
danielebarchiesi@2 414 variable_set('googleanalytics_roles', array());
danielebarchiesi@2 415
danielebarchiesi@2 416 // Check tracking code visibility.
danielebarchiesi@2 417 $this->drupalGet('');
danielebarchiesi@2 418 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed for anonymous users.');
danielebarchiesi@2 419 $this->drupalGet('admin');
danielebarchiesi@2 420 $this->assertRaw('/403.html', '[testGoogleAnalyticsRoleVisibility]: 403 Forbidden tracking code is shown for anonymous users if every role except the selected ones is selected.');
danielebarchiesi@2 421
danielebarchiesi@2 422 $this->drupalLogin($this->admin_user);
danielebarchiesi@2 423
danielebarchiesi@2 424 $this->drupalGet('');
danielebarchiesi@2 425 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed on frontpage for authenticated users.');
danielebarchiesi@2 426 $this->drupalGet('admin');
danielebarchiesi@2 427 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and NOT displayed in admin section for authenticated users.');
danielebarchiesi@2 428
danielebarchiesi@2 429 // Disable tracking for authenticated users.
danielebarchiesi@2 430 variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
danielebarchiesi@2 431
danielebarchiesi@2 432 $this->drupalGet('');
danielebarchiesi@2 433 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed on frontpage for excluded authenticated users.');
danielebarchiesi@2 434 $this->drupalGet('admin');
danielebarchiesi@2 435 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed in admin section for excluded authenticated users.');
danielebarchiesi@2 436
danielebarchiesi@2 437 $this->drupalLogout();
danielebarchiesi@2 438 $this->drupalGet('');
danielebarchiesi@2 439 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.');
danielebarchiesi@2 440 }
danielebarchiesi@2 441
danielebarchiesi@2 442 }