t('Google Analytics basic tests'),
'description' => t('Test basic Google Analytics module functionality.'),
'group' => 'Google Analytics',
);
}
function setUp() {
parent::setUp('googleanalytics');
$permissions = array('administer google analytics');
// User to set up google_analytics.
$user = $this->drupalCreateUser($permissions);
$this->drupalLogin($user);
}
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 formated like UA-xxxxxx-x.'), '[testGoogleAnalytics]: Account number validated.');
}
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.');
/* Sample JS code as added to page:
*/
// Test whether tracking code uses latest JS.
variable_set('googleanalytics_cache', 0);
variable_set('googleanalytics_legacy_version', 0);
$this->drupalGet('');
$this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTracking]: Latest tracking code used.');
// Test whether tracking code uses legacy JS.
variable_set('googleanalytics_legacy_version', 1);
$this->drupalGet('');
$this->assertRaw('google-analytics.com/urchin.js', '[testGoogleAnalyticsTracking]: Legacy 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/urchin.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/urchin.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.');
}
}