You are here

public function PiwikBasicTest::testPiwikPageVisibility in Piwik Web Analytics 8

Tests if page visibility works.

File

src/Tests/PiwikBasicTest.php, line 90

Class

PiwikBasicTest
Test basic functionality of Piwik module.

Namespace

Drupal\piwik\Tests

Code

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();

  // Show tracking on "every page except the listed pages".
  $this
    ->config('piwik.settings')
    ->set('visibility.request_path_mode', 0)
    ->save();

  // Disable tracking one "admin*" pages only.
  $this
    ->config('piwik.settings')
    ->set('visibility.request_path_pages', "/admin\n/admin/*")
    ->save();

  // Enable tracking only for authenticated users only.
  $this
    ->config('piwik.settings')
    ->set('visibility.user_role_roles', [
    AccountInterface::AUTHENTICATED_ROLE => AccountInterface::AUTHENTICATED_ROLE,
  ])
    ->save();

  // 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/config/system/piwik');

  // Checking for tracking URI here, as $site_id 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.
  $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');

  // Checking for tracking URI here, as $site_id 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.
  $this
    ->config('piwik.settings')
    ->set('visibility.request_path_mode', 0)
    ->save();

  // Enable tracking code for all user roles.
  $this
    ->config('piwik.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 = "', '[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
    ->randomMachineName(64));
  $this
    ->assertRaw('"404/URL = "', '[testPiwikPageVisibility]: 404 Not Found tracking code shown on non-existent page.');
}