<?php
// $Id: googleanalytics_basic.test,v 1.7.2.3 2010/06/05 01:51:39 hass Exp $

/**
 * @file
 * Test file for Google Analytics module.
 */
class GoogleAnalyticsBasicTest extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name' => t('Google Analytics basic tests'),
      'description' => t('Test basic Google Analytics module functionality.'),
      'group' => 'Google Analytics',
    );
  }

  public function setUp() {
    parent::setUp('googleanalytics');

    $permissions = array('administer google analytics');

    // User to set up google_analytics.
    $user = $this->drupalCreateUser($permissions);
    $this->drupalLogin($user);
  }

  public function testGoogleAnalytics() {
    // Check for setting page's presence.
    $this->drupalGet('admin/settings/googleanalytics');
    $this->assertRaw(t('Google Analytics account number'), '[testGoogleAnalytics]: Settings page displayed.');

    // Check for account code validation.
    $edit['googleanalytics_account'] = $this->randomName(2);
    $this->drupalPost('admin/settings/googleanalytics', $edit, 'Save configuration');
    $this->assertRaw(t('A valid Google Analytics account number is case sensitive and formatted like UA-xxxxxx-x.'), '[testGoogleAnalytics]: Invalid account number validated.');
  }

  public function testGoogleAnalyticsTracking() {
    // Set visibility to hide tracking code on admin page only,
    // track authenticated users.
    variable_set('googleanalytics_visibility', 0);
    variable_set('googleanalytics_pages', 'admin*');
    variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
    $ua_code = 'UA-123456-7';
    variable_set('googleanalytics_account', $ua_code);

    // Check tracking code visibility.
    $this->drupalGet('');
    $this->assertRaw($ua_code, '[testGoogleAnalyticsTracking]: Tracking code is displayed for authenticated users.');

    /* Sample JS code as added to page:
    <script type="text/javascript" src="/sites/all/modules/google_analytics/googleanalytics.js?w"></script>
    <script type="text/javascript">
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-123456-7']);
      _gaq.push(['_trackPageview']);
    
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    </script>
    */

    // Test whether tracking code uses latest JS.
    variable_set('googleanalytics_cache', 0);
    $this->drupalGet('');
    $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTracking]: Latest tracking code used.');

    // Test whether tracking code is not included on pages to omit.
    $this->drupalGet('admin');
    $this->assertNoRaw($ua_code, '[testGoogleAnalyticsTracking]: Tracking code is not displayed on admin page.');
    $this->drupalGet('admin/settings/googleanalytics');
    // Checking for tracking code URI here, as $ua_code is displayed in the form.
    $this->assertNoRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTracking]: Tracking code is not displayed on admin subpage.');

    // Test whether tracking code display is properly flipped.
    variable_set('googleanalytics_visibility', 1);
    $this->drupalGet('admin');
    $this->assertRaw($ua_code, '[testGoogleAnalyticsTracking]: Tracking code is displayed on admin page.');
    $this->drupalGet('admin/settings/googleanalytics');
    // Checking for tracking code URI here, as $ua_code is displayed in the form.
    $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTracking]: Tracking code is displayed on admin subpage.');
    $this->drupalGet('');
    $this->assertNoRaw($ua_code, '[testGoogleAnalyticsTracking]: Tracking code is not displayed on front page.');

    // Test whether tracking code is not display for anonymous.
    $this->drupalGet('logout');
    $this->drupalGet('');
    $this->assertNoRaw($ua_code, '[testGoogleAnalyticsTracking]: Tracking code is not displayed for anonymous.');
  }

}
