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