2016-12-02 12:37:06 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-12-02 12:37:06 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-12-02 12:37:06 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-12-02 12:37:06 +03:00
|
|
|
namespace OCA\DAV\Tests\Files\Sharing;
|
|
|
|
|
|
|
|
use OC\Files\View;
|
|
|
|
use OCA\DAV\Files\Sharing\FilesDropPlugin;
|
|
|
|
use Sabre\DAV\Exception\MethodNotAllowed;
|
|
|
|
use Sabre\DAV\Server;
|
|
|
|
use Sabre\HTTP\RequestInterface;
|
|
|
|
use Sabre\HTTP\ResponseInterface;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class FilesDropPluginTest extends TestCase {
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var View|\PHPUnit\Framework\MockObject\MockObject */
|
2016-12-02 12:37:06 +03:00
|
|
|
private $view;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Server|\PHPUnit\Framework\MockObject\MockObject */
|
2016-12-02 12:37:06 +03:00
|
|
|
private $server;
|
|
|
|
|
|
|
|
/** @var FilesDropPlugin */
|
|
|
|
private $plugin;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var RequestInterface|\PHPUnit\Framework\MockObject\MockObject */
|
2016-12-02 12:37:06 +03:00
|
|
|
private $request;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ResponseInterface|\PHPUnit\Framework\MockObject\MockObject */
|
2016-12-02 12:37:06 +03:00
|
|
|
private $response;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-12-02 12:37:06 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->view = $this->createMock(View::class);
|
|
|
|
$this->server = $this->createMock(Server::class);
|
|
|
|
$this->plugin = new FilesDropPlugin();
|
|
|
|
|
|
|
|
$this->request = $this->createMock(RequestInterface::class);
|
|
|
|
$this->response = $this->createMock(ResponseInterface::class);
|
|
|
|
|
|
|
|
$this->response->expects($this->never())
|
|
|
|
->method($this->anything());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInitialize() {
|
|
|
|
$this->server->expects($this->once())
|
|
|
|
->method('on')
|
|
|
|
->with(
|
2020-03-09 18:32:04 +03:00
|
|
|
$this->equalTo('beforeMethod:*'),
|
2016-12-02 12:37:06 +03:00
|
|
|
$this->equalTo([$this->plugin, 'beforeMethod']),
|
|
|
|
$this->equalTo(999)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotEnabled() {
|
|
|
|
$this->view->expects($this->never())
|
|
|
|
->method($this->anything());
|
|
|
|
|
|
|
|
$this->request->expects($this->never())
|
|
|
|
->method($this->anything());
|
|
|
|
|
|
|
|
$this->plugin->beforeMethod($this->request, $this->response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValid() {
|
|
|
|
$this->plugin->enable();
|
|
|
|
$this->plugin->setView($this->view);
|
|
|
|
|
|
|
|
$this->request->method('getMethod')
|
|
|
|
->willReturn('PUT');
|
|
|
|
|
|
|
|
$this->request->method('getPath')
|
|
|
|
->willReturn('file.txt');
|
|
|
|
|
|
|
|
$this->request->method('getBaseUrl')
|
|
|
|
->willReturn('https://example.com');
|
|
|
|
|
|
|
|
$this->view->method('file_exists')
|
|
|
|
->with('/file.txt')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->request->expects($this->once())
|
|
|
|
->method('setUrl')
|
|
|
|
->with('https://example.com/file.txt');
|
|
|
|
|
|
|
|
$this->plugin->beforeMethod($this->request, $this->response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFileAlreadyExistsValid() {
|
|
|
|
$this->plugin->enable();
|
|
|
|
$this->plugin->setView($this->view);
|
|
|
|
|
|
|
|
$this->request->method('getMethod')
|
|
|
|
->willReturn('PUT');
|
|
|
|
|
|
|
|
$this->request->method('getPath')
|
|
|
|
->willReturn('file.txt');
|
|
|
|
|
|
|
|
$this->request->method('getBaseUrl')
|
|
|
|
->willReturn('https://example.com');
|
|
|
|
|
|
|
|
$this->view->method('file_exists')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($path) {
|
2016-12-02 12:37:06 +03:00
|
|
|
if ($path === 'file.txt' || $path === '/file.txt') {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-12-02 12:37:06 +03:00
|
|
|
|
|
|
|
$this->request->expects($this->once())
|
|
|
|
->method('setUrl')
|
|
|
|
->with($this->equalTo('https://example.com/file (2).txt'));
|
|
|
|
|
|
|
|
$this->plugin->beforeMethod($this->request, $this->response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoMKCOL() {
|
|
|
|
$this->plugin->enable();
|
|
|
|
$this->plugin->setView($this->view);
|
|
|
|
|
|
|
|
$this->request->method('getMethod')
|
|
|
|
->willReturn('MKCOL');
|
|
|
|
|
|
|
|
$this->expectException(MethodNotAllowed::class);
|
|
|
|
|
|
|
|
$this->plugin->beforeMethod($this->request, $this->response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoSubdirPut() {
|
|
|
|
$this->plugin->enable();
|
|
|
|
$this->plugin->setView($this->view);
|
|
|
|
|
|
|
|
$this->request->method('getMethod')
|
|
|
|
->willReturn('PUT');
|
|
|
|
|
|
|
|
$this->request->method('getPath')
|
|
|
|
->willReturn('folder/file.txt');
|
|
|
|
|
|
|
|
$this->request->method('getBaseUrl')
|
|
|
|
->willReturn('https://example.com');
|
|
|
|
|
|
|
|
$this->view->method('file_exists')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($path) {
|
2016-12-02 12:37:06 +03:00
|
|
|
if ($path === 'file.txt' || $path === '/file.txt') {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-12-02 12:37:06 +03:00
|
|
|
|
|
|
|
$this->request->expects($this->once())
|
|
|
|
->method('setUrl')
|
|
|
|
->with($this->equalTo('https://example.com/file (2).txt'));
|
|
|
|
|
|
|
|
$this->plugin->beforeMethod($this->request, $this->response);
|
|
|
|
}
|
|
|
|
}
|