acquia_vwo-1.0.x-dev/tests/src/Functional/AcquiaVwoIntegrationTest.php
tests/src/Functional/AcquiaVwoIntegrationTest.php
<?php
namespace Drupal\Tests\acquia_vwo\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
// cspell:ignore user_opt nocontrol Pathconfig
// cspell:ignore Teste Pessoa Sessao Novo optout
/**
* Acquia VWO Integration Tests.
*
* @group acquia_vwo
*/
class AcquiaVwoIntegrationTest extends AcquiaVwoTestBase {
use AcquiaVwoConfigTrait;
use AcquiaVwoContentTrait;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'language',
'locale',
];
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
$this->createAuthUser();
$this->createAdminUser();
// Add a second language.
$language = ConfigurableLanguage::createFromLangcode('pt-br');
$language->save();
}
/**
* Test visibility settings.
*/
public function testVisibility() {
// Create initial config.
$this->setInitialConfig();
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/config/system/acquia_vwo/visibility');
$page = $this->getSession()->getPage();
$page->findButton('Save configuration')->click();
// Set Mapping.
$this->setFieldMapping();
// Create content.
$this->createNodes();
// Check caches for anonymous user.
$this->checkCachesAnonymous();
// Test path based visibility.
$this->checkPathConfiguration();
// Test Scripts.
$this->checkScripts();
// Check caches for authenticated user.
$this->checkCachesAuthUser();
// Test scripts on node type basis.
$this->checkNodeTypeVisibility();
// Test role level visibility.
$this->checkRoles();
}
/**
* Test scripts on the page.
*/
private function checkScripts() {
// Check scripts.
$this->drupalLogin($this->authUser);
$this->drupalGet('node/1');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->responseContains('window._vwo_code || (function');
$this->assertSession()->responseContains('var account_id=123456');
$this->assertSession()->responseContains('settings_tolerance=2000');
$this->assertSession()->responseContains('v=d.querySelector(\'#vwoCode\')');
$this->assertSession()
->responseContains('window.VWO.data = window.VWO.data || {};');
$this->assertSession()->responseContains('window.VWO.data.acquia={');
$this->assertSession()
->responseContains('{"drupal":{"title":"Test article","content_type":"vwo_article","taxonomy"');
$this->assertSession()
->responseContains('taxonomy":{"content_section":["Section 1"],"content_keywords":["Foo","Bar"],"persona":["Persona 1"]}}}');
// Check that when we re-save the node, the changes will reflect.
$this->updateContent();
$this->drupalGet('node/1');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()
->responseContains('{"drupal":{"title":"Updated title');
// Test script with translated content.
$this->drupalGet('/pt-br/teste_vwo');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->responseContains('var account_id=123456');
$this->assertSession()
->responseContains('window.VWO.data = window.VWO.data || {};');
$this->assertSession()->responseContains('window.VWO.data.acquia={');
$this->assertSession()
->responseContains('{"drupal":{"title":"Teste","content_type":"vwo_article","taxonomy"');
$this->assertSession()
->responseContains('taxonomy":{"content_section":["Sessao 1"],"content_keywords":["Foo","Bar"],"persona":["Pessoa 1"]}}}');
}
/**
* Tests cache tags and Drupal cache.
*/
private function checkCachesAnonymous() {
$this->drupalLogout();
$this->assertCacheTag('config:acquia_vwo.settings');
$this->assertHeader('X-Drupal-Cache', 'MISS');
$this->getSession()->reload();
$this->assertHeader('X-Drupal-Cache', 'HIT');
}
/**
* Tests cache tags and dynamic cache.
*/
private function checkCachesAuthUser() {
$this->assertCacheTag('config:acquia_vwo.settings');
$this->assertHeader('X-Drupal-Dynamic-Cache', 'MISS');
$this->getSession()->reload();
$this->assertHeader('X-Drupal-Dynamic-Cache', 'HIT');
}
/**
* Helper to test content type visibility.
*/
private function checkNodeTypeVisibility() {
// Visibility filters for content type are disabled by default.
// The Vwo scripts are added to all content types.
$this->drupalGet('node/1');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()
->responseContains('window.VWO.data = window.VWO.data || {};');
$this->assertCacheTag('config:acquia_vwo.settings');
// Enable integration on VWO Blog content type only.
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/config/system/acquia_vwo/visibility');
$this->assertSession()->statusCodeEquals(200);
$page = $this->getSession()->getPage();
$page->findField('VWO Blog')->selectOption('vwo_blog');
$page->findButton('Save configuration')->click();
// Test integration on VWO article.
$this->drupalGet('node/1');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()
->responseNotContains('window.VWO.data = window.VWO.data || {};');
$this->assertCacheTag('config:acquia_vwo.settings');
// Enable integration on VWO Article content type.
$this->drupalGet('/admin/config/system/acquia_vwo/visibility');
$this->assertSession()->statusCodeEquals(200);
$page = $this->getSession()->getPage();
$page->findField('VWO Article')->selectOption('vwo_article');
$page->findButton('Save configuration')->click();
// Test integration on VWO article.
$this->drupalLogin($this->authUser);
$this->drupalGet('node/1');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()
->responseContains('window.VWO.data = window.VWO.data || {};');
$this->assertCacheTag('config:acquia_vwo.settings');
// Make sure cache tags for current user are not added to content.
$current_user = \Drupal::currentUser();
$this->assertNoCacheTag('user:' . $current_user->id());
}
/**
* Helper to test role level visibility.
*/
private function checkRoles() {
// Set visibility only for anonymous users.
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/config/system/acquia_vwo/visibility');
$this->assertSession()->statusCodeEquals(200);
$page = $this->getSession()->getPage();
$page->findField('Anonymous user')->selectOption('anonymous');
$page->findButton('Save configuration')->click();
// Test integration on VWO article.
$this->drupalGet('node/1');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()
->responseNotContains('window.VWO.data = window.VWO.data || {};');
$this->assertCacheContext('user.roles:authenticated');
// Check script as anonymous.
$this->drupalLogout();
$this->drupalGet('node/1');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()
->responseContains('window.VWO.data = window.VWO.data || {};');
// Check cache contexts.
$this->assertNoCacheContext('user');
// $this->assertCacheContext('user.roles');
$this->assertCacheContext('user.roles:authenticated');
// Check as authenticated user again.
$this->drupalLogin($this->authUser);
$this->drupalGet('node/1');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()
->responseNotContains('window.VWO.data = window.VWO.data || {};');
// Check cache contexts.
$this->assertNoCacheContext('user');
$this->assertCacheContext('user.roles:authenticated');
// Add authenticated role.
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/config/system/acquia_vwo/visibility');
$this->assertSession()->statusCodeEquals(200);
$page = $this->getSession()->getPage();
$page->findField('Authenticated user')->selectOption('authenticated');
$page->findButton('Save configuration')->click();
// Check as authenticated user again.
$this->drupalLogin($this->authUser);
$this->drupalGet('node/1');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()
->responseContains('window.VWO.data = window.VWO.data || {};');
// Check cache contexts.
$this->assertNoCacheContext('user');
$this->assertCacheContext('user.roles:authenticated');
}
/**
* Helper to test visibility based on paths.
*/
private function checkPathConfiguration() {
// Admin routes are excluded by default.
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/config/system/acquia_vwo');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()
->responseNotContains('window.VWO.data = window.VWO.data || {};');
}
}
