diff --git a/core/modules/hal/src/Normalizer/FileEntityNormalizer.php b/core/modules/hal/src/Normalizer/FileEntityNormalizer.php index c210b09..17efebf 100644 --- a/core/modules/hal/src/Normalizer/FileEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/FileEntityNormalizer.php @@ -7,9 +7,7 @@ namespace Drupal\hal\Normalizer; -use Drupal\Component\Utility\SafeMarkup; use Symfony\Component\Serializer\Exception\RuntimeException; -use Symfony\Component\Serializer\Exception\UnexpectedValueException; /** * Converts the Drupal entity object structure to a HAL array structure. @@ -45,13 +43,13 @@ public function denormalize($data, $class, $format = NULL, array $context = arra // Decode and save to file if it's a new file. if (!isset($context['request_method']) || $context['request_method'] != 'patch') { $file_contents = base64_decode($file_data); - $dirname = drupal_dirname($entity->getFileUri()); + $dirname = \Drupal::service('file_system')->dirname($entity->getFileUri()); file_prepare_directory($dirname, FILE_CREATE_DIRECTORY); if ($uri = file_unmanaged_save_data($file_contents, $entity->getFileUri())) { $entity->setFileUri($uri); } else { - throw new RuntimeException(SafeMarkup::format('Failed to write @filename.', array('@filename' => $entity->getFilename()))); + throw new RuntimeException('failed to write ' . $entity->getFilename()); } } diff --git a/core/modules/hal/src/Tests/EntityTest.php b/core/modules/hal/src/Tests/EntityTest.php index 5bc136fd..b227eae 100644 --- a/core/modules/hal/src/Tests/EntityTest.php +++ b/core/modules/hal/src/Tests/EntityTest.php @@ -8,6 +8,8 @@ namespace Drupal\hal\Tests; use Drupal\comment\Tests\CommentTestTrait; +use Drupal\file\Entity\File; +use Drupal\user\Entity\User; /** * Tests that nodes and terms are correctly normalized and denormalized. @@ -227,7 +229,9 @@ public function testComment() { * Tests the normalization of files. */ public function testFile() { - $user = entity_create('user', array('name' => $this->randomMachineName())); + $user = User::create([ + 'name' => $this->randomMachineName(), + ]); $user->save(); $file_uri = 'public://' . $this->randomMachineName(); @@ -236,11 +240,11 @@ public function testFile() { $data = file_get_contents($file_uri); $data = base64_encode($data); file_put_contents($file_uri, 'hello world'); - $file = entity_create('file', array( + $file = File::create([ 'uid' => $user->id(), 'uri' => $file_uri, 'status' => FILE_STATUS_PERMANENT, - )); + ]); $file->save(); $original_values = $file->toArray(); @@ -250,7 +254,7 @@ public function testFile() { // 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, 'Drupal\file\Entity\File', $this->format, array('request_method' => 'patch')); + $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()); diff --git a/core/modules/hal/src/Tests/FileFieldNormalizeTest.php b/core/modules/hal/src/Tests/FileFieldNormalizeTest.php index 7975d7d..b0cfa22 100644 --- a/core/modules/hal/src/Tests/FileFieldNormalizeTest.php +++ b/core/modules/hal/src/Tests/FileFieldNormalizeTest.php @@ -19,14 +19,14 @@ class FileFieldNormalizeTest extends NormalizerTestBase { /** * {@inheritdoc} */ - public static $modules = array( + public static $modules = [ 'entity_test', 'field', 'image', 'hal', 'system', 'file', - ); + ]; /** * {@inheritdoc} @@ -34,7 +34,7 @@ class FileFieldNormalizeTest extends NormalizerTestBase { public function setUp() { parent::setUp(); $this->installEntitySchema('file'); - $this->installSchema('file', array('file_usage')); + $this->installSchema('file', ['file_usage']); } /** @@ -44,35 +44,35 @@ public function testFileFieldNormalize() { // Create a file. $file_name = $this->randomMachineName() . '.txt'; file_put_contents("public://$file_name", $this->randomString()); - $file = entity_create('file', array( + $file = File::create([ 'uri' => "public://$file_name", )); $file->save(); // Attach a file field to the bundle. - FieldStorageConfig::create(array( + FieldStorageConfig::create([ 'type' => 'file', 'entity_type' => 'entity_test', 'field_name' => 'field_file', - ))->save(); - FieldConfig::create(array( + ])->save(); + FieldConfig::create([ 'field_name' => 'field_file', 'entity_type' => 'entity_test', 'bundle' => 'entity_test', - ))->save(); + ])->save(); // Create an entity referencing the file. - $entity = entity_create('entity_test', array( - 'field_file' => array( + $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, 'Drupal\entity_test\Entity\EntityTest', $this->format); - $this->assertEqual($entity->toArray()['field_file'], $deserialized->toArray()['field_file'], "File field is preserved."); + $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.'); } /** @@ -82,36 +82,36 @@ public function testImageFieldNormalize() { // Create a file. $file_name = $this->randomMachineName() . '.png'; file_put_contents("public://$file_name", $this->randomString()); - $file = entity_create('file', array( + $file = File::create([ 'uri' => "public://$file_name", - )); + ]); $file->save(); // Attach an image field to the bundle. - FieldStorageConfig::create(array( + FieldStorageConfig::create([ 'type' => 'image', 'entity_type' => 'entity_test', 'field_name' => 'field_image', - ))->save(); - FieldConfig::create(array( + ])->save(); + FieldConfig::create([ 'field_name' => 'field_image', 'entity_type' => 'entity_test', 'bundle' => 'entity_test', - ))->save(); + ])->save(); // Create an entity referencing the file. - $entity = entity_create('entity_test', array( - 'field_image' => array( + $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, 'Drupal\entity_test\Entity\EntityTest', $this->format); - $this->assertEqual($entity->toArray()['field_image'], $deserialized->toArray()['field_image'], "Image field is preserved."); + $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.'); } }