/**
  * 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());
 }
 /**
  * Check in DB for problem added to contest
  * 
  * @param array $problemData
  * @param array $contestData
  * @param Request $r
  */
 public static function assertProblemAddedToContest($problemData, $contestData, $r)
 {
     // Get problem and contest from DB
     $problem = ProblemsDAO::getByAlias($problemData["request"]["alias"]);
     $contest = ContestsDAO::getByAlias($contestData["request"]["alias"]);
     // Get problem-contest and verify it
     $contest_problems = ContestProblemsDAO::getByPK($contest->getContestId(), $problem->getProblemId());
     self::assertNotNull($contest_problems);
     self::assertEquals($r["points"], $contest_problems->getPoints());
     self::assertEquals($r["order_in_contest"], $contest_problems->getOrder());
 }
Example #3
0
 /**
  *
  */
 public function testViewProblemInAContestDetailsValid()
 {
     // Get a contest
     $contestData = ContestsFactory::createContest();
     // Get a user to be the author
     $author = UserFactory::createUser();
     // Get a problem
     $problemData = ProblemsFactory::createProblem(null, null, 1, $author);
     // Add the problem to the contest
     ContestsFactory::addProblemToContest($problemData, $contestData);
     // Get a user for our scenario
     $contestant = UserFactory::createUser();
     // Prepare our request
     $r = new Request();
     $r["contest_alias"] = $contestData["request"]["alias"];
     $r["problem_alias"] = $problemData["request"]["alias"];
     // Log in the user
     $r["auth_token"] = $this->login($contestant);
     // Explicitly join contest
     ContestController::apiOpen($r);
     // Call api
     $response = ProblemController::apiDetails($r);
     // Get problem and contest from DB to check it
     $problemDAO = ProblemsDAO::getByAlias($problemData["request"]["alias"]);
     $contestDAO = ContestsDAO::getByAlias($contestData["request"]["alias"]);
     $contestantsDAO = UsersDAO::search(new Users(array("username" => $contestant->getUsername())));
     $contestantDAO = $contestantsDAO[0];
     // Assert data
     $this->assertEquals($response["title"], $problemDAO->getTitle());
     $this->assertEquals($response["alias"], $problemDAO->getAlias());
     $this->assertEquals($response["validator"], $problemDAO->getValidator());
     $this->assertEquals($response["time_limit"], $problemDAO->getTimeLimit());
     $this->assertEquals($response["memory_limit"], $problemDAO->getMemoryLimit());
     $this->assertEquals($response["problemsetter"]['username'], $author->username);
     $this->assertEquals($response["problemsetter"]['name'], $author->name);
     $this->assertEquals($response["source"], $problemDAO->getSource());
     $this->assertContains("<h1>Entrada</h1>", $response["problem_statement"]);
     $this->assertEquals($response["order"], $problemDAO->getOrder());
     $this->assertEquals($response["score"], 0);
     // Default data
     $this->assertEquals(0, $problemDAO->getVisits());
     $this->assertEquals(0, $problemDAO->getSubmissions());
     $this->assertEquals(0, $problemDAO->getAccepted());
     $this->assertEquals(0, $problemDAO->getDifficulty());
     // Verify that we have an empty array of runs
     $this->assertEquals(0, count($response["runs"]));
     // Verify that problem was marked as Opened
     $problem_opened = ContestProblemOpenedDAO::getByPK($contestDAO->getContestId(), $problemDAO->getProblemId(), $contestantDAO->getUserId());
     $this->assertNotNull($problem_opened);
     // Verify open time
     $this->assertEquals(Utils::GetPhpUnixTimestamp(), Utils::GetPhpUnixTimestamp($problem_opened->getOpenTime()));
 }
 /**
  * Checks the contest details response
  *
  * @param type $contestData
  * @param type $problems
  * @param type $response
  */
 private function assertContestDetails($contestData, $problems, $response)
 {
     // To validate, grab the contest object directly from the DB
     $contest = ContestsDAO::getByAlias($contestData['request']['alias']);
     // Assert we are getting correct data
     $this->assertEquals($contest->getDescription(), $response['description']);
     $this->assertEquals(Utils::GetPhpUnixTimestamp($contest->getStartTime()), $response['start_time']);
     $this->assertEquals(Utils::GetPhpUnixTimestamp($contest->getFinishTime()), $response['finish_time']);
     $this->assertEquals($contest->getWindowLength(), $response['window_length']);
     $this->assertEquals($contest->getAlias(), $response['alias']);
     $this->assertEquals($contest->getPointsDecayFactor(), $response['points_decay_factor']);
     $this->assertEquals($contest->getPartialScore(), $response['partial_score']);
     $this->assertEquals($contest->getSubmissionsGap(), $response['submissions_gap']);
     $this->assertEquals($contest->getFeedback(), $response['feedback']);
     $this->assertEquals($contest->getPenalty(), $response['penalty']);
     $this->assertEquals($contest->getScoreboard(), $response['scoreboard']);
     $this->assertEquals($contest->penalty_type, $response['penalty_type']);
     $this->assertEquals($contest->getPenaltyCalcPolicy(), $response['penalty_calc_policy']);
     // Assert we have our problems
     $numOfProblems = count($problems);
     $this->assertEquals($numOfProblems, count($response['problems']));
     // Assert problem data
     $i = 0;
     foreach ($response['problems'] as $problem_array) {
         // Get problem from DB
         $problem = ProblemsDAO::getByAlias($problems[$i]['request']['alias']);
         // Assert data in DB
         $this->assertEquals($problem->getTitle(), $problem_array['title']);
         $this->assertEquals($problem->getAlias(), $problem_array['alias']);
         $this->assertEquals($problem->getValidator(), $problem_array['validator']);
         $this->assertEquals($problem->getTimeLimit(), $problem_array['time_limit']);
         $this->assertEquals($problem->getMemoryLimit(), $problem_array['memory_limit']);
         $this->assertEquals($problem->getVisits(), $problem_array['visits']);
         $this->assertEquals($problem->getSubmissions(), $problem_array['submissions']);
         $this->assertEquals($problem->getAccepted(), $problem_array['accepted']);
         $this->assertEquals($problem->getOrder(), $problem_array['order']);
         // Get points of problem from Contest-Problem relationship
         $problemInContest = ContestProblemsDAO::getByPK($contest->getContestId(), $problem->getProblemId());
         $this->assertEquals($problemInContest->getPoints(), $problem_array['points']);
         $i++;
     }
 }
 /**
  * Validate the request of apiCreate
  * 
  * @param Request $r
  * @throws InvalidDatabaseOperationException
  * @throws NotFoundException
  */
 private static function validateCreate(Request $r)
 {
     Validators::isStringNonEmpty($r["contest_alias"], "contest_alias");
     Validators::isStringNonEmpty($r["problem_alias"], "problem_alias");
     Validators::isStringNonEmpty($r["message"], "message");
     try {
         $r["contest"] = ContestsDAO::getByAlias($r["contest_alias"]);
         $r["problem"] = ProblemsDAO::getByAlias($r["problem_alias"]);
     } catch (Exception $e) {
         throw new InvalidDatabaseOperationException($e);
     }
     if (is_null($r["contest"])) {
         throw new NotFoundException("contestNotFound");
     }
     if (is_null($r["problem"])) {
         throw new NotFoundException("problemNotFound");
     }
     // Is the combination contest_id and problem_id valid?
     if (is_null(ContestProblemsDAO::getByPK($r["contest"]->getContestId(), $r["problem"]->getProblemId()))) {
         throw new NotFoundException("problemNotFoundInContest");
     }
 }
 /**
  * Validator for List API
  *
  * @param Request $r
  * @throws ForbiddenAccessException
  * @throws InvalidDatabaseOperationException
  * @throws NotFoundException
  */
 private static function validateList(Request $r)
 {
     // Defaults for offset and rowcount
     if (!isset($r['offset'])) {
         $r['offset'] = 0;
     }
     if (!isset($r['rowcount'])) {
         $r['rowcount'] = 100;
     }
     if (!Authorization::IsSystemAdmin($r['current_user_id'])) {
         throw new ForbiddenAccessException('userNotAllowed');
     }
     Validators::isNumber($r['offset'], 'offset', false);
     Validators::isNumber($r['rowcount'], 'rowcount', false);
     Validators::isInEnum($r['status'], 'status', array('new', 'waiting', 'compiling', 'running', 'ready'), false);
     Validators::isInEnum($r['verdict'], 'verdict', array('AC', 'PA', 'WA', 'TLE', 'MLE', 'OLE', 'RTE', 'RFE', 'CE', 'JE', 'NO-AC'), false);
     // Check filter by problem, is optional
     if (!is_null($r['problem_alias'])) {
         Validators::isStringNonEmpty($r['problem_alias'], 'problem');
         try {
             $r['problem'] = ProblemsDAO::getByAlias($r['problem_alias']);
         } catch (Exception $e) {
             // Operation failed in the data layer
             throw new InvalidDatabaseOperationException($e);
         }
         if (is_null($r['problem'])) {
             throw new NotFoundException('problemNotFound');
         }
     }
     Validators::isInEnum($r['language'], 'language', array('c', 'cpp', 'cpp11', 'java', 'py', 'rb', 'pl', 'cs', 'pas', 'kp', 'kj', 'cat', 'hs'), false);
     // Get user if we have something in username
     if (!is_null($r['username'])) {
         try {
             $r['user'] = UserController::resolveUser($r['username']);
         } catch (NotFoundException $e) {
             // If not found, simply ignore it
             $r['username'] = null;
             $r['user'] = null;
         }
     }
 }
