/**
  * @param string $projectId
  * @param array $object (json encoded)
  * @return string Id of text updated/added
  */
 public static function updateText($projectId, $object)
 {
     $projectModel = new ProjectModel($projectId);
     ProjectCommands::checkIfArchivedAndThrow($projectModel);
     $textModel = new TextModel($projectModel);
     $isNewText = $object['id'] == '';
     if (!$isNewText) {
         $textModel->read($object['id']);
     }
     JsonDecoder::decode($textModel, $object);
     TextCommands::makeValidRange($object);
     if (TextCommands::hasRange($object)) {
         $usxTrimHelper = new UsxTrimHelper($textModel->content, $object['startCh'] || 0, $object['startVs'] || 0, $object['endCh'] || 0, $object['endVs'] || 0);
         $textModel->content = $usxTrimHelper->trimUsx();
     }
     $textId = $textModel->write();
     if ($isNewText) {
         ActivityCommands::addText($projectModel, $textId, $textModel);
     }
     return $textId;
 }
 public function testGetActivityForProject_ProjectWithTextQuestionAnswerAndComments_DtoAsExpected()
 {
     $environ = new MongoTestEnvironment();
     $environ->clean();
     $project = $environ->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE);
     $text = new TextModel($project);
     $text->title = "Text 1";
     $text->content = "text content";
     $textId = $text->write();
     $a1 = ActivityCommands::addText($project, $textId, $text);
     $user1Id = $environ->createUser("user1", "user1", "*****@*****.**");
     $user2Id = $environ->createUser("user2", "user2", "*****@*****.**");
     $user3Id = $environ->createUser("user3", "user3", "*****@*****.**");
     $a2 = ActivityCommands::addUserToProject($project, $user1Id);
     $a3 = ActivityCommands::addUserToProject($project, $user2Id);
     $a4 = ActivityCommands::addUserToProject($project, $user3Id);
     // Workflow is first to create a question
     $question = new QuestionModel($project);
     $question->title = "the question";
     $question->description = "question description";
     $question->textRef->id = $textId;
     $questionId = $question->write();
     $a5 = ActivityCommands::addQuestion($project, $questionId, $question);
     // Then to add an answer to a question
     $answer = new AnswerModel();
     $answer->content = "first answer";
     $answer->score = 10;
     $answer->userRef->id = $user3Id;
     $answer->textHightlight = "text highlight";
     $answerId = $question->writeAnswer($answer);
     $a6 = ActivityCommands::addAnswer($project, $questionId, $answer);
     // Followed by comments
     $comment1 = new CommentModel();
     $comment1->content = "first comment";
     $comment1->userRef->id = $user1Id;
     $comment1Id = QuestionModel::writeComment($project->databaseName(), $questionId, $answerId, $comment1);
     $a7 = ActivityCommands::addComment($project, $questionId, $answerId, $comment1);
     $comment2 = new CommentModel();
     $comment2->content = "second comment";
     $comment2->userRef->id = $user2Id;
     QuestionModel::writeComment($project->databaseName(), $questionId, $answerId, $comment2);
     $a8 = ActivityCommands::addComment($project, $questionId, $answerId, $comment2);
     // updated answer
     $question->read($questionId);
     $answer_updated = $question->readAnswer($answerId);
     $answer_updated->content = "first answer revised";
     $question->writeAnswer($answer_updated);
     $a9 = ActivityCommands::updateAnswer($project, $questionId, $answer_updated);
     // updated comment1
     $question->read($questionId);
     $comment1_updated = $question->readComment($answerId, $comment1Id);
     $comment1_updated->content = "first comment revised";
     QuestionModel::writeComment($project->databaseName(), $questionId, $answerId, $comment1_updated);
     $a10 = ActivityCommands::updateComment($project, $questionId, $answerId, $comment1_updated);
     $dto = ActivityListDto::getActivityForProject($project);
     $expectedProjectRef = ['id' => $project->id->asString(), 'type' => 'sfchecks'];
     $this->assertEquals('add_text', $dto[$a1]['action']);
     $this->assertEquals($expectedProjectRef, $dto[$a1]['projectRef']);
     $this->assertEquals($project->projectName, $dto[$a1]['content']['project']);
     $this->assertEquals($textId, $dto[$a1]['textRef']);
     $this->assertEquals($text->title, $dto[$a1]['content']['text']);
     $this->assertEquals('add_user_to_project', $dto[$a2]['action']);
     $this->assertEquals($expectedProjectRef, $dto[$a2]['projectRef']);
     $this->assertEquals($project->projectName, $dto[$a2]['content']['project']);
     $this->assertEquals($user1Id, $dto[$a2]['userRef']['id']);
     $this->assertEquals('user1', $dto[$a2]['userRef']['username']);
     $this->assertEquals('user1.png', $dto[$a2]['userRef']['avatar_ref']);
     $this->assertEquals('user1', $dto[$a2]['content']['user']);
     $this->assertEquals('add_user_to_project', $dto[$a3]['action']);
     $this->assertEquals($expectedProjectRef, $dto[$a3]['projectRef']);
     $this->assertEquals($project->projectName, $dto[$a3]['content']['project']);
     $this->assertEquals($user2Id, $dto[$a3]['userRef']['id']);
     $this->assertEquals('user2', $dto[$a3]['userRef']['username']);
     $this->assertEquals('user2.png', $dto[$a3]['userRef']['avatar_ref']);
     $this->assertEquals('user2', $dto[$a3]['content']['user']);
     $this->assertEquals('add_user_to_project', $dto[$a4]['action']);
     $this->assertEquals($expectedProjectRef, $dto[$a4]['projectRef']);
     $this->assertEquals($project->projectName, $dto[$a4]['content']['project']);
     $this->assertEquals($user3Id, $dto[$a4]['userRef']['id']);
     $this->assertEquals('user3', $dto[$a4]['userRef']['username']);
     $this->assertEquals('user3.png', $dto[$a4]['userRef']['avatar_ref']);
     $this->assertEquals('user3', $dto[$a4]['content']['user']);
     $this->assertEquals('add_question', $dto[$a5]['action']);
     $this->assertEquals($expectedProjectRef, $dto[$a5]['projectRef']);
     $this->assertEquals($project->projectName, $dto[$a5]['content']['project']);
     $this->assertEquals($textId, $dto[$a5]['textRef']);
     $this->assertEquals($text->title, $dto[$a5]['content']['text']);
     $this->assertEquals($questionId, $dto[$a5]['questionRef']);
     $this->assertEquals($question->title, $dto[$a5]['content']['question']);
     $this->assertEquals('add_answer', $dto[$a6]['action']);
     $this->assertEquals($expectedProjectRef, $dto[$a6]['projectRef']);
     $this->assertEquals($project->projectName, $dto[$a6]['content']['project']);
     $this->assertEquals($textId, $dto[$a6]['textRef']);
     $this->assertEquals($text->title, $dto[$a6]['content']['text']);
     $this->assertEquals($questionId, $dto[$a6]['questionRef']);
     $this->assertEquals($question->title, $dto[$a6]['content']['question']);
     $this->assertEquals($user3Id, $dto[$a6]['userRef']['id']);
     $this->assertEquals('user3', $dto[$a6]['userRef']['username']);
     $this->assertEquals('user3.png', $dto[$a6]['userRef']['avatar_ref']);
     $this->assertEquals($answer->content, $dto[$a6]['content']['answer']);
     $this->assertEquals('user3', $dto[$a6]['content']['user']);
     $this->assertEquals('add_comment', $dto[$a7]['action']);
     $this->assertEquals($expectedProjectRef, $dto[$a7]['projectRef']);
     $this->assertEquals($project->projectName, $dto[$a7]['content']['project']);
     $this->assertEquals($textId, $dto[$a7]['textRef']);
     $this->assertEquals($text->title, $dto[$a7]['content']['text']);
     $this->assertEquals($questionId, $dto[$a7]['questionRef']);
     $this->assertEquals($question->title, $dto[$a7]['content']['question']);
     $this->assertEquals($user1Id, $dto[$a7]['userRef']['id']);
     $this->assertEquals('user1', $dto[$a7]['userRef']['username']);
     $this->assertEquals('user1.png', $dto[$a7]['userRef']['avatar_ref']);
     $this->assertEquals('user1', $dto[$a7]['content']['user']);
     $this->assertEquals($user3Id, $dto[$a7]['userRef2']['id']);
     $this->assertEquals('user3', $dto[$a7]['userRef2']['username']);
     $this->assertEquals('user3.png', $dto[$a7]['userRef2']['avatar_ref']);
     $this->assertEquals('user3', $dto[$a7]['content']['user2']);
     $this->assertEquals($answer->content, $dto[$a7]['content']['answer']);
     $this->assertEquals($comment1->content, $dto[$a7]['content']['comment']);
     $this->assertEquals('add_comment', $dto[$a8]['action']);
     $this->assertEquals($expectedProjectRef, $dto[$a8]['projectRef']);
     $this->assertEquals($project->projectName, $dto[$a8]['content']['project']);
     $this->assertEquals($textId, $dto[$a8]['textRef']);
     $this->assertEquals($text->title, $dto[$a8]['content']['text']);
     $this->assertEquals($questionId, $dto[$a8]['questionRef']);
     $this->assertEquals($question->title, $dto[$a8]['content']['question']);
     $this->assertEquals($user2Id, $dto[$a8]['userRef']['id']);
     $this->assertEquals('user2', $dto[$a8]['userRef']['username']);
     $this->assertEquals('user2.png', $dto[$a8]['userRef']['avatar_ref']);
     $this->assertEquals('user2', $dto[$a8]['content']['user']);
     $this->assertEquals($user3Id, $dto[$a8]['userRef2']['id']);
     $this->assertEquals('user3', $dto[$a8]['userRef2']['username']);
     $this->assertEquals('user3.png', $dto[$a8]['userRef2']['avatar_ref']);
     $this->assertEquals('user3', $dto[$a8]['content']['user2']);
     $this->assertEquals($answer->content, $dto[$a8]['content']['answer']);
     $this->assertEquals($comment2->content, $dto[$a8]['content']['comment']);
     $this->assertEquals('update_answer', $dto[$a9]['action']);
     $this->assertEquals($expectedProjectRef, $dto[$a9]['projectRef']);
     $this->assertEquals($project->projectName, $dto[$a9]['content']['project']);
     $this->assertEquals($textId, $dto[$a9]['textRef']);
     $this->assertEquals($text->title, $dto[$a9]['content']['text']);
     $this->assertEquals($questionId, $dto[$a9]['questionRef']);
     $this->assertEquals($question->title, $dto[$a9]['content']['question']);
     $this->assertEquals($user3Id, $dto[$a9]['userRef']['id']);
     $this->assertEquals('user3', $dto[$a9]['userRef']['username']);
     $this->assertEquals('user3.png', $dto[$a9]['userRef']['avatar_ref']);
     $this->assertEquals('user3', $dto[$a9]['content']['user']);
     $this->assertEquals($answer_updated->content, $dto[$a9]['content']['answer']);
     $this->assertEquals('update_comment', $dto[$a10]['action']);
     $this->assertEquals($expectedProjectRef, $dto[$a10]['projectRef']);
     $this->assertEquals($project->projectName, $dto[$a10]['content']['project']);
     $this->assertEquals($textId, $dto[$a10]['textRef']);
     $this->assertEquals($text->title, $dto[$a10]['content']['text']);
     $this->assertEquals($questionId, $dto[$a10]['questionRef']);
     $this->assertEquals($question->title, $dto[$a10]['content']['question']);
     $this->assertEquals($user1Id, $dto[$a10]['userRef']['id']);
     $this->assertEquals('user1', $dto[$a10]['userRef']['username']);
     $this->assertEquals('user1.png', $dto[$a10]['userRef']['avatar_ref']);
     $this->assertEquals('user1', $dto[$a10]['content']['user']);
     $this->assertEquals($user3Id, $dto[$a10]['userRef2']['id']);
     $this->assertEquals('user3', $dto[$a10]['userRef2']['username']);
     $this->assertEquals('user3.png', $dto[$a10]['userRef2']['avatar_ref']);
     $this->assertEquals('user3', $dto[$a10]['content']['user2']);
     $this->assertEquals($answer_updated->content, $dto[$a10]['content']['answer']);
     $this->assertEquals($comment1_updated->content, $dto[$a10]['content']['comment']);
 }