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