View source
<?php
namespace Drupal\Tests\conditional_fields\FunctionalJavascript;
use Drupal\conditional_fields\ConditionalFieldsInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Tests\conditional_fields\FunctionalJavascript\TestCases\ConditionalFieldValueInterface;
use Drupal\Core\Datetime\DrupalDateTime;
class ConditionalFieldDateListTest extends ConditionalFieldTestBase implements ConditionalFieldValueInterface {
protected $screenshotPath = 'sites/simpletest/conditional_fields/datelist/';
protected $testName = 'DateList';
protected $fieldName = 'test_datelist';
protected $fieldSelectors;
protected function setUp() {
parent::setUp();
$this->fieldSelectors = [
'day' => "[name=\"{$this->fieldName}[0][value][day]\"]",
'month' => "[name=\"{$this->fieldName}[0][value][month]\"]",
'year' => "[name=\"{$this->fieldName}[0][value][year]\"]",
];
$fieldStorageDefinition = [
'field_name' => $this->fieldName,
'entity_type' => 'node',
'type' => 'datetime',
'settings' => [
'datetime_type' => 'date',
],
];
$fieldStorage = FieldStorageConfig::create($fieldStorageDefinition);
$fieldStorage
->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => 'article',
]);
$field
->save();
EntityFormDisplay::load('node.article.default')
->setComponent($this->fieldName, [
'type' => 'datetime_datelist',
])
->save();
}
public function testVisibleValueWidget() {
$date = new DrupalDateTime();
$day = $date
->format('j');
$wrong_day = (int) $day + 1;
$month = $date
->format('n');
$year = $date
->format('Y');
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
$this->fieldName . '[0][value][day]' => $day,
$this->fieldName . '[0][value][month]' => $month,
$this->fieldName . '[0][value][year]' => $year,
'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 ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelectors['day'], $day);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelectors['month'], $month);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
$this
->changeField($this->fieldSelectors['year'], $year);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '04. Article Body field is not visible');
$this
->changeField($this->fieldSelectors['day'], $wrong_day);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '05. Article Body field is visible');
}
public function testVisibleValueRegExp() {
$day = '11';
$month = '12';
$month_wrong = '5';
$year = '2019';
$this
->baseTestSteps();
$this
->createCondition('body', $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' => '[\\d]{4}-[12]{2}-[\\d]{2}',
'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 ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelectors['day'], $day);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelectors['month'], $month);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
$this
->changeField($this->fieldSelectors['year'], $year);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '04. Article Body field is not visible');
$this
->changeField($this->fieldSelectors['day'], $month_wrong);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '05. Article Body field is visible');
}
public function testVisibleValueAnd() {
$date = new DrupalDateTime();
$day = $date
->format('j');
$wrong_day = (int) $day + 1;
$month = $date
->format('n');
$year = $date
->format('Y');
$date_2 = new DrupalDateTime("-1 year");
$day_2 = $date_2
->format('j');
$month_2 = $date_2
->format('n');
$year_2 = $date_2
->format('Y');
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND,
'values' => implode("\r\n", [
$date
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
$date_2
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
]),
'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 ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelectors['day'], $day);
$this
->changeField($this->fieldSelectors['month'], $month);
$this
->changeField($this->fieldSelectors['year'], $year);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelectors['day'], $day_2);
$this
->changeField($this->fieldSelectors['month'], $month_2);
$this
->changeField($this->fieldSelectors['year'], $year_2);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
$this
->changeField($this->fieldSelectors['day'], $wrong_day);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '05. Article Body field is visible');
}
public function testVisibleValueOr() {
$date = new DrupalDateTime();
$date
->createFromTimestamp(time());
$day = $date
->format('j');
$wrong_day = (int) $day + 1;
$month = $date
->format('n');
$year = $date
->format('Y');
$date_2 = new DrupalDateTime();
$date_2
->createFromTimestamp(strtotime("-1 year"));
$day_2 = $date_2
->format('j');
$month_2 = $date_2
->format('n');
$year_2 = $date_2
->format('Y');
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
'values' => implode("\r\n", [
$date
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
$date_2
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
]),
'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 ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelectors['day'], $day);
$this
->changeField($this->fieldSelectors['month'], $month);
$this
->changeField($this->fieldSelectors['year'], $year);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeField($this->fieldSelectors['day'], $day_2);
$this
->changeField($this->fieldSelectors['month'], $month_2);
$this
->changeField($this->fieldSelectors['year'], $year_2);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '03. Article Body field is not visible');
$this
->changeField($this->fieldSelectors['day'], $wrong_day);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '05. Article Body field is visible');
}
public function testVisibleValueNot() {
$date = new DrupalDateTime();
$day = $date
->format('j');
$wrong_day = (int) $day + 1;
$month = $date
->format('n');
$year = $date
->format('Y');
$date_2 = new DrupalDateTime();
$date_2
->createFromTimestamp(strtotime("-1 year"));
$day_2 = $date_2
->format('j');
$month_2 = $date_2
->format('n');
$year_2 = $date_2
->format('Y');
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT,
'values' => implode("\r\n", [
$date
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
$date_2
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
]),
'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 ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '01. Article Body field is not visible');
$this
->changeField($this->fieldSelectors['day'], $day);
$this
->changeField($this->fieldSelectors['month'], $month);
$this
->changeField($this->fieldSelectors['year'], $year);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelectors['day'], $day_2);
$this
->changeField($this->fieldSelectors['month'], $month_2);
$this
->changeField($this->fieldSelectors['year'], $year_2);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
$this
->changeField($this->fieldSelectors['day'], $wrong_day);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '05. Article Body field is not visible');
}
public function testVisibleValueXor() {
$date = new DrupalDateTime();
$day = $date
->format('j');
$wrong_day = (int) $day + 1;
$month = $date
->format('n');
$year = $date
->format('Y');
$date_2 = new DrupalDateTime();
$date_2
->createFromTimestamp(strtotime("-1 year"));
$day_2 = $date_2
->format('j');
$month_2 = $date_2
->format('n');
$year_2 = $date_2
->format('Y');
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-' . $this->testName . __FUNCTION__ . '.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR,
'values' => implode("\r\n", [
$date
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
$date_2
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
]),
'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 ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelectors['day'], $day);
$this
->changeField($this->fieldSelectors['month'], $month);
$this
->changeField($this->fieldSelectors['year'], $year);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelectors['day'], $day_2);
$this
->changeField($this->fieldSelectors['month'], $month_2);
$this
->changeField($this->fieldSelectors['year'], $year_2);
$this
->createScreenshot($this->screenshotPath . '04-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
$this
->changeField($this->fieldSelectors['day'], $wrong_day);
$this
->createScreenshot($this->screenshotPath . '06-' . $this->testName . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '05. Article Body field is visible');
}
}