acquia_vwo-1.0.x-dev/tests/src/Functional/AcquiaVwoAdminTest.php
tests/src/Functional/AcquiaVwoAdminTest.php
<?php
namespace Drupal\Tests\acquia_vwo\Functional;
use Drupal\KernelTests\AssertContentTrait;
// cspell:ignore nocontrol optout vwoid
/**
* Acquia VWO Admin Tests.
*
* @group acquia_vwo
*/
class AcquiaVwoAdminTest extends AcquiaVwoTestBase {
use AssertContentTrait;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'help',
'field',
'field_ui',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'claro';
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
$this->createAuthUser();
$this->adminUser = $this->drupalCreateUser([
'access content',
'access help pages',
'access administration pages',
'administer site configuration',
'administer modules',
'administer permissions',
'administer acquia vwo',
]);
}
/**
* Test access to the admin pages.
*/
public function testAdminAccess() {
// Anonymous user.
$this->drupalGet('/admin/config/system/acquia_vwo/field_mapping');
$this->assertSession()->pageTextContains($this->t('Access denied'));
// Normal user.
$this->drupalLogin($this->authUser);
$this->drupalGet('/admin/config/system/acquia_vwo/field_mapping');
$this->assertSession()->pageTextContains($this->t('Access denied'));
// Admin user.
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/config/system/acquia_vwo/field_mapping');
$this->assertSession()->pageTextNotContains($this->t('Access denied'));
$this->assertSession()->pageTextContains($this->t('Field mapping'));
}
/**
* Test links on module page.
*/
public function testModulePage() {
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/modules');
// Test link to permissions.
$this->assertSession()
->elementExists('css', 'a[href*="/admin/people/permissions/module/acquia_vwo"][id="edit-modules-acquia-vwo-links-permissions"]');
// Test link to configure.
$this->assertSession()
->elementExists('css', 'a[href*="/admin/config/system/acquia_vwo"][id="edit-modules-acquia-vwo-links-configure"]');
}
/**
* Test Help strings.
*/
public function testHelpStrings() {
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/help');
$this->clickLink('Acquia VWO');
$this->assertSession()->pageTextContains($this->t('VWO is an easy to use A/B, split and multivariate testing tool.'));
$this->assertSession()->pageTextContains($this->t('In order to use this module, you will need a VWO account.'));
$this->assertSession()->pageTextContains($this->t('Sign up for a Free 30 day trial.'));
$this->assertSession()->pageTextContains($this->t('Once the Account ID has been set on the settings page'));
$this->assertSession()->elementExists('css', 'a[href*="https://vwo.com/free-trial"]');
$this->assertSession()->elementExists('css', 'a[href*="/admin/config/system/acquia_vwo"]');
$this->drupalGet('/admin/config/system/acquia_vwo');
$this->assertSession()->pageTextContains($this->t('VWO is an easy to use A/B, split and multivariate testing tool.'));
$this->assertSession()->pageTextContains($this->t('In order to use this module, you will need a VWO account.'));
$this->assertSession()->elementExists('css', 'a[href*="https://vwo.com/free-trial"]');
$this->drupalGet('/admin/config/system/acquia_vwo/vwoid');
$this->assertSession()->pageTextContains($this->t('Your VWO Account ID can be extracted from the "VWO Smart Code"'));
$this->drupalGet('/admin/config/system/acquia_vwo/field_mapping');
$this->assertSession()->pageTextContains($this->t('By mapping common taxonomies from your content types to these fields'));
$this->assertSession()->pageTextContains($this->t('This data can then be used to segment and target your audience.'));
}
/**
* Test the form that extracts account Id from scripts.
*/
public function testExtractIdForm() {
$this->drupalLogin($this->adminUser);
// Text regex to extract the Ids.
$variations = [
'var _vis_opt_account_id=123;' => '123',
'var _vis_opt_account_id = 1234;' => '1234',
'var account_id=12345;' => '12345',
'var account_id = 123456;' => '123456',
];
foreach ($variations as $script => $expected_value) {
// Set the script value.
$this->drupalGet('/admin/config/system/acquia_vwo/vwoid');
$page = $this->getSession()->getPage();
$page->findField('VWO Smart Code')->setValue($script);
$page->findButton('Extract Account ID from pasted VWO Smart Code')->click();
// Check account Id.
$this->drupalGet('/admin/config/system/acquia_vwo/');
$this->assertSession()->fieldValueEquals('Account', $expected_value);
$page = $this->getSession()->getPage();
$field_value = $page->findField('Account')->getValue();
$this->assertEquals($expected_value, $field_value, 'The account Id is correct.');
}
}
/**
* Tests the field mapping form.
*/
public function testFieldMapping() {
$this->adminUser = $this->drupalCreateUser([
'access content',
'administer site configuration',
'administer node fields',
'administer node display',
'access administration pages',
'administer acquia vwo',
]);
$this->drupalLogin($this->adminUser);
// Check that the fields are created properly on content type.
$this->drupalGet('admin/structure/types/manage/vwo_article/fields');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('field_vwo_keywords');
$this->assertSession()->pageTextContains('field_vwo_persona');
$this->assertSession()->pageTextContains('field_vwo_section');
// Check mapping.
$this->drupalGet('admin/config/system/acquia_vwo/field_mapping');
$this->assertSession()->statusCodeEquals(200);
// Update config.
$edit = [
'edit-content-section' => 'field_vwo_section',
'edit-content-keywords' => 'field_vwo_keywords',
'edit-persona' => 'field_vwo_persona',
];
$this->submitForm($edit, $this->t('Save configuration'));
$this->drupalGet('admin/config/system/acquia_vwo/field_mapping');
$this->assertSession()
->fieldValueEquals('Content Section', 'field_vwo_section');
$this->assertSession()
->fieldValueEquals('Content Keywords', 'field_vwo_keywords');
$this->assertSession()->fieldValueEquals('Persona', 'field_vwo_persona');
}
/**
* Test the visibility form.
*/
public function testVisibilityForm() {
$this->drupalLogin($this->adminUser);
// Configure account Id.
$this->drupalGet('/admin/config/system/acquia_vwo');
$page = $this->getSession()->getPage();
$page->findField('VWO Account ID')->setValue('1234');
$page->findButton('Save configuration')->click();
// Check visibility form.
$this->drupalGet('/admin/config/system/acquia_vwo/visibility');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($this->t('User specific visibility settings'));
// Turn on visibility options.
$page = $this->getSession()->getPage();
$page->findField('Customize scripts visibility')->selectOption('on');
$page->findButton('Save configuration')->click();
$this->assertSession()->pageTextContains($this->t('Request Path'));
$this->assertSession()->pageTextContains($this->t('User Role'));
$this->assertSession()->pageTextContains($this->t('Content Type'));
// Check user control.
$user_control = $page->findField('Users cannot control whether or not the VWO integration is enabled.')->getValue();
$this->assertEquals('nocontrol', $user_control, 'Users have no control on the script visibility');
// Visit account page.
$this->drupalGet('/user');
$this->clickLink('Edit');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->elementNotExists('xpath', '//input[@id="edit-user-opt"]');
// Turn on user control with opt out.
$this->drupalGet('/admin/config/system/acquia_vwo/visibility');
$page = $this->getSession()->getPage();
$page->findField('Enabled default, but let individual users turn it off.')->selectOption('optout');
$page->findButton('Save configuration')->click();
// Visit account page.
$this->drupalGet('/user');
$this->clickLink('Edit');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->checkboxChecked('edit-user-opt');
// Turn on user control with opt in.
$this->drupalGet('/admin/config/system/acquia_vwo/visibility');
$page = $this->getSession()->getPage();
$page->findField('Disabled by default but let individual users turn it on.')->selectOption('optin');
$page->findButton('Save configuration')->click();
// Visit account page.
$this->drupalGet('/user');
$this->clickLink('Edit');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->checkboxNotChecked('edit-user-opt');
}
}
