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/config/system/googleanalytics');
$this->assertRaw(t('Web Property ID'), '[testGoogleAnalytics]: Settings page displayed.');
// Check for account code validation.
$edit['googleanalytics_account'] = $this->randomName(2);
$this->drupalPost('admin/config/system/googleanalytics', $edit, 'Save configuration');
$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.');
}
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:
*/
// 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/config/system/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/config/system/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('user/logout');
$this->drupalGet('');
$this->assertNoRaw($ua_code, '[testGoogleAnalyticsTracking]: Tracking code is not displayed for anonymous.');
}
}