Limit comment message to 1k chars

This commit is contained in:
Arthur Schiwon 2016-02-09 03:14:30 +01:00
parent 850ac0cf84
commit 347ad3e223
4 changed files with 49 additions and 1 deletions

View file

@ -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;
}

View file

@ -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);

View file

@ -0,0 +1,27 @@
<?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 OCP\Comments;
/**
* Exception for not found entity
* @since 9.0.0
*/
class MessageTooLongException extends \Exception {}

View file

@ -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);
}
}