2016-01-11 20:09:00 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-03-01 19:25:15 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2016-01-11 20:09:00 +03:00
|
|
|
*
|
|
|
|
* @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/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
namespace OCA\DAV\Tests\unit\Comments;
|
2016-01-11 20:09:00 +03:00
|
|
|
|
|
|
|
use OC\Comments\Comment;
|
|
|
|
use OCA\DAV\Comments\CommentsPlugin as CommentsPluginImplementation;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCA\DAV\Comments\EntityCollection;
|
2016-02-09 15:59:13 +03:00
|
|
|
use OCP\Comments\IComment;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\Comments\ICommentsManager;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
use Sabre\DAV\INode;
|
|
|
|
use Sabre\DAV\Tree;
|
|
|
|
use Sabre\HTTP\RequestInterface;
|
|
|
|
use Sabre\HTTP\ResponseInterface;
|
2016-01-11 20:09:00 +03:00
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
class CommentsPluginTest extends \Test\TestCase {
|
2016-01-11 20:09:00 +03:00
|
|
|
/** @var \Sabre\DAV\Server */
|
|
|
|
private $server;
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
/** @var Tree */
|
2016-01-11 20:09:00 +03:00
|
|
|
private $tree;
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
/** @var ICommentsManager */
|
2016-01-11 20:09:00 +03:00
|
|
|
private $commentsManager;
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
/** @var IUserSession */
|
2016-01-11 20:09:00 +03:00
|
|
|
private $userSession;
|
|
|
|
|
|
|
|
/** @var CommentsPluginImplementation */
|
|
|
|
private $plugin;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
public function setUp(): void {
|
2016-01-11 20:09:00 +03:00
|
|
|
parent::setUp();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->tree = $this->getMockBuilder(Tree::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->server = $this->getMockBuilder('\Sabre\DAV\Server')
|
|
|
|
->setConstructorArgs([$this->tree])
|
|
|
|
->setMethods(['getRequestUri'])
|
|
|
|
->getMock();
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->commentsManager = $this->getMockBuilder(ICommentsManager::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->userSession = $this->getMockBuilder(IUserSession::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-01-11 20:09:00 +03:00
|
|
|
|
|
|
|
$this->plugin = new CommentsPluginImplementation($this->commentsManager, $this->userSession);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateComment() {
|
|
|
|
$commentData = [
|
|
|
|
'actorType' => 'users',
|
|
|
|
'verb' => 'comment',
|
|
|
|
'message' => 'my first comment',
|
|
|
|
];
|
|
|
|
|
|
|
|
$comment = new Comment([
|
|
|
|
'objectType' => 'files',
|
|
|
|
'objectId' => '42',
|
|
|
|
'actorType' => 'users',
|
|
|
|
'actorId' => 'alice'
|
|
|
|
] + $commentData);
|
|
|
|
$comment->setId('23');
|
|
|
|
|
|
|
|
$path = 'comments/files/42';
|
|
|
|
|
|
|
|
$requestData = json_encode($commentData);
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-01-11 20:09:00 +03:00
|
|
|
$user->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('alice'));
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$node = $this->getMockBuilder(EntityCollection::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('files'));
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue('42'));
|
|
|
|
|
2016-02-05 18:30:37 +03:00
|
|
|
$node->expects($this->once())
|
|
|
|
->method('setReadMarker')
|
|
|
|
->with(null);
|
|
|
|
|
2016-01-11 20:09:00 +03:00
|
|
|
$this->commentsManager->expects($this->once())
|
|
|
|
->method('create')
|
|
|
|
->with('users', 'alice', 'files', '42')
|
|
|
|
->will($this->returnValue($comment));
|
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
|
|
|
|
// technically, this is a shortcut. Inbetween EntityTypeCollection would
|
|
|
|
// be returned, but doing it exactly right would not be really
|
|
|
|
// unit-testing like, as it would require to haul in a lot of other
|
|
|
|
// things.
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/' . $path)
|
|
|
|
->will($this->returnValue($node));
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$request = $this->getMockBuilder(RequestInterface::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getPath')
|
|
|
|
->will($this->returnValue('/' . $path));
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getBodyAsString')
|
|
|
|
->will($this->returnValue($requestData));
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('Content-Type')
|
|
|
|
->will($this->returnValue('application/json'));
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getUrl')
|
|
|
|
->will($this->returnValue('http://example.com/dav/' . $path));
|
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('setHeader')
|
|
|
|
->with('Content-Location', 'http://example.com/dav/' . $path . '/23');
|
|
|
|
|
|
|
|
$this->server->expects($this->any())
|
|
|
|
->method('getRequestUri')
|
|
|
|
->will($this->returnValue($path));
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
|
|
|
$this->plugin->httpPost($request, $response);
|
|
|
|
}
|
|
|
|
|
2016-01-26 15:16:14 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \Sabre\DAV\Exception\NotFound
|
|
|
|
*/
|
2016-01-11 20:09:00 +03:00
|
|
|
public function testCreateCommentInvalidObject() {
|
|
|
|
$commentData = [
|
|
|
|
'actorType' => 'users',
|
|
|
|
'verb' => 'comment',
|
|
|
|
'message' => 'my first comment',
|
|
|
|
];
|
|
|
|
|
|
|
|
$comment = new Comment([
|
|
|
|
'objectType' => 'files',
|
|
|
|
'objectId' => '666',
|
|
|
|
'actorType' => 'users',
|
|
|
|
'actorId' => 'alice'
|
|
|
|
] + $commentData);
|
|
|
|
$comment->setId('23');
|
|
|
|
|
|
|
|
$path = 'comments/files/666';
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-01-11 20:09:00 +03:00
|
|
|
$user->expects($this->never())
|
|
|
|
->method('getUID');
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$node = $this->getMockBuilder(EntityCollection::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->never())
|
|
|
|
->method('getName');
|
|
|
|
$node->expects($this->never())
|
|
|
|
->method('getId');
|
|
|
|
|
|
|
|
$this->commentsManager->expects($this->never())
|
|
|
|
->method('create');
|
|
|
|
|
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('getUser');
|
|
|
|
|
|
|
|
// technically, this is a shortcut. Inbetween EntityTypeCollection would
|
|
|
|
// be returned, but doing it exactly right would not be really
|
|
|
|
// unit-testing like, as it would require to haul in a lot of other
|
|
|
|
// things.
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/' . $path)
|
|
|
|
->will($this->throwException(new \Sabre\DAV\Exception\NotFound()));
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$request = $this->getMockBuilder(RequestInterface::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getPath')
|
|
|
|
->will($this->returnValue('/' . $path));
|
|
|
|
|
|
|
|
$request->expects($this->never())
|
|
|
|
->method('getBodyAsString');
|
|
|
|
|
|
|
|
$request->expects($this->never())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('Content-Type');
|
|
|
|
|
|
|
|
$request->expects($this->never())
|
|
|
|
->method('getUrl');
|
|
|
|
|
|
|
|
$response->expects($this->never())
|
|
|
|
->method('setHeader');
|
|
|
|
|
|
|
|
$this->server->expects($this->any())
|
|
|
|
->method('getRequestUri')
|
|
|
|
->will($this->returnValue($path));
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
|
|
|
$this->plugin->httpPost($request, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Sabre\DAV\Exception\BadRequest
|
|
|
|
*/
|
|
|
|
public function testCreateCommentInvalidActor() {
|
|
|
|
$commentData = [
|
|
|
|
'actorType' => 'robots',
|
|
|
|
'verb' => 'comment',
|
|
|
|
'message' => 'my first comment',
|
|
|
|
];
|
|
|
|
|
|
|
|
$comment = new Comment([
|
|
|
|
'objectType' => 'files',
|
|
|
|
'objectId' => '42',
|
|
|
|
'actorType' => 'users',
|
|
|
|
'actorId' => 'alice'
|
|
|
|
] + $commentData);
|
|
|
|
$comment->setId('23');
|
|
|
|
|
|
|
|
$path = 'comments/files/42';
|
|
|
|
|
|
|
|
$requestData = json_encode($commentData);
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-01-11 20:09:00 +03:00
|
|
|
$user->expects($this->never())
|
|
|
|
->method('getUID');
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$node = $this->getMockBuilder(EntityCollection::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('files'));
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue('42'));
|
|
|
|
|
|
|
|
$this->commentsManager->expects($this->never())
|
|
|
|
->method('create');
|
|
|
|
|
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('getUser');
|
|
|
|
|
|
|
|
// technically, this is a shortcut. Inbetween EntityTypeCollection would
|
|
|
|
// be returned, but doing it exactly right would not be really
|
|
|
|
// unit-testing like, as it would require to haul in a lot of other
|
|
|
|
// things.
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/' . $path)
|
|
|
|
->will($this->returnValue($node));
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$request = $this->getMockBuilder(RequestInterface::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getPath')
|
|
|
|
->will($this->returnValue('/' . $path));
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getBodyAsString')
|
|
|
|
->will($this->returnValue($requestData));
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('Content-Type')
|
|
|
|
->will($this->returnValue('application/json'));
|
|
|
|
|
|
|
|
$request->expects($this->never())
|
|
|
|
->method('getUrl');
|
|
|
|
|
|
|
|
$response->expects($this->never())
|
|
|
|
->method('setHeader');
|
|
|
|
|
|
|
|
$this->server->expects($this->any())
|
|
|
|
->method('getRequestUri')
|
|
|
|
->will($this->returnValue($path));
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
|
|
|
$this->plugin->httpPost($request, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Sabre\DAV\Exception\UnsupportedMediaType
|
|
|
|
*/
|
|
|
|
public function testCreateCommentUnsupportedMediaType() {
|
|
|
|
$commentData = [
|
|
|
|
'actorType' => 'users',
|
|
|
|
'verb' => 'comment',
|
|
|
|
'message' => 'my first comment',
|
|
|
|
];
|
|
|
|
|
|
|
|
$comment = new Comment([
|
|
|
|
'objectType' => 'files',
|
|
|
|
'objectId' => '42',
|
|
|
|
'actorType' => 'users',
|
|
|
|
'actorId' => 'alice'
|
|
|
|
] + $commentData);
|
|
|
|
$comment->setId('23');
|
|
|
|
|
|
|
|
$path = 'comments/files/42';
|
|
|
|
|
|
|
|
$requestData = json_encode($commentData);
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-01-11 20:09:00 +03:00
|
|
|
$user->expects($this->never())
|
|
|
|
->method('getUID');
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$node = $this->getMockBuilder(EntityCollection::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('files'));
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue('42'));
|
|
|
|
|
|
|
|
$this->commentsManager->expects($this->never())
|
|
|
|
->method('create');
|
|
|
|
|
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('getUser');
|
|
|
|
|
|
|
|
// technically, this is a shortcut. Inbetween EntityTypeCollection would
|
|
|
|
// be returned, but doing it exactly right would not be really
|
|
|
|
// unit-testing like, as it would require to haul in a lot of other
|
|
|
|
// things.
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/' . $path)
|
|
|
|
->will($this->returnValue($node));
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$request = $this->getMockBuilder(RequestInterface::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getPath')
|
|
|
|
->will($this->returnValue('/' . $path));
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getBodyAsString')
|
|
|
|
->will($this->returnValue($requestData));
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('Content-Type')
|
|
|
|
->will($this->returnValue('application/trumpscript'));
|
|
|
|
|
|
|
|
$request->expects($this->never())
|
|
|
|
->method('getUrl');
|
|
|
|
|
|
|
|
$response->expects($this->never())
|
|
|
|
->method('setHeader');
|
|
|
|
|
|
|
|
$this->server->expects($this->any())
|
|
|
|
->method('getRequestUri')
|
|
|
|
->will($this->returnValue($path));
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
|
|
|
$this->plugin->httpPost($request, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Sabre\DAV\Exception\BadRequest
|
|
|
|
*/
|
|
|
|
public function testCreateCommentInvalidPayload() {
|
|
|
|
$commentData = [
|
|
|
|
'actorType' => 'users',
|
|
|
|
'verb' => '',
|
|
|
|
'message' => '',
|
|
|
|
];
|
|
|
|
|
|
|
|
$comment = new Comment([
|
|
|
|
'objectType' => 'files',
|
|
|
|
'objectId' => '42',
|
|
|
|
'actorType' => 'users',
|
|
|
|
'actorId' => 'alice',
|
|
|
|
'message' => 'dummy',
|
|
|
|
'verb' => 'dummy'
|
|
|
|
]);
|
|
|
|
$comment->setId('23');
|
|
|
|
|
|
|
|
$path = 'comments/files/42';
|
|
|
|
|
|
|
|
$requestData = json_encode($commentData);
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-01-11 20:09:00 +03:00
|
|
|
$user->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('alice'));
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$node = $this->getMockBuilder(EntityCollection::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('files'));
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue('42'));
|
|
|
|
|
|
|
|
$this->commentsManager->expects($this->once())
|
|
|
|
->method('create')
|
|
|
|
->with('users', 'alice', 'files', '42')
|
|
|
|
->will($this->returnValue($comment));
|
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
|
|
|
|
// technically, this is a shortcut. Inbetween EntityTypeCollection would
|
|
|
|
// be returned, but doing it exactly right would not be really
|
|
|
|
// unit-testing like, as it would require to haul in a lot of other
|
|
|
|
// things.
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/' . $path)
|
|
|
|
->will($this->returnValue($node));
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$request = $this->getMockBuilder(RequestInterface::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getPath')
|
|
|
|
->will($this->returnValue('/' . $path));
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getBodyAsString')
|
|
|
|
->will($this->returnValue($requestData));
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('Content-Type')
|
|
|
|
->will($this->returnValue('application/json'));
|
|
|
|
|
|
|
|
$request->expects($this->never())
|
|
|
|
->method('getUrl');
|
|
|
|
|
|
|
|
$response->expects($this->never())
|
|
|
|
->method('setHeader');
|
|
|
|
|
|
|
|
$this->server->expects($this->any())
|
2016-02-09 15:59:13 +03:00
|
|
|
->method('getRequestUri')
|
|
|
|
->will($this->returnValue($path));
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
|
|
|
$this->plugin->httpPost($request, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Sabre\DAV\Exception\BadRequest
|
|
|
|
* @expectedExceptionMessage Message exceeds allowed character limit of
|
|
|
|
*/
|
|
|
|
public function testCreateCommentMessageTooLong() {
|
|
|
|
$commentData = [
|
|
|
|
'actorType' => 'users',
|
|
|
|
'verb' => 'comment',
|
|
|
|
'message' => str_pad('', IComment::MAX_MESSAGE_LENGTH + 1, 'x'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$comment = new Comment([
|
|
|
|
'objectType' => 'files',
|
|
|
|
'objectId' => '42',
|
|
|
|
'actorType' => 'users',
|
|
|
|
'actorId' => 'alice',
|
|
|
|
'verb' => 'comment',
|
|
|
|
]);
|
|
|
|
$comment->setId('23');
|
|
|
|
|
|
|
|
$path = 'comments/files/42';
|
|
|
|
|
|
|
|
$requestData = json_encode($commentData);
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-02-09 15:59:13 +03:00
|
|
|
$user->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('alice'));
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$node = $this->getMockBuilder(EntityCollection::class)
|
2016-02-09 15:59:13 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('files'));
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue('42'));
|
|
|
|
|
|
|
|
$node->expects($this->never())
|
|
|
|
->method('setReadMarker');
|
|
|
|
|
|
|
|
$this->commentsManager->expects($this->once())
|
|
|
|
->method('create')
|
|
|
|
->with('users', 'alice', 'files', '42')
|
|
|
|
->will($this->returnValue($comment));
|
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
|
|
|
|
// technically, this is a shortcut. Inbetween EntityTypeCollection would
|
|
|
|
// be returned, but doing it exactly right would not be really
|
|
|
|
// unit-testing like, as it would require to haul in a lot of other
|
|
|
|
// things.
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/' . $path)
|
|
|
|
->will($this->returnValue($node));
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$request = $this->getMockBuilder(RequestInterface::class)
|
2016-02-09 15:59:13 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2016-02-09 15:59:13 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getPath')
|
|
|
|
->will($this->returnValue('/' . $path));
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getBodyAsString')
|
|
|
|
->will($this->returnValue($requestData));
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('Content-Type')
|
|
|
|
->will($this->returnValue('application/json'));
|
|
|
|
|
|
|
|
$response->expects($this->never())
|
|
|
|
->method('setHeader');
|
|
|
|
|
|
|
|
$this->server->expects($this->any())
|
2016-01-11 20:09:00 +03:00
|
|
|
->method('getRequestUri')
|
|
|
|
->will($this->returnValue($path));
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
|
|
|
$this->plugin->httpPost($request, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Sabre\DAV\Exception\ReportNotSupported
|
|
|
|
*/
|
|
|
|
public function testOnReportInvalidNode() {
|
|
|
|
$path = 'totally/unrelated/13';
|
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/' . $path)
|
2016-07-15 10:52:46 +03:00
|
|
|
->will($this->returnValue(
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->getMockBuilder(INode::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
));
|
2016-01-11 20:09:00 +03:00
|
|
|
|
|
|
|
$this->server->expects($this->any())
|
|
|
|
->method('getRequestUri')
|
|
|
|
->will($this->returnValue($path));
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
2016-01-26 23:19:44 +03:00
|
|
|
$this->plugin->onReport(CommentsPluginImplementation::REPORT_NAME, [], '/' . $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Sabre\DAV\Exception\ReportNotSupported
|
|
|
|
*/
|
|
|
|
public function testOnReportInvalidReportName() {
|
|
|
|
$path = 'comments/files/42';
|
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/' . $path)
|
2016-07-15 10:52:46 +03:00
|
|
|
->will($this->returnValue(
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->getMockBuilder(INode::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
));
|
2016-01-26 23:19:44 +03:00
|
|
|
|
|
|
|
$this->server->expects($this->any())
|
|
|
|
->method('getRequestUri')
|
|
|
|
->will($this->returnValue($path));
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
|
|
|
$this->plugin->onReport('{whoever}whatever', [], '/' . $path);
|
2016-01-11 20:09:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testOnReportDateTimeEmpty() {
|
|
|
|
$path = 'comments/files/42';
|
|
|
|
|
|
|
|
$parameters = [
|
|
|
|
[
|
|
|
|
'name' => '{http://owncloud.org/ns}limit',
|
|
|
|
'value' => 5,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => '{http://owncloud.org/ns}offset',
|
|
|
|
'value' => 10,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => '{http://owncloud.org/ns}datetime',
|
|
|
|
'value' => '',
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$node = $this->getMockBuilder(EntityCollection::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('findChildren')
|
|
|
|
->with(5, 10, null)
|
|
|
|
->will($this->returnValue([]));
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('setHeader')
|
|
|
|
->with('Content-Type', 'application/xml; charset=utf-8');
|
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('setStatus')
|
|
|
|
->with(207);
|
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('setBody');
|
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/' . $path)
|
|
|
|
->will($this->returnValue($node));
|
|
|
|
|
|
|
|
$this->server->expects($this->any())
|
|
|
|
->method('getRequestUri')
|
|
|
|
->will($this->returnValue($path));
|
|
|
|
$this->server->httpResponse = $response;
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
2016-01-26 23:19:44 +03:00
|
|
|
$this->plugin->onReport(CommentsPluginImplementation::REPORT_NAME, $parameters, '/' . $path);
|
2016-01-11 20:09:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testOnReport() {
|
|
|
|
$path = 'comments/files/42';
|
|
|
|
|
|
|
|
$parameters = [
|
|
|
|
[
|
|
|
|
'name' => '{http://owncloud.org/ns}limit',
|
|
|
|
'value' => 5,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => '{http://owncloud.org/ns}offset',
|
|
|
|
'value' => 10,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => '{http://owncloud.org/ns}datetime',
|
|
|
|
'value' => '2016-01-10 18:48:00',
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$node = $this->getMockBuilder(EntityCollection::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('findChildren')
|
|
|
|
->with(5, 10, new \DateTime($parameters[2]['value']))
|
|
|
|
->will($this->returnValue([]));
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2016-01-11 20:09:00 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('setHeader')
|
|
|
|
->with('Content-Type', 'application/xml; charset=utf-8');
|
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('setStatus')
|
|
|
|
->with(207);
|
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('setBody');
|
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/' . $path)
|
|
|
|
->will($this->returnValue($node));
|
|
|
|
|
|
|
|
$this->server->expects($this->any())
|
|
|
|
->method('getRequestUri')
|
|
|
|
->will($this->returnValue($path));
|
|
|
|
$this->server->httpResponse = $response;
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
2016-01-26 23:19:44 +03:00
|
|
|
$this->plugin->onReport(CommentsPluginImplementation::REPORT_NAME, $parameters, '/' . $path);
|
2016-01-11 20:09:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|