edcf3dca1f845b3faf43ca3aebb1982bb3a94232
[yaffs-website] / web / core / modules / hal / tests / src / Kernel / FileNormalizeTest.php
1 <?php
2
3 namespace Drupal\Tests\hal\Kernel;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Tests that file entities can be normalized in HAL.
9  *
10  * @group hal
11  */
12 class FileNormalizeTest extends NormalizerTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['file'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26     $this->installEntitySchema('file');
27   }
28
29   /**
30    * Tests the normalize function.
31    */
32   public function testNormalize() {
33     $file_params = [
34       'filename' => 'test_1.txt',
35       'uri' => 'public://test_1.txt',
36       'filemime' => 'text/plain',
37       'status' => FILE_STATUS_PERMANENT,
38     ];
39     // Create a new file entity.
40     $file = File::create($file_params);
41     file_put_contents($file->getFileUri(), 'hello world');
42     $file->save();
43
44     $expected_array = [
45       'uri' => [
46         [
47           'value' => $file->getFileUri(),
48           'url' => file_url_transform_relative(file_create_url($file->getFileUri())),
49         ],
50       ],
51     ];
52
53     $normalized = $this->serializer->normalize($file, $this->format);
54     $this->assertEqual($normalized['uri'], $expected_array['uri'], 'URI is normalized.');
55
56   }
57
58 }