View source
<?php
namespace Drupal\Tests\conditional_fields\FunctionalJavascript;
use Drupal\conditional_fields\ConditionalFieldsInterface;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Tests\conditional_fields\FunctionalJavascript\TestCases\ConditionalFieldCheckedUncheckedInterface;
use Drupal\Tests\conditional_fields\FunctionalJavascript\TestCases\ConditionalFieldValueInterface;
class ConditionalFieldRadiosTest extends ConditionalFieldTestBase implements ConditionalFieldValueInterface, ConditionalFieldCheckedUncheckedInterface {
use EntityReferenceTestTrait;
protected $screenshotPath = 'sites/simpletest/conditional_fields/radios/';
protected $testName = 'Radios';
protected $taxonomyName;
protected $termsCount;
protected function setUp() {
parent::setUp();
$this->taxonomyName = $this
->getRandomGenerator()
->word(8);
$vocabulary = Vocabulary::create([
'name' => $this->taxonomyName,
'vid' => $this->taxonomyName,
]);
$vocabulary
->save();
$this->termsCount = mt_rand(3, 5);
for ($i = 1; $i <= $this->termsCount; $i++) {
$termName = $this
->getRandomGenerator()
->word(8);
Term::create([
'parent' => [],
'name' => $termName,
'vid' => $this->taxonomyName,
])
->save();
}
$handler_settings = [
'target_bundles' => [
$vocabulary
->id() => $vocabulary
->id(),
],
];
$this
->createEntityReferenceField('node', 'article', 'field_' . $this->taxonomyName, $this->taxonomyName, 'taxonomy_term', 'default', $handler_settings);
EntityFormDisplay::load('node.article.default')
->setComponent('field_' . $this->taxonomyName, [
'type' => 'options_buttons',
])
->save();
}
public function testVisibleValueWidget() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->taxonomyName, 'visible', 'value');
$this
->changeField('#edit-values-set', ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET);
$term_id = mt_rand(1, $this->termsCount);
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id, $term_id);
$this
->getSession()
->executeScript("jQuery('#conditional-field-edit-form').submit();");
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->assertSession()
->pageTextContains('body field_' . $this->taxonomyName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->waitUntilHidden('.field--name-body', 0, '01. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id, $term_id);
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id);
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
}
public function testVisibleValueRegExp() {
$this
->baseTestSteps();
$term_id = mt_rand(1, $this->termsCount);
do {
$term_id_f = mt_rand(1, $this->termsCount);
} while ($term_id_f == $term_id);
$this
->createCondition('body', 'field_' . $this->taxonomyName, 'visible', 'value');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX,
'regex' => "^[{$term_id}]\$",
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->assertSession()
->pageTextContains('body field_' . $this->taxonomyName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->waitUntilHidden('.field--name-body', 0, '01. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id, $term_id);
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id);
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_f, $term_id_f);
$this
->waitUntilHidden('.field--name-body', 50, '04. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_f);
$this
->waitUntilHidden('.field--name-body', 50, '05. Article Body field is visible');
}
public function testVisibleValueAnd() {
$this
->baseTestSteps();
$term_id = mt_rand(1, $this->termsCount);
do {
$term_id_2 = mt_rand(1, $this->termsCount);
} while ($term_id_2 == $term_id);
$this
->createCondition('body', 'field_' . $this->taxonomyName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND,
'values' => "{$term_id}\r\n{$term_id_2}",
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->assertSession()
->pageTextContains('body field_' . $this->taxonomyName . ' visible value');
$this
->createScreenshot($this->screenshotPath . '02-' . $this->testName . __FUNCTION__ . '.png');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '03-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 0, '01. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id, $term_id);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id);
$this
->createScreenshot($this->screenshotPath . '05-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
}
public function testVisibleValueOr() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->taxonomyName, 'visible', 'value');
$term_id_1 = mt_rand(1, $this->termsCount);
do {
$term_id_2 = mt_rand(1, $this->termsCount);
} while ($term_id_2 == $term_id_1);
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
'values' => "{$term_id_1}\r\n{$term_id_2}",
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->assertSession()
->pageTextContains('body field_' . $this->taxonomyName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->waitUntilHidden('.field--name-body', 0, '01. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1, $term_id_1);
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1);
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilVisible('.field--name-body', 50, '04. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2);
$this
->waitUntilHidden('.field--name-body', 50, '05. Article Body field is visible');
}
public function testVisibleValueNot() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->taxonomyName, 'visible', 'value');
$term_id_1 = mt_rand(1, $this->termsCount);
do {
$term_id_2 = mt_rand(1, $this->termsCount);
} while ($term_id_2 == $term_id_1);
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT,
'values' => "{$term_id_1}\r\n{$term_id_2}",
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->assertSession()
->pageTextContains('body field_' . $this->taxonomyName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->waitUntilVisible('.field--name-body', 0, '01. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1, $term_id_1);
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1);
$this
->waitUntilVisible('.field--name-body', 50, '03. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilHidden('.field--name-body', 50, '04. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2);
$this
->waitUntilVisible('.field--name-body', 50, '05. Article Body field is not visible');
}
public function testVisibleValueXor() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->taxonomyName, 'visible', 'value');
$term_id_1 = mt_rand(1, $this->termsCount);
do {
$term_id_2 = mt_rand(1, $this->termsCount);
} while ($term_id_2 == $term_id_1);
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR,
'values' => "{$term_id_1}\r\n{$term_id_2}",
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->assertSession()
->pageTextContains('body field_' . $this->taxonomyName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->waitUntilHidden('.field--name-body', 0, '01. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1, $term_id_1);
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1);
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilVisible('.field--name-body', 50, '04. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2);
$this
->waitUntilHidden('.field--name-body', 50, '05. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1, $term_id_1);
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilVisible('.field--name-body', 50, '04. Article Body field is not visible');
}
public function testVisibleChecked() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->taxonomyName, 'visible', 'checked');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->assertSession()
->pageTextContains('body field_' . $this->taxonomyName . ' visible checked');
$this
->drupalGet('node/add/article');
$this
->waitUntilHidden('.field--name-body', 0, 'Article Body field is visible');
for ($term_id = 1; $term_id < $this->termsCount; $term_id++) {
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id, $term_id);
$this
->waitUntilVisible('.field--name-body', 50, $term_id . '. Article Body field is not visible');
}
}
public function testVisibleUnchecked() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->taxonomyName, 'visible', '!checked');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->assertSession()
->pageTextContains('body field_' . $this->taxonomyName . ' visible !checked');
$this
->drupalGet('node/add/article');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
for ($term_id = 1; $term_id < $this->termsCount; $term_id++) {
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id, $term_id);
$this
->waitUntilHidden('.field--name-body', 50, $term_id . '. Article Body field is visible');
}
}
public function testInvisibleUnchecked() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->taxonomyName, '!visible', '!checked');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->assertSession()
->pageTextContains('body field_' . $this->taxonomyName . ' !visible !checked');
$this
->drupalGet('node/add/article');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
for ($term_id = 1; $term_id < $this->termsCount; $term_id++) {
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id, $term_id);
$this
->waitUntilVisible('.field--name-body', 50, $term_id . '. Article Body field is not visible');
}
}
}