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\link\LinkItemInterface;
use Drupal\Tests\conditional_fields\FunctionalJavascript\TestCases\ConditionalFieldFilledEmptyInterface;
use Drupal\Tests\conditional_fields\FunctionalJavascript\TestCases\ConditionalFieldValueInterface;
class ConditionalFieldLinkFieldTest extends ConditionalFieldTestBase implements ConditionalFieldValueInterface, ConditionalFieldFilledEmptyInterface {
protected $screenshotPath = 'sites/simpletest/conditional_fields/link_field/';
public static $modules = [
'conditional_fields',
'node',
'link',
];
protected $fieldName = 'link_field';
protected $fieldSelector;
protected $fieldStorageDefinition;
protected $fieldStorage;
protected $field;
protected function setUp() {
parent::setUp();
$this->fieldSelector = '[name="' . $this->fieldName . '[0][uri]"]';
$this->fieldStorageDefinition = [
'field_name' => $this->fieldName,
'entity_type' => 'node',
'type' => 'link',
];
$this->fieldStorage = FieldStorageConfig::create($this->fieldStorageDefinition);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'article',
'settings' => [
'title' => DRUPAL_DISABLED,
'link_type' => LinkItemInterface::LINK_GENERIC,
],
]);
$this->field
->save();
EntityFormDisplay::load('node.article.default')
->setComponent($this->fieldName, [
'type' => 'link_default',
])
->save();
}
public function testVisibleValueWidget() {
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-testFieldLinkVisibleValueWidget.png');
$external_url = 'https://drupal.org';
$wrong_url = 'https://drupal.com';
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
$this->fieldName . '[0][uri]' => $external_url,
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-testFieldLinkVisibleValueWidget.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testFieldLinkVisibleValueWidget.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '04-testFieldLinkVisibleValueWidget.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelector, $external_url);
$this
->createScreenshot($this->screenshotPath . '05-testFieldLinkVisibleValueWidget.png');
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeField($this->fieldSelector, $wrong_url);
$this
->createScreenshot($this->screenshotPath . '06-testFieldLinkVisibleValueWidget.png');
$this
->waitUntilHidden('.field--name-body', 50, '04. Article Body field is visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '06-testFieldLinkVisibleValueWidget.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
}
public function testVisibleValueRegExp() {
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-testFieldLinkVisibleValueWidget.png');
$external_url = 'https://drupal.org';
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX,
'regex' => '^https?:\\/\\/drupal\\.org',
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-testFieldLinkVisibleValueWidget.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testFieldLinkVisibleValueWidget.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '04-testFieldLinkVisibleValueWidget.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is not visible');
$this
->changeField($this->fieldSelector, $external_url);
$this
->createScreenshot($this->screenshotPath . '05-testFieldLinkVisibleValueWidget.png');
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '06-testFieldLinkVisibleValueWidget.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is not visible');
}
public function testVisibleValueAnd() {
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-link-field-add-filed-conditions.png');
$urls = [
'node/add',
'node/1',
];
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND,
'values' => implode("\r\n", $urls),
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-link-field-post-add-list-options-filed-conditions.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-link-field-submit-options-filed-conditions.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-link-field-body-invisible-when-controlled-field-has-no-value.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelector, 'https://drupal.org');
$this
->createScreenshot($this->screenshotPath . '05-link-field-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelector, $urls[0]);
$this
->createScreenshot($this->screenshotPath . '06-link-field-body-visible-when-controlled-field-has-value.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
$this
->changeField($this->fieldSelector, $urls[1]);
$this
->createScreenshot($this->screenshotPath . '07-link-field-body-visible-when-controlled-field-has-value.png');
$this
->waitUntilHidden('.field--name-body', 50, '04. Article Body field is visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-link-field-body-invisible-when-controlled-field-has-no-value-again.png');
$this
->waitUntilHidden('.field--name-body', 50, '05. Article Body field is visible');
}
public function testVisibleValueOr() {
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-link-field-add-filed-conditions.png');
$urls = [
'node/add',
'node/1',
];
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
'values' => implode("\r\n", $urls),
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-link-field-post-add-list-options-filed-conditions.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-link-field-submit-options-filed-conditions.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-link-field-body-invisible-when-controlled-field-has-no-value.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelector, 'https://drupal.org');
$this
->createScreenshot($this->screenshotPath . '05-link-field-body-invisible-when-controlled-field-has-wrong-value.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelector, $urls[0]);
$this
->createScreenshot($this->screenshotPath . '06-link-field-body-visible-when-controlled-field-has-value.png');
$this
->waitUntilVisible('.field--name-body', 50, '03. Article Body field is not visible');
$this
->changeField($this->fieldSelector, $urls[1]);
$this
->createScreenshot($this->screenshotPath . '07-link-field-body-visible-when-controlled-field-has-value.png');
$this
->waitUntilVisible('.field--name-body', 50, '04. Article Body field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-link-field-body-invisible-when-controlled-field-has-no-value-again.png');
$this
->waitUntilHidden('.field--name-body', 50, '05. Article Body field is visible');
}
public function testVisibleValueNot() {
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-testFieldLinkVisibleValueNot.png');
$urls = [
'node/add',
'node/1',
];
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT,
'values' => implode("\r\n", $urls),
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-testFieldLinkVisibleValueNot.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testFieldLinkVisibleValueNot.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-testFieldLinkVisibleValueNot.png');
$this
->waitUntilVisible('.field--name-body', 50, '01. Article Body field is not visible');
$this
->changeField($this->fieldSelector, $urls[0]);
$this
->createScreenshot($this->screenshotPath . '05-testFieldLinkVisibleValueNot.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelector, $urls[1]);
$this
->createScreenshot($this->screenshotPath . '06-testFieldLinkVisibleValueNot.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-testFieldLinkVisibleValueNot.png');
$this
->waitUntilVisible('.field--name-body', 50, '04. Article Body field is not visible');
}
public function testVisibleValueXor() {
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-testFieldLinkVisibleValueNot.png');
$urls = [
'node/add',
'node/1',
];
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR,
'values' => implode("\r\n", $urls),
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-testFieldLinkVisibleValueNot.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testFieldLinkVisibleValueNot.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-testFieldLinkVisibleValueNot.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelector, $urls[0]);
$this
->createScreenshot($this->screenshotPath . '05-testFieldLinkVisibleValueNot.png');
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeField($this->fieldSelector, $urls[1]);
$this
->createScreenshot($this->screenshotPath . '06-testFieldLinkVisibleValueNot.png');
$this
->waitUntilVisible('.field--name-body', 50, '03. Article Body field is not visible');
$this
->changeField($this->fieldSelector, "http://example.com");
$this
->createScreenshot($this->screenshotPath . '06-testFieldLinkVisibleValueNot.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-testFieldLinkVisibleValueNot.png');
$this
->waitUntilHidden('.field--name-body', 50, '04. Article Body field is visible');
}
public function testVisibleFilled() {
$this
->baseTestSteps();
$url = 'https://drupal.org';
$this
->createCondition('body', $this->fieldName, 'visible', '!empty');
$this
->createScreenshot($this->screenshotPath . '01-testFieldLinkVisibleValueNot.png');
$this
->createScreenshot($this->screenshotPath . '02-testFieldLinkVisibleValueNot.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testFieldLinkVisibleValueNot.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible !empty');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-testFieldLinkVisibleValueNot.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelector, $url);
$this
->createScreenshot($this->screenshotPath . '05-testFieldLinkVisibleValueNot.png');
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-testFieldLinkVisibleValueNot.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
}
public function testVisibleEmpty() {
$this
->baseTestSteps();
$url = 'https://drupal.org';
$this
->createCondition('body', $this->fieldName, 'visible', 'empty');
$this
->createScreenshot($this->screenshotPath . '01-testFieldLinkVisibleValueNot.png');
$this
->createScreenshot($this->screenshotPath . '02-testFieldLinkVisibleValueNot.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testFieldLinkVisibleValueNot.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible empty');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-testFieldLinkVisibleValueNot.png');
$this
->waitUntilVisible('.field--name-body', 50, '01. Article Body field is not visible');
$this
->changeField($this->fieldSelector, $url);
$this
->createScreenshot($this->screenshotPath . '05-testFieldLinkVisibleValueNot.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-testFieldLinkVisibleValueNot.png');
$this
->waitUntilVisible('.field--name-body', 50, '03. Article Body field is not visible');
}
public function testInvisibleFilled() {
$this
->baseTestSteps();
$url = 'https://drupal.org';
$this
->createCondition('body', $this->fieldName, '!visible', '!empty');
$this
->createScreenshot($this->screenshotPath . '01-testFieldLinkVisibleValueNot.png');
$this
->createScreenshot($this->screenshotPath . '02-testFieldLinkVisibleValueNot.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testFieldLinkVisibleValueNot.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' !visible !empty');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-testFieldLinkVisibleValueNot.png');
$this
->waitUntilVisible('.field--name-body', 50, '01. Article Body field is not visible');
$this
->changeField($this->fieldSelector, $url);
$this
->createScreenshot($this->screenshotPath . '05-testFieldLinkVisibleValueNot.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-testFieldLinkVisibleValueNot.png');
$this
->waitUntilVisible('.field--name-body', 50, '03. Article Body field is not visible');
}
public function testInvisibleEmpty() {
$this
->baseTestSteps();
$url = 'https://drupal.org';
$this
->createCondition('body', $this->fieldName, '!visible', 'empty');
$this
->createScreenshot($this->screenshotPath . '01-testFieldLinkVisibleValueNot.png');
$this
->createScreenshot($this->screenshotPath . '02-testFieldLinkVisibleValueNot.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testFieldLinkVisibleValueNot.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' !visible empty');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-testFieldLinkVisibleValueNot.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelector, $url);
$this
->createScreenshot($this->screenshotPath . '05-testFieldLinkVisibleValueNot.png');
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-testFieldLinkVisibleValueNot.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
}
}