View source
<?php
namespace Drupal\Tests\conditional_fields\FunctionalJavascript;
use Drupal\conditional_fields\ConditionalFieldsInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Tests\conditional_fields\FunctionalJavascript\TestCases\ConditionalFieldValueInterface;
class ConditionalFieldSelectMultipleTest extends ConditionalFieldTestBase implements ConditionalFieldValueInterface {
public static $modules = [
'conditional_fields',
'node',
'options',
];
protected $screenshotPath = 'sites/simpletest/conditional_fields/select_multiple/';
protected $fieldName = 'test_options';
protected $fieldSelector;
protected $fieldStorageDefinition;
protected $fieldStorage;
protected $field;
protected function setUp() {
parent::setUp();
$this->fieldSelector = "[name=\"field_{$this->fieldName}[]\"]";
$this->fieldStorageDefinition = [
'field_name' => 'field_' . $this->fieldName,
'entity_type' => 'node',
'type' => 'list_integer',
'cardinality' => -1,
'settings' => [
'allowed_values' => [
'One',
'Two',
'Three',
],
],
];
$this->fieldStorage = FieldStorageConfig::create($this->fieldStorageDefinition);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'article',
]);
$this->field
->save();
EntityFormDisplay::load('node.article.default')
->setComponent('field_' . $this->fieldName, [
'type' => 'options_select',
])
->save();
}
public function testVisibleValueWidget() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-add-list-options-filed-conditions.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
'field_' . $this->fieldName . '[]' => [
0,
1,
],
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-post-add-list-options-filed-conditions.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-submit-list-options-filed-conditions.png');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-body-invisible-when-controlled-field-has-no-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
0,
]);
$this
->createScreenshot($this->screenshotPath . '05-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
0,
1,
]);
$this
->createScreenshot($this->screenshotPath . '06-body-visible-when-controlled-field-has-value.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, [
'_none',
]);
$this
->createScreenshot($this->screenshotPath . '07-body-invisible-when-controlled-field-has-no-value-again.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
}
public function testVisibleValueRegExp() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-add-list-options-filed-conditions.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX,
'regex' => '[0|1]',
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-post-add-list-options-filed-conditions.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-submit-list-options-filed-conditions.png');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-body-invisible-when-controlled-field-has-no-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
0,
]);
$this
->createScreenshot($this->screenshotPath . '05-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, [
0,
1,
]);
$this
->createScreenshot($this->screenshotPath . '06-body-visible-when-controlled-field-has-value.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, [
3,
]);
$this
->createScreenshot($this->screenshotPath . '07-body-visible-when-controlled-field-has-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
0,
3,
]);
$this
->createScreenshot($this->screenshotPath . '08-body-visible-when-controlled-field-has-value.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, [
'_none',
]);
$this
->createScreenshot($this->screenshotPath . '09-body-invisible-when-controlled-field-has-no-value-again.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
}
public function testVisibleValueAnd() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-add-list-options-filed-conditions.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND,
'values' => "0\r\n1",
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-post-add-list-options-filed-conditions.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-submit-list-options-filed-conditions.png');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-body-invisible-when-controlled-field-has-no-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
0,
]);
$this
->createScreenshot($this->screenshotPath . '05-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
0,
3,
]);
$this
->createScreenshot($this->screenshotPath . '06-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
0,
1,
]);
$this
->createScreenshot($this->screenshotPath . '07-body-visible-when-controlled-field-has-value.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, [
'_none',
]);
$this
->createScreenshot($this->screenshotPath . '08-body-invisible-when-controlled-field-has-no-value-again.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
}
public function testVisibleValueOr() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-add-list-options-filed-conditions.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
'values' => "0\r\n1",
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-post-add-list-options-filed-conditions.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-submit-list-options-filed-conditions.png');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-body-invisible-when-controlled-field-has-no-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
0,
]);
$this
->createScreenshot($this->screenshotPath . '05-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, [
0,
3,
]);
$this
->createScreenshot($this->screenshotPath . '06-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, [
3,
]);
$this
->createScreenshot($this->screenshotPath . '07-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
0,
1,
]);
$this
->createScreenshot($this->screenshotPath . '08-body-visible-when-controlled-field-has-value.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, [
'_none',
]);
$this
->createScreenshot($this->screenshotPath . '09-body-invisible-when-controlled-field-has-no-value-again.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
}
public function testVisibleValueNot() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-add-list-options-filed-conditions.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT,
'values' => "0\r\n1",
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-post-add-list-options-filed-conditions.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-submit-list-options-filed-conditions.png');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-body-invisible-when-controlled-field-has-no-value.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, [
0,
]);
$this
->createScreenshot($this->screenshotPath . '05-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
0,
3,
]);
$this
->createScreenshot($this->screenshotPath . '06-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
3,
]);
$this
->createScreenshot($this->screenshotPath . '07-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, [
0,
1,
]);
$this
->createScreenshot($this->screenshotPath . '08-body-visible-when-controlled-field-has-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
'_none',
]);
$this
->createScreenshot($this->screenshotPath . '09-body-invisible-when-controlled-field-has-no-value-again.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
}
public function testVisibleValueXor() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-add-list-options-filed-conditions.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR,
'values' => "0\r\n1",
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-post-add-list-options-filed-conditions.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-submit-list-options-filed-conditions.png');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-body-invisible-when-controlled-field-has-no-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
0,
]);
$this
->createScreenshot($this->screenshotPath . '05-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, [
0,
3,
]);
$this
->createScreenshot($this->screenshotPath . '06-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, [
3,
]);
$this
->createScreenshot($this->screenshotPath . '07-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
0,
1,
]);
$this
->createScreenshot($this->screenshotPath . '08-body-visible-when-controlled-field-has-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, [
'_none',
]);
$this
->createScreenshot($this->screenshotPath . '09-body-invisible-when-controlled-field-has-no-value-again.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
}
protected function changeField($selector, $value = '') {
$value = json_encode($value);
$this
->getSession()
->executeScript("jQuery('{$selector}').val({$value}).trigger('keyup').trigger('change');");
}
}