3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\file\Entity\File;
8 * Tests \Drupal\file\Entity\File::load().
12 class LoadTest extends FileManagedUnitTestBase {
14 * Try to load a non-existent file by fid.
16 public function testLoadMissingFid() {
17 $this->assertFalse(File::load(-1), 'Try to load an invalid fid fails.');
18 $this->assertFileHooksCalled([]);
22 * Try to load a non-existent file by URI.
24 public function testLoadMissingFilepath() {
25 $files = entity_load_multiple_by_properties('file', ['uri' => 'foobar://misc/druplicon.png']);
26 $this->assertFalse(reset($files), "Try to load a file that doesn't exist in the database fails.");
27 $this->assertFileHooksCalled([]);
31 * Try to load a non-existent file by status.
33 public function testLoadInvalidStatus() {
34 $files = entity_load_multiple_by_properties('file', ['status' => -99]);
35 $this->assertFalse(reset($files), 'Trying to load a file with an invalid status fails.');
36 $this->assertFileHooksCalled([]);
40 * Load a single file and ensure that the correct values are returned.
42 public function testSingleValues() {
43 // Create a new file entity from scratch so we know the values.
44 $file = $this->createFile('druplicon.txt', NULL, 'public');
45 $by_fid_file = File::load($file->id());
46 $this->assertFileHookCalled('load');
47 $this->assertTrue(is_object($by_fid_file), '\Drupal\file\Entity\File::load() returned an object.');
48 $this->assertEqual($by_fid_file->id(), $file->id(), 'Loading by fid got the same fid.', 'File');
49 $this->assertEqual($by_fid_file->getFileUri(), $file->getFileUri(), 'Loading by fid got the correct filepath.', 'File');
50 $this->assertEqual($by_fid_file->getFilename(), $file->getFilename(), 'Loading by fid got the correct filename.', 'File');
51 $this->assertEqual($by_fid_file->getMimeType(), $file->getMimeType(), 'Loading by fid got the correct MIME type.', 'File');
52 $this->assertEqual($by_fid_file->isPermanent(), $file->isPermanent(), 'Loading by fid got the correct status.', 'File');
53 $this->assertTrue($by_fid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
57 * This will test loading file data from the database.
59 public function testMultiple() {
60 // Create a new file entity.
61 $file = $this->createFile('druplicon.txt', NULL, 'public');
65 $by_path_files = entity_load_multiple_by_properties('file', ['uri' => $file->getFileUri()]);
66 $this->assertFileHookCalled('load');
67 $this->assertEqual(1, count($by_path_files), 'entity_load_multiple_by_properties() returned an array of the correct size.');
68 $by_path_file = reset($by_path_files);
69 $this->assertTrue($by_path_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
70 $this->assertEqual($by_path_file->id(), $file->id(), 'Loading by filepath got the correct fid.', 'File');
74 $by_fid_files = File::loadMultiple([$file->id()]);
75 $this->assertFileHooksCalled([]);
76 $this->assertEqual(1, count($by_fid_files), '\Drupal\file\Entity\File::loadMultiple() returned an array of the correct size.');
77 $by_fid_file = reset($by_fid_files);
78 $this->assertTrue($by_fid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
79 $this->assertEqual($by_fid_file->getFileUri(), $file->getFileUri(), 'Loading by fid got the correct filepath.', 'File');
83 * Loads a single file and ensure that the correct values are returned.
85 public function testUuidValues() {
86 // Create a new file entity from scratch so we know the values.
87 $file = $this->createFile('druplicon.txt', NULL, 'public');
91 $by_uuid_file = \Drupal::entityManager()->loadEntityByUuid('file', $file->uuid());
92 $this->assertFileHookCalled('load');
93 $this->assertTrue(is_object($by_uuid_file), '\Drupal::entityManager()->loadEntityByUuid() returned a file object.');
94 if (is_object($by_uuid_file)) {
95 $this->assertEqual($by_uuid_file->id(), $file->id(), 'Loading by UUID got the same fid.', 'File');
96 $this->assertTrue($by_uuid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');