Пример #1
0
 /**
  * Creates a valid clarification
  */
 public function testCreateValidClarification()
 {
     // Get a problem
     $problemData = ProblemsFactory::createProblem();
     // Get a contest
     $contestData = ContestsFactory::createContest();
     // Add the problem to the contest
     ContestsFactory::addProblemToContest($problemData, $contestData);
     // Create our contestant who will submit the clarification
     $contestant = UserFactory::createUser();
     // Call the API
     $this->detourBroadcasterCalls();
     $clarificationData = ClarificationsFactory::createClarification($problemData, $contestData, $contestant);
     // Assert status of new contest
     $this->assertArrayHasKey("clarification_id", $clarificationData['response']);
     // Verify that clarification was inserted in the database
     $clarification = ClarificationsDAO::getByPK($clarificationData['response']['clarification_id']);
     // Verify our retreived clarificatoin
     $this->assertNotNull($clarification);
     $this->assertEquals($clarificationData['request']['message'], $clarification->getMessage());
     // We need to verify that the contest and problem IDs where properly saved
     // Extractiing the contest and problem from DB to check IDs
     $problem = ProblemsDAO::getByAlias($problemData["request"]["alias"]);
     $contest = ContestsDAO::getByAlias($contestData["request"]["alias"]);
     $this->assertEquals($contest->getContestId(), $clarification->getContestId());
     $this->assertEquals($problem->getProblemId(), $clarification->getProblemId());
 }
 /**
  * Basic test for answer
  *
  */
 public function testUpdateAnswer()
 {
     // Get a problem
     $problemData = ProblemsFactory::createProblem();
     // Get a contest
     $contestData = ContestsFactory::createContest();
     // Add the problem to the contest
     ContestsFactory::addProblemToContest($problemData, $contestData);
     // Create our contestant who will submit the clarification
     $contestant = UserFactory::createUser();
     // Create clarification
     $this->detourBroadcasterCalls($this->exactly(2));
     $clarificationData = ClarificationsFactory::createClarification($problemData, $contestData, $contestant);
     // Update answer
     $newAnswer = 'new answer';
     $response = ClarificationsFactory::answer($clarificationData, $contestData, $newAnswer);
     // Get clarification from DB
     $clarification = ClarificationsDAO::getByPK($clarificationData['response']['clarification_id']);
     // Validate that clarification stays the same
     $this->assertEquals($clarificationData['request']['message'], $clarification->getMessage());
     $this->assertEquals($clarificationData['request']['public'], $clarification->getPublic());
     // Validate our update
     $this->assertEquals($newAnswer, $clarification->getAnswer());
 }
 public function testPublicClarificationsCanBeViewed()
 {
     // Get a problem
     $problemData = ProblemsFactory::createProblem();
     // Get a contest
     $contestData = ContestsFactory::createContest();
     // Add the problem to the contest
     ContestsFactory::addProblemToContest($problemData, $contestData);
     // Create our contestant who will submit the clarification
     $contestant = UserFactory::createUser();
     // Create our contestant who will try to view the clarification
     $contestant2 = UserFactory::createUser();
     // Create the clarification, note that contestant will create it
     $this->detourBroadcasterCalls();
     $clarificationData = ClarificationsFactory::createClarification($problemData, $contestData, $contestant);
     // Manually set the just created clarification to PUBLIC
     $clarification = ClarificationsDAO::getByPK($clarificationData['response']['clarification_id']);
     $clarification->setPublic('1');
     ClarificationsDAO::save($clarification);
     // Prepare the request object
     $r = new Request();
     $r['clarification_id'] = $clarificationData['response']['clarification_id'];
     // Log in with the author of the clarification
     $r['auth_token'] = $this->login($contestant2);
     // Call API
     $response = ClarificationController::apiDetails($r);
     // Check the data we got
     $this->assertClarification($r['clarification_id'], $response);
 }
Пример #4
0
 /**
  * Validate Details API request
  * 
  * @param Request $r
  * @throws InvalidDatabaseOperationException
  * @throws NotFoundException
  * @throws ForbiddenAccessException
  */
 private static function validateDetails(Request $r)
 {
     Validators::isNumber($r["clarification_id"], "clarification_id");
     // Check that the clarification actually exists
     try {
         $r["clarification"] = ClarificationsDAO::getByPK($r["clarification_id"]);
     } catch (Exception $e) {
         throw new InvalidDatabaseOperationException($e);
     }
     if (is_null($r["clarification"])) {
         throw new NotFoundException("clarificationNotFound");
     }
     // If the clarification is private, verify that our user is invited or is contest director
     if ($r["clarification"]->public != 1) {
         if (!Authorization::CanViewClarification($r["current_user_id"], $r["clarification"])) {
             throw new ForbiddenAccessException();
         }
     }
 }