[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/google_analytics/ -> googleanalytics.test (source)

   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  
  85    function testGoogleAnalyticsTrackingCode() {
  86      $ua_code = 'UA-123456-2';
  87      variable_set('googleanalytics_account', $ua_code);
  88  
  89      // Show tracking code on every page except the listed pages.
  90      variable_set('googleanalytics_visibility', 0);
  91      // Enable tracking code for all user roles.
  92      variable_set('googleanalytics_roles', array());
  93  
  94      /* Sample JS code as added to page:
  95      <script type="text/javascript" src="/sites/all/modules/google_analytics/googleanalytics.js?w"></script>
  96      <script type="text/javascript">
  97        var _gaq = _gaq || [];
  98        _gaq.push(['_setAccount', 'UA-123456-7']);
  99        _gaq.push(['_trackPageview']);
 100  
 101        (function() {
 102          var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
 103          ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
 104          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 105        })();
 106      </script>
 107      */
 108  
 109      // Test whether tracking code uses latest JS.
 110      variable_set('googleanalytics_cache', 0);
 111      $this->drupalGet('');
 112      $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTrackingCode]: Latest tracking code used.');
 113  
 114      // Test whether anonymize visitors IP address feature has been enabled.
 115      $this->drupalGet('');
 116      $this->assertNoRaw('_gaq.push(["_gat._anonymizeIp"]);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address not found on frontpage.');
 117      // Enable anonymizing of IP addresses.
 118      variable_set('googleanalytics_tracker_anonymizeip', 1);
 119      $this->drupalGet('');
 120      $this->assertRaw('_gaq.push(["_gat._anonymizeIp"]);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address found on frontpage.');
 121  
 122      // Test whether the BEFORE and AFTER code is added to the tracker.
 123      variable_set('googleanalytics_codesnippet_before', '_setDetectFlash(false);');
 124      variable_set('googleanalytics_codesnippet_after', '_gaq.push(["t2._setAccount", "UA-123456-3"]);_gaq.push(["t2._trackPageview"]);');
 125      $this->drupalGet('');
 126      $this->assertRaw('_setDetectFlash(false);', '[testGoogleAnalyticsTrackingCode]: Before codesnippet has been found with "Flash" detection disabled.');
 127      $this->assertRaw('t2._setAccount', '[testGoogleAnalyticsTrackingCode]: After codesnippet with "t2" tracker has been found.');
 128    }
 129  
 130  }
 131  
 132  class GoogleAnalyticsRolesTest extends DrupalWebTestCase {
 133  
 134    public static function getInfo() {
 135      return array(
 136        'name' => t('Google Analytics role tests'),
 137        'description' => t('Test roles functionality of Google Analytics module.'),
 138        'group' => 'Google Analytics',
 139      );
 140    }
 141  
 142    function setUp() {
 143      parent::setUp('googleanalytics');
 144  
 145      $permissions = array(
 146        'access administration pages',
 147        'administer google analytics',
 148      );
 149  
 150      // User to set up google_analytics.
 151      $this->admin_user = $this->drupalCreateUser($permissions);
 152    }
 153  
 154    function testGoogleAnalyticsRolesTracking() {
 155      $ua_code = 'UA-123456-4';
 156      variable_set('googleanalytics_account', $ua_code);
 157  
 158      // Test if the default settings are working as expected.
 159  
 160      // Add to the selected roles only.
 161      variable_set('googleanalytics_visibility_roles', 0);
 162      // Enable tracking for all users.
 163      variable_set('googleanalytics_roles', array());
 164  
 165      // Check tracking code visibility.
 166      $this->drupalGet('');
 167      $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for anonymous users on frontpage with default settings.');
 168  
 169      $this->drupalLogin($this->admin_user);
 170  
 171      $this->drupalGet('');
 172      $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users on frontpage with default settings.');
 173      $this->drupalGet('admin');
 174      $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for authenticated users in admin section with default settings.');
 175  
 176      // Test if the non-default settings are working as expected.
 177  
 178      // Enable tracking only for authenticated users.
 179      variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
 180  
 181      $this->drupalGet('');
 182      $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users only on frontpage.');
 183  
 184      $this->drupalLogout();
 185      $this->drupalGet('');
 186      $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for anonymous users on frontpage.');
 187  
 188      // Add to every role except the selected ones.
 189      variable_set('googleanalytics_visibility_roles', 1);
 190      // Enable tracking for all users.
 191      variable_set('googleanalytics_roles', array());
 192  
 193      // Check tracking code visibility.
 194      $this->drupalGet('');
 195      $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed for anonymous users.');
 196  
 197      $this->drupalLogin($this->admin_user);
 198  
 199      $this->drupalGet('');
 200      $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed on frontpage for authenticated users.');
 201      $this->drupalGet('admin');
 202      $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and NOT displayed in admin section for authenticated users.');
 203  
 204      // Disable tracking for authenticated users.
 205      variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
 206  
 207      $this->drupalGet('');
 208      $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed on frontpage for excluded authenticated users.');
 209      $this->drupalGet('admin');
 210      $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed in admin section for excluded authenticated users.');
 211  
 212      $this->drupalLogout();
 213      $this->drupalGet('');
 214      $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.');
 215    }
 216  
 217  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7