View source
<?php
namespace Drupal\Tests\file\Kernel;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
class SpaceUsedTest extends FileManagedUnitTestBase {
protected function setUp() : void {
parent::setUp();
$this
->createFileWithSize('public://example1.txt', 50, 2);
$this
->createFileWithSize('public://example2.txt', 20, 2);
$this
->createFileWithSize('public://example3.txt', 100, 3);
$this
->createFileWithSize('public://example4.txt', 200, 3);
$this
->createFileWithSize('public://example5.txt', 1, 2, 0);
$this
->createFileWithSize('public://example6.txt', 3, 3, 0);
}
protected function createFileWithSize($uri, $size, $uid, $status = FileInterface::STATUS_PERMANENT) {
file_put_contents($uri, $this
->randomMachineName($size));
$file = File::create([
'uri' => $uri,
'uid' => $uid,
'status' => $status,
]);
$file
->save();
return $file;
}
public function testFileSpaceUsed() {
$file = $this->container
->get('entity_type.manager')
->getStorage('file');
$this
->assertEquals(70, $file
->spaceUsed(2));
$this
->assertEquals(300, $file
->spaceUsed(3));
$this
->assertEquals(370, $file
->spaceUsed());
$this
->assertEquals(4, $file
->spaceUsed(NULL, 0));
$this
->assertEquals(370, $file
->spaceUsed());
$this
->assertEquals(0, $file
->spaceUsed(1, 0));
$this
->assertEquals(0, $file
->spaceUsed(1));
$this
->assertEquals(1, $file
->spaceUsed(2, 0));
$this
->assertEquals(70, $file
->spaceUsed(2));
$this
->assertEquals(3, $file
->spaceUsed(3, 0));
$this
->assertEquals(300, $file
->spaceUsed(3));
}
}