GoogleAnalyticsUninstallTest.php in Google Analytics 4.x
File
tests/src/Functional/GoogleAnalyticsUninstallTest.php
View source
<?php
namespace Drupal\Tests\google_analytics\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;
class GoogleAnalyticsUninstallTest extends BrowserTestBase {
use StringTranslationTrait;
public static $modules = [
'google_analytics',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected function setUp() {
parent::setUp();
$permissions = [
'access administration pages',
'administer google analytics',
'administer modules',
];
$this->adminUser = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->adminUser);
}
public function testGoogleAnalyticsUninstall() {
$cache_path = 'public://google_analytics';
$ua_code = 'UA-123456-1';
$this
->config('google_analytics.settings')
->set('account', $ua_code)
->save();
$this
->config('google_analytics.settings')
->set('cache', 1)
->save();
$this
->drupalGet('');
$file_system = \Drupal::service('file_system');
$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.');
$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.'));
$this
->assertFalse($file_system
->prepareDirectory($cache_path), 'Cache directory "public://google_analytics" has been removed.');
}
}