diff --git a/src/Api/Jobs/PreviewCommentJob.php b/src/Api/Jobs/PreviewCommentJob.php index 068209fa..32363e1b 100644 --- a/src/Api/Jobs/PreviewCommentJob.php +++ b/src/Api/Jobs/PreviewCommentJob.php @@ -5,13 +5,21 @@ class PreviewCommentJob extends AbstractJob { $user = Auth::getCurrentUser(); $text = $this->getArgument(self::TEXT); - $post = PostModel::findById($this->getArgument(self::POST_ID)); - $comment = CommentModel::spawn(); + if ($this->hasArgument(self::POST_ID)) + { + $post = PostModel::findById($this->getArgument(self::POST_ID)); + $comment = CommentModel::spawn(); + $comment->setPost($post); + } + else + { + $comment = CommentModel::findById($this->getArgument(self::COMMENT_ID)); + } + $comment->setCommenter($user); $comment->setDateTime(time()); $comment->setText($text); - $comment->setPost($post); $comment->validate(); diff --git a/src/Controllers/CommentController.php b/src/Controllers/CommentController.php index fc9314fa..dc380ff3 100644 --- a/src/Controllers/CommentController.php +++ b/src/Controllers/CommentController.php @@ -14,22 +14,19 @@ class CommentController $context->transport->paginator = $ret; } - public function previewAction() - { - $comment = Api::run( - new PreviewCommentJob(), - [ - PreviewCommentJob::POST_ID => InputHelper::get('post-id'), - PreviewCommentJob::TEXT => InputHelper::get('text') - ]); - - getContext()->transport->textPreview = $comment->getTextMarkdown(); - } - public function addAction() { if (InputHelper::get('sender') == 'preview') - return $this->previewAction(); + { + $comment = Api::run( + new PreviewCommentJob(), + [ + PreviewCommentJob::POST_ID => InputHelper::get('post-id'), + PreviewCommentJob::TEXT => InputHelper::get('text') + ]); + + getContext()->transport->textPreview = $comment->getTextMarkdown(); + } Api::run( new AddCommentJob(), @@ -47,7 +44,16 @@ class CommentController public function editAction($id) { if (InputHelper::get('sender') == 'preview') - return $this->previewAction(); + { + $comment = Api::run( + new PreviewCommentJob(), + [ + PreviewCommentJob::COMMENT_ID => $id, + PreviewCommentJob::TEXT => InputHelper::get('text') + ]); + + getContext()->transport->textPreview = $comment->getTextMarkdown(); + } Api::run( new EditCommentJob(), diff --git a/tests/JobTests/PreviewCommentJobTest.php b/tests/JobTests/PreviewCommentJobTest.php index e29d7850..1909902c 100644 --- a/tests/JobTests/PreviewCommentJobTest.php +++ b/tests/JobTests/PreviewCommentJobTest.php @@ -57,6 +57,38 @@ class PreviewCommentJobTest extends AbstractTest }, 'Comment must have at most'); } + public function testViaPost() + { + $this->prepare(); + $post = $this->mockPost(Auth::getCurrentUser()); + + $this->assert->doesNotThrow(function() use ($post) + { + Api::run( + new PreviewCommentJob(), + [ + PreviewCommentJob::POST_ID => $post->getId(), + PreviewCommentJob::TEXT => 'alohaaa', + ]); + }); + } + + public function testViaComment() + { + $this->prepare(); + $comment = $this->mockComment(Auth::getCurrentUser()); + + $this->assert->doesNotThrow(function() use ($comment) + { + Api::run( + new PreviewCommentJob(), + [ + PreviewCommentJob::COMMENT_ID => $comment->getId(), + PreviewCommentJob::TEXT => 'alohaaa', + ]); + }); + } + protected function runApi($text) {