t('Piwik basic tests'),
'description' => t('Test basic functionality of Piwik module.'),
'group' => 'Piwik',
);
}
function setUp() {
parent::setUp('piwik');
$permissions = array(
'access administration pages',
'administer piwik',
);
// User to set up piwik.
$this->admin_user = $this->drupalCreateUser($permissions);
$this->drupalLogin($this->admin_user);
}
function testPiwikConfiguration() {
// Check for setting page's presence.
$this->drupalGet('admin/settings/piwik');
$this->assertRaw(t('Piwik site ID'), '[testPiwikConfiguration]: Settings page displayed.');
// Check for account code validation.
$edit['piwik_site_id'] = $this->randomName(2);
$this->drupalPost('admin/settings/piwik', $edit, 'Save configuration');
$this->assertRaw(t('A valid Piwik site ID is an integer only.'), '[testPiwikConfiguration]: Invalid Piwik site ID number validated.');
}
function testPiwikPageVisibility() {
$ua_code = '1';
variable_set('piwik_site_id', $ua_code);
variable_get('piwik_url_http', 'http://example.com/piwik/');
variable_get('piwik_url_https', 'https://example.com/piwik/');
// Show tracking on "every page except the listed pages".
variable_set('piwik_visibility_pages', 0);
// Disable tracking one "admin*" pages only.
variable_set('piwik_pages', "admin\nadmin/*");
// Enable tracking only for authenticated users only.
variable_set('piwik_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
// Check tracking code visibility.
$this->drupalGet('');
$this->assertRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is displayed for authenticated users.');
// Test whether tracking code is not included on pages to omit.
$this->drupalGet('admin');
$this->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is not displayed on admin page.');
$this->drupalGet('admin/settings/piwik');
// Checking for tracking code URI here, as $ua_code is displayed in the form.
$this->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is not displayed on admin subpage.');
// Test whether tracking code display is properly flipped.
variable_set('piwik_visibility_pages', 1);
$this->drupalGet('admin');
$this->assertRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is displayed on admin page.');
$this->drupalGet('admin/settings/piwik');
// Checking for tracking code URI here, as $ua_code is displayed in the form.
$this->assertRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is displayed on admin subpage.');
$this->drupalGet('');
$this->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is NOT displayed on front page.');
// Test whether tracking code is not display for anonymous.
$this->drupalLogout();
$this->drupalGet('');
$this->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is NOT displayed for anonymous.');
// Switch back to every page except the listed pages.
variable_set('piwik_visibility_pages', 0);
// Enable tracking code for all user roles.
variable_set('piwik_roles', array());
// Test whether 403 forbidden tracking code is shown if user has no access.
//$this->drupalGet('admin');
//$this->assertRaw('"403/URL = "', '[testPiwikPageVisibility]: 403 Forbidden tracking code shown if user has no access.');
// Test whether 404 not found tracking code is shown on non-existent pages.
$this->drupalGet($this->randomName(64));
$this->assertRaw('"404/URL = "', '[testPiwikPageVisibility]: 404 Not Found tracking code shown on non-existent page.');
}
function testPiwikTrackingCode() {
$ua_code = '2';
variable_set('piwik_site_id', $ua_code);
variable_get('piwik_url_http', 'http://example.com/piwik/');
variable_get('piwik_url_https', 'https://example.com/piwik/');
// Show tracking code on every page except the listed pages.
variable_set('piwik_visibility_pages', 0);
// Enable tracking code for all user roles.
variable_set('piwik_roles', array());
/* Sample JS code as added to page:
*/
// Test whether tracking code uses latest JS.
variable_set('piwik_cache', 0);
$this->drupalGet('');
$this->assertRaw('u+"piwik.php"', '[testPiwikTrackingCode]: Latest tracking code used.');
// Test if tracking of User ID is enabled.
variable_set('piwik_trackuserid', 1);
$this->drupalGet('');
$this->assertRaw('_paq.push(["setUserId", ', '[testPiwikTrackingCode]: Tracking code for User ID is enabled.');
// Test if tracking of User ID is disabled.
variable_set('piwik_trackuserid', 0);
$this->drupalGet('');
$this->assertNoRaw('_paq.push(["setUserId", ', '[testPiwikTrackingCode]: Tracking code for User ID is disabled.');
// Test whether single domain tracking is active.
$this->drupalGet('');
$this->assertNoRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: Single domain tracking is active.');
// Enable "One domain with multiple subdomains".
variable_set('piwik_domain_mode', 1);
$this->drupalGet('');
// Test may run on localhost, an ipaddress or real domain name.
// TODO: Workaround to run tests successfully. This feature cannot tested reliable.
global $cookie_domain;
if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
$this->assertRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: One domain with multiple subdomains is active on real host.');
}
else {
// Special cases, Localhost and IP addresses don't show 'setCookieDomain'.
$this->assertNoRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: One domain with multiple subdomains may be active on localhost (test result is not reliable).');
}
// Test whether the BEFORE and AFTER code is added to the tracker.
variable_set('piwik_codesnippet_before', '_paq.push(["setLinkTrackingTimer", 250]);');
variable_set('piwik_codesnippet_after', '_paq.push(["t2.setSiteId", 2]);_gaq.push(["t2.trackPageView"]);');
$this->drupalGet('');
$this->assertRaw('setLinkTrackingTimer', '[testPiwikTrackingCode]: Before codesnippet has been found with "setLinkTrackingTimer" set.');
$this->assertRaw('t2.trackPageView', '[testPiwikTrackingCode]: After codesnippet with "t2" tracker has been found.');
}
}
class PiwikRolesTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => t('Piwik role tests'),
'description' => t('Test roles functionality of Piwik module.'),
'group' => 'Piwik',
);
}
function setUp() {
parent::setUp('piwik');
$permissions = array(
'access administration pages',
'administer piwik',
);
// User to set up piwik.
$this->admin_user = $this->drupalCreateUser($permissions);
}
function testPiwikRolesTracking() {
$ua_code = '1';
variable_set('piwik_site_id', $ua_code);
variable_get('piwik_url_http', 'http://example.com/piwik/');
variable_get('piwik_url_https', 'https://example.com/piwik/');
// Test if the default settings are working as expected.
// Add to the selected roles only.
variable_set('piwik_visibility_roles', 0);
// Enable tracking for all users.
variable_set('piwik_roles', array());
// Check tracking code visibility.
$this->drupalGet('');
$this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed for anonymous users on frontpage with default settings.');
//$this->drupalGet('admin');
//$this->assertRaw('"403/URL = "', '[testPiwikRoleVisibility]: 403 Forbidden tracking code is displayed for anonymous users in admin section with default settings.');
$this->drupalLogin($this->admin_user);
$this->drupalGet('');
$this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed for authenticated users on frontpage with default settings.');
$this->drupalGet('admin');
$this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is NOT displayed for authenticated users in admin section with default settings.');
// Test if the non-default settings are working as expected.
// Enable tracking only for authenticated users.
variable_set('piwik_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
$this->drupalGet('');
$this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed for authenticated users only on frontpage.');
$this->drupalLogout();
$this->drupalGet('');
$this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is NOT displayed for anonymous users on frontpage.');
// Add to every role except the selected ones.
variable_set('piwik_visibility_roles', 1);
// Enable tracking for all users.
variable_set('piwik_roles', array());
// Check tracking code visibility.
$this->drupalGet('');
$this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is added to every role and displayed for anonymous users.');
//$this->drupalGet('admin');
//$this->assertRaw('"403/URL = "', '[testPiwikRoleVisibility]: 403 Forbidden tracking code is shown for anonymous users if every role except the selected ones is selected.');
$this->drupalLogin($this->admin_user);
$this->drupalGet('');
$this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is added to every role and displayed on frontpage for authenticated users.');
$this->drupalGet('admin');
$this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is added to every role and NOT displayed in admin section for authenticated users.');
// Disable tracking for authenticated users.
variable_set('piwik_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
$this->drupalGet('');
$this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is NOT displayed on frontpage for excluded authenticated users.');
$this->drupalGet('admin');
$this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is NOT displayed in admin section for excluded authenticated users.');
$this->drupalLogout();
$this->drupalGet('');
$this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.');
}
}
class PiwikPhpFilterTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Piwik php filter tests',
'description' => 'Test php filter functionality of Piwik module.',
'group' => 'Piwik',
);
}
function setUp() {
parent::setUp('piwik', 'php');
// Administrator with all permissions.
$permissions_admin_user = array(
'access administration pages',
'administer piwik',
'use PHP for tracking visibility',
);
$this->admin_user = $this->drupalCreateUser($permissions_admin_user);
// Administrator who cannot configure tracking visibility with PHP.
$permissions_delegated_admin_user = array(
'access administration pages',
'administer piwik',
);
$this->delegated_admin_user = $this->drupalCreateUser($permissions_delegated_admin_user);
}
function testPiwikPhpFilter() {
$ua_code = '1';
$this->drupalLogin($this->admin_user);
$edit = array();
$edit['piwik_site_id'] = $ua_code;
$edit['piwik_url_http'] = 'http://example.com/piwik/';
$edit['piwik_url_https'] = 'https://example.com/piwik/';
$edit['piwik_url_skiperror'] = TRUE; // Required for testing only.
$edit['piwik_visibility_pages'] = 2;
$edit['piwik_pages'] = '';
$this->drupalPost('admin/settings/piwik', $edit, t('Save configuration'));
// Compare saved setting with posted setting.
$piwik_pages = variable_get('piwik_pages', $this->randomName(8));
$this->assertEqual('', $piwik_pages, '[testPiwikPhpFilter]: PHP code snippet is intact.');
// Check tracking code visibility.
variable_set('piwik_pages', '');
$this->drupalGet('');
$this->assertRaw('u+"piwik.php"', '[testPiwikPhpFilter]: Tracking is displayed on frontpage page.');
$this->drupalGet('admin');
$this->assertRaw('u+"piwik.php"', '[testPiwikPhpFilter]: Tracking is displayed on admin page.');
variable_set('piwik_pages', '');
$this->drupalGet('');
$this->assertNoRaw('u+"piwik.php"', '[testPiwikPhpFilter]: Tracking is not displayed on frontpage page.');
// Test administration form.
variable_set('piwik_pages', '');
$this->drupalGet('admin/settings/piwik');
$this->assertRaw(t('Add if the following PHP code returns TRUE
(PHP-mode, experts only).'), '[testPiwikPhpFilter]: Permission to administer PHP for tracking visibility.');
$this->assertRaw(check_plain(''), '[testPiwikPhpFilter]: PHP code snippted is displayed.');
// Login the delegated user and check if fields are visible.
$this->drupalLogin($this->delegated_admin_user);
$this->drupalGet('admin/settings/piwik');
$this->assertNoRaw(t('Add if the following PHP code returns TRUE
(PHP-mode, experts only).'), '[testPiwikPhpFilter]: No permission to administer PHP for tracking visibility.');
$this->assertNoRaw(check_plain(''), '[testPiwikPhpFilter]: No permission to view PHP code snippted.');
// Set a different value and verify that this is still the same after the post.
variable_set('piwik_pages', '');
$edit = array();
$edit['piwik_site_id'] = $ua_code;
$edit['piwik_url_http'] = 'http://example.com/piwik/';
$edit['piwik_url_https'] = 'https://example.com/piwik/';
$edit['piwik_url_skiperror'] = TRUE; // Required for testing only.
$this->drupalPost('admin/settings/piwik', $edit, t('Save configuration'));
// Compare saved setting with posted setting.
$piwik_visibility_pages = variable_get('piwik_visibility_pages', 0);
$piwik_pages = variable_get('piwik_pages', $this->randomName(8));
$this->assertEqual(2, $piwik_visibility_pages, '[testPiwikPhpFilter]: Pages on which this PHP code returns TRUE is selected.');
$this->assertEqual('', $piwik_pages, '[testPiwikPhpFilter]: PHP code snippet is intact.');
}
}