View source
<?php
namespace Drupal\piwik\Tests;
use Drupal\Core\Session\AccountInterface;
use Drupal\simpletest\WebTestBase;
class PiwikBasicTest extends WebTestBase {
protected $noSnippetUser;
public static $modules = [
'piwik',
];
protected function setUp() {
parent::setUp();
$permissions = [
'access administration pages',
'administer piwik',
];
$this->noSnippetUser = $this
->drupalCreateUser($permissions);
$permissions[] = 'add JS snippets for piwik';
$this->admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->admin_user);
}
public function testPiwikConfiguration() {
$this
->drupalGet('admin/config/system/piwik');
$this
->assertRaw(t('Piwik site ID'), '[testPiwikConfiguration]: Settings page displayed.');
$edit['piwik_site_id'] = $this
->randomMachineName(2);
$edit['piwik_url_http'] = 'http://www.example.com/piwik/';
$this
->drupalPostForm('admin/config/system/piwik', $edit, 'Save configuration');
$this
->assertRaw(t('A valid Piwik site ID is an integer only.'), '[testPiwikConfiguration]: Invalid Piwik site ID number validated.');
$edit = [];
$edit['piwik_site_id'] = 1;
$edit['piwik_url_http'] = 'http://www.example.com/piwik/';
$edit['piwik_url_https'] = 'https://www.example.com/piwik/';
$this
->drupalPostForm('admin/config/system/piwik', $edit, t('Save configuration'));
$this
->assertRaw('The validation of "http://www.example.com/piwik/piwik.php" failed with an exception', '[testPiwikConfiguration]: HTTP URL exception shown.');
$this
->assertRaw('The validation of "https://www.example.com/piwik/piwik.php" failed with an exception', '[testPiwikConfiguration]: HTTPS URL exception shown.');
$this
->assertFieldByName('piwik_codesnippet_before');
$this
->assertFieldByName('piwik_codesnippet_after');
$this
->assertNoFieldByXPath("//textarea[@name='piwik_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is enabled.');
$this
->assertNoFieldByXPath("//textarea[@name='piwik_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is enabled.');
$this
->drupalLogin($this->noSnippetUser);
$this
->drupalGet('admin/config/system/piwik');
$this
->assertFieldByName('piwik_codesnippet_before');
$this
->assertFieldByName('piwik_codesnippet_after');
$this
->assertFieldByXPath("//textarea[@name='piwik_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is disabled.');
$this
->assertFieldByXPath("//textarea[@name='piwik_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is disabled.');
}
public function testPiwikPageVisibility() {
$site_id = '1';
$this
->config('piwik.settings')
->set('site_id', $site_id)
->save();
$this
->config('piwik.settings')
->set('url_http', 'http://www.example.com/piwik/')
->save();
$this
->config('piwik.settings')
->set('url_https', 'https://www.example.com/piwik/')
->save();
$this
->config('piwik.settings')
->set('visibility.request_path_mode', 0)
->save();
$this
->config('piwik.settings')
->set('visibility.request_path_pages', "/admin\n/admin/*")
->save();
$this
->config('piwik.settings')
->set('visibility.user_role_roles', [
AccountInterface::AUTHENTICATED_ROLE => AccountInterface::AUTHENTICATED_ROLE,
])
->save();
$this
->drupalGet('');
$this
->assertRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is displayed for authenticated users.');
$this
->drupalGet('admin');
$this
->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is not displayed on admin page.');
$this
->drupalGet('admin/config/system/piwik');
$this
->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is not displayed on admin subpage.');
$this
->config('piwik.settings')
->set('visibility.request_path_mode', 1)
->save();
$this
->drupalGet('admin');
$this
->assertRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is displayed on admin page.');
$this
->drupalGet('admin/config/system/piwik');
$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.');
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is NOT displayed for anonymous.');
$this
->config('piwik.settings')
->set('visibility.request_path_mode', 0)
->save();
$this
->config('piwik.settings')
->set('visibility.user_role_roles', [])
->save();
$this
->drupalGet('admin');
$this
->assertRaw('"403/URL = "', '[testPiwikPageVisibility]: 403 Forbidden tracking code shown if user has no access.');
$this
->drupalGet($this
->randomMachineName(64));
$this
->assertRaw('"404/URL = "', '[testPiwikPageVisibility]: 404 Not Found tracking code shown on non-existent page.');
}
public function testPiwikTrackingCode() {
$site_id = '2';
$this
->config('piwik.settings')
->set('site_id', $site_id)
->save();
$this
->config('piwik.settings')
->set('url_http', 'http://www.example.com/piwik/')
->save();
$this
->config('piwik.settings')
->set('url_https', 'https://www.example.com/piwik/')
->save();
$this
->config('piwik.settings')
->set('visibility.request_path_mode', 0)
->save();
$this
->config('piwik.settings')
->set('visibility.user_role_roles', [])
->save();
$this
->config('piwik.settings')
->set('cache', 0)
->save();
$this
->drupalGet('');
$this
->assertRaw('u+"piwik.php"', '[testPiwikTrackingCode]: Latest tracking code used.');
$this
->config('piwik.settings')
->set('track.userid', 1)
->save();
$this
->drupalGet('');
$this
->assertRaw('_paq.push(["setUserId", ', '[testPiwikTrackingCode]: Tracking code for User ID is enabled.');
$this
->config('piwik.settings')
->set('track.userid', 0)
->save();
$this
->drupalGet('');
$this
->assertNoRaw('_paq.push(["setUserId", ', '[testPiwikTrackingCode]: Tracking code for User ID is disabled.');
$this
->drupalGet('');
$this
->assertNoRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: Single domain tracking is active.');
$this
->config('piwik.settings')
->set('domain_mode', 1)
->save();
$this
->drupalGet('');
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 {
$this
->assertNoRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: One domain with multiple subdomains may be active on localhost (test result is not reliable).');
}
$this
->config('piwik.settings')
->set('codesnippet.before', '_paq.push(["setLinkTrackingTimer", 250]);')
->save();
$this
->config('piwik.settings')
->set('codesnippet.after', '_paq.push(["t2.setSiteId", 2]);if(1 == 1 && 2 < 3 && 2 > 1){console.log("Piwik: Custom condition works.");}_gaq.push(["t2.trackPageView"]);')
->save();
$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.');
$this
->assertRaw('if(1 == 1 && 2 < 3 && 2 > 1){console.log("Piwik: Custom condition works.");}', '[testPiwikTrackingCode]: JavaScript code is not HTML escaped.');
}
}