diff options
author | Vincent Petry <pvince81@owncloud.com> | 2016-05-25 18:26:32 +0300 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2016-05-25 18:26:32 +0300 |
commit | fa5fd68d751322c4281a991c0c2afa81a874afac (patch) | |
tree | fad8374ef5ab79e786db9c72f69524e49248a86e /apps/dav/tests/unit/Comments | |
parent | 379f8a1e4538a76d4b9bbc4adf0ebe7b12b1d104 (diff) | |
parent | 5882e21b3bff0afe2152eb3971d674bdb91e45a2 (diff) |
Merge pull request #24844 from owncloud/dav-test-psr4
Move DAV Tests to PSR-4
Diffstat (limited to 'apps/dav/tests/unit/Comments')
-rw-r--r-- | apps/dav/tests/unit/Comments/CommentsNodeTest.php | 459 | ||||
-rw-r--r-- | apps/dav/tests/unit/Comments/CommentsPluginTest.php | 742 | ||||
-rw-r--r-- | apps/dav/tests/unit/Comments/EntityCollectionTest.php | 118 | ||||
-rw-r--r-- | apps/dav/tests/unit/Comments/EntityTypeCollectionTest.php | 92 | ||||
-rw-r--r-- | apps/dav/tests/unit/Comments/RootCollectionTest.php | 170 |
5 files changed, 1581 insertions, 0 deletions
diff --git a/apps/dav/tests/unit/Comments/CommentsNodeTest.php b/apps/dav/tests/unit/Comments/CommentsNodeTest.php new file mode 100644 index 00000000000..d812ad62cb1 --- /dev/null +++ b/apps/dav/tests/unit/Comments/CommentsNodeTest.php @@ -0,0 +1,459 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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\Comments; + +use OCA\DAV\Comments\CommentNode; +use OCP\Comments\IComment; +use OCP\Comments\MessageTooLongException; + +class CommentsNodeTest extends \Test\TestCase { + + protected $commentsManager; + protected $comment; + protected $node; + protected $userManager; + protected $logger; + protected $userSession; + + public function setUp() { + parent::setUp(); + + $this->commentsManager = $this->getMock('\OCP\Comments\ICommentsManager'); + $this->comment = $this->getMock('\OCP\Comments\IComment'); + $this->userManager = $this->getMock('\OCP\IUserManager'); + $this->userSession = $this->getMock('\OCP\IUserSession'); + $this->logger = $this->getMock('\OCP\ILogger'); + + $this->node = new CommentNode( + $this->commentsManager, + $this->comment, + $this->userManager, + $this->userSession, + $this->logger + ); + } + + public function testDelete() { + $user = $this->getMock('\OCP\IUser'); + + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('alice')); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + + $this->comment->expects($this->once()) + ->method('getId') + ->will($this->returnValue('19')); + + $this->comment->expects($this->any()) + ->method('getActorType') + ->will($this->returnValue('users')); + + $this->comment->expects($this->any()) + ->method('getActorId') + ->will($this->returnValue('alice')); + + $this->commentsManager->expects($this->once()) + ->method('delete') + ->with('19'); + + $this->node->delete(); + } + + /** + * @expectedException \Sabre\DAV\Exception\Forbidden + */ + public function testDeleteForbidden() { + $user = $this->getMock('\OCP\IUser'); + + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('mallory')); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + + $this->comment->expects($this->never()) + ->method('getId'); + + $this->comment->expects($this->any()) + ->method('getActorType') + ->will($this->returnValue('users')); + + $this->comment->expects($this->any()) + ->method('getActorId') + ->will($this->returnValue('alice')); + + $this->commentsManager->expects($this->never()) + ->method('delete'); + + $this->node->delete(); + } + + public function testGetName() { + $id = '19'; + $this->comment->expects($this->once()) + ->method('getId') + ->will($this->returnValue($id)); + + $this->assertSame($this->node->getName(), $id); + } + + /** + * @expectedException \Sabre\DAV\Exception\MethodNotAllowed + */ + public function testSetName() { + $this->node->setName('666'); + } + + public function testGetLastModified() { + $this->assertSame($this->node->getLastModified(), null); + } + + public function testUpdateComment() { + $msg = 'Hello Earth'; + + $user = $this->getMock('\OCP\IUser'); + + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('alice')); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + + $this->comment->expects($this->once()) + ->method('setMessage') + ->with($msg); + + $this->comment->expects($this->any()) + ->method('getActorType') + ->will($this->returnValue('users')); + + $this->comment->expects($this->any()) + ->method('getActorId') + ->will($this->returnValue('alice')); + + $this->commentsManager->expects($this->once()) + ->method('save') + ->with($this->comment); + + $this->assertTrue($this->node->updateComment($msg)); + } + + public function testUpdateCommentLogException() { + $msg = null; + + $user = $this->getMock('\OCP\IUser'); + + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('alice')); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + + $this->comment->expects($this->once()) + ->method('setMessage') + ->with($msg) + ->will($this->throwException(new \Exception('buh!'))); + + $this->comment->expects($this->any()) + ->method('getActorType') + ->will($this->returnValue('users')); + + $this->comment->expects($this->any()) + ->method('getActorId') + ->will($this->returnValue('alice')); + + $this->commentsManager->expects($this->never()) + ->method('save'); + + $this->logger->expects($this->once()) + ->method('logException'); + + $this->assertFalse($this->node->updateComment($msg)); + } + + /** + * @expectedException \Sabre\DAV\Exception\BadRequest + * @expectedExceptionMessage Message exceeds allowed character limit of + */ + public function testUpdateCommentMessageTooLongException() { + $user = $this->getMock('\OCP\IUser'); + + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('alice')); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + + $this->comment->expects($this->once()) + ->method('setMessage') + ->will($this->throwException(new MessageTooLongException())); + + $this->comment->expects($this->any()) + ->method('getActorType') + ->will($this->returnValue('users')); + + $this->comment->expects($this->any()) + ->method('getActorId') + ->will($this->returnValue('alice')); + + $this->commentsManager->expects($this->never()) + ->method('save'); + + $this->logger->expects($this->once()) + ->method('logException'); + + // imagine 'foo' has >1k characters. comment is mocked anyway. + $this->node->updateComment('foo'); + } + + /** + * @expectedException \Sabre\DAV\Exception\Forbidden + */ + public function testUpdateForbiddenByUser() { + $msg = 'HaXX0r'; + + $user = $this->getMock('\OCP\IUser'); + + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('mallory')); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + + $this->comment->expects($this->never()) + ->method('setMessage'); + + $this->comment->expects($this->any()) + ->method('getActorType') + ->will($this->returnValue('users')); + + $this->comment->expects($this->any()) + ->method('getActorId') + ->will($this->returnValue('alice')); + + $this->commentsManager->expects($this->never()) + ->method('save'); + + $this->node->updateComment($msg); + } + + /** + * @expectedException \Sabre\DAV\Exception\Forbidden + */ + public function testUpdateForbiddenByType() { + $msg = 'HaXX0r'; + + $user = $this->getMock('\OCP\IUser'); + + $user->expects($this->never()) + ->method('getUID'); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + + $this->comment->expects($this->never()) + ->method('setMessage'); + + $this->comment->expects($this->any()) + ->method('getActorType') + ->will($this->returnValue('bots')); + + $this->commentsManager->expects($this->never()) + ->method('save'); + + $this->node->updateComment($msg); + } + + /** + * @expectedException \Sabre\DAV\Exception\Forbidden + */ + public function testUpdateForbiddenByNotLoggedIn() { + $msg = 'HaXX0r'; + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue(null)); + + $this->comment->expects($this->never()) + ->method('setMessage'); + + $this->comment->expects($this->any()) + ->method('getActorType') + ->will($this->returnValue('users')); + + $this->commentsManager->expects($this->never()) + ->method('save'); + + $this->node->updateComment($msg); + } + + public function testPropPatch() { + $propPatch = $this->getMockBuilder('Sabre\DAV\PropPatch') + ->disableOriginalConstructor() + ->getMock(); + + $propPatch->expects($this->once()) + ->method('handle') + ->with('{http://owncloud.org/ns}message'); + + $this->node->propPatch($propPatch); + } + + public function testGetProperties() { + $ns = '{http://owncloud.org/ns}'; + $expected = [ + $ns . 'id' => '123', + $ns . 'parentId' => '12', + $ns . 'topmostParentId' => '2', + $ns . 'childrenCount' => 3, + $ns . 'message' => 'such a nice file you have…', + $ns . 'verb' => 'comment', + $ns . 'actorType' => 'users', + $ns . 'actorId' => 'alice', + $ns . 'actorDisplayName' => 'Alice of Wonderland', + $ns . 'creationDateTime' => new \DateTime('2016-01-10 18:48:00'), + $ns . 'latestChildDateTime' => new \DateTime('2016-01-12 18:48:00'), + $ns . 'objectType' => 'files', + $ns . 'objectId' => '1848', + $ns . 'isUnread' => null, + ]; + + $this->comment->expects($this->once()) + ->method('getId') + ->will($this->returnValue($expected[$ns . 'id'])); + + $this->comment->expects($this->once()) + ->method('getParentId') + ->will($this->returnValue($expected[$ns . 'parentId'])); + + $this->comment->expects($this->once()) + ->method('getTopmostParentId') + ->will($this->returnValue($expected[$ns . 'topmostParentId'])); + + $this->comment->expects($this->once()) + ->method('getChildrenCount') + ->will($this->returnValue($expected[$ns . 'childrenCount'])); + + $this->comment->expects($this->once()) + ->method('getMessage') + ->will($this->returnValue($expected[$ns . 'message'])); + + $this->comment->expects($this->once()) + ->method('getVerb') + ->will($this->returnValue($expected[$ns . 'verb'])); + + $this->comment->expects($this->exactly(2)) + ->method('getActorType') + ->will($this->returnValue($expected[$ns . 'actorType'])); + + $this->comment->expects($this->exactly(2)) + ->method('getActorId') + ->will($this->returnValue($expected[$ns . 'actorId'])); + + $this->comment->expects($this->once()) + ->method('getCreationDateTime') + ->will($this->returnValue($expected[$ns . 'creationDateTime'])); + + $this->comment->expects($this->once()) + ->method('getLatestChildDateTime') + ->will($this->returnValue($expected[$ns . 'latestChildDateTime'])); + + $this->comment->expects($this->once()) + ->method('getObjectType') + ->will($this->returnValue($expected[$ns . 'objectType'])); + + $this->comment->expects($this->once()) + ->method('getObjectId') + ->will($this->returnValue($expected[$ns . 'objectId'])); + + $user = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $user->expects($this->once()) + ->method('getDisplayName') + ->will($this->returnValue($expected[$ns . 'actorDisplayName'])); + + $this->userManager->expects($this->once()) + ->method('get') + ->with('alice') + ->will($this->returnValue($user)); + + $properties = $this->node->getProperties(null); + + foreach($properties as $name => $value) { + $this->assertTrue(array_key_exists($name, $expected)); + $this->assertSame($expected[$name], $value); + unset($expected[$name]); + } + $this->assertTrue(empty($expected)); + } + + public function readCommentProvider() { + $creationDT = new \DateTime('2016-01-19 18:48:00'); + $diff = new \DateInterval('PT2H'); + $readDT1 = clone $creationDT; $readDT1->sub($diff); + $readDT2 = clone $creationDT; $readDT2->add($diff); + return [ + [$creationDT, $readDT1, 'true'], + [$creationDT, $readDT2, 'false'], + [$creationDT, null, 'true'], + ]; + } + + /** + * @dataProvider readCommentProvider + * @param $expected + */ + public function testGetPropertiesUnreadProperty($creationDT, $readDT, $expected) { + $this->comment->expects($this->any()) + ->method('getCreationDateTime') + ->will($this->returnValue($creationDT)); + + $this->commentsManager->expects($this->once()) + ->method('getReadMark') + ->will($this->returnValue($readDT)); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($this->getMock('\OCP\IUser'))); + + $properties = $this->node->getProperties(null); + + $this->assertTrue(array_key_exists(CommentNode::PROPERTY_NAME_UNREAD, $properties)); + $this->assertSame($properties[CommentNode::PROPERTY_NAME_UNREAD], $expected); + } +} diff --git a/apps/dav/tests/unit/Comments/CommentsPluginTest.php b/apps/dav/tests/unit/Comments/CommentsPluginTest.php new file mode 100644 index 00000000000..608717bd0af --- /dev/null +++ b/apps/dav/tests/unit/Comments/CommentsPluginTest.php @@ -0,0 +1,742 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Vincent Petry <pvince81@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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\Comments; + +use OC\Comments\Comment; +use OCA\DAV\Comments\CommentsPlugin as CommentsPluginImplementation; +use OCP\Comments\IComment; +use Sabre\DAV\Exception\NotFound; + +class CommentsPluginTest extends \Test\TestCase { + /** @var \Sabre\DAV\Server */ + private $server; + + /** @var \Sabre\DAV\Tree */ + private $tree; + + /** @var \OCP\Comments\ICommentsManager */ + private $commentsManager; + + /** @var \OCP\IUserSession */ + private $userSession; + + /** @var CommentsPluginImplementation */ + private $plugin; + + public function setUp() { + parent::setUp(); + $this->tree = $this->getMockBuilder('\Sabre\DAV\Tree') + ->disableOriginalConstructor() + ->getMock(); + + $this->server = $this->getMockBuilder('\Sabre\DAV\Server') + ->setConstructorArgs([$this->tree]) + ->setMethods(['getRequestUri']) + ->getMock(); + + $this->commentsManager = $this->getMock('\OCP\Comments\ICommentsManager'); + $this->userSession = $this->getMock('\OCP\IUserSession'); + + $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); + + $user = $this->getMock('OCP\IUser'); + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('alice')); + + $node = $this->getMockBuilder('\OCA\DAV\Comments\EntityCollection') + ->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->once()) + ->method('setReadMarker') + ->with(null); + + $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)); + + $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface') + ->disableOriginalConstructor() + ->getMock(); + + $response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface') + ->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); + } + + /** + * @expectedException \Sabre\DAV\Exception\NotFound + */ + 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'; + + $user = $this->getMock('OCP\IUser'); + $user->expects($this->never()) + ->method('getUID'); + + $node = $this->getMockBuilder('\OCA\DAV\Comments\EntityCollection') + ->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())); + + $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface') + ->disableOriginalConstructor() + ->getMock(); + + $response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface') + ->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); + + $user = $this->getMock('OCP\IUser'); + $user->expects($this->never()) + ->method('getUID'); + + $node = $this->getMockBuilder('\OCA\DAV\Comments\EntityCollection') + ->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)); + + $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface') + ->disableOriginalConstructor() + ->getMock(); + + $response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface') + ->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); + + $user = $this->getMock('OCP\IUser'); + $user->expects($this->never()) + ->method('getUID'); + + $node = $this->getMockBuilder('\OCA\DAV\Comments\EntityCollection') + ->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)); + + $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface') + ->disableOriginalConstructor() + ->getMock(); + + $response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface') + ->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); + + $user = $this->getMock('OCP\IUser'); + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('alice')); + + $node = $this->getMockBuilder('\OCA\DAV\Comments\EntityCollection') + ->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)); + + $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface') + ->disableOriginalConstructor() + ->getMock(); + + $response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface') + ->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\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); + + $user = $this->getMock('OCP\IUser'); + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('alice')); + + $node = $this->getMockBuilder('\OCA\DAV\Comments\EntityCollection') + ->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)); + + $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface') + ->disableOriginalConstructor() + ->getMock(); + + $response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface') + ->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()) + ->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) + ->will($this->returnValue($this->getMock('\Sabre\DAV\INode'))); + + $this->server->expects($this->any()) + ->method('getRequestUri') + ->will($this->returnValue($path)); + $this->plugin->initialize($this->server); + + $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) + ->will($this->returnValue($this->getMock('\Sabre\DAV\INode'))); + + $this->server->expects($this->any()) + ->method('getRequestUri') + ->will($this->returnValue($path)); + $this->plugin->initialize($this->server); + + $this->plugin->onReport('{whoever}whatever', [], '/' . $path); + } + + 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' => '', + ] + ]; + + $node = $this->getMockBuilder('\OCA\DAV\Comments\EntityCollection') + ->disableOriginalConstructor() + ->getMock(); + $node->expects($this->once()) + ->method('findChildren') + ->with(5, 10, null) + ->will($this->returnValue([])); + + $response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface') + ->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); + + $this->plugin->onReport(CommentsPluginImplementation::REPORT_NAME, $parameters, '/' . $path); + } + + 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', + ] + ]; + + $node = $this->getMockBuilder('\OCA\DAV\Comments\EntityCollection') + ->disableOriginalConstructor() + ->getMock(); + $node->expects($this->once()) + ->method('findChildren') + ->with(5, 10, new \DateTime($parameters[2]['value'])) + ->will($this->returnValue([])); + + $response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface') + ->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); + + $this->plugin->onReport(CommentsPluginImplementation::REPORT_NAME, $parameters, '/' . $path); + } + + + +} diff --git a/apps/dav/tests/unit/Comments/EntityCollectionTest.php b/apps/dav/tests/unit/Comments/EntityCollectionTest.php new file mode 100644 index 00000000000..157d58c9eca --- /dev/null +++ b/apps/dav/tests/unit/Comments/EntityCollectionTest.php @@ -0,0 +1,118 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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\Comments; + +class EntityCollectionTest extends \Test\TestCase { + + /** @var \OCP\Comments\ICommentsManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $commentsManager; + /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $userManager; + /** @var \OCP\ILogger|\PHPUnit_Framework_MockObject_MockObject */ + protected $logger; + /** @var \OCA\DAV\Comments\EntityCollection */ + protected $collection; + /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + protected $userSession; + + public function setUp() { + parent::setUp(); + + $this->commentsManager = $this->getMock('\OCP\Comments\ICommentsManager'); + $this->userManager = $this->getMock('\OCP\IUserManager'); + $this->userSession = $this->getMock('\OCP\IUserSession'); + $this->logger = $this->getMock('\OCP\ILogger'); + + $this->collection = new \OCA\DAV\Comments\EntityCollection( + '19', + 'files', + $this->commentsManager, + $this->userManager, + $this->userSession, + $this->logger + ); + } + + public function testGetId() { + $this->assertSame($this->collection->getId(), '19'); + } + + public function testGetChild() { + $this->commentsManager->expects($this->once()) + ->method('get') + ->with('55') + ->will($this->returnValue($this->getMock('\OCP\Comments\IComment'))); + + $node = $this->collection->getChild('55'); + $this->assertTrue($node instanceof \OCA\DAV\Comments\CommentNode); + } + + /** + * @expectedException \Sabre\DAV\Exception\NotFound + */ + public function testGetChildException() { + $this->commentsManager->expects($this->once()) + ->method('get') + ->with('55') + ->will($this->throwException(new \OCP\Comments\NotFoundException())); + + $this->collection->getChild('55'); + } + + public function testGetChildren() { + $this->commentsManager->expects($this->once()) + ->method('getForObject') + ->with('files', '19') + ->will($this->returnValue([$this->getMock('\OCP\Comments\IComment')])); + + $result = $this->collection->getChildren(); + + $this->assertSame(count($result), 1); + $this->assertTrue($result[0] instanceof \OCA\DAV\Comments\CommentNode); + } + + public function testFindChildren() { + $dt = new \DateTime('2016-01-10 18:48:00'); + $this->commentsManager->expects($this->once()) + ->method('getForObject') + ->with('files', '19', 5, 15, $dt) + ->will($this->returnValue([$this->getMock('\OCP\Comments\IComment')])); + + $result = $this->collection->findChildren(5, 15, $dt); + + $this->assertSame(count($result), 1); + $this->assertTrue($result[0] instanceof \OCA\DAV\Comments\CommentNode); + } + + public function testChildExistsTrue() { + $this->assertTrue($this->collection->childExists('44')); + } + + public function testChildExistsFalse() { + $this->commentsManager->expects($this->once()) + ->method('get') + ->with('44') + ->will($this->throwException(new \OCP\Comments\NotFoundException())); + + $this->assertFalse($this->collection->childExists('44')); + } +} diff --git a/apps/dav/tests/unit/Comments/EntityTypeCollectionTest.php b/apps/dav/tests/unit/Comments/EntityTypeCollectionTest.php new file mode 100644 index 00000000000..51db44380fa --- /dev/null +++ b/apps/dav/tests/unit/Comments/EntityTypeCollectionTest.php @@ -0,0 +1,92 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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\Comments; + +use OCA\DAV\Comments\EntityCollection as EntityCollectionImplemantation; + +class EntityTypeCollectionTest extends \Test\TestCase { + + /** @var \OCP\Comments\ICommentsManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $commentsManager; + /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $userManager; + /** @var \OCP\ILogger|\PHPUnit_Framework_MockObject_MockObject */ + protected $logger; + /** @var \OCA\DAV\Comments\EntityTypeCollection */ + protected $collection; + /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + protected $userSession; + + protected $childMap = []; + + public function setUp() { + parent::setUp(); + + $this->commentsManager = $this->getMock('\OCP\Comments\ICommentsManager'); + $this->userManager = $this->getMock('\OCP\IUserManager'); + $this->userSession = $this->getMock('\OCP\IUserSession'); + $this->logger = $this->getMock('\OCP\ILogger'); + + $instance = $this; + + $this->collection = new \OCA\DAV\Comments\EntityTypeCollection( + 'files', + $this->commentsManager, + $this->userManager, + $this->userSession, + $this->logger, + function ($child) use ($instance) { + return !empty($instance->childMap[$child]); + } + ); + } + + public function testChildExistsYes() { + $this->childMap[17] = true; + $this->assertTrue($this->collection->childExists('17')); + } + + public function testChildExistsNo() { + $this->assertFalse($this->collection->childExists('17')); + } + + public function testGetChild() { + $this->childMap[17] = true; + + $ec = $this->collection->getChild('17'); + $this->assertTrue($ec instanceof EntityCollectionImplemantation); + } + + /** + * @expectedException \Sabre\DAV\Exception\NotFound + */ + public function testGetChildException() { + $this->collection->getChild('17'); + } + + /** + * @expectedException \Sabre\DAV\Exception\MethodNotAllowed + */ + public function testGetChildren() { + $this->collection->getChildren(); + } +} diff --git a/apps/dav/tests/unit/Comments/RootCollectionTest.php b/apps/dav/tests/unit/Comments/RootCollectionTest.php new file mode 100644 index 00000000000..8f3b83e4264 --- /dev/null +++ b/apps/dav/tests/unit/Comments/RootCollectionTest.php @@ -0,0 +1,170 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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\Comments; + +use OCA\DAV\Comments\EntityTypeCollection as EntityTypeCollectionImplementation; +use OCP\Comments\CommentsEntityEvent; +use Symfony\Component\EventDispatcher\EventDispatcher; + +class RootCollectionTest extends \Test\TestCase { + + /** @var \OCP\Comments\ICommentsManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $commentsManager; + /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $userManager; + /** @var \OCP\ILogger|\PHPUnit_Framework_MockObject_MockObject */ + protected $logger; + /** @var \OCA\DAV\Comments\RootCollection */ + protected $collection; + /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + protected $userSession; + /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface */ + protected $dispatcher; + /** @var \OCP\IUser|\PHPUnit_Framework_MockObject_MockObject */ + protected $user; + + public function setUp() { + parent::setUp(); + + $this->user = $this->getMock('\OCP\IUser'); + + $this->commentsManager = $this->getMock('\OCP\Comments\ICommentsManager'); + $this->userManager = $this->getMock('\OCP\IUserManager'); + $this->userSession = $this->getMock('\OCP\IUserSession'); + $this->dispatcher = new EventDispatcher(); + $this->logger = $this->getMock('\OCP\ILogger'); + + $this->collection = new \OCA\DAV\Comments\RootCollection( + $this->commentsManager, + $this->userManager, + $this->userSession, + $this->dispatcher, + $this->logger + ); + } + + protected function prepareForInitCollections() { + $this->user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('alice')); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $this->dispatcher->addListener(CommentsEntityEvent::EVENT_ENTITY, function(CommentsEntityEvent $event) { + $event->addEntityCollection('files', function() { + return true; + }); + }); + } + + /** + * @expectedException \Sabre\DAV\Exception\Forbidden + */ + public function testCreateFile() { + $this->collection->createFile('foo'); + } + + /** + * @expectedException \Sabre\DAV\Exception\Forbidden + */ + public function testCreateDirectory() { + $this->collection->createDirectory('foo'); + } + + public function testGetChild() { + $this->prepareForInitCollections(); + $etc = $this->collection->getChild('files'); + $this->assertTrue($etc instanceof EntityTypeCollectionImplementation); + } + + /** + * @expectedException \Sabre\DAV\Exception\NotFound + */ + public function testGetChildInvalid() { + $this->prepareForInitCollections(); + $this->collection->getChild('robots'); + } + + /** + * @expectedException \Sabre\DAV\Exception\NotAuthenticated + */ + public function testGetChildNoAuth() { + $this->collection->getChild('files'); + } + + public function testGetChildren() { + $this->prepareForInitCollections(); + $children = $this->collection->getChildren(); + $this->assertFalse(empty($children)); + foreach($children as $child) { + $this->assertTrue($child instanceof EntityTypeCollectionImplementation); + } + } + + /** + * @expectedException \Sabre\DAV\Exception\NotAuthenticated + */ + public function testGetChildrenNoAuth() { + $this->collection->getChildren(); + } + + public function testChildExistsYes() { + $this->prepareForInitCollections(); + $this->assertTrue($this->collection->childExists('files')); + } + + public function testChildExistsNo() { + $this->prepareForInitCollections(); + $this->assertFalse($this->collection->childExists('robots')); + } + + /** + * @expectedException \Sabre\DAV\Exception\NotAuthenticated + */ + public function testChildExistsNoAuth() { + $this->collection->childExists('files'); + } + + /** + * @expectedException \Sabre\DAV\Exception\Forbidden + */ + public function testDelete() { + $this->collection->delete(); + } + + public function testGetName() { + $this->assertSame('comments', $this->collection->getName()); + } + + /** + * @expectedException \Sabre\DAV\Exception\Forbidden + */ + public function testSetName() { + $this->collection->setName('foobar'); + } + + public function testGetLastModified() { + $this->assertSame(null, $this->collection->getLastModified()); + } +} |