Chris@0: drupalCreateUser([ Chris@0: 'access content', Chris@0: 'view test entity', Chris@0: 'administer entity_test content', Chris@0: 'administer entity_test form display', Chris@0: 'administer content types', Chris@0: 'administer node fields', Chris@0: ]); Chris@0: Chris@0: $this->drupalLogin($web_user); Chris@0: $field_name = 'field_timestamp'; Chris@0: $type = 'timestamp'; Chris@0: $widget_type = 'datetime_timestamp'; Chris@0: $formatter_type = 'timestamp'; Chris@0: Chris@0: $this->fieldStorage = FieldStorageConfig::create([ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'entity_test', Chris@0: 'type' => $type, Chris@0: ]); Chris@0: $this->fieldStorage->save(); Chris@0: $this->field = FieldConfig::create([ Chris@0: 'field_storage' => $this->fieldStorage, Chris@0: 'bundle' => 'entity_test', Chris@0: 'required' => TRUE, Chris@0: ]); Chris@0: $this->field->save(); Chris@0: Chris@0: EntityFormDisplay::load('entity_test.entity_test.default') Chris@0: ->setComponent($field_name, ['type' => $widget_type]) Chris@0: ->save(); Chris@0: Chris@0: $this->displayOptions = [ Chris@0: 'type' => $formatter_type, Chris@0: 'label' => 'hidden', Chris@0: ]; Chris@0: Chris@0: EntityViewDisplay::create([ Chris@0: 'targetEntityType' => $this->field->getTargetEntityTypeId(), Chris@0: 'bundle' => $this->field->getTargetBundle(), Chris@0: 'mode' => 'full', Chris@0: 'status' => TRUE, Chris@0: ])->setComponent($field_name, $this->displayOptions) Chris@0: ->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the "datetime_timestamp" widget. Chris@0: */ Chris@0: public function testWidget() { Chris@0: // Build up a date in the UTC timezone. Chris@0: $value = '2012-12-31 00:00:00'; Chris@0: $date = new DrupalDateTime($value, 'UTC'); Chris@0: Chris@0: // Update the timezone to the system default. Chris@0: $date->setTimezone(timezone_open(drupal_get_user_timezone())); Chris@0: Chris@0: // Display creation form. Chris@0: $this->drupalGet('entity_test/add'); Chris@0: Chris@0: // Make sure the "datetime_timestamp" widget is on the page. Chris@0: $fields = $this->xpath('//div[contains(@class, "field--widget-datetime-timestamp") and @id="edit-field-timestamp-wrapper"]'); Chris@0: $this->assertEquals(1, count($fields)); Chris@0: Chris@0: // Look for the widget elements and make sure they are empty. Chris@0: $this->assertSession()->fieldExists('field_timestamp[0][value][date]'); Chris@0: $this->assertSession()->fieldValueEquals('field_timestamp[0][value][date]', ''); Chris@0: $this->assertSession()->fieldExists('field_timestamp[0][value][time]'); Chris@0: $this->assertSession()->fieldValueEquals('field_timestamp[0][value][time]', ''); Chris@0: Chris@0: // Submit the date. Chris@0: $date_format = DateFormat::load('html_date')->getPattern(); Chris@0: $time_format = DateFormat::load('html_time')->getPattern(); Chris@0: Chris@0: $edit = [ Chris@0: 'field_timestamp[0][value][date]' => $date->format($date_format), Chris@0: 'field_timestamp[0][value][time]' => $date->format($time_format), Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, 'Save'); Chris@0: Chris@0: // Make sure the submitted date is set as the default in the widget. Chris@0: $this->assertSession()->fieldExists('field_timestamp[0][value][date]'); Chris@0: $this->assertSession()->fieldValueEquals('field_timestamp[0][value][date]', $date->format($date_format)); Chris@0: $this->assertSession()->fieldExists('field_timestamp[0][value][time]'); Chris@0: $this->assertSession()->fieldValueEquals('field_timestamp[0][value][time]', $date->format($time_format)); Chris@0: Chris@0: // Make sure the entity was saved. Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->getSession()->getCurrentUrl(), $match); Chris@0: $id = $match[1]; Chris@0: $this->assertSession()->pageTextContains(sprintf('entity_test %s has been created.', $id)); Chris@0: Chris@0: // Make sure the timestamp is output properly with the default formatter. Chris@0: $medium = DateFormat::load('medium')->getPattern(); Chris@0: $this->drupalGet('entity_test/' . $id); Chris@0: $this->assertSession()->pageTextContains($date->format($medium)); Chris@0: } Chris@0: Chris@0: }