Version 1
[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    * Try to load a non-existent file by fid.
15    */
16   public function testLoadMissingFid() {
17     $this->assertFalse(File::load(-1), 'Try to load an invalid fid fails.');
18     $this->assertFileHooksCalled([]);
19   }
20
21   /**
22    * Try to load a non-existent file by URI.
23    */
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([]);
28   }
29
30   /**
31    * Try to load a non-existent file by status.
32    */
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([]);
37   }
38
39   /**
40    * Load a single file and ensure that the correct values are returned.
41    */
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.');
54   }
55
56   /**
57    * This will test loading file data from the database.
58    */
59   public function testMultiple() {
60     // Create a new file entity.
61     $file = $this->createFile('druplicon.txt', NULL, 'public');
62
63     // Load by path.
64     file_test_reset();
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');
71
72     // Load by fid.
73     file_test_reset();
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');
80   }
81
82   /**
83    * Loads a single file and ensure that the correct values are returned.
84    */
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');
88     $file->save();
89     file_test_reset();
90
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.');
97     }
98   }
99
100 }