View source
<?php
namespace Drupal\Tests\conditional_fields\FunctionalJavascript;
use Drupal\conditional_fields\ConditionalFieldsInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\conditional_fields\FunctionalJavascript\TestCases\ConditionalFieldValueInterface;
class ConditionalFieldSelectTest extends ConditionalFieldTestBase implements ConditionalFieldValueInterface {
public static $modules = [
'conditional_fields',
'node',
'options',
];
protected $screenshotPath = 'sites/simpletest/conditional_fields/select/';
protected $fieldNames = [
'select_single_entity_reference',
'select_single_list_integer',
'select_single_list_float',
'select_single_list_string',
];
protected $fieldSelectors;
protected $fieldStorageDefinitions;
protected $fieldStorages;
protected $fields;
protected function setUp() {
parent::setUp();
foreach ($this->fieldNames as $fieldName) {
$this->fieldSelectors[$fieldName] = "[name=\"{$fieldName}\"]";
$this->fieldStorageDefinitions[$fieldName] = [
'field_name' => $fieldName,
'entity_type' => 'node',
'type' => str_replace('select_single_', '', $fieldName),
'cardinality' => 1,
];
}
$this->fieldStorageDefinitions['select_single_entity_reference']['settings']['target_type'] = 'user';
$this->fieldStorageDefinitions['select_single_list_integer']['settings']['allowed_values'] = [
1 => '1',
2 => '2',
3 => '3',
];
$this->fieldStorageDefinitions['select_single_list_float']['settings']['allowed_values'] = [
'1.5' => '1.5',
'2.5' => '2.5',
'3.5' => '3.5',
];
$this->fieldStorageDefinitions['select_single_list_string']['settings']['allowed_values'] = [
'one' => 'One',
'two' => 'Two',
'three' => 'Three',
];
$entity_form_display = EntityFormDisplay::load('node.article.default');
foreach ($this->fieldNames as $fieldName) {
$this->fieldStorages[$fieldName] = FieldStorageConfig::create($this->fieldStorageDefinitions[$fieldName]);
$this->fieldStorages[$fieldName]
->save();
$this->fields[$fieldName] = FieldConfig::create([
'field_storage' => $this->fieldStorages[$fieldName],
'bundle' => 'article',
]);
$this->fields[$fieldName]
->save();
$entity_form_display
->setComponent($fieldName, [
'type' => 'options_select',
]);
}
$entity_form_display
->save();
}
public function testVisibleValueWidget() {
$this
->baseTestSteps();
$correct_values = [
'select_single_entity_reference' => 1,
'select_single_list_integer' => 1,
'select_single_list_float' => 1.5,
'select_single_list_string' => 'one',
];
$wrong_values = [
'select_single_entity_reference' => 3,
'select_single_list_integer' => 3,
'select_single_list_float' => 3.5,
'select_single_list_string' => 'three',
];
foreach ($this->fieldNames as $fieldName) {
$this
->createCondition('body', $fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $fieldName . '_' . __FUNCTION__ . '.png');
$data = [
'[name="condition"]' => 'value',
'[name="values_set"]' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
$this->fieldSelectors[$fieldName] => $correct_values[$fieldName],
'[name="grouping"]' => 'AND',
'[name="state"]' => 'visible',
'[name="effect"]' => 'show',
];
foreach ($data as $selector => $value) {
$this
->changeField($selector, $value);
}
$this
->getSession()
->wait(1000, '!jQuery.active');
$this
->getSession()
->executeScript("jQuery('#conditional-field-edit-form').submit();");
$this
->createScreenshot($this->screenshotPath . '02-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . $fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelectors[$fieldName], $wrong_values[$fieldName]);
$this
->createScreenshot($this->screenshotPath . '05-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelectors[$fieldName], $correct_values[$fieldName]);
$this
->createScreenshot($this->screenshotPath . '06-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelectors[$fieldName], '_none');
$this
->createScreenshot($this->screenshotPath . '07-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->click('li > button > .dropbutton-arrow');
$this
->clickLink('Delete');
$this
->submitForm([], 'Confirm');
$this
->createScreenshot($this->screenshotPath . '08-' . $fieldName . '_' . __FUNCTION__ . '.png');
}
}
public function testVisibleValueRegExp() {
$this
->baseTestSteps();
$reg_patterns = [
'select_single_entity_reference' => '[1]+',
'select_single_list_integer' => '[1-2]+',
'select_single_list_float' => '[1-2]+\\.5',
'select_single_list_string' => '^(one|two)$',
];
$correct_values = [
'select_single_entity_reference' => 1,
'select_single_list_integer' => 1,
'select_single_list_float' => 1.5,
'select_single_list_string' => 'one',
];
$wrong_values = [
'select_single_entity_reference' => 3,
'select_single_list_integer' => 3,
'select_single_list_float' => 3.5,
'select_single_list_string' => 'three',
];
foreach ($this->fieldNames as $fieldName) {
$this
->createCondition('body', $fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $fieldName . '_' . __FUNCTION__ . '.png');
$data = [
'[name="condition"]' => 'value',
'[name="values_set"]' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX,
'[name="regex"]' => $reg_patterns[$fieldName],
'[name="grouping"]' => 'AND',
'[name="state"]' => 'visible',
'[name="effect"]' => 'show',
];
foreach ($data as $selector => $value) {
$this
->changeField($selector, $value);
}
$this
->getSession()
->wait(1000, '!jQuery.active');
$this
->getSession()
->executeScript("jQuery('#conditional-field-edit-form').submit();");
$this
->createScreenshot($this->screenshotPath . '02-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . $fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '04.' . $fieldName . '. Article Body field is visible');
$this
->changeField($this->fieldSelectors[$fieldName], $wrong_values[$fieldName]);
$this
->createScreenshot($this->screenshotPath . '05-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '05.' . $fieldName . '. Article Body field is visible');
$this
->changeField($this->fieldSelectors[$fieldName], $correct_values[$fieldName]);
$this
->createScreenshot($this->screenshotPath . '06-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '06.' . $fieldName . '. Article Body field is not visible');
$this
->changeField($this->fieldSelectors[$fieldName], '_none');
$this
->createScreenshot($this->screenshotPath . '07-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '07.' . $fieldName . '. Article Body field is visible');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->click('li > button > .dropbutton-arrow');
$this
->clickLink('Delete');
$this
->submitForm([], 'Confirm');
$this
->createScreenshot($this->screenshotPath . '08-' . $fieldName . '_' . __FUNCTION__ . '.png');
}
}
public function testVisibleValueAnd() {
$this
->baseTestSteps();
$allowed_values = [
'select_single_entity_reference' => "1\n2",
'select_single_list_integer' => "1\n2",
'select_single_list_float' => "1.5\n2.5",
'select_single_list_string' => "one\ntwo",
];
$test_values = [
'select_single_entity_reference' => 1,
'select_single_list_integer' => 1,
'select_single_list_float' => 1.5,
'select_single_list_string' => 'one',
];
foreach ($this->fieldNames as $fieldName) {
$this
->createCondition('body', $fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $fieldName . '_' . __FUNCTION__ . '.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND,
'values' => $allowed_values[$fieldName],
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . $fieldName . ' visible value');
$this
->clickLink('Edit');
$this
->createScreenshot($this->screenshotPath . '03.5-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '04.' . $fieldName . '. Article Body field is visible');
$this
->changeField($this->fieldSelectors[$fieldName], $test_values[$fieldName]);
$this
->createScreenshot($this->screenshotPath . '05-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '05.' . $fieldName . '. Article Body field is visible');
$this
->changeField($this->fieldSelectors[$fieldName], '_none');
$this
->createScreenshot($this->screenshotPath . '07-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '07.' . $fieldName . '. Article Body field is visible');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->click('li > button > .dropbutton-arrow');
$this
->clickLink('Delete');
$this
->submitForm([], 'Confirm');
$this
->createScreenshot($this->screenshotPath . '08-' . $fieldName . '_' . __FUNCTION__ . '.png');
}
}
public function testVisibleValueOr() {
$this
->baseTestSteps();
$allowed_values = [
'select_single_entity_reference' => "1\n2",
'select_single_list_integer' => "1\n2",
'select_single_list_float' => "1.5\n2.5",
'select_single_list_string' => "one\ntwo",
];
$correct_values = [
'select_single_entity_reference' => 1,
'select_single_list_integer' => 1,
'select_single_list_float' => 1.5,
'select_single_list_string' => 'one',
];
$wrong_values = [
'select_single_entity_reference' => 3,
'select_single_list_integer' => 3,
'select_single_list_float' => 3.5,
'select_single_list_string' => 'three',
];
foreach ($this->fieldNames as $fieldName) {
$this
->createCondition('body', $fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $fieldName . '_' . __FUNCTION__ . '.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
'values' => $allowed_values[$fieldName],
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . $fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '04.' . $fieldName . '. Article Body field is visible');
$this
->changeField($this->fieldSelectors[$fieldName], $correct_values[$fieldName]);
$this
->createScreenshot($this->screenshotPath . '05-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '05.' . $fieldName . '. Article Body field is not visible');
$this
->changeField($this->fieldSelectors[$fieldName], $wrong_values[$fieldName]);
$this
->createScreenshot($this->screenshotPath . '05-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '05.' . $fieldName . '. Article Body field is visible');
$this
->changeField($this->fieldSelectors[$fieldName], '_none');
$this
->createScreenshot($this->screenshotPath . '07-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '07.' . $fieldName . '. Article Body field is visible');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->click('li > button > .dropbutton-arrow');
$this
->clickLink('Delete');
$this
->submitForm([], 'Confirm');
$this
->createScreenshot($this->screenshotPath . '08-' . $fieldName . '_' . __FUNCTION__ . '.png');
}
}
public function testVisibleValueNot() {
$this
->baseTestSteps();
$allowed_values = [
'select_single_entity_reference' => "1\n2",
'select_single_list_integer' => "1\n2",
'select_single_list_float' => "1.5\n2.5",
'select_single_list_string' => "one\ntwo",
];
$correct_values = [
'select_single_entity_reference' => 3,
'select_single_list_integer' => 3,
'select_single_list_float' => 3.5,
'select_single_list_string' => 'three',
];
$wrong_values = [
'select_single_entity_reference' => 1,
'select_single_list_integer' => 1,
'select_single_list_float' => 1.5,
'select_single_list_string' => 'one',
];
foreach ($this->fieldNames as $fieldName) {
$this
->createCondition('body', $fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $fieldName . '_' . __FUNCTION__ . '.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT,
'values' => $allowed_values[$fieldName],
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . $fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '04.' . $fieldName . '. Article Body field is visible');
$this
->changeField($this->fieldSelectors[$fieldName], $correct_values[$fieldName]);
$this
->createScreenshot($this->screenshotPath . '05-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '05.' . $fieldName . '. Article Body field is not visible');
$this
->changeField($this->fieldSelectors[$fieldName], $wrong_values[$fieldName]);
$this
->createScreenshot($this->screenshotPath . '05-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '05.' . $fieldName . '. Article Body field is visible');
$this
->changeField($this->fieldSelectors[$fieldName], '_none');
$this
->createScreenshot($this->screenshotPath . '07-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '07.' . $fieldName . '. Article Body field is visible');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->click('li > button > .dropbutton-arrow');
$this
->clickLink('Delete');
$this
->submitForm([], 'Confirm');
$this
->createScreenshot($this->screenshotPath . '08-' . $fieldName . '_' . __FUNCTION__ . '.png');
}
}
public function testVisibleValueXor() {
$this
->baseTestSteps();
$allowed_values = [
'select_single_entity_reference' => "1\n2",
'select_single_list_integer' => "1\n2",
'select_single_list_float' => "1.5\n2.5",
'select_single_list_string' => "one\ntwo",
];
$correct_values = [
'select_single_entity_reference' => 1,
'select_single_list_integer' => 1,
'select_single_list_float' => 1.5,
'select_single_list_string' => 'one',
];
foreach ($this->fieldNames as $fieldName) {
$this
->createCondition('body', $fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $fieldName . '_' . __FUNCTION__ . '.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR,
'values' => $allowed_values[$fieldName],
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . $fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '04.' . $fieldName . '. Article Body field is visible');
$this
->changeField($this->fieldSelectors[$fieldName], $correct_values[$fieldName]);
$this
->createScreenshot($this->screenshotPath . '05-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '05.' . $fieldName . '. Article Body field is not visible');
$this
->changeField($this->fieldSelectors[$fieldName], $correct_values[$fieldName]);
$this
->createScreenshot($this->screenshotPath . '05-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '06.' . $fieldName . '. Article Body field is not visible');
$this
->changeField($this->fieldSelectors[$fieldName], '_none');
$this
->createScreenshot($this->screenshotPath . '07-' . $fieldName . '_' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '07.' . $fieldName . '. Article Body field is visible');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->click('li > button > .dropbutton-arrow');
$this
->clickLink('Delete');
$this
->submitForm([], 'Confirm');
$this
->createScreenshot($this->screenshotPath . '08-' . $fieldName . '_' . __FUNCTION__ . '.png');
}
}
}