diff --git a/core/modules/hal/src/Tests/FileFieldNormalizeTest.php b/core/modules/hal/src/Tests/FileFieldNormalizeTest.php deleted file mode 100644 index d03b088..0000000 --- a/core/modules/hal/src/Tests/FileFieldNormalizeTest.php +++ /dev/null @@ -1,117 +0,0 @@ -installEntitySchema('file'); - $this->installSchema('file', ['file_usage']); - } - - /** - * Tests that file field is identical before and after de/serialization. - */ - public function testFileFieldNormalize() { - // Create a file. - $file_name = 'test_file_field_normalize.txt'; - file_put_contents("public://$file_name", 'hello world'); - $file = File::create([ - 'uri' => "public://$file_name", - )); - $file->save(); - - // Attach a file field to the bundle. - FieldStorageConfig::create([ - 'type' => 'file', - 'entity_type' => 'entity_test', - 'field_name' => 'field_file', - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_file', - 'entity_type' => 'entity_test', - 'bundle' => 'entity_test', - ])->save(); - - // Create an entity referencing the file. - $entity = EntityTest::create([ - 'field_file' => [ - 'target_id' => $file->id(), - 'display' => 0, - 'description' => 'An attached file', - ], - ]); - - $serialized = $this->container->get('serializer')->serialize($entity, $this->format); - $deserialized = $this->container->get('serializer')->deserialize($serialized, EntityTest::class, $this->format); - $this->assertEqual($entity->toArray()['field_file'], $deserialized->toArray()['field_file'], 'File field is preserved.'); - } - - /** - * Tests that image field is identical before and after de/serialization. - */ - public function testImageFieldNormalize() { - // Create a file. - $file_name = $this->randomMachineName() . '.png'; - file_put_contents("public://$file_name", $this->randomString()); - $file = File::create([ - 'uri' => "public://$file_name", - ]); - $file->save(); - - // Attach an image field to the bundle. - FieldStorageConfig::create([ - 'type' => 'image', - 'entity_type' => 'entity_test', - 'field_name' => 'field_image', - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_image', - 'entity_type' => 'entity_test', - 'bundle' => 'entity_test', - ])->save(); - - // Create an entity referencing the file. - $entity = EntityTest::create([ - 'field_image' => [ - 'target_id' => $file->id(), - 'title' => $this->randomString(), - 'alt' => $this->randomString(), - 'width' => 400, - 'height' => 300, - ], - ]); - - $serialized = $this->container->get('serializer')->serialize($entity, $this->format); - $deserialized = $this->container->get('serializer')->deserialize($serialized, EntityTest::class, $this->format); - $this->assertEqual($entity->toArray()['field_image'], $deserialized->toArray()['field_image'], 'Image field is preserved.'); - } -} diff --git a/core/modules/hal/tests/src/Kernel/EntityNormalizeTest.php b/core/modules/hal/tests/src/Kernel/EntityNormalizeTest.php index b44c3ed..2c1e5b5 100644 --- a/core/modules/hal/tests/src/Kernel/EntityNormalizeTest.php +++ b/core/modules/hal/tests/src/Kernel/EntityNormalizeTest.php @@ -218,31 +218,31 @@ public function testFile() { 'name' => 'fileTestingUser', ]); $user->save(); + $file_uri = 'public://normalization_test_file'; $file_contents = 'hello world'; $data = base64_encode($file_contents); file_put_contents($file_uri, $file_contents); + $file = File::create([ 'uid' => $user->id(), 'uri' => $file_uri, 'status' => FILE_STATUS_PERMANENT, ]); + $file->save(); $original_values = $file->toArray(); - unset($original_values['fid']); - $normalized = $this->serializer->normalize($file, $this->format); + // Adding data to the entity. $normalized['data'][0]['value'] = $data; + // Use PATCH to avoid trying to create new file on denormalize. $denormalized_file = $this->serializer->denormalize($normalized, File::class, $this->format, ['request_method' => 'patch']); - // Verify that the ID was skipped by the normalizer. - $this->assertEqual(NULL, $denormalized_file->id()); - // Loop over the remaining fields and verify that they are identical. foreach ($original_values as $field_name => $field_values) { - $this->assertEqual($field_values, $denormalized_file->get($field_name)->getValue()); + $this->assertEquals($field_values, $denormalized_file->get($field_name)->getValue()); } } } diff --git a/core/modules/hal/tests/src/Kernel/FileFieldNormalizeTest.php b/core/modules/hal/tests/src/Kernel/FileFieldNormalizeTest.php new file mode 100644 index 0000000..e122658 --- /dev/null +++ b/core/modules/hal/tests/src/Kernel/FileFieldNormalizeTest.php @@ -0,0 +1,119 @@ +installEntitySchema('file'); + $this->installSchema('file', ['file_usage']); + } + + /** + * Tests that file field is identical before and after de/serialization. + */ + public function testFileFieldNormalize() { + // Create a file. + $file_name = 'test_file_field_normalize.txt'; + file_put_contents("public://$file_name", 'hello world'); + $file = File::create([ + 'uri' => "public://$file_name", + ]); + $file->save(); + + // Attach a file field to the bundle. + FieldStorageConfig::create([ + 'type' => 'file', + 'entity_type' => 'entity_test', + 'field_name' => 'field_file', + ])->save(); + FieldConfig::create([ + 'field_name' => 'field_file', + 'entity_type' => 'entity_test', + 'bundle' => 'entity_test', + ])->save(); + + // Create an entity referencing the file. + $entity = EntityTest::create([ + 'field_file' => [ + 'target_id' => $file->id(), + 'display' => 0, + 'description' => 'An attached file', + ], + ]); + + $serialized = $this->container->get('serializer')->serialize($entity, $this->format); + $deserialized = $this->container->get('serializer')->deserialize($serialized, EntityTest::class, $this->format); + $this->assertEqual($entity->toArray()['field_file'], $deserialized->toArray()['field_file'], 'File field is preserved.'); + } + + /** + * Tests that image field is identical before and after de/serialization. + */ + public function testImageFieldNormalize() { + // Create a file. + $file_name = $this->randomMachineName() . '.png'; + file_put_contents("public://$file_name", $this->randomString()); + $file = File::create([ + 'uri' => "public://$file_name", + ]); + $file->save(); + + // Attach an image field to the bundle. + FieldStorageConfig::create([ + 'type' => 'image', + 'entity_type' => 'entity_test', + 'field_name' => 'field_image', + ])->save(); + FieldConfig::create([ + 'field_name' => 'field_image', + 'entity_type' => 'entity_test', + 'bundle' => 'entity_test', + ])->save(); + + // Create an entity referencing the file. + $entity = EntityTest::create([ + 'field_image' => [ + 'target_id' => $file->id(), + 'title' => $this->randomString(), + 'alt' => $this->randomString(), + 'width' => 400, + 'height' => 300, + ], + ]); + + $serialized = $this->container->get('serializer')->serialize($entity, $this->format); + $deserialized = $this->container->get('serializer')->deserialize($serialized, EntityTest::class, $this->format); + $this->assertEqual($entity->toArray()['field_image'], $deserialized->toArray()['field_image'], 'Image field is preserved.'); + } +}