3 namespace Drupal\Tests\datetime\Functional\EntityResource\EntityTest;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\field\Entity\FieldStorageConfig;
10 use Drupal\Tests\entity_test\Functional\Rest\EntityTestResourceTestBase;
11 use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
12 use GuzzleHttp\RequestOptions;
15 * Tests the datetime field constraint with 'date' items.
19 class EntityTestDateonlyTest extends EntityTestResourceTestBase {
21 use AnonResourceTestTrait;
24 * The ISO date string to use throughout the test.
28 protected static $dateString = '2017-03-01';
31 * Datetime test field name.
35 protected static $fieldName = 'field_dateonly';
40 public static $modules = ['datetime', 'entity_test'];
45 public function setUp() {
48 // Add datetime field.
49 FieldStorageConfig::create([
50 'field_name' => static::$fieldName,
52 'entity_type' => static::$entityTypeId,
53 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE],
58 'field_name' => static::$fieldName,
59 'entity_type' => static::$entityTypeId,
60 'bundle' => $this->entity->bundle(),
61 'settings' => ['default_value' => static::$dateString],
65 // Reload entity so that it has the new field.
66 $this->entity = $this->entityStorage->load($this->entity->id());
67 $this->entity->set(static::$fieldName, ['value' => static::$dateString]);
68 $this->entity->save();
74 protected function createEntity() {
75 $entity_test = EntityTest::create([
77 'type' => static::$entityTypeId,
78 static::$fieldName => static::$dateString,
80 $entity_test->setOwnerId(0);
89 protected function getExpectedNormalizedEntity() {
90 return parent::getExpectedNormalizedEntity() + [
91 static::$fieldName => [
93 'value' => $this->entity->get(static::$fieldName)->value,
102 protected function getNormalizedPostEntity() {
103 return parent::getNormalizedPostEntity() + [
104 static::$fieldName => [
106 'value' => static::$dateString,
115 protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {
116 parent::assertNormalizationEdgeCases($method, $url, $request_options);
118 if ($this->entity->getEntityType()->hasKey('bundle')) {
119 $fieldName = static::$fieldName;
121 // DX: 422 when date type is incorrect.
122 $normalization = $this->getNormalizedPostEntity();
123 $normalization[static::$fieldName][0]['value'] = [
127 $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
128 $response = $this->request($method, $url, $request_options);
129 $message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value must be a string.\n{$fieldName}.0.value: This value should be of the correct primitive type.\n";
130 $this->assertResourceErrorResponse(422, $message, $response);
132 // DX: 422 when date format is incorrect.
133 $normalization = $this->getNormalizedPostEntity();
134 $value = '2017-03-01T01:02:03';
135 $normalization[static::$fieldName][0]['value'] = $value;
137 $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
138 $response = $this->request($method, $url, $request_options);
139 $message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value '{$value}' is invalid for the format 'Y-m-d'\n";
140 $this->assertResourceErrorResponse(422, $message, $response);
142 // DX: 422 when value is not a valid date.
143 $normalization = $this->getNormalizedPostEntity();
144 $value = '2017-13-55';
145 $normalization[static::$fieldName][0]['value'] = $value;
147 $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
148 $response = $this->request($method, $url, $request_options);
149 $message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value '{$value}' did not parse properly for the format 'Y-m-d'\n{$fieldName}.0.value: This value should be of the correct primitive type.\n";
150 $this->assertResourceErrorResponse(422, $message, $response);