public function testMultipleUnreadModels_multipleUsers_multipleUpdates_multipleVisitsToQuestionPage_usersHaveDifferentUnreadStates()
 {
     $project = self::$environ->createProject("unread_test", "unreadCode");
     $projectId = $project->id->asString();
     $userId1 = self::$environ->createUser('user1', 'user1', 'user1');
     $user1 = new UserModel($userId1);
     $user1->addProject($project->id->asString());
     $user1->write();
     $userId2 = self::$environ->createUser('user2', 'user2', 'user2');
     $user2 = new UserModel($userId2);
     $user2->addProject($project->id->asString());
     $user2->write();
     $userId3 = self::$environ->createUser('user3', 'user3', 'user3');
     $user3 = new UserModel($userId3);
     $user3->addProject($project->id->asString());
     $user3->write();
     $answer1 = array('content' => "test answer 1", 'id' => '');
     $answer2 = array('content' => "test answer 2", 'id' => '');
     $text = new TextModel($project);
     $text->title = "Text 1";
     $usx = MongoTestEnvironment::usxSample();
     $text->content = $usx;
     $textId = $text->write();
     $question = new QuestionModel($project);
     $question->title = "test question";
     $question->textRef->id = $textId;
     $questionId = $question->write();
     QuestionCommands::updateAnswer($projectId, $questionId, $answer1, $userId1);
     QuestionCommands::updateAnswer($projectId, $questionId, $answer2, $userId2);
     // the answer author does NOT get their answer marked as unread
     $unreadModel = new UnreadAnswerModel($userId1, $projectId, $questionId);
     $this->assertCount(1, $unreadModel->unreadItems());
     // the answer author does NOT get their answer marked as unread
     $unreadModel = new UnreadAnswerModel($userId2, $projectId, $questionId);
     $this->assertCount(1, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId3, $projectId, $questionId);
     $this->assertCount(2, $unreadModel->unreadItems());
     // user1 visits question page
     QuestionCommentDto::encode($projectId, $questionId, $userId1);
     $unreadModel = new UnreadAnswerModel($userId1, $projectId, $questionId);
     $this->assertCount(0, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId2, $projectId, $questionId);
     $this->assertCount(1, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId3, $projectId, $questionId);
     $this->assertCount(2, $unreadModel->unreadItems());
     // user2 visits question page
     QuestionCommentDto::encode($projectId, $questionId, $userId2);
     $unreadModel = new UnreadAnswerModel($userId1, $projectId, $questionId);
     $this->assertCount(0, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId2, $projectId, $questionId);
     $this->assertCount(0, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId3, $projectId, $questionId);
     $this->assertCount(2, $unreadModel->unreadItems());
     // user2 visits question page
     QuestionCommentDto::encode($projectId, $questionId, $userId3);
     $unreadModel = new UnreadAnswerModel($userId1, $projectId, $questionId);
     $this->assertCount(0, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId2, $projectId, $questionId);
     $this->assertCount(0, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId3, $projectId, $questionId);
     $this->assertCount(0, $unreadModel->unreadItems());
 }
 /**
  * Returns the AnswerModel as an AnswerDTO, a part of the QuestionDTO.
  * @param AnswerModel $answerModel
  * @return array
  */
 private static function encodeAnswer($answerModel)
 {
     $answerDTO = QuestionCommentDto::encodeAnswer($answerModel);
     $answerId = $answerModel->id->asString();
     $dto = array();
     $dto[$answerId] = $answerDTO;
     return $dto;
 }
 public function question_comment_dto($questionId)
 {
     return QuestionCommentDto::encode($this->projectId, $questionId, $this->userId);
 }
 /**
  * @expectedException Api\Library\Shared\Palaso\Exception\ResourceNotAvailableException
  */
 public function testEncode_ArchivedText_ManagerCanViewContributorCannot()
 {
     $project = self::$environ->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE);
     $managerId = self::$environ->createUser("manager", "manager", "*****@*****.**");
     $contributorId = self::$environ->createUser("contributor1", "contributor1", "*****@*****.**");
     $project->addUser($managerId, ProjectRoles::MANAGER);
     $project->addUser($contributorId, ProjectRoles::CONTRIBUTOR);
     $project->write();
     // Question not archived but Text is archived
     $text = new TextModel($project);
     $text->title = "Text 1";
     $text->isArchived = true;
     $textId = $text->write();
     $question = new QuestionModel($project);
     $question->title = "the question";
     $question->description = "question description";
     $question->textRef->id = $textId;
     $questionId = $question->write();
     $dto = QuestionCommentDto::encode($project->id->asString(), $questionId, $managerId);
     // Manager can view Question of archived Text
     $this->assertEquals('the question', $dto['question']['title']);
     // Contributor cannot view Question of archived Text, throw Exception
     self::$environ->inhibitErrorDisplay();
     QuestionCommentDto::encode($project->id->asString(), $questionId, $contributorId);
     // nothing runs in the current test function after an exception. IJH 2014-11
 }