Example #7
0
 /**
  * Validator for List API
  * 
  * @param Request $r
  * @throws ForbiddenAccessException
  * @throws InvalidDatabaseOperationException
  * @throws NotFoundException
  */
 private static function validateList(Request $r)
 {
     // Defaults for offset and rowcount
     if (!isset($r["offset"])) {
         $r["offset"] = 0;
     }
     if (!isset($r["rowcount"])) {
         $r["rowcount"] = 100;
     }
     if (!Authorization::IsSystemAdmin($r["current_user_id"])) {
         throw new ForbiddenAccessException("userNotAllowed");
     }
     Validators::isNumber($r["offset"], "offset", false);
     Validators::isNumber($r["rowcount"], "rowcount", false);
     Validators::isInEnum($r["status"], "status", array('new', 'waiting', 'compiling', 'running', 'ready'), false);
     Validators::isInEnum($r["verdict"], "verdict", array("AC", "PA", "WA", "TLE", "MLE", "OLE", "RTE", "RFE", "CE", "JE", "NO-AC"), false);
     // Check filter by problem, is optional
     if (!is_null($r["problem_alias"])) {
         Validators::isStringNonEmpty($r["problem_alias"], "problem");
         try {
             $r["problem"] = ProblemsDAO::getByAlias($r["problem_alias"]);
         } catch (Exception $e) {
             // Operation failed in the data layer
             throw new InvalidDatabaseOperationException($e);
         }
         if (is_null($r["problem"])) {
             throw new NotFoundException("problemNotFound");
         }
     }
     Validators::isInEnum($r["language"], "language", array('c', 'cpp', 'cpp11', 'java', 'py', 'rb', 'pl', 'cs', 'pas', 'kp', 'kj', 'cat', 'hs'), false);
     // Get user if we have something in username
     if (!is_null($r["username"])) {
         try {
             $r["user"] = UserController::resolveUser($r["username"]);
         } catch (NotFoundException $e) {
             // If not found, simply ignore it
             $r["username"] = null;
             $r["user"] = null;
         }
     }
 }
 /**
  * Validate problem Details API
  *
  * @param Request $r
  * @throws ApiException
  * @throws InvalidDatabaseOperationException
  * @throws NotFoundException
  * @throws ForbiddenAccessException
  */
 private static function validateRuns(Request $r)
 {
     Validators::isStringNonEmpty($r['problem_alias'], 'problem_alias');
     // Is the problem valid?
     try {
         $r['problem'] = ProblemsDAO::getByAlias($r['problem_alias']);
     } catch (ApiException $apiException) {
         throw $apiException;
     } catch (Exception $e) {
         throw new InvalidDatabaseOperationException($e);
     }
     if ($r['problem'] == null) {
         throw new NotFoundException('problemNotFound');
     }
 }
Example #9
0
 /**
  * Basic new run test
  */
 public function testNewRunValid()
 {
     $r = $this->setValidRequest();
     $this->detourGraderCalls();
     // Call API
     $response = RunController::apiCreate($r);
     $this->assertRun($r, $response);
     // Check problem submissions (1)
     $problem = ProblemsDAO::getByAlias($r["problem_alias"]);
     $this->assertEquals(1, $problem->getSubmissions());
 }