D7 backport: Restructured tests and more tests added.
[project/google_analytics.git] / googleanalytics.test
CommitLineData
8afc0408 1<?php
2// $Id$
3
4/**
5 * @file
6 * Test file for Google Analytics module.
7 */
8class GoogleAnalyticsBasicTest extends DrupalWebTestCase {
9
10 public static function getInfo() {
11 return array(
12 'name' => t('Google Analytics basic tests'),
9c8ef09a 13 'description' => t('Test basic functionality of Google Analytics module.'),
8afc0408 14 'group' => 'Google Analytics',
15 );
16 }
17
9057d6d6 18 function setUp() {
8afc0408 19 parent::setUp('googleanalytics');
20
21 $permissions = array('administer google analytics');
22
23 // User to set up google_analytics.
9c8ef09a 24 $this->admin_user = $this->drupalCreateUser($permissions);
25 $this->drupalLogin($this->admin_user);
8afc0408 26 }
27
9c8ef09a 28 function testGoogleAnalyticsConfiguration() {
8afc0408 29 // Check for setting page's presence.
30 $this->drupalGet('admin/settings/googleanalytics');
9c8ef09a 31 $this->assertRaw(t('Web Property ID'), '[testGoogleAnalyticsConfiguration]: Settings page displayed.');
8afc0408 32
33 // Check for account code validation.
34 $edit['googleanalytics_account'] = $this->randomName(2);
35 $this->drupalPost('admin/settings/googleanalytics', $edit, 'Save configuration');
9c8ef09a 36 $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.');
8afc0408 37 }
38
9c8ef09a 39 function testGoogleAnalyticsPageVisibility() {
40 $ua_code = 'UA-123456-1';
41 variable_set('googleanalytics_account', $ua_code);
42
43 // Show tracking on "every page except the listed pages".
8afc0408 44 variable_set('googleanalytics_visibility', 0);
9c8ef09a 45 // Disable tracking one "admin*" pages only.
46 variable_set('googleanalytics_pages', "admin\nadmin/*");
47 // Enable tracking only for authenticated users only.
8afc0408 48 variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
8afc0408 49
50 // Check tracking code visibility.
51 $this->drupalGet('');
9c8ef09a 52 $this->assertRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed for authenticated users.');
53
54 // Test whether tracking code is not included on pages to omit.
55 $this->drupalGet('admin');
56 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin page.');
57 $this->drupalGet('admin/settings/googleanalytics');
58 // Checking for tracking code URI here, as $ua_code is displayed in the form.
59 $this->assertNoRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin subpage.');
60
61 // Test whether tracking code display is properly flipped.
62 variable_set('googleanalytics_visibility', 1);
63 $this->drupalGet('admin');
64 $this->assertRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin page.');
65 $this->drupalGet('admin/settings/googleanalytics');
66 // Checking for tracking code URI here, as $ua_code is displayed in the form.
67 $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin subpage.');
68 $this->drupalGet('');
69 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is NOT displayed on front page.');
70
71 // Test whether tracking code is not display for anonymous.
72 $this->drupalLogout();
73 $this->drupalGet('');
74 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is NOT displayed for anonymous.');
75
76 // Switch back to every page except the listed pages.
77 variable_set('googleanalytics_visibility', 0);
78 // Enable tracking code for all user roles.
79 variable_set('googleanalytics_roles', array());
80
81 // Test whether 404 not found tracking code is shown on non-existent pages.
82 $this->drupalGet($this->randomName(64));
83 $this->assertRaw('/404.html', '[testGoogleAnalyticsPageVisibility]: 404 Not Found tracking code shown on non-existent page.');
84 }
85
86 function testGoogleAnalyticsTrackingCode() {
87 $ua_code = 'UA-123456-2';
88 variable_set('googleanalytics_account', $ua_code);
89
90 // Show tracking code on every page except the listed pages.
91 variable_set('googleanalytics_visibility', 0);
92 // Enable tracking code for all user roles.
93 variable_set('googleanalytics_roles', array());
8afc0408 94
95 /* Sample JS code as added to page:
96 <script type="text/javascript" src="/sites/all/modules/google_analytics/googleanalytics.js?w"></script>
5e96c261 97 <script type="text/javascript">
98 var _gaq = _gaq || [];
99 _gaq.push(['_setAccount', 'UA-123456-7']);
100 _gaq.push(['_trackPageview']);
bdb10b7f 101
5e96c261 102 (function() {
103 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
104 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
105 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
106 })();
107 </script>
8afc0408 108 */
109
110 // Test whether tracking code uses latest JS.
111 variable_set('googleanalytics_cache', 0);
112 $this->drupalGet('');
9c8ef09a 113 $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTrackingCode]: Latest tracking code used.');
8afc0408 114
9c8ef09a 115 // Test whether anonymize visitors IP address feature has been enabled.
8afc0408 116 $this->drupalGet('');
9c8ef09a 117 $this->assertNoRaw('_gaq.push(["_gat._anonymizeIp"]);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address not found on frontpage.');
118 // Enable anonymizing of IP addresses.
119 variable_set('googleanalytics_tracker_anonymizeip', 1);
120 $this->drupalGet('');
121 $this->assertRaw('_gaq.push(["_gat._anonymizeIp"]);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address found on frontpage.');
8afc0408 122
9c8ef09a 123 // Test whether the BEFORE and AFTER code is added to the tracker.
124 variable_set('googleanalytics_codesnippet_before', '_setDetectFlash(false);');
125 variable_set('googleanalytics_codesnippet_after', '_gaq.push(["t2._setAccount", "UA-123456-3"]);_gaq.push(["t2._trackPageview"]);');
8afc0408 126 $this->drupalGet('');
9c8ef09a 127 $this->assertRaw('_setDetectFlash(false);', '[testGoogleAnalyticsTrackingCode]: Before codesnippet has been found with "Flash" detection disabled.');
128 $this->assertRaw('t2._setAccount', '[testGoogleAnalyticsTrackingCode]: After codesnippet with "t2" tracker has been found.');
8afc0408 129 }
130
131}
e7be54af 132
133class GoogleAnalyticsRolesTest extends DrupalWebTestCase {
134
135 public static function getInfo() {
136 return array(
137 'name' => t('Google Analytics role tests'),
138 'description' => t('Test roles functionality of Google Analytics module.'),
139 'group' => 'Google Analytics',
140 );
141 }
142
143 function setUp() {
144 parent::setUp('googleanalytics');
145
146 $permissions = array(
147 'access administration pages',
148 'administer google analytics',
149 );
150
151 // User to set up google_analytics.
152 $this->admin_user = $this->drupalCreateUser($permissions);
153 }
154
155 function testGoogleAnalyticsRolesTracking() {
156 $ua_code = 'UA-123456-4';
157 variable_set('googleanalytics_account', $ua_code);
158
159 // Test if the default settings are working as expected.
160
161 // Add to the selected roles only.
162 variable_set('googleanalytics_visibility_roles', 0);
163 // Enable tracking for all users.
164 variable_set('googleanalytics_roles', array());
165
166 // Check tracking code visibility.
167 $this->drupalGet('');
168 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for anonymous users on frontpage with default settings.');
169
170 $this->drupalLogin($this->admin_user);
171
172 $this->drupalGet('');
173 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users on frontpage with default settings.');
174 $this->drupalGet('admin');
175 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for authenticated users in admin section with default settings.');
176
177 // Test if the non-default settings are working as expected.
178
179 // Enable tracking only for authenticated users.
180 variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
181
182 $this->drupalGet('');
183 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users only on frontpage.');
184
185 $this->drupalLogout();
186 $this->drupalGet('');
187 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for anonymous users on frontpage.');
188
189 // Add to every role except the selected ones.
190 variable_set('googleanalytics_visibility_roles', 1);
191 // Enable tracking for all users.
192 variable_set('googleanalytics_roles', array());
193
194 // Check tracking code visibility.
195 $this->drupalGet('');
196 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed for anonymous users.');
197
198 $this->drupalLogin($this->admin_user);
199
200 $this->drupalGet('');
201 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed on frontpage for authenticated users.');
202 $this->drupalGet('admin');
203 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and NOT displayed in admin section for authenticated users.');
204
205 // Disable tracking for authenticated users.
206 variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
207
208 $this->drupalGet('');
209 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed on frontpage for excluded authenticated users.');
210 $this->drupalGet('admin');
211 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed in admin section for excluded authenticated users.');
212
213 $this->drupalLogout();
214 $this->drupalGet('');
215 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.');
216 }
217
218}