You are here

public function GoogleAnalyticsStatusMessagesTest::testGoogleAnalyticsStatusMessages in Google Analytics 8.3

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/GoogleAnalyticsStatusMessagesTest.php \Drupal\Tests\google_analytics\Functional\GoogleAnalyticsStatusMessagesTest::testGoogleAnalyticsStatusMessages()
  2. 4.x tests/src/Functional/GoogleAnalyticsStatusMessagesTest.php \Drupal\Tests\google_analytics\Functional\GoogleAnalyticsStatusMessagesTest::testGoogleAnalyticsStatusMessages()

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

File

tests/src/Functional/GoogleAnalyticsStatusMessagesTest.php, line 56

Class

GoogleAnalyticsStatusMessagesTest
Test status messages functionality of Google Analytics module.

Namespace

Drupal\Tests\google_analytics\Functional

Code

public function testGoogleAnalyticsStatusMessages() {
  $ua_code = 'UA-123456-4';
  $this
    ->config('google_analytics.settings')
    ->set('account', $ua_code)
    ->save();

  // Enable logging of errors only.
  $this
    ->config('google_analytics.settings')
    ->set('track.messages', [
    'error' => 'error',
  ])
    ->save();
  $this
    ->drupalPostForm('user/login', [], $this
    ->t('Log in'));
  $this
    ->assertRaw('gtag("event", "Error message", {"event_category":"Messages","event_label":"Username field is required."});');
  $this
    ->assertRaw('gtag("event", "Error message", {"event_category":"Messages","event_label":"Password field is required."});');

  // Testing this drupal_set_message() requires an extra test module.
  $this
    ->drupalGet('google-analytics-test/drupal-messenger-add-message');
  $this
    ->assertNoRaw('gtag("event", "Status message", {"event_category":"Messages","event_label":"Example status message."});');
  $this
    ->assertNoRaw('gtag("event", "Warning message", {"event_category":"Messages","event_label":"Example warning message."});');
  $this
    ->assertRaw('gtag("event", "Error message", {"event_category":"Messages","event_label":"Example error message."});');
  $this
    ->assertRaw('gtag("event", "Error message", {"event_category":"Messages","event_label":"Example error message with html tags and link."});');

  // Enable logging of status, warnings and errors.
  $this
    ->config('google_analytics.settings')
    ->set('track.messages', [
    'status' => 'status',
    'warning' => 'warning',
    'error' => 'error',
  ])
    ->save();
  $this
    ->drupalGet('google-analytics-test/drupal-messenger-add-message');
  $this
    ->assertRaw('gtag("event", "Status message", {"event_category":"Messages","event_label":"Example status message."});');
  $this
    ->assertRaw('gtag("event", "Warning message", {"event_category":"Messages","event_label":"Example warning message."});');
  $this
    ->assertRaw('gtag("event", "Error message", {"event_category":"Messages","event_label":"Example error message."});');
  $this
    ->assertRaw('gtag("event", "Error message", {"event_category":"Messages","event_label":"Example error message with html tags and link."});');
}