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 ConditionalFieldTextTextareaTest extends ConditionalFieldTestBase implements ConditionalFieldValueInterface {
protected $screenshotPath = 'sites/simpletest/conditional_fields/textarea/';
protected $testName = 'TextTextarea';
protected $fieldName = 'single_textarea';
protected $fieldSelector;
protected $targetFieldName = 'body';
protected $targetFieldWrapp = '';
protected $fieldStorageDefinition;
protected $fieldStorage;
protected $field;
protected function setUp() {
parent::setUp();
$this->fieldSelector = '[name="field_' . $this->fieldName . '[0][value]"]';
$this->targetFieldWrapp = '.field--name-' . str_replace('_', '-', $this->targetFieldName);
$this->fieldStorageDefinition = [
'field_name' => 'field_' . $this->fieldName,
'entity_type' => 'node',
'type' => 'text_long',
'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',
])
->save();
}
public function testVisibleValueWidget() {
$this
->baseTestSteps();
$this
->createCondition($this->targetFieldName, 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$text = 'drupal test textarea';
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
'field_' . $this->fieldName . '[0][value]' => $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($this->targetFieldName . ' field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is not visible');
$this
->changeField($this->fieldSelector, $text);
$this
->createScreenshot($this->screenshotPath . '05-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is not visible');
}
public function testVisibleValueAnd() {
$this
->baseTestSteps();
$this
->createCondition($this->targetFieldName, 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$text = [
$this
->randomMachineName(),
$this
->randomMachineName(),
];
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND,
'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($this->targetFieldName . ' field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, 'https://drupal.org');
$this
->createScreenshot($this->screenshotPath . '05-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, $text[0]);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, $text[1]);
$this
->createScreenshot($this->screenshotPath . '07-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is not visible');
}
public function testVisibleValueOr() {
$this
->baseTestSteps();
$this
->createCondition($this->targetFieldName, 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$text = [
'drupal textarea text first',
'drupal textarea 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($this->targetFieldName . ' field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, 'https://drupal.org');
$this
->createScreenshot($this->screenshotPath . '05-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, $text[0]);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is not visible');
$this
->changeField($this->fieldSelector, $text[1]);
$this
->createScreenshot($this->screenshotPath . '07-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
}
public function testVisibleValueRegExp() {
$this
->baseTestSteps();
$this
->createCondition($this->targetFieldName, 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX,
'regex' => '.*data\\=[\\d]+.*',
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$text_without_expresion = 'The field in not empty';
$text_with_expresion = 'The field has data=2 text';
$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($this->targetFieldName . ' field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, $text_without_expresion);
$this
->createScreenshot($this->screenshotPath . '05-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, $text_with_expresion);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
}
public function testVisibleValueNot() {
$this
->baseTestSteps();
$this
->createCondition($this->targetFieldName, 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$text = [
"first string",
"second string",
];
$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($this->targetFieldName . ' field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is not visible');
$this
->changeField($this->fieldSelector, $text[0]);
$this
->createScreenshot($this->screenshotPath . '05-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, $text[1]);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '07-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is not visible');
}
public function testVisibleValueXor() {
$this
->baseTestSteps();
$this
->createCondition($this->targetFieldName, 'field_' . $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$text = [
$this
->randomMachineName(),
$this
->randomMachineName(),
];
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR,
'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($this->targetFieldName . ' field_' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, $text[0]);
$this
->createScreenshot($this->screenshotPath . '05-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is not visible');
$this
->changeField($this->fieldSelector, $text[1]);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '07-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden($this->targetFieldWrapp, 50, 'Article \'' . $this->targetFieldName . '\' field is visible');
}
public function testCreateConfigVisibleFilled() {
$this
->baseTestSteps();
$this
->createCondition($this->targetFieldName, 'field_' . $this->fieldName, 'visible', '!empty');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->assertSession()
->pageTextContains($this->targetFieldName . ' field_' . $this->fieldName . ' visible !empty');
$this
->drupalGet('node/add/article');
$this
->waitUntilHidden($this->targetFieldWrapp, 0, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, 'This field is not empty.');
$this
->waitUntilVisible($this->targetFieldWrapp, 10, 'Article \'' . $this->targetFieldName . '\' field is not visible');
}
public function testCreateConfigInvisibleEmpty() {
$this
->baseTestSteps();
$this
->createCondition($this->targetFieldName, 'field_' . $this->fieldName, '!visible', 'empty');
$this
->drupalGet('admin/structure/conditional_fields/node/article');
$this
->assertSession()
->pageTextContains($this->targetFieldName . ' field_' . $this->fieldName . ' !visible empty');
$this
->drupalGet('node/add/article');
$this
->waitUntilHidden($this->targetFieldWrapp, 0, 'Article \'' . $this->targetFieldName . '\' field is visible');
$this
->changeField($this->fieldSelector, 'This field is not empty.');
$this
->waitUntilVisible($this->targetFieldWrapp, 10, 'Article \'' . $this->targetFieldName . '\' field is not visible');
}
}