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