You are here

public function GoogleAnalyticsStatusMessagesTest::testGoogleAnalyticsStatusMessages in Google Analytics 8.2

Same name and namespace in other branches
  1. 8.3 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 44

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', [], t('Log in'));
  $this
    ->assertSession()
    ->responseContains('ga("send", "event", "Messages", "Error message", "Username field is required.");');
  $this
    ->assertSession()
    ->responseContains('ga("send", "event", "Messages", "Error message", "Password field is required.");');

  // Testing this drupal_set_message() requires an extra test module.
  $this
    ->drupalGet('google-analytics-test/drupal-messenger-add-message');
  $this
    ->assertSession()
    ->responseNotContains('ga("send", "event", "Messages", "Status message", "Example status message.");');
  $this
    ->assertSession()
    ->responseNotContains('ga("send", "event", "Messages", "Warning message", "Example warning message.");');
  $this
    ->assertSession()
    ->responseContains('ga("send", "event", "Messages", "Error message", "Example error message.");');
  $this
    ->assertSession()
    ->responseContains('ga("send", "event", "Messages", "Error message", "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
    ->assertSession()
    ->responseContains('ga("send", "event", "Messages", "Status message", "Example status message.");');
  $this
    ->assertSession()
    ->responseContains('ga("send", "event", "Messages", "Warning message", "Example warning message.");');
  $this
    ->assertSession()
    ->responseContains('ga("send", "event", "Messages", "Error message", "Example error message.");');
  $this
    ->assertSession()
    ->responseContains('ga("send", "event", "Messages", "Error message", "Example error message with html tags and link.");');
}