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\ConditionalFieldValueInterface;
class ConditionalFieldCheckboxesTest extends ConditionalFieldTestBase implements ConditionalFieldValueInterface {
use EntityReferenceTestTrait;
protected $screenshotPath = 'sites/simpletest/conditional_fields/checkboxes/';
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, 6);
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, -1);
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_1 = mt_rand(1, $this->termsCount);
do {
$term_id_2 = mt_rand(1, $this->termsCount);
} while ($term_id_2 == $term_id_1);
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1, $term_id_1);
$this
->getSession()
->executeScript("jQuery('#conditional-field-edit-form').submit();");
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->assertSession()
->pageTextContains('body field_' . $this->taxonomyName . ' visible value');
$this
->clickLink('Edit');
$this
->createScreenshot($this->screenshotPath . '01. Checkboxes' . __FUNCTION__ . '.jpg');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '02. Checkboxes' . __FUNCTION__ . '.jpg');
$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
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilHidden('.field--name-body', 60, '02. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilVisible('.field--name-body', 60, '03. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1, $term_id_1);
$this
->waitUntilHidden('.field--name-body', 60, '04. Article Body field is visible');
}
public function testVisibleValueRegExp() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->taxonomyName, 'visible', 'value');
$this
->changeField('#edit-values-set', ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX);
$term_id_1 = mt_rand(1, $this->termsCount);
do {
$term_id_2 = mt_rand(1, $this->termsCount);
} while ($term_id_2 == $term_id_1);
$this
->changeSelect('#edit-regex', "{$term_id_1}");
$this
->getSession()
->executeScript("jQuery('#conditional-field-edit-form').submit();");
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$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
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilVisible('.field--name-body', 60, '02. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1);
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2);
$this
->waitUntilHidden('.field--name-body', 60, '03. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1, $term_id_1);
$this
->waitUntilVisible('.field--name-body', 60, '04. Article Body field is not visible');
}
public function testVisibleValueAnd() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->taxonomyName, 'visible', 'value');
$this
->changeField('#edit-values-set', ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND);
$term_id_1 = mt_rand(1, $this->termsCount);
do {
$term_id_2 = mt_rand(1, $this->termsCount);
} while ($term_id_2 == $term_id_1);
$values = $term_id_1 . '\\r\\n' . $term_id_2;
$this
->changeField('#edit-values', $values);
$this
->getSession()
->executeScript("jQuery('#conditional-field-edit-form').submit();");
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$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
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->createScreenshot($this->screenshotPath . 'scr1BodyVisCheckboxes.jpg');
$this
->waitUntilVisible('.field--name-body', 60, '02. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1, $term_id_1);
$this
->waitUntilHidden('.field--name-body', 60, '03. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilHidden('.field--name-body', 60, '04. Article Body field is visible');
$this
->createScreenshot($this->screenshotPath . 'scr2BodyHidCheckboxes.jpg');
}
public function testVisibleValueOr() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->taxonomyName, 'visible', 'value');
$this
->changeField('#edit-values-set', ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR);
$term_id_1 = mt_rand(1, $this->termsCount);
do {
$term_id_2 = mt_rand(1, $this->termsCount);
} while ($term_id_2 == $term_id_1);
$values = $term_id_1 . '\\r\\n' . $term_id_2;
$this
->changeField('#edit-values', $values);
$this
->getSession()
->executeScript("jQuery('#conditional-field-edit-form').submit();");
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$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', 60, '02. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1, $term_id_1);
$this
->waitUntilHidden('.field--name-body', 60, '03. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilVisible('.field--name-body', 60, '04. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilHidden('.field--name-body', 60, '05. Article Body field is visible');
}
public function testVisibleValueNot() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->taxonomyName, 'visible', 'value');
$this
->changeField('#edit-values-set', ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT);
$term_id_1 = mt_rand(1, $this->termsCount);
do {
$term_id_2 = mt_rand(1, $this->termsCount);
} while ($term_id_2 == $term_id_1);
$values = $term_id_1 . '\\r\\n' . $term_id_2;
$this
->changeField('#edit-values', $values);
$this
->getSession()
->executeScript("jQuery('#conditional-field-edit-form').submit();");
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$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', 60, '02. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1, $term_id_1);
$this
->waitUntilVisible('.field--name-body', 60, '03. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilHidden('.field--name-body', 60, '04. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilVisible('.field--name-body', 60, '05. Article Body field is not visible');
}
public function testVisibleValueXor() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->taxonomyName, 'visible', 'value');
$this
->changeField('#edit-values-set', ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR);
$term_id_1 = mt_rand(1, $this->termsCount);
do {
$term_id_2 = mt_rand(1, $this->termsCount);
} while ($term_id_2 == $term_id_1);
$values = $term_id_1 . '\\r\\n' . $term_id_2;
$this
->changeField('#edit-values', $values);
$this
->getSession()
->executeScript("jQuery('#conditional-field-edit-form').submit();");
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$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', 60, '02. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_1, $term_id_1);
$this
->waitUntilHidden('.field--name-body', 60, '03. Article Body field is visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilVisible('.field--name-body', 60, '04. Article Body field is not visible');
$this
->changeSelect('#edit-field-' . $this->taxonomyName . '-' . $term_id_2, $term_id_2);
$this
->waitUntilHidden('.field--name-body', 60, '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
->waitUntilHidden('.field--name-body', 60, '05. Article Body field is visible');
}
}