/** * Creates / Updates a comment on the given answer. * @param string $projectId * @param string $questionId * @param string $answerId * @param array $comment * @param string $userId * @return array Dto */ public static function updateComment($projectId, $questionId, $answerId, $comment, $userId) { $projectModel = new ProjectModel($projectId); ProjectCommands::checkIfArchivedAndThrow($projectModel); $questionModel = new QuestionModel($projectModel, $questionId); $authorId = $userId; if ($comment['id'] != '') { // update existing comment $oldComment = $questionModel->readComment($answerId, $comment['id']); $authorId = $oldComment->userRef->asString(); } $commentModel = new CommentModel(); JsonDecoder::decode($commentModel, $comment); $commentModel->userRef->id = $authorId; $commentId = QuestionModel::writeComment($projectModel->databaseName(), $questionId, $answerId, $commentModel); $questionModel->read($questionId); $newComment = $questionModel->readComment($answerId, $commentId); $commentDTO = QuestionCommentDto::encodeComment($newComment); if ($comment['id'] != '') { // TODO log the activity after we confirm that the comment was successfully updated ; cjh 2013-08 ActivityCommands::updateComment($projectModel, $questionId, $answerId, $newComment); } else { ActivityCommands::addComment($projectModel, $questionId, $answerId, $newComment); } $dto = array(); $dto[$commentId] = $commentDTO; return $dto; }
public function testUpdateComment_ExistingComment_OriginalAuthorIsPreserved() { $environ = new MongoTestEnvironment(); $environ->clean(); $project = $environ->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE); $text = new TextModel($project); $text->title = 'Text 1'; $usx = MongoTestEnvironment::usxSample(); $text->content = $usx; $textId = $text->write(); $question = new QuestionModel($project); $question->textRef->id = $textId; $questionId = $question->write(); $user1Id = $environ->createUser('user1', 'user1', 'user1'); $user2Id = $environ->createUser('user2', 'user2', 'user2'); $answer = new AnswerModel(); $answer->content = 'the answer'; $answer->userRef->id = $user2Id; $answerId = $question->writeAnswer($answer); $comment = new CommentModel(); $comment->userRef->id = $user1Id; $comment->content = 'the comment'; $commentId = $question->writeComment($project->databaseName(), $questionId, $answerId, $comment); $commentArray = array('id' => $commentId, 'content' => 'updated comment'); QuestionCommands::updateComment($project->id->asString(), $questionId, $answerId, $commentArray, $user2Id); $question->read($questionId); $newComment = $question->readComment($answerId, $commentId); $this->assertEquals($user1Id, $newComment->userRef->asString()); }