View source
<?php
namespace Drupal\FunctionalTests\Datetime;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
class TimestampTest extends BrowserTestBase {
protected $displayOptions;
protected $fieldStorage;
protected $field;
public static $modules = [
'node',
'entity_test',
'field_ui',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$web_user = $this
->drupalCreateUser([
'access content',
'view test entity',
'administer entity_test content',
'administer entity_test form display',
'administer content types',
'administer node fields',
]);
$this
->drupalLogin($web_user);
$field_name = 'field_timestamp';
$type = 'timestamp';
$widget_type = 'datetime_timestamp';
$formatter_type = 'timestamp';
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => $type,
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
'required' => TRUE,
]);
$this->field
->save();
EntityFormDisplay::load('entity_test.entity_test.default')
->setComponent($field_name, [
'type' => $widget_type,
])
->save();
$this->displayOptions = [
'type' => $formatter_type,
'label' => 'hidden',
];
EntityViewDisplay::create([
'targetEntityType' => $this->field
->getTargetEntityTypeId(),
'bundle' => $this->field
->getTargetBundle(),
'mode' => 'full',
'status' => TRUE,
])
->setComponent($field_name, $this->displayOptions)
->save();
}
public function testWidget() {
$value = '2012-12-31 00:00:00';
$date = new DrupalDateTime($value, 'UTC');
$date
->setTimezone(timezone_open(date_default_timezone_get()));
$this
->drupalGet('entity_test/add');
$fields = $this
->xpath('//div[contains(@class, "field--widget-datetime-timestamp") and @id="edit-field-timestamp-wrapper"]');
$this
->assertCount(1, $fields);
$this
->assertSession()
->fieldExists('field_timestamp[0][value][date]');
$this
->assertSession()
->fieldValueEquals('field_timestamp[0][value][date]', '');
$this
->assertSession()
->fieldExists('field_timestamp[0][value][time]');
$this
->assertSession()
->fieldValueEquals('field_timestamp[0][value][time]', '');
$date_format = DateFormat::load('html_date')
->getPattern();
$time_format = DateFormat::load('html_time')
->getPattern();
$edit = [
'field_timestamp[0][value][date]' => $date
->format($date_format),
'field_timestamp[0][value][time]' => $date
->format($time_format),
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertSession()
->fieldExists('field_timestamp[0][value][date]');
$this
->assertSession()
->fieldValueEquals('field_timestamp[0][value][date]', $date
->format($date_format));
$this
->assertSession()
->fieldExists('field_timestamp[0][value][time]');
$this
->assertSession()
->fieldValueEquals('field_timestamp[0][value][time]', $date
->format($time_format));
preg_match('|entity_test/manage/(\\d+)|', $this
->getSession()
->getCurrentUrl(), $match);
$id = $match[1];
$this
->assertSession()
->pageTextContains(sprintf('entity_test %s has been created.', $id));
$medium = DateFormat::load('medium')
->getPattern();
$this
->drupalGet('entity_test/' . $id);
$this
->assertSession()
->pageTextContains($date
->format($medium));
}
}