| 60b6111f |
1 | <?php |
| 2 | // $Id$ |
| 3 | |
| 97110728 |
4 | /** |
| 5 | * @file |
| 6 | * Test file for Google Analytics module. |
| 7 | */ |
| 8 | class GoogleAnalyticsBasicTest extends DrupalWebTestCase { |
| 60b6111f |
9 | |
| 6117131f |
10 | public static function getInfo() { |
| 97110728 |
11 | return array( |
| 12 | 'name' => t('Google Analytics basic tests'), |
| 13 | 'description' => t('Test basic Google Analytics module functionality.'), |
| 14 | 'group' => 'Google Analytics', |
| 15 | ); |
| 60b6111f |
16 | } |
| 17 | |
| 8aa9815e |
18 | public function setUp() { |
| 62d4d6c8 |
19 | parent::setUp('googleanalytics'); |
| 20 | |
| 21 | $permissions = array('administer google analytics'); |
| 60b6111f |
22 | |
| 97110728 |
23 | // User to set up google_analytics. |
| 24 | $user = $this->drupalCreateUser($permissions); |
| 25 | $this->drupalLogin($user); |
| 60b6111f |
26 | } |
| 97110728 |
27 | |
| 8aa9815e |
28 | public function testGoogleAnalytics() { |
| 60b6111f |
29 | // Check for setting page's presence. |
| 097e118e |
30 | $this->drupalGet('admin/config/system/googleanalytics'); |
| 5727b3df |
31 | $this->assertRaw(t('Web Property ID'), '[testGoogleAnalytics]: Settings page displayed.'); |
| 60b6111f |
32 | |
| 33 | // Check for account code validation. |
| 34 | $edit['googleanalytics_account'] = $this->randomName(2); |
| 097e118e |
35 | $this->drupalPost('admin/config/system/googleanalytics', $edit, 'Save configuration'); |
| 5727b3df |
36 | $this->assertRaw(t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxx-x.'), '[testGoogleAnalytics]: Invalid Web Property ID number validated.'); |
| 60b6111f |
37 | } |
| 97110728 |
38 | |
| 8aa9815e |
39 | public function testGoogleAnalyticsTracking() { |
| 60b6111f |
40 | // Set visibility to hide tracking code on admin page only, |
| 41 | // track authenticated users. |
| 97110728 |
42 | variable_set('googleanalytics_visibility', 0); |
| 43 | variable_set('googleanalytics_pages', 'admin*'); |
| 44 | variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)); |
| 60b6111f |
45 | $ua_code = 'UA-123456-7'; |
| 97110728 |
46 | variable_set('googleanalytics_account', $ua_code); |
| 60b6111f |
47 | |
| 48 | // Check tracking code visibility. |
| 49 | $this->drupalGet(''); |
| 97110728 |
50 | $this->assertRaw($ua_code, '[testGoogleAnalyticsTracking]: Tracking code is displayed for authenticated users.'); |
| 60b6111f |
51 | |
| 52 | /* Sample JS code as added to page: |
| 53 | <script type="text/javascript" src="/sites/all/modules/google_analytics/googleanalytics.js?w"></script> |
| 4626fc8d |
54 | <script type="text/javascript"> |
| 55 | var _gaq = _gaq || []; |
| 56 | _gaq.push(['_setAccount', 'UA-123456-7']); |
| 57 | _gaq.push(['_trackPageview']); |
| 5727b3df |
58 | |
| 4626fc8d |
59 | (function() { |
| 60 | var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; |
| 61 | ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; |
| 62 | var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); |
| 63 | })(); |
| 64 | </script> |
| 60b6111f |
65 | */ |
| 66 | |
| 67 | // Test whether tracking code uses latest JS. |
| 97110728 |
68 | variable_set('googleanalytics_cache', 0); |
| 60b6111f |
69 | $this->drupalGet(''); |
| 97110728 |
70 | $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTracking]: Latest tracking code used.'); |
| 60b6111f |
71 | |
| 60b6111f |
72 | // Test whether tracking code is not included on pages to omit. |
| 73 | $this->drupalGet('admin'); |
| 97110728 |
74 | $this->assertNoRaw($ua_code, '[testGoogleAnalyticsTracking]: Tracking code is not displayed on admin page.'); |
| 097e118e |
75 | $this->drupalGet('admin/config/system/googleanalytics'); |
| 60b6111f |
76 | // Checking for tracking code URI here, as $ua_code is displayed in the form. |
| 97110728 |
77 | $this->assertNoRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTracking]: Tracking code is not displayed on admin subpage.'); |
| 60b6111f |
78 | |
| 79 | // Test whether tracking code display is properly flipped. |
| 97110728 |
80 | variable_set('googleanalytics_visibility', 1); |
| 60b6111f |
81 | $this->drupalGet('admin'); |
| 97110728 |
82 | $this->assertRaw($ua_code, '[testGoogleAnalyticsTracking]: Tracking code is displayed on admin page.'); |
| 097e118e |
83 | $this->drupalGet('admin/config/system/googleanalytics'); |
| 60b6111f |
84 | // Checking for tracking code URI here, as $ua_code is displayed in the form. |
| 97110728 |
85 | $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTracking]: Tracking code is displayed on admin subpage.'); |
| 60b6111f |
86 | $this->drupalGet(''); |
| 97110728 |
87 | $this->assertNoRaw($ua_code, '[testGoogleAnalyticsTracking]: Tracking code is not displayed on front page.'); |
| 88 | |
| 60b6111f |
89 | // Test whether tracking code is not display for anonymous. |
| 7c745c6a |
90 | $this->drupalGet('user/logout'); |
| 60b6111f |
91 | $this->drupalGet(''); |
| 97110728 |
92 | $this->assertNoRaw($ua_code, '[testGoogleAnalyticsTracking]: Tracking code is not displayed for anonymous.'); |
| 60b6111f |
93 | } |
| 94 | |
| 95 | } |