2017-07-31 23:46:19 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2019-12-03 21:57:53 +03:00
|
|
|
* @copyright Copyright (c) 2017, ownCloud GmbH
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-07-31 23:46:19 +03:00
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2017-07-31 23:46:19 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\Tests\unit\Upload;
|
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\DAV\Connector\Sabre\Directory;
|
2017-07-31 23:46:19 +03:00
|
|
|
use OCA\DAV\Upload\ChunkingPlugin;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\DAV\Upload\FutureFile;
|
2017-07-31 23:46:19 +03:00
|
|
|
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;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2017-07-31 23:46:19 +03:00
|
|
|
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() {
|
|
|
|
$node = $this->createMock(Directory::class);
|
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('source')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($node);
|
2017-07-31 23:46:19 +03:00
|
|
|
$this->response->expects($this->never())
|
|
|
|
->method('setStatus');
|
|
|
|
|
|
|
|
$this->assertNull($this->plugin->beforeMove('source', 'target'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBeforeMoveFutureFileSkipNonExisting() {
|
|
|
|
$sourceNode = $this->createMock(FutureFile::class);
|
|
|
|
$sourceNode->expects($this->once())
|
|
|
|
->method('getSize')
|
|
|
|
->willReturn(4);
|
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('source')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($sourceNode);
|
2017-07-31 23:46:19 +03:00
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('nodeExists')
|
|
|
|
->with('target')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2017-07-31 23:46:19 +03:00
|
|
|
$this->response->expects($this->never())
|
|
|
|
->method('setStatus');
|
|
|
|
$this->request->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('OC-Total-Length')
|
|
|
|
->willReturn(4);
|
|
|
|
|
|
|
|
$this->assertNull($this->plugin->beforeMove('source', 'target'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBeforeMoveFutureFileMoveIt() {
|
|
|
|
$sourceNode = $this->createMock(FutureFile::class);
|
|
|
|
$sourceNode->expects($this->once())
|
|
|
|
->method('getSize')
|
|
|
|
->willReturn(4);
|
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('source')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($sourceNode);
|
2017-07-31 23:46:19 +03:00
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('nodeExists')
|
|
|
|
->with('target')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2017-07-31 23:46:19 +03:00
|
|
|
$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'));
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2017-07-31 23:46:19 +03:00
|
|
|
public function testBeforeMoveSizeIsWrong() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Sabre\DAV\Exception\BadRequest::class);
|
|
|
|
$this->expectExceptionMessage('Chunks on server do not sum up to 4 but to 3 bytes');
|
|
|
|
|
2017-07-31 23:46:19 +03:00
|
|
|
$sourceNode = $this->createMock(FutureFile::class);
|
|
|
|
$sourceNode->expects($this->once())
|
|
|
|
->method('getSize')
|
|
|
|
->willReturn(3);
|
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('source')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($sourceNode);
|
2017-07-31 23:46:19 +03:00
|
|
|
$this->request->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('OC-Total-Length')
|
|
|
|
->willReturn('4');
|
|
|
|
|
|
|
|
$this->assertFalse($this->plugin->beforeMove('source', 'target'));
|
|
|
|
}
|
|
|
|
}
|