View source
<?php
namespace Drupal\Tests\google_tag\Functional;
use Drupal\google_tag\Entity\Container;
use Drupal\Tests\BrowserTestBase;
abstract class GTMTestBase extends BrowserTestBase {
protected static $modules = [
'google_tag',
];
protected $types = [
'script',
'noscript',
];
protected $basePath;
protected $adminUser;
protected $nonAdminUser;
protected $variables = [];
protected function setUp() {
$this->defaultTheme = 'stark';
parent::setUp();
$this->basePath = $this
->config('google_tag.settings')
->get('uri');
}
public function testModule() {
try {
$this
->modifySettings();
$this
->createData();
$this
->saveContainers();
$this
->checkSnippetContents();
$this
->checkPageResponse();
$this
->deleteContainers();
$this
->submitContainers();
$this
->checkSnippetContents();
$this
->checkPageResponse();
$this
->modifySettings(FALSE);
$this
->checkPageResponse();
} catch (\Exception $e) {
parent::assertTrue(TRUE, t('Inside CATCH block'));
watchdog_exception('gtm_test', $e);
} finally {
parent::assertTrue(TRUE, t('Inside FINALLY block'));
}
}
protected function modifySettings($include_file = TRUE) {
$config = $this
->config('google_tag.settings');
$settings = $config
->get();
unset($settings['_core']);
$settings['include_file'] = $include_file;
$settings['flush_snippets'] = 1;
$settings['debug_output'] = 1;
$settings['_default_container']['role_toggle'] = 'include listed';
$settings['_default_container']['role_list'] = [
'content viewer' => 'content viewer',
];
$config
->setData($settings)
->save();
}
protected function createData() {
$this
->drupalCreateRole([
'access content',
'administer google tag manager',
], 'admin user');
$this->adminUser = $this
->drupalCreateUser();
$this->adminUser->roles[] = 'admin user';
$this->adminUser
->save();
$this
->drupalCreateRole([
'access content',
], 'content viewer');
$this->nonAdminUser = $this
->drupalCreateUser();
$this->nonAdminUser->roles[] = 'content viewer';
$this->nonAdminUser
->save();
}
protected function saveContainers() {
foreach ($this->variables as $key => $variables) {
$container = new Container([], 'google_tag_container');
$container
->enforceIsNew();
$container
->set('id', $variables->id);
$values = (array) $variables;
unset($values['id']);
array_walk($values, function ($value, $key) use ($container) {
$container->{$key} = $value;
});
$container
->save();
$manager = $this->container
->get('google_tag.container_manager');
$manager
->createAssets($container);
}
}
protected function deleteContainers() {
foreach ($this->variables as $key => $variables) {
$container = $this->container
->get('entity_type.manager')
->getStorage('google_tag_container')
->load($key);
$container
->delete();
}
$manager = $this->container
->get('google_tag.container_manager');
$ids = $manager
->loadContainerIDs();
$message = 'No containers found after delete';
parent::assertTrue(empty($ids), $message);
$directory = $this
->config('google_tag.settings')
->get('uri');
if ($this
->config('google_tag.settings')
->get('flush_snippets')) {
if (!empty($directory)) {
$this->container
->get('file_system')
->deleteRecursive($directory . '/google_tag');
}
}
$message = 'No snippet files found after delete';
parent::assertDirectoryNotExists($directory . '/google_tag', $message);
}
protected function submitContainers() {
$this
->drupalLogin($this->adminUser);
foreach ($this->variables as $key => $variables) {
$edit = (array) $variables;
$this
->drupalPostForm('/admin/config/system/google-tag/add', $edit, 'Save');
$text = 'Created @count snippet files for %container container based on configuration.';
$args = [
'@count' => 3,
'%container' => $variables->label,
];
$text = t($text, $args);
$this
->assertSession()
->responseContains($text);
$text = 'Created @count snippet files for @container container based on configuration.';
$args = [
'@count' => 3,
'@container' => $variables->label,
];
$text = t($text, $args);
$this
->assertSession()
->pageTextContains($text);
}
}
protected function getSnippetFromFile($key, $type) {
$url = "{$this->basePath}/google_tag/{$key}/google_tag.{$type}.js";
return @file_get_contents($url);
}
protected function getSnippetFromCache($key, $type) {
$cid = "google_tag:{$type}:{$key}";
$cache = $this->container
->get('cache.data')
->get($cid);
return $cache ? $cache->data : '';
}
protected function checkSnippetContents() {
}
protected function verifyScriptSnippet($contents, $variables) {
$status = strpos($contents, "'{$variables->container_id}'") !== FALSE;
$message = 'Found in script snippet file: container_id';
parent::assertTrue($status, $message);
$status = strpos($contents, "gtm_preview={$variables->environment_id}") !== FALSE;
$message = 'Found in script snippet file: environment_id';
parent::assertTrue($status, $message);
$status = strpos($contents, "gtm_auth={$variables->environment_token}") !== FALSE;
$message = 'Found in script snippet file: environment_token';
parent::assertTrue($status, $message);
}
protected function verifyNoScriptSnippet($contents, $variables) {
$status = strpos($contents, "id={$variables->container_id}") !== FALSE;
$message = 'Found in noscript snippet cache: container_id';
parent::assertTrue($status, $message);
$status = strpos($contents, "gtm_preview={$variables->environment_id}") !== FALSE;
$message = 'Found in noscript snippet cache: environment_id';
parent::assertTrue($status, $message);
$status = strpos($contents, "gtm_auth={$variables->environment_token}") !== FALSE;
$message = 'Found in noscript snippet cache: environment_token';
parent::assertTrue($status, $message);
}
protected function verifyDataLayerSnippet($contents, $variables) {
}
protected function checkPageResponse() {
$this
->drupalLogin($this->nonAdminUser);
}
protected function verifyScriptTag($realpath) {
$query_string = $this->container
->get('state')
->get('system.css_js_query_string') ?: '0';
$text = "src=\"{$realpath}?{$query_string}\"";
$this
->assertSession()
->responseContains($text);
$xpath = "//script[@src=\"{$realpath}?{$query_string}\"]";
$elements = $this
->xpath($xpath);
$status = !empty($elements);
$message = 'Found script tag in page response';
parent::assertTrue($status, $message);
}
protected function verifyScriptTagInline($variables, $cache) {
$id = $variables->container_id;
$xpath = "//script[contains(text(), '{$id}')]";
$elements = $this
->xpath($xpath);
if (!is_array($elements) || count($elements) > 1) {
$message = 'Found only one script tag';
parent::assertFalse($status, $message);
return;
}
$contents = $elements[0]
->getHtml();
$status = strpos($contents, "(window,document,'script','dataLayer','{$id}')") !== FALSE;
$message = 'Found in script tag: container_id and data data_layer';
parent::assertTrue($status, $message);
$status = strpos($contents, "gtm_preview={$variables->environment_id}") !== FALSE;
$message = 'Found in script tag: environment_id';
parent::assertTrue($status, $message);
$status = strpos($contents, "gtm_auth={$variables->environment_token}") !== FALSE;
$message = 'Found in script tag: environment_token';
parent::assertTrue($status, $message);
$message = 'Contents of script tag matches cache';
parent::assertTrue($contents == $cache, $message);
}
protected function verifyNoScriptTag($realpath, $variables, $cache = '') {
$index = isset($variables->weight) ? $variables->weight - 1 : 0;
$xpath = '//noscript//iframe';
$elements = $this
->xpath($xpath);
$contents = $elements[$index]
->getAttribute('src');
$status = strpos($contents, "id={$variables->container_id}") !== FALSE;
$message = 'Found in noscript tag: container_id';
parent::assertTrue($status, $message);
$status = strpos($contents, "gtm_preview={$variables->environment_id}") !== FALSE;
$message = 'Found in noscript tag: environment_id';
parent::assertTrue($status, $message);
$status = strpos($contents, "gtm_auth={$variables->environment_token}") !== FALSE;
$message = 'Found in noscript tag: environment_token';
parent::assertTrue($status, $message);
if ($cache) {
$message = 'Contents of noscript tag matches cache';
parent::assertTrue(strpos($cache, $contents) !== FALSE, $message);
}
}
protected function verifyNoScriptTagInline($variables, $cache) {
$this
->verifyNoScriptTag('', $variables, $cache);
}
}