Security update for Core, with self-updated composer
[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   protected function setUp() {
14     parent::setUp();
15
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);
21
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);
25   }
26
27   /**
28    * Creates a file with a given size.
29    *
30    * @param string $uri
31    *   URI of the file to create.
32    * @param int $size
33    *   Size of the file.
34    * @param int $uid
35    *   File owner ID.
36    * @param int $status
37    *   Whether the file should be permanent or temporary.
38    *
39    * @return \Drupal\Core\Entity\EntityInterface
40    *   The file entity.
41    */
42   protected function createFileWithSize($uri, $size, $uid, $status = FILE_STATUS_PERMANENT) {
43     file_put_contents($uri, $this->randomMachineName($size));
44     $file = File::create([
45       'uri' => $uri,
46       'uid' => $uid,
47       'status' => $status,
48     ]);
49     $file->save();
50     return $file;
51   }
52
53   /**
54    * Test different users with the default status.
55    */
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);
62
63     // Test the status fields
64     $this->assertEqual($file->spaceUsed(NULL, 0), 4);
65     $this->assertEqual($file->spaceUsed(NULL, FILE_STATUS_PERMANENT), 370);
66
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);
74   }
75
76 }