googleanalytics.test 20 KB

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