acquia_vwo-1.0.x-dev/tests/src/FunctionalJavascript/AcquiaVwoIntegrationTest.php
tests/src/FunctionalJavascript/AcquiaVwoIntegrationTest.php
<?php
namespace Drupal\Tests\acquia_vwo\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\acquia_vwo\Functional\AcquiaVwoConfigTrait;
use Drupal\Tests\acquia_vwo\Functional\AcquiaVwoContentTrait;
use Drupal\language\Entity\ConfigurableLanguage;
// cspell:ignore user_opt optout
/**
* VWO Functional Javascript tests.
*
* @group acquia_vwo
*/
class AcquiaVwoIntegrationTest extends WebDriverTestBase {
use AcquiaVwoConfigTrait;
use AcquiaVwoContentTrait;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'acquia_vwo',
'acquia_vwo_test',
'language',
'locale',
];
/**
* {@inheritdoc}
*/
protected $profile = 'minimal';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* A user without the "Administer Acquia VWO" permission.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $authUser;
/**
* A user with the "Administer Acquia VWO" permission.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
$this->createAuthUser();
$this->createAdminUser();
// Add a second language.
$language = ConfigurableLanguage::createFromLangcode('pt-br');
$language->save();
}
/**
* Creates an authenticated user.
*/
private function createAuthUser() {
$this->authUser = $this->drupalCreateUser([
'access content',
]);
}
/**
* Creates an admin user.
*/
private function createAdminUser() {
$this->adminUser = $this->drupalCreateUser([
'administer acquia vwo',
]);
}
/**
* Helper to update the user data to opt in/out.
*
* @param string $opt
* Opt in or out.
*/
private function updateUserData($opt) {
$this->drupalGet('/user');
$this->clickLink('Edit');
$page = $this->getSession()->getPage();
$page->findField('Include VWO A/B testing')->setValue($opt);
$page->findButton('Save')->click();
}
/**
* Helper to test visibility based on user profile configuration.
*/
public function testUserOptInOut() {
// 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();
// Create content.
$this->createNodes();
// Log in.
$this->drupalLogin($this->authUser);
// Change visibility to never evaluate the scripts
// unless the user decides to include.
$this->setUserControl('optout');
// Update user config to opt out.
$this->updateUserData(0);
// Check that VWO script is not evaluated.
$this->drupalGet('node/1');
$session = $this->getSession();
$this->assertSame('undefined', $session->evaluateScript("return typeof window._vwo_code"));
// Update user config to opt in.
$this->updateUserData(1);
// Check that VWO script is evaluated.
$this->drupalGet('node/1');
$session = $this->getSession();
$this->assertSame('object', $session->evaluateScript("typeof window._vwo_code"));
// Change visibility to always evaluate the script
// unless the user decides not to include.
$this->setUserControl('optin');
// Update user config to opt out.
$this->updateUserData(0);
// Check that VWO script is not evaluated.
$this->drupalGet('node/1');
$session = $this->getSession();
$this->assertSame('undefined', $session->evaluateScript("typeof window._vwo_code"));
// Update user config to opt in.
$this->updateUserData(1);
// Check that VWO script is evaluated.
$this->drupalGet('node/1');
$session = $this->getSession();
$this->assertSame('object', $session->evaluateScript("typeof window._vwo_code"));
}
}
