You are here

public function DatetimeWidgetTest::testValidation in Typed Data API enhancements 8

@covers ::form @covers ::flagViolations

Overrides FormWidgetBrowserTestBase::testValidation

File

tests/src/Functional/TypedDataFormWidget/DatetimeWidgetTest.php, line 112

Class

DatetimeWidgetTest
Class DatetimeWidgetTest.

Namespace

Drupal\Tests\typed_data\Functional\TypedDataFormWidget

Code

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);

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();
  $path = 'admin/config/user-interface/typed-data-widgets/' . $this->widget
    ->getPluginId();
  $this
    ->drupalGet($path);

  // Try to save with no date.
  $this
    ->fillField('data[value][date]', '');
  $this
    ->pressButton('Submit');
  $assert
    ->fieldExists('data[value][date]')
    ->hasClass('error');

  // Make sure the changes have not been saved.
  $this
    ->drupalGet($path);
  $assert
    ->fieldValueEquals('data[value][date]', '2017-04-18');
  $assert
    ->fieldValueEquals('data[value][time]', '06:20:52');

  // Now try to save with no time.
  $this
    ->fillField('data[value][time]', '');
  $this
    ->pressButton('Submit');
  $assert
    ->fieldExists('data[value][time]')
    ->hasClass('error');
}