public function testAnswerCRUD_Works() { $e = new MongoTestEnvironment(); $e->clean(); $textRef = MongoTestEnvironment::mockId(); $project = $e->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE); // Create Question $question = new QuestionModel($project); $question->title = "Some Question"; $question->textRef->id = $textRef; $questionId = $question->write(); // List $question->read($questionId); $this->assertCount(0, $question->answers); // Create $answer = new AnswerModel(); $answer->content = 'Some answer'; $id = $question->writeAnswer($answer); $comment = new CommentModel(); $comment->content = 'Some comment'; $commentId = QuestionModel::writeComment($project->databaseName(), $questionId, $id, $comment); $this->assertNotNull($id); $this->assertInternalType('string', $id); $this->assertEquals(24, strlen($id)); $this->assertEquals($answer->id->asString(), $id); // Read back $otherQuestion = new QuestionModel($project, $questionId); $otherAnswer = $otherQuestion->answers[$id]; $this->assertEquals($id, $otherAnswer->id->asString()); $this->assertEquals('Some answer', $otherAnswer->content); $this->assertCount(1, $otherAnswer->comments); // Update $otherAnswer->content = 'Other answer'; // Note: Updates to the AnswerModel should not clobber child nodes such as comments. Hence this test. // See https://github.com/sillsdev/sfwebchecks/issues/39 unset($otherAnswer->comments[$commentId]); $otherQuestion->read($otherQuestion->id->asString()); $otherId = $otherQuestion->writeAnswer($otherAnswer); $this->assertEquals($id, $otherId); // Read back $otherQuestion = new QuestionModel($project, $questionId); $otherAnswer = $otherQuestion->answers[$id]; $this->assertEquals($id, $otherAnswer->id->asString()); $this->assertEquals('Other answer', $otherAnswer->content); $this->assertCount(1, $otherAnswer->comments); // List $this->assertCount(1, $otherQuestion->answers); // Delete QuestionModel::removeAnswer($project->databaseName(), $questionId, $id); // List $otherQuestion->read($questionId); $this->assertCount(0, $otherQuestion->answers); }
public function testAnswerCRUD_Works() { $environ = new MongoTestEnvironment(); $environ->clean(); $textRef = MongoTestEnvironment::mockId(); $project = $environ->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE); // Create Question $question = new QuestionModel($project); $question->title = "Some Question"; $question->textRef->id = $textRef; $questionId = $question->write(); // Create Answer $answer = new AnswerModel(); $answer->content = 'Some answer'; $answerId = $question->writeAnswer($answer); // List $question->read($questionId); $count = count($question->answers[$answerId]->comments); $this->assertEquals(0, $count); // Create $comment = new CommentModel(); $comment->content = 'Some comment'; $id = QuestionModel::writeComment($project->databaseName(), $questionId, $answerId, $comment); $this->assertNotNull($id); $this->assertInternalType('string', $id); $this->assertEquals(24, strlen($id)); $this->assertEquals($comment->id->asString(), $id); // Read back $otherQuestion = new QuestionModel($project, $questionId); $otherComment = $otherQuestion->answers[$answerId]->comments[$id]; $this->assertEquals($id, $otherComment->id->asString()); $this->assertEquals('Some comment', $otherComment->content); // Update $otherComment->content = 'Other comment'; $otherId = $question->writeComment($project->databaseName(), $questionId, $answerId, $otherComment); $this->assertEquals($id, $otherId); // Read back $otherQuestion = new QuestionModel($project, $questionId); $otherComment = $otherQuestion->answers[$answerId]->comments[$id]; $this->assertEquals($id, $otherComment->id->asString()); $this->assertEquals('Other comment', $otherComment->content); // List $count = count($otherQuestion->answers[$answerId]->comments); $this->assertEquals(1, $count); // Delete QuestionModel::removeComment($project->databaseName(), $questionId, $answerId, $id); // List $otherQuestion->read($questionId); $count = count($otherQuestion->answers[$answerId]->comments); $this->assertEquals(0, $count); }
/** * 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()); }