From 505a062e52fd996d3cba83b7893e2fe53609b358 Mon Sep 17 00:00:00 2001
From: Dries <dries@buytaert.net>
Date: Wed, 18 Jun 2014 21:54:41 +0200
Subject: [PATCH] Issue #2277705 by mashermike, juampy: Fixed Files don't have
 URI nor href.

---
 core/modules/file/src/Entity/File.php         |  7 ++
 core/modules/file/src/Tests/FileItemTest.php  |  1 +
 .../src/Normalizer/FileEntityNormalizer.php   |  2 +-
 .../hal/src/Tests/FileNormalizeTest.php       | 90 +++++++++++++++++++
 4 files changed, 99 insertions(+), 1 deletion(-)
 create mode 100644 core/modules/hal/src/Tests/FileNormalizeTest.php

diff --git a/core/modules/file/src/Entity/File.php b/core/modules/file/src/Entity/File.php
index b7dc34755578..f61c49147852 100644
--- a/core/modules/file/src/Entity/File.php
+++ b/core/modules/file/src/Entity/File.php
@@ -74,6 +74,13 @@ public function setFileUri($uri) {
     $this->get('uri')->value = $uri;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function url($rel = 'canonical', $options = array()) {
+    return file_create_url($this->getFileUri());
+  }
+
   /**
    * {@inheritdoc}
    */
diff --git a/core/modules/file/src/Tests/FileItemTest.php b/core/modules/file/src/Tests/FileItemTest.php
index 57b908a71ab6..ea3ba4ade2b0 100644
--- a/core/modules/file/src/Tests/FileItemTest.php
+++ b/core/modules/file/src/Tests/FileItemTest.php
@@ -82,6 +82,7 @@ public function testFileItem() {
     $this->assertEqual($entity->file_test->display, 1);
     $this->assertEqual($entity->file_test->description, $description);
     $this->assertEqual($entity->file_test->entity->getFileUri(), $this->file->getFileUri());
+    $this->assertEqual($entity->file_test->entity->url(), $url = file_create_url($this->file->getFileUri()));
     $this->assertEqual($entity->file_test->entity->id(), $this->file->id());
     $this->assertEqual($entity->file_test->entity->uuid(), $this->file->uuid());
 
diff --git a/core/modules/hal/src/Normalizer/FileEntityNormalizer.php b/core/modules/hal/src/Normalizer/FileEntityNormalizer.php
index 18ddf99d099f..f993024f6060 100644
--- a/core/modules/hal/src/Normalizer/FileEntityNormalizer.php
+++ b/core/modules/hal/src/Normalizer/FileEntityNormalizer.php
@@ -55,7 +55,7 @@ public function __construct(EntityManagerInterface $entity_manager, ClientInterf
   public function normalize($entity, $format = NULL, array $context = array()) {
     $data = parent::normalize($entity, $format, $context);
     // Replace the file url with a full url for the file.
-    $data['uri'][0]['value'] = file_create_url($data['uri'][0]['value']);
+    $data['uri'][0]['value'] = $this->getEntityUri($entity);
 
     return $data;
   }
diff --git a/core/modules/hal/src/Tests/FileNormalizeTest.php b/core/modules/hal/src/Tests/FileNormalizeTest.php
new file mode 100644
index 000000000000..a3ecbf191846
--- /dev/null
+++ b/core/modules/hal/src/Tests/FileNormalizeTest.php
@@ -0,0 +1,90 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\hal\Tests\FileNormalizeTest.
+ */
+
+namespace Drupal\hal\Tests;
+
+use Drupal\Core\Cache\MemoryBackend;
+use Drupal\hal\Encoder\JsonEncoder;
+use Drupal\hal\Normalizer\FieldItemNormalizer;
+use Drupal\hal\Normalizer\FileEntityNormalizer;
+use Drupal\rest\LinkManager\LinkManager;
+use Drupal\rest\LinkManager\RelationLinkManager;
+use Drupal\rest\LinkManager\TypeLinkManager;
+use Symfony\Component\Serializer\Serializer;
+
+
+/**
+ * Test the HAL normalizer.
+ */
+class FileNormalizeTest extends NormalizerTestBase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'File Normalize Test',
+      'description' => 'Test that file entities can be normalized in HAL.',
+      'group' => 'HAL',
+    );
+  }
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = array('file');
+
+  /**
+   * {@inheritdoc}
+   */
+  function setUp() {
+    parent::setUp();
+    $this->installEntitySchema('file');
+
+    $entity_manager = \Drupal::entityManager();
+    $link_manager = new LinkManager(new TypeLinkManager(new MemoryBackend('default')), new RelationLinkManager(new MemoryBackend('default'), $entity_manager));
+
+    // Set up the mock serializer.
+    $normalizers = array(
+      new FieldItemNormalizer(),
+      new FileEntityNormalizer($entity_manager, \Drupal::httpClient(), $link_manager, \Drupal::moduleHandler()),
+    );
+
+    $encoders = array(
+      new JsonEncoder(),
+    );
+    $this->serializer = new Serializer($normalizers, $encoders);
+  }
+
+
+  /**
+   * Tests the normalize function.
+   */
+  public function testNormalize() {
+    $file_params = array(
+      'filename' => 'test_1.txt',
+      'uri' => 'public://test_1.txt',
+      'filemime' => 'text/plain',
+      'status' => FILE_STATUS_PERMANENT,
+    );
+    // Create a new file entity.
+    $file = entity_create('file', $file_params);
+    file_put_contents($file->getFileUri(), 'hello world');
+    $file->save();
+
+    $expected_array = array(
+      'uri' => array(
+        array(
+          'value' => file_create_url($file->getFileUri())),
+      ),
+    );
+
+    $normalized = $this->serializer->normalize($file, $this->format);
+    $this->assertEqual($normalized['uri'], $expected_array['uri'], 'URI is normalized.');
+
+  }
+
+}
-- 
GitLab