public function MatomoBasicTest::testMatomoPageVisibility in Matomo Analytics 8
Tests if page visibility works.
File
- tests/
src/ Functional/ MatomoBasicTest.php, line 97
Class
- MatomoBasicTest
- Test basic functionality of Matomo module.
Namespace
Drupal\Tests\matomo\FunctionalCode
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();
// Show tracking on "every page except the listed pages".
$this
->config('matomo.settings')
->set('visibility.request_path_mode', 0)
->save();
// Disable tracking one "admin*" pages only.
$this
->config('matomo.settings')
->set('visibility.request_path_pages', "/admin\n/admin/*")
->save();
// Enable tracking only for authenticated users only.
$this
->config('matomo.settings')
->set('visibility.user_role_roles', [
AccountInterface::AUTHENTICATED_ROLE => AccountInterface::AUTHENTICATED_ROLE,
])
->save();
// Check tracking code visibility.
$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.');
// Test whether tracking code is not included on pages to omit.
$this
->drupalGet('admin');
$this
->assertNoRaw('u+"matomo.php"', '[testMatomoPageVisibility]: Tracking code is not displayed on admin page.');
$this
->drupalGet('admin/config/system/matomo');
// Checking for tracking URI here, as $site_id is displayed in the form.
$this
->assertNoRaw('u+"matomo.php"', '[testMatomoPageVisibility]: Tracking code is not displayed on admin subpage.');
// Test whether tracking code display is properly flipped.
$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');
// Checking for tracking URI here, as $site_id is displayed in the form.
$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.');
// Test whether tracking code is not display for anonymous.
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertNoRaw('u+"matomo.php"', '[testMatomoPageVisibility]: Tracking code is NOT displayed for anonymous.');
// Switch back to every page except the listed pages.
$this
->config('matomo.settings')
->set('visibility.request_path_mode', 0)
->save();
// Enable tracking code for all user roles.
$this
->config('matomo.settings')
->set('visibility.user_role_roles', [])
->save();
// Test whether 403 forbidden tracking code is shown if user has no access.
$this
->drupalGet('admin');
$this
->assertRaw('"403/URL = "', '[testMatomoPageVisibility]: 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
->randomMachineName(64));
$this
->assertRaw('"404/URL = "', '[testMatomoPageVisibility]: 404 Not Found tracking code shown on non-existent page.');
}