View source
<?php
namespace Drupal\Tests\conditional_fields\FunctionalJavascript;
use Drupal\conditional_fields\ConditionalFieldsInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\node\Entity\Node;
use Drupal\Tests\RandomGeneratorTrait;
class ConditionalFieldEntityReferenceTagsTest extends ConditionalFieldTestBase {
use EntityReferenceTestTrait;
use RandomGeneratorTrait;
public static $modules = [
'conditional_fields',
'node',
];
protected $screenshotPath = 'sites/simpletest/conditional_fields/entity_reference_tags/';
protected $fieldName = 'entity_reference_tags';
protected $fieldSelector;
protected $fieldStorageDefinition;
protected $fieldStorage;
protected $field;
protected function setUp() {
parent::setUp();
$this->fieldSelector = '[name="field_' . $this->fieldName . '[target_id]"]';
$handler_settings = [
'target_bundles' => [
'article' => 'article',
],
];
$this
->createEntityReferenceField('node', 'article', 'field_' . $this->fieldName, $this->fieldName, 'node', 'default', $handler_settings, -1);
EntityFormDisplay::load('node.article.default')
->setComponent('field_' . $this->fieldName, [
'type' => 'entity_reference_autocomplete_tags',
])
->save();
}
public function testVisibleValueWidget() {
$this
->baseTestSteps();
$node = Node::create([
'type' => 'article',
'title' => 'Referenced node',
]);
$node
->save();
$referenced_format_valid = sprintf("%s (%d)", $node
->label(), $node
->id());
$referenced_format_wrong = sprintf("%s ", $node
->label());
$this
->createCondition('body', 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-entity-reference-tags-add-filed-conditions.png');
$data = [
'[name="condition"]' => 'value',
'[name="values_set"]' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
$this->fieldSelector => $referenced_format_valid,
'[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-entity-reference-tags-post-add-list-options-filed-conditions.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-entity-reference-tags-submit-entity-reference-filed-conditions.png');
$this
->assertSession()
->pageTextContains('body field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-entity-reference-tags-body-invisible-when-controlled-field-has-no-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, $this
->randomMachineName());
$this
->createScreenshot($this->screenshotPath . '05-entity-reference-tags-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, $referenced_format_valid);
$this
->createScreenshot($this->screenshotPath . '06-entity-reference-tags-body-visible-when-controlled-field-has-valid-value.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, $referenced_format_wrong);
$this
->createScreenshot($this->screenshotPath . '07-entity-reference-tags-body-invisible-when-controlled-field-has-value-in-wrong-format.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-entity-reference-tags-body-invisible-when-controlled-field-has-no-value-again.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is not visible');
}
}