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);
     $count = count($question->answers);
     $this->assertEqual(0, $count);
     // 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->assertIsA($id, 'string');
     $this->assertEqual(24, strlen($id));
     $this->assertEqual($id, $answer->id->asString());
     // Read back
     $otherQuestion = new QuestionModel($project, $questionId);
     $otherAnswer = $otherQuestion->answers[$id];
     $this->assertEqual($id, $otherAnswer->id->asString());
     $this->assertEqual('Some answer', $otherAnswer->content);
     $this->assertEqual(1, count($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->assertEqual($id, $otherId);
     // Read back
     $otherQuestion = new QuestionModel($project, $questionId);
     $otherAnswer = $otherQuestion->answers[$id];
     $this->assertEqual($id, $otherAnswer->id->asString());
     $this->assertEqual('Other answer', $otherAnswer->content);
     $this->assertEqual(1, count($otherAnswer->comments));
     // List
     $this->assertEqual(1, count($otherQuestion->answers));
     // Delete
     QuestionModel::removeAnswer($project->databaseName(), $questionId, $id);
     // List
     $otherQuestion->read($questionId);
     $this->assertEqual(0, count($otherQuestion->answers));
 }
 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();
     // Create Answer
     $answer = new AnswerModel();
     $answer->content = 'Some answer';
     $answerId = $question->writeAnswer($answer);
     // List
     $question->read($questionId);
     $count = count($question->answers[$answerId]->comments);
     $this->assertEqual(0, $count);
     // Create
     $comment = new CommentModel();
     $comment->content = 'Some comment';
     $id = QuestionModel::writeComment($project->databaseName(), $questionId, $answerId, $comment);
     $this->assertNotNull($id);
     $this->assertIsA($id, 'string');
     $this->assertEqual(24, strlen($id));
     $this->assertEqual($id, $comment->id->asString());
     // Read back
     $otherQuestion = new QuestionModel($project, $questionId);
     $otherComment = $otherQuestion->answers[$answerId]->comments[$id];
     $this->assertEqual($id, $otherComment->id->asString());
     $this->assertEqual('Some comment', $otherComment->content);
     // Update
     $otherComment->content = 'Other comment';
     $otherId = $question->writeComment($project->databaseName(), $questionId, $answerId, $otherComment);
     $this->assertEqual($id, $otherId);
     // Read back
     $otherQuestion = new QuestionModel($project, $questionId);
     $otherComment = $otherQuestion->answers[$answerId]->comments[$id];
     $this->assertEqual($id, $otherComment->id->asString());
     $this->assertEqual('Other comment', $otherComment->content);
     // List
     $count = count($otherQuestion->answers[$answerId]->comments);
     $this->assertEqual(1, $count);
     // Delete
     QuestionModel::removeComment($project->databaseName(), $questionId, $answerId, $id);
     // List
     $otherQuestion->read($questionId);
     $count = count($otherQuestion->answers[$answerId]->comments);
     $this->assertEqual(0, $count);
 }
 public static function updateQuestion($projectId, $object)
 {
     $projectModel = new \Api\Model\ProjectModel($projectId);
     $questionModel = new \Api\Model\QuestionModel($projectModel);
     $isNewQuestion = $object['id'] == '';
     if (!$isNewQuestion) {
         $questionModel->read($object['id']);
     }
     JsonDecoder::decode($questionModel, $object);
     $questionId = $questionModel->write();
     if ($isNewQuestion) {
         ActivityCommands::addQuestion($projectModel, $questionId, $questionModel);
     }
     return $questionId;
 }
 public function testUpdateComment_ExistingComment_OriginalAuthorIsPreserved()
 {
     $e = new MongoTestEnvironment();
     $e->clean();
     $project = $e->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 = $e->createUser("user1", "user1", "user1");
     $user2Id = $e->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->assertEqual($user1Id, $newComment->userRef->asString());
 }
 /**
  * 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);
     $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;
 }