You are here

public function GoogleAnalyticsUninstallTest::testGoogleAnalyticsUninstall in Google Analytics 4.x

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

Tests if the module cleans up the disk on uninstall.

File

tests/src/Functional/GoogleAnalyticsUninstallTest.php, line 58

Class

GoogleAnalyticsUninstallTest
Test uninstall functionality of Google Analytics module.

Namespace

Drupal\Tests\google_analytics\Functional

Code

public function testGoogleAnalyticsUninstall() {
  $cache_path = 'public://google_analytics';
  $ua_code = 'UA-123456-1';

  // Show tracker in pages.
  $this
    ->config('google_analytics.settings')
    ->set('account', $ua_code)
    ->save();

  // Enable local caching of gtag.js.
  $this
    ->config('google_analytics.settings')
    ->set('cache', 1)
    ->save();

  // Load page to get the gtag.js downloaded into local cache.
  $this
    ->drupalGet('');
  $file_system = \Drupal::service('file_system');

  // Test if the directory and gtag.js exists.
  $this
    ->assertTrue($file_system
    ->prepareDirectory($cache_path), 'Cache directory "public://google_analytics" has been found.');
  $this
    ->assertTrue(file_exists($cache_path . '/gtag.js'), 'Cached analytics.js tracking file has been found.');
  $this
    ->assertTrue(file_exists($cache_path . '/gtag.js.gz'), 'Cached analytics.js.gz tracking file has been found.');

  // Uninstall the module.
  $edit = [];
  $edit['uninstall[google_analytics]'] = TRUE;
  $this
    ->drupalPostForm('admin/modules/uninstall', $edit, $this
    ->t('Uninstall'));
  $this
    ->assertNoText(\Drupal::translation()
    ->translate('Configuration deletions'));
  $this
    ->drupalPostForm(NULL, NULL, $this
    ->t('Uninstall'));
  $this
    ->assertText($this
    ->t('The selected modules have been uninstalled.'));

  // Test if the directory and all files have been removed.
  $this
    ->assertFalse($file_system
    ->prepareDirectory($cache_path), 'Cache directory "public://google_analytics" has been removed.');
}