X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Fdatetime%2Ftests%2Fsrc%2FFunctional%2FEntityResource%2FEntityTest%2FEntityTestDatetimeTest.php;fp=web%2Fcore%2Fmodules%2Fdatetime%2Ftests%2Fsrc%2FFunctional%2FEntityResource%2FEntityTest%2FEntityTestDatetimeTest.php;h=ffce48c340cffbcb4ef68a7c8d6bd59dfc2ad143;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hp=0000000000000000000000000000000000000000;hpb=aea91e65e895364e460983b890e295aa5d5540a5;p=yaffs-website diff --git a/web/core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDatetimeTest.php b/web/core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDatetimeTest.php new file mode 100644 index 000000000..ffce48c34 --- /dev/null +++ b/web/core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDatetimeTest.php @@ -0,0 +1,154 @@ + static::$fieldName, + 'type' => 'datetime', + 'entity_type' => static::$entityTypeId, + 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME], + ]) + ->save(); + + FieldConfig::create([ + 'field_name' => static::$fieldName, + 'entity_type' => static::$entityTypeId, + 'bundle' => $this->entity->bundle(), + 'settings' => ['default_value' => static::$dateString], + ]) + ->save(); + + // Reload entity so that it has the new field. + $this->entity = $this->entityStorage->load($this->entity->id()); + $this->entity->set(static::$fieldName, ['value' => static::$dateString]); + $this->entity->save(); + } + + /** + * {@inheritdoc} + */ + protected function createEntity() { + $entity_test = EntityTest::create([ + 'name' => 'Llama', + 'type' => static::$entityTypeId, + static::$fieldName => static::$dateString, + ]); + $entity_test->setOwnerId(0); + $entity_test->save(); + + return $entity_test; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedNormalizedEntity() { + return parent::getExpectedNormalizedEntity() + [ + static::$fieldName => [ + [ + 'value' => $this->entity->get(static::$fieldName)->value, + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function getNormalizedPostEntity() { + return parent::getNormalizedPostEntity() + [ + static::$fieldName => [ + [ + 'value' => static::$dateString, + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) { + parent::assertNormalizationEdgeCases($method, $url, $request_options); + + if ($this->entity->getEntityType()->hasKey('bundle')) { + $fieldName = static::$fieldName; + + // DX: 422 when date type is incorrect. + $normalization = $this->getNormalizedPostEntity(); + $normalization[static::$fieldName][0]['value'] = [ + '2017', '03', '01', '21', '53', '00', + ]; + + $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format); + $response = $this->request($method, $url, $request_options); + $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"; + $this->assertResourceErrorResponse(422, $message, $response); + + // DX: 422 when date format is incorrect. + $normalization = $this->getNormalizedPostEntity(); + $value = '2017-03-01'; + $normalization[static::$fieldName][0]['value'] = $value; + + $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format); + $response = $this->request($method, $url, $request_options); + $message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value '{$value}' is invalid for the format 'Y-m-d\\TH:i:s'\n"; + $this->assertResourceErrorResponse(422, $message, $response); + + // DX: 422 when date format is incorrect. + $normalization = $this->getNormalizedPostEntity(); + $value = '2017-13-55T20:02:00'; + $normalization[static::$fieldName][0]['value'] = $value; + + $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format); + $response = $this->request($method, $url, $request_options); + $message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value '{$value}' did not parse properly for the format 'Y-m-d\\TH:i:s'\n{$fieldName}.0.value: This value should be of the correct primitive type.\n"; + $this->assertResourceErrorResponse(422, $message, $response); + } + } + +}