diff --git a/lib/private/comments/comment.php b/lib/private/comments/comment.php index 36189d9523..27e63c9855 100644 --- a/lib/private/comments/comment.php +++ b/lib/private/comments/comment.php @@ -22,6 +22,7 @@ namespace OC\Comments; use OCP\Comments\IComment; use OCP\Comments\IllegalIDChangeException; +use OCP\Comments\MessageTooLongException; class Comment implements IComment { @@ -184,13 +185,18 @@ class Comment implements IComment { * * @param string $message * @return IComment + * @throws MessageTooLongException * @since 9.0.0 */ public function setMessage($message) { if(!is_string($message)) { throw new \InvalidArgumentException('String expected.'); } - $this->data['message'] = trim($message); + $message = trim($message); + if(mb_strlen($message, 'UTF-8') > IComment::MAX_MESSAGE_LENGTH) { + throw new MessageTooLongException('Comment message must not exceed ' . IComment::MAX_MESSAGE_LENGTH . ' characters'); + } + $this->data['message'] = $message; return $this; } diff --git a/lib/public/comments/icomment.php b/lib/public/comments/icomment.php index e695b5193f..a7f4b4c617 100644 --- a/lib/public/comments/icomment.php +++ b/lib/public/comments/icomment.php @@ -29,6 +29,7 @@ namespace OCP\Comments; * @since 9.0.0 */ interface IComment { + const MAX_MESSAGE_LENGTH = 1000; /** * returns the ID of the comment @@ -119,8 +120,12 @@ interface IComment { /** * sets the message of the comment and returns itself * + * When the given message length exceeds MAX_MESSAGE_LENGTH an + * MessageTooLongException shall be thrown. + * * @param string $message * @return IComment + * @throws MessageTooLongException * @since 9.0.0 */ public function setMessage($message); diff --git a/lib/public/comments/messagetoolongexception.php b/lib/public/comments/messagetoolongexception.php new file mode 100644 index 0000000000..5b2809ae9c --- /dev/null +++ b/lib/public/comments/messagetoolongexception.php @@ -0,0 +1,27 @@ + + * + * @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 + * + */ +namespace OCP\Comments; + +/** + * Exception for not found entity + * @since 9.0.0 + */ +class MessageTooLongException extends \Exception {} diff --git a/tests/lib/comments/comment.php b/tests/lib/comments/comment.php index e6f9c941c9..9b3f2ab166 100644 --- a/tests/lib/comments/comment.php +++ b/tests/lib/comments/comment.php @@ -2,6 +2,7 @@ namespace Test\Comments; +use OCP\Comments\IComment; use Test\TestCase; class Test_Comments_Comment extends TestCase @@ -107,6 +108,15 @@ class Test_Comments_Comment extends TestCase $comment->$setter($type, $id); } + /** + * @expectedException \OCP\Comments\MessageTooLongException + */ + public function testSetUberlongMessage() { + $comment = new \OC\Comments\Comment(); + $msg = str_pad('', IComment::MAX_MESSAGE_LENGTH + 1, 'x'); + $comment->setMessage($msg); + } + }