3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\file\Entity\File;
8 * Tests the spaceUsed() function.
12 class SpaceUsedTest extends FileManagedUnitTestBase {
14 protected function setUp() {
17 // Create records for a couple of users with different sizes.
18 $this->createFileWithSize('public://example1.txt', 50, 2);
19 $this->createFileWithSize('public://example2.txt', 20, 2);
20 $this->createFileWithSize('public://example3.txt', 100, 3);
21 $this->createFileWithSize('public://example4.txt', 200, 3);
23 // Now create some non-permanent files.
24 $this->createFileWithSize('public://example5.txt', 1, 2, 0);
25 $this->createFileWithSize('public://example6.txt', 3, 3, 0);
29 * Creates a file with a given size.
32 * URI of the file to create.
38 * Whether the file should be permanent or temporary.
40 * @return \Drupal\Core\Entity\EntityInterface
43 protected function createFileWithSize($uri, $size, $uid, $status = FILE_STATUS_PERMANENT) {
44 file_put_contents($uri, $this->randomMachineName($size));
45 $file = File::create([
55 * Test different users with the default status.
57 public function testFileSpaceUsed() {
58 $file = $this->container->get('entity.manager')->getStorage('file');
59 // Test different users with default status.
60 $this->assertEqual($file->spaceUsed(2), 70);
61 $this->assertEqual($file->spaceUsed(3), 300);
62 $this->assertEqual($file->spaceUsed(), 370);
64 // Test the status fields
65 $this->assertEqual($file->spaceUsed(NULL, 0), 4);
66 $this->assertEqual($file->spaceUsed(NULL, FILE_STATUS_PERMANENT), 370);
68 // Test both the user and status.
69 $this->assertEqual($file->spaceUsed(1, 0), 0);
70 $this->assertEqual($file->spaceUsed(1, FILE_STATUS_PERMANENT), 0);
71 $this->assertEqual($file->spaceUsed(2, 0), 1);
72 $this->assertEqual($file->spaceUsed(2, FILE_STATUS_PERMANENT), 70);
73 $this->assertEqual($file->spaceUsed(3, 0), 3);
74 $this->assertEqual($file->spaceUsed(3, FILE_STATUS_PERMANENT), 300);