You are here

public function GoogleAnalyticsRolesTest::testGoogleAnalyticsRolesTracking in Google Analytics 4.x

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/GoogleAnalyticsRolesTest.php \Drupal\Tests\google_analytics\Functional\GoogleAnalyticsRolesTest::testGoogleAnalyticsRolesTracking()
  2. 8.2 tests/src/Functional/GoogleAnalyticsRolesTest.php \Drupal\Tests\google_analytics\Functional\GoogleAnalyticsRolesTest::testGoogleAnalyticsRolesTracking()

Tests if roles based tracking works.

File

tests/src/Functional/GoogleAnalyticsRolesTest.php, line 54

Class

GoogleAnalyticsRolesTest
Test roles functionality of Google Analytics module.

Namespace

Drupal\Tests\google_analytics\Functional

Code

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

  // Test if the default settings are working as expected.
  // Add to the selected roles only.
  $this
    ->config('google_analytics.settings')
    ->set('visibility.user_role_mode', 0)
    ->save();

  // Enable tracking for all users.
  $this
    ->config('google_analytics.settings')
    ->set('visibility.user_role_roles', [])
    ->save();

  // Check tracking code visibility.
  $this
    ->drupalGet('');
  $this
    ->assertRaw($ua_code);
  $this
    ->drupalGet('admin');
  $this
    ->assertResponse(403);
  $this
    ->assertRaw('/403.html');
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('');
  $this
    ->assertRaw($ua_code);
  $this
    ->drupalGet('admin');
  $this
    ->assertNoRaw($ua_code);

  // Test if the non-default settings are working as expected.
  // Enable tracking only for authenticated users.
  $this
    ->config('google_analytics.settings')
    ->set('visibility.user_role_roles', [
    AccountInterface::AUTHENTICATED_ROLE => AccountInterface::AUTHENTICATED_ROLE,
  ])
    ->save();
  $this
    ->drupalGet('');
  $this
    ->assertRaw($ua_code);
  $this
    ->drupalLogout();
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw($ua_code);

  // Add to every role except the selected ones.
  $this
    ->config('google_analytics.settings')
    ->set('visibility.user_role_mode', 1)
    ->save();

  // Enable tracking for all users.
  $this
    ->config('google_analytics.settings')
    ->set('visibility.user_role_roles', [])
    ->save();

  // Check tracking code visibility.
  $this
    ->drupalGet('');
  $this
    ->assertRaw($ua_code);
  $this
    ->drupalGet('admin');
  $this
    ->assertResponse(403);
  $this
    ->assertRaw('/403.html');
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('');
  $this
    ->assertRaw($ua_code);
  $this
    ->drupalGet('admin');
  $this
    ->assertNoRaw($ua_code);

  // Disable tracking for authenticated users.
  $this
    ->config('google_analytics.settings')
    ->set('visibility.user_role_roles', [
    AccountInterface::AUTHENTICATED_ROLE => AccountInterface::AUTHENTICATED_ROLE,
  ])
    ->save();
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw($ua_code);
  $this
    ->drupalGet('admin');
  $this
    ->assertNoRaw($ua_code);
  $this
    ->drupalLogout();
  $this
    ->drupalGet('');
  $this
    ->assertRaw($ua_code);
}