Issue #1956616: Change wording from DoubleClick to Display Features
[project/google_analytics.git] / googleanalytics.test
1 <?php
2
3 /**
4 * @file
5 * Test file for Google Analytics module.
6 */
7 class GoogleAnalyticsBasicTest extends DrupalWebTestCase {
8
9 public static function getInfo() {
10 return array(
11 'name' => t('Google Analytics basic tests'),
12 'description' => t('Test basic functionality of Google Analytics module.'),
13 'group' => 'Google Analytics',
14 );
15 }
16
17 function setUp() {
18 parent::setUp('googleanalytics');
19
20 $permissions = array('administer google analytics');
21
22 // User to set up google_analytics.
23 $this->admin_user = $this->drupalCreateUser($permissions);
24 $this->drupalLogin($this->admin_user);
25 }
26
27 function testGoogleAnalyticsConfiguration() {
28 // Check for setting page's presence.
29 $this->drupalGet('admin/settings/googleanalytics');
30 $this->assertRaw(t('Web Property ID'), '[testGoogleAnalyticsConfiguration]: Settings page displayed.');
31
32 // Check for account code validation.
33 $edit['googleanalytics_account'] = $this->randomName(2);
34 $this->drupalPost('admin/settings/googleanalytics', $edit, 'Save configuration');
35 $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.');
36 }
37
38 function testGoogleAnalyticsPageVisibility() {
39 $ua_code = 'UA-123456-1';
40 variable_set('googleanalytics_account', $ua_code);
41
42 // Show tracking on "every page except the listed pages".
43 variable_set('googleanalytics_visibility', 0);
44 // Disable tracking one "admin*" pages only.
45 variable_set('googleanalytics_pages', "admin\nadmin/*");
46 // Enable tracking only for authenticated users only.
47 variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
48
49 // Check tracking code visibility.
50 $this->drupalGet('');
51 $this->assertRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed for authenticated users.');
52
53 // Test whether tracking code is not included on pages to omit.
54 $this->drupalGet('admin');
55 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin page.');
56 $this->drupalGet('admin/settings/googleanalytics');
57 // Checking for tracking code URI here, as $ua_code is displayed in the form.
58 $this->assertNoRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin subpage.');
59
60 // Test whether tracking code display is properly flipped.
61 variable_set('googleanalytics_visibility', 1);
62 $this->drupalGet('admin');
63 $this->assertRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin page.');
64 $this->drupalGet('admin/settings/googleanalytics');
65 // Checking for tracking code URI here, as $ua_code is displayed in the form.
66 $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin subpage.');
67 $this->drupalGet('');
68 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is NOT displayed on front page.');
69
70 // Test whether tracking code is not display for anonymous.
71 $this->drupalLogout();
72 $this->drupalGet('');
73 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is NOT displayed for anonymous.');
74
75 // Switch back to every page except the listed pages.
76 variable_set('googleanalytics_visibility', 0);
77 // Enable tracking code for all user roles.
78 variable_set('googleanalytics_roles', array());
79
80 // Test whether 404 not found tracking code is shown on non-existent pages.
81 $this->drupalGet($this->randomName(64));
82 $this->assertRaw('/404.html', '[testGoogleAnalyticsPageVisibility]: 404 Not Found tracking code shown on non-existent page.');
83
84 // DNT Tests:
85 // Enable caching of pages for anonymous users.
86 variable_set('cache', 1);
87 // Test whether DNT headers will fail to disable embedding of tracking code.
88 $this->drupalGet('', array(), array('DNT: 1'));
89 $this->assertRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT header send from client, but page caching is enabled and tracker cannot removed.');
90 // DNT works only with caching of pages for anonymous users disabled.
91 variable_set('cache', 0);
92 $this->drupalGet('');
93 $this->assertRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: Tracking is enabled without DNT header.');
94 // Test whether DNT header is able to remove the tracking code.
95 $this->drupalGet('', array(), array('DNT: 1'));
96 $this->assertNoRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT header received from client. Tracking has been disabled by browser.');
97 // Disable DNT feature and see if tracker is still embedded.
98 variable_set('googleanalytics_privacy_donottrack', 0);
99 $this->drupalGet('', array(), array('DNT: 1'));
100 $this->assertRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT feature is disabled, DNT header from browser has been ignored.');
101 }
102
103 function testGoogleAnalyticsTrackingCode() {
104 $ua_code = 'UA-123456-2';
105 variable_set('googleanalytics_account', $ua_code);
106
107 // Show tracking code on every page except the listed pages.
108 variable_set('googleanalytics_visibility', 0);
109 // Enable tracking code for all user roles.
110 variable_set('googleanalytics_roles', array());
111
112 /* Sample JS code as added to page:
113 <script type="text/javascript" src="/sites/all/modules/google_analytics/googleanalytics.js?w"></script>
114 <script type="text/javascript">
115 var _gaq = _gaq || [];
116 _gaq.push(['_setAccount', 'UA-123456-7']);
117 _gaq.push(['_trackPageview']);
118
119 (function() {
120 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
121 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
122 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
123 })();
124 </script>
125 */
126
127 // Test whether tracking code uses latest JS.
128 variable_set('googleanalytics_cache', 0);
129 $this->drupalGet('');
130 $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTrackingCode]: Latest tracking code used.');
131
132 // Test whether the alternate doubleclick library is used
133 variable_set('googleanalytics_trackdoubleclick', 1);
134 $this->drupalGet('');
135 $this->assertRaw('stats.g.doubleclick.net/dc.js', '[testGoogleAnalyticsTrackingCode]: Doubleclick tracking code used.');
136
137 // Test whether anonymize visitors IP address feature has been enabled.
138 $this->drupalGet('');
139 $this->assertNoRaw('_gaq.push(["_gat._anonymizeIp"]);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address not found on frontpage.');
140 // Enable anonymizing of IP addresses.
141 variable_set('googleanalytics_tracker_anonymizeip', 1);
142 $this->drupalGet('');
143 $this->assertRaw('_gaq.push(["_gat._anonymizeIp"]);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address found on frontpage.');
144
145 // Test whether the BEFORE and AFTER code is added to the tracker.
146 variable_set('googleanalytics_codesnippet_before', '_setDetectFlash(false);');
147 variable_set('googleanalytics_codesnippet_after', '_gaq.push(["t2._setAccount", "UA-123456-3"]);_gaq.push(["t2._trackPageview"]);');
148 $this->drupalGet('');
149 $this->assertRaw('_setDetectFlash(false);', '[testGoogleAnalyticsTrackingCode]: Before codesnippet has been found with "Flash" detection disabled.');
150 $this->assertRaw('t2._setAccount', '[testGoogleAnalyticsTrackingCode]: After codesnippet with "t2" tracker has been found.');
151 }
152 }
153
154 class GoogleAnalyticsCustomVariablesTest extends DrupalWebTestCase {
155
156 public static function getInfo() {
157 return array(
158 'name' => t('Google Analytics Custom Variables tests'),
159 'description' => t('Test custom variables functionality of Google Analytics module.'),
160 'group' => 'Google Analytics',
161 'dependencies' => array('token'),
162 );
163 }
164
165 function setUp() {
166 parent::setUp('googleanalytics', 'token');
167
168 $permissions = array(
169 'access administration pages',
170 'administer google analytics',
171 );
172
173 // User to set up google_analytics.
174 $this->admin_user = $this->drupalCreateUser($permissions);
175 }
176
177 function testGoogleAnalyticsCustomVariables() {
178 $ua_code = 'UA-123456-3';
179 variable_set('googleanalytics_account', $ua_code);
180
181 // Basic test if the feature works.
182 $custom_vars = array(
183 'slots' => array(
184 1 => array(
185 'slot' => 1,
186 'name' => 'Foo 1',
187 'value' => 'Bar 1',
188 'scope' => 3,
189 ),
190 2 => array(
191 'slot' => 2,
192 'name' => 'Foo 2',
193 'value' => 'Bar 2',
194 'scope' => 2,
195 ),
196 3 => array(
197 'slot' => 3,
198 'name' => 'Foo 3',
199 'value' => 'Bar 3',
200 'scope' => 3,
201 ),
202 4 => array(
203 'slot' => 4,
204 'name' => 'Foo 4',
205 'value' => 'Bar 4',
206 'scope' => 2,
207 ),
208 5 => array(
209 'slot' => 5,
210 'name' => 'Foo 5',
211 'value' => 'Bar 5',
212 'scope' => 1,
213 ),
214 )
215 );
216 variable_set('googleanalytics_custom_var', $custom_vars);
217 $this->drupalGet('');
218
219 foreach ($custom_vars['slots'] as $slot) {
220 $this->assertRaw("_gaq.push(['_setCustomVar', " . $slot['slot'] . ", \"" . $slot['name'] . "\", \"" . $slot['value'] . "\", " . $slot['scope'] . "]);", '[testGoogleAnalyticsCustomVariables]: _setCustomVar ' . $slot['slot'] . ' is shown.');
221 }
222
223 // Test whether tokens are replaced in custom variable names.
224 $site_slogan = $this->randomName(16);
225 variable_set('site_slogan', $site_slogan);
226
227 $custom_vars = array(
228 'slots' => array(
229 1 => array(
230 'slot' => 1,
231 'name' => 'Name: [site-slogan]',
232 'value' => 'Value: [site-slogan]',
233 'scope' => 3,
234 ),
235 2 => array(
236 'slot' => 2,
237 'name' => '',
238 'value' => $this->randomName(16),
239 'scope' => 1,
240 ),
241 3 => array(
242 'slot' => 3,
243 'name' => $this->randomName(16),
244 'value' => '',
245 'scope' => 2,
246 ),
247 4 => array(
248 'slot' => 4,
249 'name' => '',
250 'value' => '',
251 'scope' => 3,
252 ),
253 5 => array(
254 'slot' => 5,
255 'name' => '',
256 'value' => '',
257 'scope' => 3,
258 ),
259 )
260 );
261 variable_set('googleanalytics_custom_var', $custom_vars);
262 $this->verbose('<pre>' . print_r($custom_vars, TRUE) . '</pre>');
263
264 $this->drupalGet('');
265 $this->assertRaw("_gaq.push(['_setCustomVar', 1, \"Name: $site_slogan\", \"Value: $site_slogan\", 3]", '[testGoogleAnalyticsCustomVariables]: Tokens have been replaced in custom variable.');
266 $this->assertNoRaw("_gaq.push(['_setCustomVar', 2,", '[testGoogleAnalyticsCustomVariables]: Value with empty name is not shown.');
267 $this->assertNoRaw("_gaq.push(['_setCustomVar', 3,", '[testGoogleAnalyticsCustomVariables]: Name with empty value is not shown.');
268 $this->assertNoRaw("_gaq.push(['_setCustomVar', 4,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.');
269 $this->assertNoRaw("_gaq.push(['_setCustomVar', 5,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.');
270 }
271 }
272
273 class GoogleAnalyticsRolesTest extends DrupalWebTestCase {
274
275 public static function getInfo() {
276 return array(
277 'name' => t('Google Analytics role tests'),
278 'description' => t('Test roles functionality of Google Analytics module.'),
279 'group' => 'Google Analytics',
280 );
281 }
282
283 function setUp() {
284 parent::setUp('googleanalytics');
285
286 $permissions = array(
287 'access administration pages',
288 'administer google analytics',
289 );
290
291 // User to set up google_analytics.
292 $this->admin_user = $this->drupalCreateUser($permissions);
293 }
294
295 function testGoogleAnalyticsRolesTracking() {
296 $ua_code = 'UA-123456-4';
297 variable_set('googleanalytics_account', $ua_code);
298
299 // Test if the default settings are working as expected.
300
301 // Add to the selected roles only.
302 variable_set('googleanalytics_visibility_roles', 0);
303 // Enable tracking for all users.
304 variable_set('googleanalytics_roles', array());
305
306 // Check tracking code visibility.
307 $this->drupalGet('');
308 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for anonymous users on frontpage with default settings.');
309
310 $this->drupalLogin($this->admin_user);
311
312 $this->drupalGet('');
313 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users on frontpage with default settings.');
314 $this->drupalGet('admin');
315 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for authenticated users in admin section with default settings.');
316
317 // Test if the non-default settings are working as expected.
318
319 // Enable tracking only for authenticated users.
320 variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
321
322 $this->drupalGet('');
323 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users only on frontpage.');
324
325 $this->drupalLogout();
326 $this->drupalGet('');
327 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for anonymous users on frontpage.');
328
329 // Add to every role except the selected ones.
330 variable_set('googleanalytics_visibility_roles', 1);
331 // Enable tracking for all users.
332 variable_set('googleanalytics_roles', array());
333
334 // Check tracking code visibility.
335 $this->drupalGet('');
336 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed for anonymous users.');
337
338 $this->drupalLogin($this->admin_user);
339
340 $this->drupalGet('');
341 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed on frontpage for authenticated users.');
342 $this->drupalGet('admin');
343 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and NOT displayed in admin section for authenticated users.');
344
345 // Disable tracking for authenticated users.
346 variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
347
348 $this->drupalGet('');
349 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed on frontpage for excluded authenticated users.');
350 $this->drupalGet('admin');
351 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed in admin section for excluded authenticated users.');
352
353 $this->drupalLogout();
354 $this->drupalGet('');
355 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.');
356 }
357
358 }