Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / datetime / tests / src / Functional / EntityResource / EntityTest / EntityTestDateonlyTest.php
1 <?php
2
3 namespace Drupal\Tests\datetime\Functional\EntityResource\EntityTest;
4
5 use Drupal\Core\Url;
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;
13
14 /**
15  * Tests the datetime field constraint with 'date' items.
16  *
17  * @group datetime
18  */
19 class EntityTestDateonlyTest extends EntityTestResourceTestBase {
20
21   use AnonResourceTestTrait;
22
23   /**
24    * The ISO date string to use throughout the test.
25    *
26    * @var string
27    */
28   protected static $dateString = '2017-03-01';
29
30   /**
31    * Datetime test field name.
32    *
33    * @var string
34    */
35   protected static $fieldName = 'field_dateonly';
36
37   /**
38    * {@inheritdoc}
39    */
40   public static $modules = ['datetime', 'entity_test'];
41
42   /**
43    * {@inheritdoc}
44    */
45   public function setUp() {
46     parent::setUp();
47
48     // Add datetime field.
49     FieldStorageConfig::create([
50       'field_name' => static::$fieldName,
51       'type' => 'datetime',
52       'entity_type' => static::$entityTypeId,
53       'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE],
54     ])
55       ->save();
56
57     FieldConfig::create([
58       'field_name' => static::$fieldName,
59       'entity_type' => static::$entityTypeId,
60       'bundle' => $this->entity->bundle(),
61       'settings' => ['default_value' => static::$dateString],
62     ])
63       ->save();
64
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();
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   protected function createEntity() {
75     $entity_test = EntityTest::create([
76       'name' => 'Llama',
77       'type' => static::$entityTypeId,
78       static::$fieldName => static::$dateString,
79     ]);
80     $entity_test->setOwnerId(0);
81     $entity_test->save();
82
83     return $entity_test;
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   protected function getExpectedNormalizedEntity() {
90     return parent::getExpectedNormalizedEntity() + [
91       static::$fieldName => [
92         [
93           'value' => $this->entity->get(static::$fieldName)->value,
94         ],
95       ],
96     ];
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   protected function getNormalizedPostEntity() {
103     return parent::getNormalizedPostEntity() + [
104       static::$fieldName => [
105         [
106           'value' => static::$dateString,
107         ],
108       ],
109     ];
110   }
111
112   /**
113    * {@inheritdoc}
114    */
115   protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {
116     parent::assertNormalizationEdgeCases($method, $url, $request_options);
117
118     if ($this->entity->getEntityType()->hasKey('bundle')) {
119       $fieldName = static::$fieldName;
120
121       // DX: 422 when date type is incorrect.
122       $normalization = $this->getNormalizedPostEntity();
123       $normalization[static::$fieldName][0]['value'] = [
124         '2017', '03', '01',
125       ];
126
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);
131
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;
136
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);
141
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;
146
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);
151     }
152   }
153
154 }