nextcloud/apps/dav/tests/unit/Connector/Sabre/QuotaPluginTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

160 lines
5.8 KiB
PHP
Raw Permalink Normal View History

2013-08-08 05:04:40 -04:00
<?php
declare(strict_types=1);
2016-01-12 09:02:16 -05:00
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2013-2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
2016-01-12 09:02:16 -05:00
*/
2016-05-25 10:04:15 -04:00
namespace OCA\DAV\Tests\unit\Connector\Sabre;
use OC\Files\View;
use OCA\DAV\Connector\Sabre\QuotaPlugin;
use OCP\Files\FileInfo;
use Test\TestCase;
class QuotaPluginTest extends TestCase {
private \Sabre\DAV\Server $server;
2013-08-08 05:04:40 -04:00
private QuotaPlugin $plugin;
2013-08-08 05:04:40 -04:00
private function init(int $quota, string $checkedPath = ''): void {
$view = $this->buildFileViewMock($quota, $checkedPath);
$this->server = new \Sabre\DAV\Server();
$this->plugin = new QuotaPlugin($view);
2013-08-08 05:04:40 -04:00
$this->plugin->initialize($this->server);
}
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'lengthProvider')]
public function testLength(?int $expected, array $headers): void {
$this->init(0);
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
2013-08-08 05:04:40 -04:00
$length = $this->plugin->getLength();
$this->assertEquals($expected, $length);
}
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'quotaOkayProvider')]
public function testCheckQuota(int $quota, array $headers): void {
$this->init($quota);
2013-08-08 07:33:00 -04:00
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
2013-08-08 07:33:00 -04:00
$result = $this->plugin->checkQuota('');
$this->assertTrue($result);
}
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'quotaExceededProvider')]
public function testCheckExceededQuota(int $quota, array $headers): void {
$this->expectException(\Sabre\DAV\Exception\InsufficientStorage::class);
$this->init($quota);
2013-08-08 07:33:00 -04:00
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
2013-08-08 07:33:00 -04:00
$this->plugin->checkQuota('');
}
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'quotaOkayProvider')]
public function testCheckQuotaOnPath(int $quota, array $headers): void {
$this->init($quota, 'sub/test.txt');
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
$result = $this->plugin->checkQuota('/sub/test.txt');
$this->assertTrue($result);
}
public static function quotaOkayProvider(): array {
return [
[1024, []],
[1024, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
[1024, ['CONTENT-LENGTH' => '512']],
[1024, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
[FileInfo::SPACE_UNKNOWN, []],
[FileInfo::SPACE_UNKNOWN, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
[FileInfo::SPACE_UNKNOWN, ['CONTENT-LENGTH' => '512']],
[FileInfo::SPACE_UNKNOWN, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
[FileInfo::SPACE_UNLIMITED, []],
[FileInfo::SPACE_UNLIMITED, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
[FileInfo::SPACE_UNLIMITED, ['CONTENT-LENGTH' => '512']],
[FileInfo::SPACE_UNLIMITED, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
];
2013-08-08 07:33:00 -04:00
}
public static function quotaExceededProvider(): array {
return [
[1023, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
[511, ['CONTENT-LENGTH' => '512']],
[2047, ['OC-TOTAL-LENGTH' => '2048', 'CONTENT-LENGTH' => '1024']],
];
2013-08-08 07:33:00 -04:00
}
public static function lengthProvider(): array {
return [
[null, []],
[1024, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
[512, ['CONTENT-LENGTH' => '512']],
[2048, ['OC-TOTAL-LENGTH' => '2048', 'CONTENT-LENGTH' => '1024']],
[4096, ['OC-TOTAL-LENGTH' => '2048', 'X-EXPECTED-ENTITY-LENGTH' => '4096']],
[null, ['X-EXPECTED-ENTITY-LENGTH' => 'A']],
[null, ['CONTENT-LENGTH' => 'A']],
[1024, ['OC-TOTAL-LENGTH' => 'A', 'CONTENT-LENGTH' => '1024']],
[1024, ['OC-TOTAL-LENGTH' => 'A', 'X-EXPECTED-ENTITY-LENGTH' => '1024']],
[2048, ['OC-TOTAL-LENGTH' => '2048', 'X-EXPECTED-ENTITY-LENGTH' => 'A']],
[2048, ['OC-TOTAL-LENGTH' => '2048', 'CONTENT-LENGTH' => 'A']],
];
2013-08-08 05:04:40 -04:00
}
public static function quotaChunkedOkProvider(): array {
return [
[1024, 0, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
[1024, 0, ['CONTENT-LENGTH' => '512']],
[1024, 0, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
// with existing chunks (allowed size = total length - chunk total size)
[400, 128, ['X-EXPECTED-ENTITY-LENGTH' => '512']],
[400, 128, ['CONTENT-LENGTH' => '512']],
[400, 128, ['OC-TOTAL-LENGTH' => '512', 'CONTENT-LENGTH' => '500']],
// \OCP\Files\FileInfo::SPACE-UNKNOWN = -2
[-2, 0, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
[-2, 0, ['CONTENT-LENGTH' => '512']],
[-2, 0, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
[-2, 128, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
[-2, 128, ['CONTENT-LENGTH' => '512']],
[-2, 128, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
];
}
public static function quotaChunkedFailProvider(): array {
return [
[400, 0, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
[400, 0, ['CONTENT-LENGTH' => '512']],
[400, 0, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
// with existing chunks (allowed size = total length - chunk total size)
[380, 128, ['X-EXPECTED-ENTITY-LENGTH' => '512']],
[380, 128, ['CONTENT-LENGTH' => '512']],
[380, 128, ['OC-TOTAL-LENGTH' => '512', 'CONTENT-LENGTH' => '500']],
];
}
/**
* Build a mock for the View class with a controlled free_space() response.
*
* @param int|float|false $quota The quota value to return from free_space().
* @param string $checkedPath The path expected as a parameter to free_space().
* @return View&\PHPUnit\Framework\MockObject\MockObject
*/
private function buildFileViewMock(int|float|false $quota, string $checkedPath): View {
// mock filesystem
$view = $this->getMockBuilder(View::class)
->onlyMethods(['free_space'])
->disableOriginalConstructor()
->getMock();
$view->expects($this->any())
->method('free_space')
->with($checkedPath)
->willReturn($quota);
2013-08-08 07:33:00 -04:00
return $view;
}
2013-08-08 05:04:40 -04:00
}