Version 1
[yaffs-website] / web / core / modules / file / tests / src / Kernel / SpaceUsedTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel;
4 use Drupal\file\Entity\File;
5
6 /**
7  * Tests the spaceUsed() function.
8  *
9  * @group file
10  */
11 class SpaceUsedTest extends FileManagedUnitTestBase {
12   protected function setUp() {
13     parent::setUp();
14
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);
20
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);
24   }
25
26   /**
27    * Creates a file with a given size.
28    *
29    * @param string $uri
30    *   URI of the file to create.
31    * @param int $size
32    *   Size of the file.
33    * @param int $uid
34    *   File owner ID.
35    * @param int $status
36    *   Whether the file should be permanent or temporary.
37    *
38    * @return \Drupal\Core\Entity\EntityInterface
39    *   The file entity.
40    */
41   protected function createFileWithSize($uri, $size, $uid, $status = FILE_STATUS_PERMANENT) {
42     file_put_contents($uri, $this->randomMachineName($size));
43     $file = File::create([
44       'uri' => $uri,
45       'uid' => $uid,
46       'status' => $status,
47     ]);
48     $file->save();
49     return $file;
50   }
51
52   /**
53    * Test different users with the default status.
54    */
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);
61
62     // Test the status fields
63     $this->assertEqual($file->spaceUsed(NULL, 0), 4);
64     $this->assertEqual($file->spaceUsed(NULL, FILE_STATUS_PERMANENT), 370);
65
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);
73   }
74
75 }