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: $this->drupalLogin($web_user); Chris@0: Chris@0: // Create a field with settings to validate. Chris@0: $this->createField(); Chris@0: Chris@0: $this->dateFormatter = $this->container->get('date.formatter'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a date test field. Chris@0: */ Chris@0: protected function createField() { Chris@17: $field_name = mb_strtolower($this->randomMachineName()); Chris@17: $field_label = Unicode::ucfirst(mb_strtolower($this->randomMachineName())); Chris@0: $type = $this->getTestFieldType(); Chris@0: $widget_type = $formatter_type = $type . '_default'; 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: 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE], Chris@0: ]); Chris@0: $this->fieldStorage->save(); Chris@0: $this->field = FieldConfig::create([ Chris@0: 'field_storage' => $this->fieldStorage, Chris@0: 'label' => $field_label, Chris@0: 'bundle' => 'entity_test', Chris@0: 'description' => 'Description for ' . $field_label, 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: 'settings' => ['format_type' => 'medium'] + $this->defaultSettings, 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: * Renders a entity_test and sets the output in the internal browser. Chris@0: * Chris@0: * @param int $id Chris@0: * The entity_test ID to render. Chris@0: * @param string $view_mode Chris@0: * (optional) The view mode to use for rendering. Defaults to 'full'. Chris@0: * @param bool $reset Chris@0: * (optional) Whether to reset the entity_test controller cache. Defaults to Chris@0: * TRUE to simplify testing. Chris@0: * Chris@0: * @return string Chris@0: * The rendered HTML output. Chris@0: */ Chris@0: protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) { Chris@0: if ($reset) { Chris@0: $this->container->get('entity_type.manager')->getStorage('entity_test')->resetCache([$id]); Chris@0: } Chris@0: $entity = EntityTest::load($id); Chris@0: $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode); Chris@0: $build = $display->build($entity); Chris@0: return (string) $this->container->get('renderer')->renderRoot($build); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the site timezone to a given timezone. Chris@0: * Chris@0: * @param string $timezone Chris@0: * The timezone identifier to set. Chris@0: */ Chris@0: protected function setSiteTimezone($timezone) { Chris@0: // Set an explicit site timezone, and disallow per-user timezones. Chris@0: $this->config('system.date') Chris@0: ->set('timezone.user.configurable', 0) Chris@0: ->set('timezone.default', $timezone) Chris@0: ->save(); Chris@0: } Chris@0: Chris@14: /** Chris@14: * Massages test date values. Chris@14: * Chris@14: * If a date object is generated directly by a test, then it needs to be Chris@14: * adjusted to behave like the computed date from the item. Chris@14: * Chris@14: * @param \Drupal\Core\Datetime\DrupalDateTime $date Chris@14: * A date object directly generated by the test. Chris@14: */ Chris@14: protected function massageTestDate($date) { Chris@14: if ($this->field->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) { Chris@14: // Set the default time for date-only items. Chris@14: $date->setDefaultDateTime(); Chris@14: } Chris@14: } Chris@14: Chris@0: }