Skip to content
Snippets Groups Projects
Commit 505a062e authored by Dries Buytaert's avatar Dries Buytaert
Browse files

Issue #2277705 by mashermike, juampy: Fixed Files don't have URI nor href.

parent ade617fd
No related branches found
No related tags found
No related merge requests found
......@@ -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}
*/
......
......@@ -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());
......
......@@ -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;
}
......
<?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.');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment