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 ConditionalFieldTextWithSummaryTest extends ConditionalFieldTestBase implements ConditionalFieldValueInterface {
protected $screenshotPath = 'sites/simpletest/conditional_fields/text_with_summary/';
protected $testName = 'TextWithSummary';
protected $fieldName = 'single_text_with_summary';
protected $fieldSelector;
protected $fieldStorageDefinition;
protected $fieldStorage;
protected $field;
protected function setUp() {
parent::setUp();
$this->fieldSelector = '[name="field_' . $this->fieldName . '[0][value]"]';
$this->fieldStorageDefinition = [
'field_name' => 'field_' . $this->fieldName,
'entity_type' => 'node',
'type' => 'text_with_summary',
'cardinality' => 1,
];
$this->fieldStorage = FieldStorageConfig::create($this->fieldStorageDefinition);
$this->fieldStorage
->save();
FieldConfig::create([
'field_name' => 'field_' . $this->fieldName,
'entity_type' => 'node',
'bundle' => 'article',
])
->save();
EntityFormDisplay::load('node.article.default')
->setComponent('field_' . $this->fieldName, [
'type' => 'text_textarea_with_summary',
])
->save();
}
public function testVisibleValueWidget() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$text = 'drupal test text_with_summary';
$data = [
'[name="condition"]' => 'value',
'[name="values_set"]' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
$this->fieldSelector => $text,
'[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-' . $this->testName . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-' . $this->testName . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' visible value');
$this
->clickLink('Edit');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '05-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelector, $text);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '07-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
}
public function testVisibleValueAnd() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$text = [
'drupal text_with_summary text first',
'drupal text_with_summary text second',
];
$text_values = implode("\r\n", $text);
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND,
'values' => $text_values,
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-' . $this->testName . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-' . $this->testName . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' visible value');
$this
->clickLink('Edit');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '05-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelector, 'drupal.org');
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelector, implode('\\r\\n', $text));
$this
->createScreenshot($this->screenshotPath . '07-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '03. Article Body field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '04. Article Body field is visible');
}
public function testVisibleValueOr() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$text = [
'drupal text_with_summary text first',
'drupal text_with_summary text second',
];
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
'values' => implode("\r\n", $text),
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-' . $this->testName . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-' . $this->testName . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' visible value');
$this
->clickLink('Edit');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '05-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelector, 'https://drupal.org');
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelector, $text[0]);
$this
->createScreenshot($this->screenshotPath . '07-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '03. Article Body field is not visible');
$this
->changeField($this->fieldSelector, $text[1]);
$this
->createScreenshot($this->screenshotPath . '08-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '04. Article Body field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '09-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '08. Article Body field is visible');
}
public function testVisibleValueRegExp() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$text = 'text_';
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX,
'regex' => $text,
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-' . $this->testName . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-' . $this->testName . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->changeField($this->fieldSelector, 'wrong text');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, 'text_correct');
$this
->createScreenshot($this->screenshotPath . '05-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, 'wrong text');
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.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-' . $this->testName . __FUNCTION__ . '.png');
$text = [
'drupal text_with_summary text first',
'drupal text_with_summary text second',
];
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT,
'values' => implode("\r\n", $text),
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-' . $this->testName . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-' . $this->testName . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, $text[0]);
$this
->createScreenshot($this->screenshotPath . '05-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, $text[1]);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '07-' . $this->testName . __FUNCTION__ . '.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-' . $this->testName . __FUNCTION__ . '.png');
$text = [
'drupal text_with_summary text first',
'drupal text_with_summary text second',
];
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
'values' => implode("\r\n", $text),
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-' . $this->testName . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-' . $this->testName . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, 'https://drupal.org');
$this
->createScreenshot($this->screenshotPath . '05-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, $text[0]);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, $text[1]);
$this
->createScreenshot($this->screenshotPath . '07-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
}
public function testCreateConfigVisibleFilled() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->fieldName, 'visible', '!empty');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' visible !empty');
$this
->drupalGet('node/add/article');
$this
->waitUntilHidden('.field--name-body', 0, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, 'This field is not empty.');
$this
->waitUntilVisible('.field--name-body', 10, 'Article Body field is not visible');
}
public function testCreateConfigInvisibleEmpty() {
$this
->baseTestSteps();
$this
->createCondition('body', 'field_' . $this->fieldName, '!visible', 'empty');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->assertSession()
->pageTextContains('body ' . 'field_' . $this->fieldName . ' !visible empty');
$this
->drupalGet('node/add/article');
$this
->waitUntilHidden('.field--name-body', 0, 'Article Body field is visible');
$this
->changeField($this->fieldSelector, 'This field is not empty.');
$this
->waitUntilVisible('.field--name-body', 10, 'Article Body field is not visible');
}
}