123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- /**
- * @copyright Copyright (c) 2017, ownCloud GmbH
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Daniel Kesselberg <mail@danielkesselberg.de>
- * @author Julius Härtl <jus@bitgrid.net>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
- namespace OCA\DAV\Tests\unit\Upload;
- use OCA\DAV\Connector\Sabre\Directory;
- use OCA\DAV\Upload\ChunkingPlugin;
- use OCA\DAV\Upload\FutureFile;
- use Sabre\DAV\Exception\NotFound;
- use Sabre\HTTP\RequestInterface;
- use Sabre\HTTP\ResponseInterface;
- use Test\TestCase;
- class ChunkingPluginTest extends TestCase {
- /**
- * @var \Sabre\DAV\Server | \PHPUnit\Framework\MockObject\MockObject
- */
- private $server;
- /**
- * @var \Sabre\DAV\Tree | \PHPUnit\Framework\MockObject\MockObject
- */
- private $tree;
- /**
- * @var ChunkingPlugin
- */
- private $plugin;
- /** @var RequestInterface | \PHPUnit\Framework\MockObject\MockObject */
- private $request;
- /** @var ResponseInterface | \PHPUnit\Framework\MockObject\MockObject */
- private $response;
- protected function setUp(): void {
- parent::setUp();
- $this->server = $this->getMockBuilder('\Sabre\DAV\Server')
- ->disableOriginalConstructor()
- ->getMock();
- $this->tree = $this->getMockBuilder('\Sabre\DAV\Tree')
- ->disableOriginalConstructor()
- ->getMock();
- $this->server->tree = $this->tree;
- $this->plugin = new ChunkingPlugin();
- $this->request = $this->createMock(RequestInterface::class);
- $this->response = $this->createMock(ResponseInterface::class);
- $this->server->httpRequest = $this->request;
- $this->server->httpResponse = $this->response;
- $this->plugin->initialize($this->server);
- }
- public function testBeforeMoveFutureFileSkip(): void {
- $node = $this->createMock(Directory::class);
- $this->tree->expects($this->any())
- ->method('getNodeForPath')
- ->with('source')
- ->willReturn($node);
- $this->response->expects($this->never())
- ->method('setStatus');
- $this->assertNull($this->plugin->beforeMove('source', 'target'));
- }
- public function testBeforeMoveDestinationIsDirectory(): void {
- $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
- $this->expectExceptionMessage('The given destination target is a directory.');
- $sourceNode = $this->createMock(FutureFile::class);
- $targetNode = $this->createMock(Directory::class);
- $this->tree->expects($this->exactly(2))
- ->method('getNodeForPath')
- ->withConsecutive(
- ['source'],
- ['target'],
- )
- ->willReturnOnConsecutiveCalls(
- $sourceNode,
- $targetNode,
- );
- $this->response->expects($this->never())
- ->method('setStatus');
- $this->assertNull($this->plugin->beforeMove('source', 'target'));
- }
- public function testBeforeMoveFutureFileSkipNonExisting(): void {
- $sourceNode = $this->createMock(FutureFile::class);
- $sourceNode->expects($this->once())
- ->method('getSize')
- ->willReturn(4);
- $this->tree->expects($this->exactly(2))
- ->method('getNodeForPath')
- ->withConsecutive(
- ['source'],
- ['target'],
- )
- ->willReturnOnConsecutiveCalls(
- $sourceNode,
- $this->throwException(new NotFound()),
- );
- $this->tree->expects($this->any())
- ->method('nodeExists')
- ->with('target')
- ->willReturn(false);
- $this->response->expects($this->once())
- ->method('setHeader')
- ->with('Content-Length', '0');
- $this->response->expects($this->once())
- ->method('setStatus')
- ->with(201);
- $this->request->expects($this->once())
- ->method('getHeader')
- ->with('OC-Total-Length')
- ->willReturn(4);
- $this->assertFalse($this->plugin->beforeMove('source', 'target'));
- }
- public function testBeforeMoveFutureFileMoveIt(): void {
- $sourceNode = $this->createMock(FutureFile::class);
- $sourceNode->expects($this->once())
- ->method('getSize')
- ->willReturn(4);
- $this->tree->expects($this->exactly(2))
- ->method('getNodeForPath')
- ->withConsecutive(
- ['source'],
- ['target'],
- )
- ->willReturnOnConsecutiveCalls(
- $sourceNode,
- $this->throwException(new NotFound()),
- );
- $this->tree->expects($this->any())
- ->method('nodeExists')
- ->with('target')
- ->willReturn(true);
- $this->tree->expects($this->once())
- ->method('move')
- ->with('source', 'target');
- $this->response->expects($this->once())
- ->method('setHeader')
- ->with('Content-Length', '0');
- $this->response->expects($this->once())
- ->method('setStatus')
- ->with(204);
- $this->request->expects($this->once())
- ->method('getHeader')
- ->with('OC-Total-Length')
- ->willReturn('4');
- $this->assertFalse($this->plugin->beforeMove('source', 'target'));
- }
- public function testBeforeMoveSizeIsWrong(): void {
- $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
- $this->expectExceptionMessage('Chunks on server do not sum up to 4 but to 3 bytes');
- $sourceNode = $this->createMock(FutureFile::class);
- $sourceNode->expects($this->once())
- ->method('getSize')
- ->willReturn(3);
- $this->tree->expects($this->exactly(2))
- ->method('getNodeForPath')
- ->withConsecutive(
- ['source'],
- ['target'],
- )
- ->willReturnOnConsecutiveCalls(
- $sourceNode,
- $this->throwException(new NotFound()),
- );
- $this->request->expects($this->once())
- ->method('getHeader')
- ->with('OC-Total-Length')
- ->willReturn('4');
- $this->assertFalse($this->plugin->beforeMove('source', 'target'));
- }
- }
|