You are here

public function GoogleAnalyticsBasicTest::testGoogleAnalyticsConfiguration in Google Analytics 4.x

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

Tests if configuration is possible.

File

tests/src/Functional/GoogleAnalyticsBasicTest.php, line 77

Class

GoogleAnalyticsBasicTest
Test basic functionality of Google Analytics module.

Namespace

Drupal\Tests\google_analytics\Functional

Code

public function testGoogleAnalyticsConfiguration() {

  // Check if Configure link is available on 'Extend' page.
  // Requires 'administer modules' permission.
  $this
    ->drupalGet('admin/modules');
  $this
    ->assertRaw('admin/config/services/google-analytics');

  // Check if Configure link is available on 'Status Reports' page.
  // NOTE: Link is only shown without UA code configured.
  // Requires 'administer site configuration' permission.
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertRaw('admin/config/services/google-analytics');

  // Check for setting page's presence.
  $this
    ->drupalGet('admin/config/services/google-analytics');
  $this
    ->assertRaw($this
    ->t('Web Property ID'));

  // Check for account code validation.
  $edit['google_analytics_account'] = $this
    ->randomMachineName(2);
  $this
    ->drupalPostForm('admin/config/services/google-analytics', $edit, $this
    ->t('Save configuration'));
  $this
    ->assertRaw($this
    ->t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'));

  // User should have access to code snippets.
  $this
    ->assertFieldByName('google_analytics_codesnippet_create');
  $this
    ->assertFieldByName('google_analytics_codesnippet_before');
  $this
    ->assertFieldByName('google_analytics_codesnippet_after');
  $this
    ->assertNoFieldByXPath("//textarea[@name='google_analytics_codesnippet_create' and @disabled='disabled']", NULL, '"Parameters" field is enabled.');
  $this
    ->assertNoFieldByXPath("//textarea[@name='google_analytics_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is enabled.');
  $this
    ->assertNoFieldByXPath("//textarea[@name='google_analytics_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is enabled.');

  // Login as user without JS permissions.
  $this
    ->drupalLogin($this->noSnippetUser);
  $this
    ->drupalGet('admin/config/services/google-analytics');

  // User should *not* have access to snippets, but parameters field.
  $this
    ->assertFieldByName('google_analytics_codesnippet_create');
  $this
    ->assertFieldByName('google_analytics_codesnippet_before');
  $this
    ->assertFieldByName('google_analytics_codesnippet_after');
  $this
    ->assertNoFieldByXPath("//textarea[@name='google_analytics_codesnippet_create' and @disabled='disabled']", NULL, '"Parameters" field is enabled.');
  $this
    ->assertFieldByXPath("//textarea[@name='google_analytics_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is disabled.');
  $this
    ->assertFieldByXPath("//textarea[@name='google_analytics_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is disabled.');
}