You are here

public function MatomoStatusMessagesTest::testMatomoStatusMessages in Matomo Analytics 8

Tests if status messages tracking is properly added to the page.

File

tests/src/Functional/MatomoStatusMessagesTest.php, line 46

Class

MatomoStatusMessagesTest
Test status messages functionality of Matomo module.

Namespace

Drupal\Tests\matomo\Functional

Code

public function testMatomoStatusMessages() {
  $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();

  // Enable logging of errors only.
  $this
    ->config('matomo.settings')
    ->set('track.messages', [
    'error' => 'error',
  ])
    ->save();
  $this
    ->drupalPostForm('user/login', [], 'Log in');
  $this
    ->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Username field is required."]);', '[testMatomoStatusMessages]: trackEvent "Username field is required." is shown.');
  $this
    ->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Password field is required."]);', '[testMatomoStatusMessages]: trackEvent "Password field is required." is shown.');

  // Testing this Drupal::messenger() requires an extra test module.
  $this
    ->drupalGet('matomo-test/drupal-messenger-add-message');
  $this
    ->assertNoRaw('_paq.push(["trackEvent", "Messages", "Status message", "Example status message."]);', '[testMatomoStatusMessages]: Example status message is not enabled for tracking.');
  $this
    ->assertNoRaw('_paq.push(["trackEvent", "Messages", "Warning message", "Example warning message."]);', '[testMatomoStatusMessages]: Example warning message is not enabled for tracking.');
  $this
    ->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Example error message."]);', '[testMatomoStatusMessages]: Example error message is shown.');
  $this
    ->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Example error message with html tags and link."]);', '[testMatomoStatusMessages]: HTML has been stripped successful from Example error message with html tags and link.');

  // Enable logging of status, warnings and errors.
  $this
    ->config('matomo.settings')
    ->set('track.messages', [
    'status' => 'status',
    'warning' => 'warning',
    'error' => 'error',
  ])
    ->save();
  $this
    ->drupalGet('matomo-test/drupal-messenger-add-message');
  $this
    ->assertRaw('_paq.push(["trackEvent", "Messages", "Status message", "Example status message."]);', '[testMatomoStatusMessages]: Example status message is enabled for tracking.');
  $this
    ->assertRaw('_paq.push(["trackEvent", "Messages", "Warning message", "Example warning message."]);', '[testMatomoStatusMessages]: Example warning message is enabled for tracking.');
  $this
    ->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Example error message."]);', '[testMatomoStatusMessages]: Example error message is shown.');
  $this
    ->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Example error message with html tags and link."]);', '[testMatomoStatusMessages]: HTML has been stripped successful from Example error message with html tags and link.');
}