Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / file / tests / src / Kernel / LoadTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Tests \Drupal\file\Entity\File::load().
9  *
10  * @group file
11  */
12 class LoadTest extends FileManagedUnitTestBase {
13
14   /**
15    * Try to load a non-existent file by fid.
16    */
17   public function testLoadMissingFid() {
18     $this->assertFalse(File::load(-1), 'Try to load an invalid fid fails.');
19     $this->assertFileHooksCalled([]);
20   }
21
22   /**
23    * Try to load a non-existent file by URI.
24    */
25   public function testLoadMissingFilepath() {
26     $files = entity_load_multiple_by_properties('file', ['uri' => 'foobar://misc/druplicon.png']);
27     $this->assertFalse(reset($files), "Try to load a file that doesn't exist in the database fails.");
28     $this->assertFileHooksCalled([]);
29   }
30
31   /**
32    * Try to load a non-existent file by status.
33    */
34   public function testLoadInvalidStatus() {
35     $files = entity_load_multiple_by_properties('file', ['status' => -99]);
36     $this->assertFalse(reset($files), 'Trying to load a file with an invalid status fails.');
37     $this->assertFileHooksCalled([]);
38   }
39
40   /**
41    * Load a single file and ensure that the correct values are returned.
42    */
43   public function testSingleValues() {
44     // Create a new file entity from scratch so we know the values.
45     $file = $this->createFile('druplicon.txt', NULL, 'public');
46     $by_fid_file = File::load($file->id());
47     $this->assertFileHookCalled('load');
48     $this->assertTrue(is_object($by_fid_file), '\Drupal\file\Entity\File::load() returned an object.');
49     $this->assertEqual($by_fid_file->id(), $file->id(), 'Loading by fid got the same fid.', 'File');
50     $this->assertEqual($by_fid_file->getFileUri(), $file->getFileUri(), 'Loading by fid got the correct filepath.', 'File');
51     $this->assertEqual($by_fid_file->getFilename(), $file->getFilename(), 'Loading by fid got the correct filename.', 'File');
52     $this->assertEqual($by_fid_file->getMimeType(), $file->getMimeType(), 'Loading by fid got the correct MIME type.', 'File');
53     $this->assertEqual($by_fid_file->isPermanent(), $file->isPermanent(), 'Loading by fid got the correct status.', 'File');
54     $this->assertTrue($by_fid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
55   }
56
57   /**
58    * This will test loading file data from the database.
59    */
60   public function testMultiple() {
61     // Create a new file entity.
62     $file = $this->createFile('druplicon.txt', NULL, 'public');
63
64     // Load by path.
65     file_test_reset();
66     $by_path_files = entity_load_multiple_by_properties('file', ['uri' => $file->getFileUri()]);
67     $this->assertFileHookCalled('load');
68     $this->assertEqual(1, count($by_path_files), 'entity_load_multiple_by_properties() returned an array of the correct size.');
69     $by_path_file = reset($by_path_files);
70     $this->assertTrue($by_path_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
71     $this->assertEqual($by_path_file->id(), $file->id(), 'Loading by filepath got the correct fid.', 'File');
72
73     // Load by fid.
74     file_test_reset();
75     $by_fid_files = File::loadMultiple([$file->id()]);
76     $this->assertFileHooksCalled([]);
77     $this->assertEqual(1, count($by_fid_files), '\Drupal\file\Entity\File::loadMultiple() returned an array of the correct size.');
78     $by_fid_file = reset($by_fid_files);
79     $this->assertTrue($by_fid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
80     $this->assertEqual($by_fid_file->getFileUri(), $file->getFileUri(), 'Loading by fid got the correct filepath.', 'File');
81   }
82
83   /**
84    * Loads a single file and ensure that the correct values are returned.
85    */
86   public function testUuidValues() {
87     // Create a new file entity from scratch so we know the values.
88     $file = $this->createFile('druplicon.txt', NULL, 'public');
89     $file->save();
90     file_test_reset();
91
92     $by_uuid_file = \Drupal::entityManager()->loadEntityByUuid('file', $file->uuid());
93     $this->assertFileHookCalled('load');
94     $this->assertTrue(is_object($by_uuid_file), '\Drupal::entityManager()->loadEntityByUuid() returned a file object.');
95     if (is_object($by_uuid_file)) {
96       $this->assertEqual($by_uuid_file->id(), $file->id(), 'Loading by UUID got the same fid.', 'File');
97       $this->assertTrue($by_uuid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
98     }
99   }
100
101 }