DatetimeWidgetTest.php in Typed Data API enhancements 8
File
tests/src/Functional/TypedDataFormWidget/DatetimeWidgetTest.php
View source
<?php
namespace Drupal\Tests\typed_data\Functional\TypedDataFormWidget;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\ListDataDefinition;
use Drupal\Core\TypedData\MapDataDefinition;
class DatetimeWidgetTest extends FormWidgetBrowserTestBase {
protected $widget;
protected function setUp() : void {
parent::setUp();
$this->widget = $this
->getFormWidgetManager()
->createInstance('datetime');
$this
->drupalLogin($this
->createUser([], NULL, TRUE));
}
public function testIsApplicable() {
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('any')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('binary')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('boolean')));
$this
->assertTrue($this->widget
->isApplicable(DataDefinition::create('datetime_iso8601')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('duration_iso8601')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('email')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('float')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('integer')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('string')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('timespan')));
$this
->assertTrue($this->widget
->isApplicable(DataDefinition::create('timestamp')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('uri')));
$this
->assertFalse($this->widget
->isApplicable(ListDataDefinition::create('string')));
$this
->assertFalse($this->widget
->isApplicable(MapDataDefinition::create()));
}
public function testFormEditing() {
$context_definition = ContextDefinition::create('datetime_iso8601')
->setLabel('Test Example Date and Time')
->setDescription('Enter the date and time.')
->setDefaultValue('2017-04-18T06:20:52');
$this->container
->get('state')
->set('typed_data_widgets.definition', $context_definition);
$path = 'admin/config/user-interface/typed-data-widgets/' . $this->widget
->getPluginId();
$this
->drupalGet($path);
$assert = $this
->assertSession();
$assert
->elementExists('xpath', '//div[@id="edit-data-value"]/preceding-sibling::h4[contains(text(), "' . $context_definition
->getLabel() . '")]');
$assert
->elementTextContains('css', 'div[id=edit-data-value--description]', $context_definition
->getDescription());
$default = new DrupalDateTime($context_definition
->getDefaultValue());
$this
->assertEquals('2017-04-18', $default
->format('Y-m-d'));
$this
->assertEquals('06:20:52', $default
->format('H:i:s'));
$assert
->fieldValueEquals('data[value][date]', '2017-04-18');
$assert
->fieldValueEquals('data[value][time]', '06:20:52');
$this
->fillField('data[value][date]', '2020-01-28');
$this
->fillField('data[value][time]', '14:00:00');
$this
->pressButton('Submit');
$assert
->pageTextContains('Value saved');
$assert
->fieldValueEquals('data[value][date]', '2020-01-28');
$assert
->fieldValueEquals('data[value][time]', '14:00:00');
$this
->pressButton('Reset');
$assert
->pageTextContains('Value reset to default');
$assert
->fieldValueEquals('data[value][date]', '2017-04-18');
$assert
->fieldValueEquals('data[value][time]', '06:20:52');
}
public function testValidation() {
$context_definition = ContextDefinition::create('datetime_iso8601')
->setLabel('Test Date and Time')
->setDefaultValue('2017-04-18T06:20:52');
$this->container
->get('state')
->set('typed_data_widgets.definition', $context_definition);
$assert = $this
->assertSession();
$path = 'admin/config/user-interface/typed-data-widgets/' . $this->widget
->getPluginId();
$this
->drupalGet($path);
$this
->fillField('data[value][date]', '');
$this
->pressButton('Submit');
$assert
->fieldExists('data[value][date]')
->hasClass('error');
$this
->drupalGet($path);
$assert
->fieldValueEquals('data[value][date]', '2017-04-18');
$assert
->fieldValueEquals('data[value][time]', '06:20:52');
$this
->fillField('data[value][time]', '');
$this
->pressButton('Submit');
$assert
->fieldExists('data[value][time]')
->hasClass('error');
}
public function testNoDefault() {
$context_definition = ContextDefinition::create('datetime_iso8601')
->setLabel('Test Example Date and Time');
$this->container
->get('state')
->set('typed_data_widgets.definition', $context_definition);
$path = 'admin/config/user-interface/typed-data-widgets/' . $this->widget
->getPluginId();
$this
->drupalGet($path);
$assert = $this
->assertSession();
$assert
->fieldValueEquals('data[value][date]', '0000-01-01');
$assert
->fieldValueEquals('data[value][time]', '12:00:00');
}
}