From 7449a692d4d8e55c88968b42d51bc186903b8901 Mon Sep 17 00:00:00 2001 From: vatseek Date: Fri, 1 Mar 2013 14:58:34 +0200 Subject: [PATCH] comments --- .travis.yml | 2 +- classes/actions/ActionBlog.class.php | 238 +++++++++++++++++---------- tests/behat/features/comment.feature | 46 ++++++ 3 files changed, 198 insertions(+), 88 deletions(-) create mode 100644 tests/behat/features/comment.feature diff --git a/.travis.yml b/.travis.yml index b587b5b1..8a5db5c9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,7 +35,7 @@ before_script: - sleep 5 # download and launch Selenium - - wget -O /tmp/selenium-server-standalone.jar http://selenium.googlecode.com/files/selenium-server-standalone-2.25.0.jar + - wget -O /tmp/selenium-server-standalone.jar http://selenium.googlecode.com/files/selenium-server-standalone-2.31.0.jar - java -jar /tmp/selenium-server-standalone.jar > /dev/null & - sleep 5 diff --git a/classes/actions/ActionBlog.class.php b/classes/actions/ActionBlog.class.php index f4f65e2e..3eb16caa 100644 --- a/classes/actions/ActionBlog.class.php +++ b/classes/actions/ActionBlog.class.php @@ -946,98 +946,162 @@ class ActionBlog extends Action { $this->Viewer_SetResponseAjax('json'); $this->SubmitComment(); } + + /** + * Проверка на соответсвие коментария требованиям безопасности + * + * @param ModuleTopic_EntityTopic $oTopic + * @param string $sText + * + * @return bool result + */ + protected function CheckComment($oTopic, $sText) { + + $bOk = true; + /** + * Проверям авторизован ли пользователь + */ + if (!$this->User_IsAuthorization()) { + $this->Message_AddErrorSingle($this->Lang_Get('need_authorization'),$this->Lang_Get('error')); + $bOk = false; + } + /** + * Проверяем топик + */ + if (!$oTopic) { + $this->Message_AddErrorSingle($this->Lang_Get('system_error'),$this->Lang_Get('error')); + $bOk = false; + } + /** + * Возможность постить коммент в топик в черновиках + */ + if (!$oTopic->getPublish() and $this->oUserCurrent->getId()!=$oTopic->getUserId() and !$this->oUserCurrent->isAdministrator()) { + $this->Message_AddErrorSingle($this->Lang_Get('system_error'),$this->Lang_Get('error')); + $bOk = false; + } + /** + * Проверяем разрешено ли постить комменты + */ + if (!$this->ACL_CanPostComment($this->oUserCurrent) and !$this->oUserCurrent->isAdministrator()) { + $this->Message_AddErrorSingle($this->Lang_Get('topic_comment_acl'),$this->Lang_Get('error')); + $bOk = false; + } + /** + * Проверяем разрешено ли постить комменты по времени + */ + if (!$this->ACL_CanPostCommentTime($this->oUserCurrent) and !$this->oUserCurrent->isAdministrator()) { + $this->Message_AddErrorSingle($this->Lang_Get('topic_comment_limit'),$this->Lang_Get('error')); + $bOk = false; + } + /** + * Проверяем запрет на добавления коммента автором топика + */ + if ($oTopic->getForbidComment()) { + $this->Message_AddErrorSingle($this->Lang_Get('topic_comment_notallow'),$this->Lang_Get('error')); + $bOk = false; + } + /** + * Проверяем текст комментария + */ + if (!func_check($sText,'text',2,10000)) { + $this->Message_AddErrorSingle($this->Lang_Get('topic_comment_add_text_error'),$this->Lang_Get('error')); + $bOk = false; + } + + $this->Hook_Run('comment_check', array('oTopic'=>$oTopic, 'sText'=>$sText, 'bOk'=>&$bOk)); + + return $bOk; + } + + /** + * Проверка на соответсвие коментария родительскому коментарию + * + * @param ModuleTopic_EntityTopic $oTopic + * @param string $sText + * @param ModuleComment_EntityComment $oCommentParent + * + * @return bool result + */ + protected function CheckParentComment($oTopic, $sText, $oCommentParent) { + + $sParentId = (int)getRequest('reply'); + $bOk = true; + /** + * Проверям на какой коммент отвечаем + */ + if (!func_check($sParentId,'id')) { + $this->Message_AddErrorSingle($this->Lang_Get('system_error'),$this->Lang_Get('error')); + $bOk = false; + } + + if ($sParentId) { + /** + * Проверяем существует ли комментарий на который отвечаем + */ + if (!($oCommentParent)) { + $this->Message_AddErrorSingle($this->Lang_Get('system_error'),$this->Lang_Get('error')); + $bOk = false; + } + /** + * Проверяем из одного топика ли новый коммент и тот на который отвечаем + */ + if ($oCommentParent->getTargetId()!=$oTopic->getId()) { + $this->Message_AddErrorSingle($this->Lang_Get('system_error'),$this->Lang_Get('error')); + $bOk = false; + } + } + + /** + * Проверка на дублирующий коммент + */ + if ($this->Comment_GetCommentUnique($oTopic->getId(),'topic',$this->oUserCurrent->getId(),$sParentId,md5($sText))) { + $this->Message_AddErrorSingle($this->Lang_Get('topic_comment_spam'),$this->Lang_Get('error')); + $bOk = false; + } + + $this->Hook_Run('comment_check_parent', array('oTopic'=>$oTopic, 'sText'=>$sText, 'oCommentParent'=>$oCommentParent, 'bOk'=>&$bOk)); + + return $bOk; + } + /** * Обработка добавление комментария к топику * */ protected function SubmitComment() { - /** - * Проверям авторизован ли пользователь - */ - if (!$this->User_IsAuthorization()) { - $this->Message_AddErrorSingle($this->Lang_Get('need_authorization'),$this->Lang_Get('error')); - return; - } - /** - * Проверяем топик - */ - if (!($oTopic=$this->Topic_GetTopicById(getRequestStr('cmt_target_id')))) { - $this->Message_AddErrorSingle($this->Lang_Get('system_error'),$this->Lang_Get('error')); - return; - } - /** - * Возможность постить коммент в топик в черновиках - */ - if (!$oTopic->getPublish() and $this->oUserCurrent->getId()!=$oTopic->getUserId() and !$this->oUserCurrent->isAdministrator()) { - $this->Message_AddErrorSingle($this->Lang_Get('system_error'),$this->Lang_Get('error')); - return; - } - /** - * Проверяем разрешено ли постить комменты - */ - if (!$this->ACL_CanPostComment($this->oUserCurrent) and !$this->oUserCurrent->isAdministrator()) { - $this->Message_AddErrorSingle($this->Lang_Get('topic_comment_acl'),$this->Lang_Get('error')); - return; - } - /** - * Проверяем разрешено ли постить комменты по времени - */ - if (!$this->ACL_CanPostCommentTime($this->oUserCurrent) and !$this->oUserCurrent->isAdministrator()) { - $this->Message_AddErrorSingle($this->Lang_Get('topic_comment_limit'),$this->Lang_Get('error')); - return; - } - /** - * Проверяем запрет на добавления коммента автором топика - */ - if ($oTopic->getForbidComment()) { - $this->Message_AddErrorSingle($this->Lang_Get('topic_comment_notallow'),$this->Lang_Get('error')); - return; - } - /** - * Проверяем текст комментария - */ - $sText=$this->Text_Parser(getRequestStr('comment_text')); - if (!func_check($sText,'text',2,10000)) { - $this->Message_AddErrorSingle($this->Lang_Get('topic_comment_add_text_error'),$this->Lang_Get('error')); - return; - } - /** - * Проверям на какой коммент отвечаем - */ - $sParentId=(int)getRequest('reply'); - if (!func_check($sParentId,'id')) { - $this->Message_AddErrorSingle($this->Lang_Get('system_error'),$this->Lang_Get('error')); - return; - } - $oCommentParent=null; - if ($sParentId!=0) { - /** - * Проверяем существует ли комментарий на который отвечаем - */ - if (!($oCommentParent=$this->Comment_GetCommentById($sParentId))) { - $this->Message_AddErrorSingle($this->Lang_Get('system_error'),$this->Lang_Get('error')); - return; - } - /** - * Проверяем из одного топика ли новый коммент и тот на который отвечаем - */ - if ($oCommentParent->getTargetId()!=$oTopic->getId()) { - $this->Message_AddErrorSingle($this->Lang_Get('system_error'),$this->Lang_Get('error')); - return; - } - } else { - /** - * Корневой комментарий - */ - $sParentId=null; - } - /** - * Проверка на дублирующий коммент - */ - if ($this->Comment_GetCommentUnique($oTopic->getId(),'topic',$this->oUserCurrent->getId(),$sParentId,md5($sText))) { - $this->Message_AddErrorSingle($this->Lang_Get('topic_comment_spam'),$this->Lang_Get('error')); - return; - } + + $oTopic = $this->Topic_GetTopicById(getRequestStr('cmt_target_id')); + $sText = $this->Text_Parser(getRequestStr('comment_text')); + $sParentId = (int)getRequest('reply'); + $oCommentParent = null; + + if (!$sParentId) { + /** + * Корневой комментарий + */ + $sParentId=null; + } + else { + /** + * Родительский комментарий + */ + $oCommentParent=$this->Comment_GetCommentById($sParentId); + } + + /** + * Проверка на соответсвие коментария требованиям безопасности + */ + if (!$this->CheckComment($oTopic, $sText)) { + return; + } + + /** + * Проверка на соответсвие коментария родительскому коментарию + */ + if (!$this->CheckParentComment($oTopic, $sText, $oCommentParent)) { + return; + } + /** * Создаём коммент */ diff --git a/tests/behat/features/comment.feature b/tests/behat/features/comment.feature new file mode 100644 index 00000000..becdee06 --- /dev/null +++ b/tests/behat/features/comment.feature @@ -0,0 +1,46 @@ +Feature: Test Base comment functionality (!!!SELENIUM NEEDED) + Test base functionality of Comments + + @mink:selenium2 + Scenario: Adding the comment + + Given I am on "/login" + Then I want to login as "admin" + + Given I am on homepage + Then I follow "Sony MicroVault Mach USB 3.0 flash drive" + + Then I wait "1000" + + Then I follow "Add comment" + And I fill in "test comment" for "comment_text" + And I press "Preview" + Then I wait "1000" + + Then I should see in element by css "content .comment-preview" values: + | value | + | test comment | + + And I press "Add" + Then I wait "1000" + + Then I should see in element by css "content .comment-content" values: + | value | + | test comment | + + Then I should see in element by css "content .comment-info" values: + | value | + | /profile/admin/">admin | + | Reply | + | Delete | + + #create subcomment + And I follow "Reply" + Then I wait "1000" + And I fill in "test subcomment" for "comment_text" + And I press "Add" + Then I wait "1000" + + Then I should see in element by css "comment_wrapper_id_2" values: + | value | + | test subcomment | \ No newline at end of file