View source
<?php
namespace Drupal\Tests\conditional_fields\FunctionalJavascript;
use Drupal\conditional_fields\ConditionalFieldsInterface;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
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 ConditionalFieldDateTimeTest extends ConditionalFieldTestBase implements ConditionalFieldValueInterface {
protected $screenshotPath = 'sites/simpletest/conditional_fields/datetime/';
protected $displayOptions;
protected $fieldName = 'test_datetime';
protected $fieldSelector;
protected function setUp() {
parent::setUp();
$this->fieldSelector = "[name=\"{$this->fieldName}[0][value][date]\"]";
$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_default',
])
->save();
$defaultSettings = [
'timezone_override' => '',
];
$this->displayOptions = [
'type' => 'datetime_default',
'label' => 'hidden',
'settings' => [
'format_type' => 'medium',
] + $defaultSettings,
];
$view_display = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load($field
->getTargetEntityTypeId() . '.' . $field
->getTargetBundle() . '.' . 'full');
if (!$view_display) {
$view_display = EntityViewDisplay::create([
'targetEntityType' => $field
->getTargetEntityTypeId(),
'bundle' => $field
->getTargetBundle(),
'mode' => 'full',
'status' => TRUE,
]);
}
if ($view_display instanceof EntityDisplayInterface) {
$view_display
->setComponent($this->fieldName, $this->displayOptions)
->save();
}
}
public function testVisibleValueWidget() {
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-testDateTimeVisibleValueWidget.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
$this->fieldName . '[0][value][date]' => DrupalDateTime::createFromTimestamp(\Drupal::time()
->getRequestTime())
->format('m-d-Y'),
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-testDateTimeVisibleValueWidget.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testDateTimeVisibleValueWidget.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '04-testDateTimeVisibleValueWidget.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelector, DrupalDateTime::createFromTimestamp(\Drupal::time()
->getRequestTime())
->format('Y-m-d'));
$this
->createScreenshot($this->screenshotPath . '05-testDateTimeVisibleValueWidget.png');
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '06-testDateTimeVisibleValueWidget.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
}
public function testVisibleValueRegExp() {
$date = new DrupalDateTime();
$date_formatted = $date
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-testDateTimeVisibleValueWidget.png');
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX,
'regex' => '^' . $date_formatted . '$',
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-testDateTimeVisibleValueWidget.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testDateTimeVisibleValueWidget.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '04-testDateTimeVisibleValueWidget.png');
$this
->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');
$this
->changeField($this->fieldSelector, $date_formatted);
$this
->createScreenshot($this->screenshotPath . '05-testDateTimeVisibleValueWidget.png');
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '06-testDateTimeVisibleValueWidget.png');
$this
->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');
}
public function testVisibleValueAnd() {
$date = new DrupalDateTime();
$date2 = new DrupalDateTime("-1 year");
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-testDateTime ' . __FUNCTION__ . '.png');
$dates = [
$date
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
$date2
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
];
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND,
'values' => implode("\r\n", $dates),
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-testDateTime ' . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testDateTime ' . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-testDateTime ' . __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 . '05-testDateTime ' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelector, $dates[0]);
$this
->createScreenshot($this->screenshotPath . '06-testDateTime ' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
$this
->changeField($this->fieldSelector, $dates[1]);
$this
->createScreenshot($this->screenshotPath . '07-testDateTime ' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '04. Article Body field is visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-testDateTime ' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '0.5Article Body field is visible');
}
public function testVisibleValueOr() {
$date = new DrupalDateTime();
$date2 = new DrupalDateTime("-1 year");
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-testDateTimeVisibleValueOr.png');
$dates = [
$date
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
$date2
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
];
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
'values' => implode("\r\n", $dates),
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-testDateTimeVisibleValueOr.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testDateTimeVisibleValueOr.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-testDateTimeVisibleValueOr.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-testDateTimeVisibleValueOr.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelector, $dates[0]);
$this
->createScreenshot($this->screenshotPath . '06-testDateTimeVisibleValueOr.png');
$this
->waitUntilVisible('.field--name-body', 50, '03. Article Body field is not visible');
$this
->changeField($this->fieldSelector, $dates[1]);
$this
->createScreenshot($this->screenshotPath . '07-testDateTimeVisibleValueOr.png');
$this
->waitUntilVisible('.field--name-body', 50, '04. Article Body field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-testDateTimeVisibleValueOr.png');
$this
->waitUntilHidden('.field--name-body', 50, '0.5 Article Body field is visible');
}
public function testVisibleValueNot() {
$date = new DrupalDateTime();
$date2 = new DrupalDateTime("-1 year");
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-testDateTime' . __FUNCTION__ . '.png');
$dates = [
$date
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
$date2
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
];
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT,
'values' => implode("\r\n", $dates),
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-testDateTime' . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testDateTime' . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-testDateTime' . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '01. Article Body field is not visible');
$this
->changeField($this->fieldSelector, 'https://drupal.org');
$this
->createScreenshot($this->screenshotPath . '05-testDateTime' . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '02. Article Body field is not visible');
$this
->changeField($this->fieldSelector, $dates[0]);
$this
->createScreenshot($this->screenshotPath . '06-testDateTime' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '03. Article Body field is visible');
$this
->changeField($this->fieldSelector, $dates[1]);
$this
->createScreenshot($this->screenshotPath . '07-testDateTime' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '04. Article Body field is visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-testDateTime' . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '0.5 Article Body field is not visible');
}
public function testVisibleValueXor() {
$date = new DrupalDateTime();
$date2 = new DrupalDateTime("-1 year");
$this
->baseTestSteps();
$this
->createCondition('body', $this->fieldName, 'visible', 'value');
$this
->createScreenshot($this->screenshotPath . '01-testDateTime' . __FUNCTION__ . '.png');
$dates = [
$date
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
$date2
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
];
$data = [
'condition' => 'value',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR,
'values' => implode("\r\n", $dates),
'grouping' => 'AND',
'state' => 'visible',
'effect' => 'show',
];
$this
->submitForm($data, 'Save settings');
$this
->createScreenshot($this->screenshotPath . '02-testDateTime' . __FUNCTION__ . '.png');
$this
->drupalGet('admin/structure/types/manage/article/conditionals');
$this
->createScreenshot($this->screenshotPath . '03-testDateTime' . __FUNCTION__ . '.png');
$this
->assertSession()
->pageTextContains('body ' . $this->fieldName . ' visible value');
$this
->drupalGet('node/add/article');
$this
->createScreenshot($this->screenshotPath . '04-testDateTime' . __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 . '05-testDateTime' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');
$this
->changeField($this->fieldSelector, $dates[0]);
$this
->createScreenshot($this->screenshotPath . '06-testDateTime' . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '03. Article Body field is not visible');
$this
->changeField($this->fieldSelector, $dates[1]);
$this
->createScreenshot($this->screenshotPath . '07-testDateTime' . __FUNCTION__ . '.png');
$this
->waitUntilVisible('.field--name-body', 50, '04. Article Body field is not visible');
$this
->changeField($this->fieldSelector, '');
$this
->createScreenshot($this->screenshotPath . '08-testDateTime' . __FUNCTION__ . '.png');
$this
->waitUntilHidden('.field--name-body', 50, '0.5 Article Body field is visible');
}
}