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