View source
<?php
namespace Drupal\Tests\matomo\Functional;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\BrowserTestBase;
class MatomoBasicTest extends BrowserTestBase {
protected $noSnippetUser;
public static $modules = [
'matomo',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$permissions = [
'access administration pages',
'administer matomo',
];
$this->noSnippetUser = $this
->drupalCreateUser($permissions);
$permissions[] = 'add js snippets for matomo';
$this->admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->admin_user);
}
public function testMatomoConfiguration() {
$this
->drupalGet('admin/config/system/matomo');
$this
->assertRaw('Matomo site ID', '[testMatomoConfiguration]: Settings page displayed.');
$edit['matomo_site_id'] = $this
->randomMachineName(2);
$edit['matomo_url_http'] = 'http://www.example.com/matomo/';
$this
->drupalPostForm('admin/config/system/matomo', $edit, 'Save configuration');
$this
->assertRaw('A valid Matomo site ID is an integer only.', '[testMatomoConfiguration]: Invalid Matomo site ID number validated.');
$edit = [];
$edit['matomo_site_id'] = 1;
$edit['matomo_url_http'] = 'http://www.example.com/matomo/';
$edit['matomo_url_https'] = 'https://www.example.com/matomo/';
$this
->drupalPostForm('admin/config/system/matomo', $edit, 'Save configuration');
$this
->assertRaw('The validation of "http://www.example.com/matomo/matomo.php" failed with an exception', '[testMatomoConfiguration]: HTTP URL exception shown.');
$this
->assertRaw('The validation of "https://www.example.com/matomo/matomo.php" failed with an exception', '[testMatomoConfiguration]: HTTPS URL exception shown.');
$this
->assertFieldByName('matomo_codesnippet_before');
$this
->assertFieldByName('matomo_codesnippet_after');
$this
->assertNoFieldByXPath("//textarea[@name='matomo_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is enabled.');
$this
->assertNoFieldByXPath("//textarea[@name='matomo_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is enabled.');
$this
->drupalLogin($this->noSnippetUser);
$this
->drupalGet('admin/config/system/matomo');
$this
->assertFieldByName('matomo_codesnippet_before');
$this
->assertFieldByName('matomo_codesnippet_after');
$this
->assertFieldByXPath("//textarea[@name='matomo_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is disabled.');
$this
->assertFieldByXPath("//textarea[@name='matomo_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is disabled.');
}
public function testMatomoPageVisibility() {
$site_id = '1';
$this
->config('matomo.settings')
->set('site_id', $site_id)
->save();
$this
->config('matomo.settings')
->set('url_http', 'http://www.example.com/matomo/')
->save();
$this
->config('matomo.settings')
->set('url_https', 'https://www.example.com/matomo/')
->save();
$this
->config('matomo.settings')
->set('visibility.request_path_mode', 0)
->save();
$this
->config('matomo.settings')
->set('visibility.request_path_pages', "/admin\n/admin/*")
->save();
$this
->config('matomo.settings')
->set('visibility.user_role_roles', [
AccountInterface::AUTHENTICATED_ROLE => AccountInterface::AUTHENTICATED_ROLE,
])
->save();
$this
->drupalGet('');
$this
->assertRaw('/matomo/js/matomo.js', '[testMatomoPageVisibility]: Custom tracking script is is displayed for authenticated users.');
$this
->assertRaw('u+"matomo.php"', '[testMatomoPageVisibility]: Tracking code is displayed for authenticated users.');
$this
->drupalGet('admin');
$this
->assertNoRaw('u+"matomo.php"', '[testMatomoPageVisibility]: Tracking code is not displayed on admin page.');
$this
->drupalGet('admin/config/system/matomo');
$this
->assertNoRaw('u+"matomo.php"', '[testMatomoPageVisibility]: Tracking code is not displayed on admin subpage.');
$this
->config('matomo.settings')
->set('visibility.request_path_mode', 1)
->save();
$this
->drupalGet('admin');
$this
->assertRaw('u+"matomo.php"', '[testMatomoPageVisibility]: Tracking code is displayed on admin page.');
$this
->drupalGet('admin/config/system/matomo');
$this
->assertRaw('u+"matomo.php"', '[testMatomoPageVisibility]: Tracking code is displayed on admin subpage.');
$this
->drupalGet('');
$this
->assertNoRaw('u+"matomo.php"', '[testMatomoPageVisibility]: Tracking code is NOT displayed on front page.');
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertNoRaw('u+"matomo.php"', '[testMatomoPageVisibility]: Tracking code is NOT displayed for anonymous.');
$this
->config('matomo.settings')
->set('visibility.request_path_mode', 0)
->save();
$this
->config('matomo.settings')
->set('visibility.user_role_roles', [])
->save();
$this
->drupalGet('admin');
$this
->assertRaw('"403/URL = "', '[testMatomoPageVisibility]: 403 Forbidden tracking code shown if user has no access.');
$this
->drupalGet($this
->randomMachineName(64));
$this
->assertRaw('"404/URL = "', '[testMatomoPageVisibility]: 404 Not Found tracking code shown on non-existent page.');
}
public function testMatomoTrackingCode() {
$site_id = '2';
$this
->config('matomo.settings')
->set('site_id', $site_id)
->save();
$this
->config('matomo.settings')
->set('url_http', 'http://www.example.com/matomo/')
->save();
$this
->config('matomo.settings')
->set('url_https', 'https://www.example.com/matomo/')
->save();
$this
->config('matomo.settings')
->set('visibility.request_path_mode', 0)
->save();
$this
->config('matomo.settings')
->set('visibility.user_role_roles', [])
->save();
$this
->config('matomo.settings')
->set('cache', 0)
->save();
$this
->drupalGet('');
$this
->assertRaw('u+"matomo.php"', '[testMatomoTrackingCode]: Latest tracking code used.');
$this
->config('matomo.settings')
->set('track.userid', 1)
->save();
$this
->drupalGet('');
$this
->assertRaw('_paq.push(["setUserId", ', '[testMatomoTrackingCode]: Tracking code for User ID is enabled.');
$this
->config('matomo.settings')
->set('track.userid', 0)
->save();
$this
->drupalGet('');
$this
->assertNoRaw('_paq.push(["setUserId", ', '[testMatomoTrackingCode]: Tracking code for User ID is disabled.');
$this
->drupalGet('');
$this
->assertNoRaw('_paq.push(["setCookieDomain"', '[testMatomoTrackingCode]: Single domain tracking is active.');
$this
->config('matomo.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"', '[testMatomoTrackingCode]: One domain with multiple subdomains is active on real host.');
}
else {
$this
->assertNoRaw('_paq.push(["setCookieDomain"', '[testMatomoTrackingCode]: One domain with multiple subdomains may be active on localhost (test result is not reliable).');
}
$this
->config('matomo.settings')
->set('codesnippet.before', '_paq.push(["setLinkTrackingTimer", 250]);')
->save();
$this
->config('matomo.settings')
->set('codesnippet.after', '_paq.push(["t2.setSiteId", 2]);if(1 == 1 && 2 < 3 && 2 > 1){console.log("Matomo: Custom condition works.");}_gaq.push(["t2.trackPageView"]);')
->save();
$this
->drupalGet('');
$this
->assertRaw('setLinkTrackingTimer', '[testMatomoTrackingCode]: Before codesnippet has been found with "setLinkTrackingTimer" set.');
$this
->assertRaw('t2.trackPageView', '[testMatomoTrackingCode]: After codesnippet with "t2" tracker has been found.');
$this
->assertRaw('if(1 == 1 && 2 < 3 && 2 > 1){console.log("Matomo: Custom condition works.");}', '[testMatomoTrackingCode]: JavaScript code is not HTML escaped.');
$this
->assertNoRaw('_paq.push(["disableCookies"]);', '[testMatomoTrackingCode]: Cookies not disabled by default.');
$this
->config('matomo.settings')
->set('privacy.disablecookies', TRUE)
->save();
$this
->drupalGet('');
$this
->assertRaw('_paq.push(["disableCookies"]);', '[testMatomoTrackingCode]: Cookies disabled.');
}
}