public function testRemoveUser() { // Get a contest $contestData = ContestsFactory::createContest(); // Create a user $user = UserFactory::createUser(); // Add user to contest ContestsFactory::addUser($contestData, $user); // Validate 0 users $r = new Request(); $r['contest_alias'] = $contestData['request']['alias']; $r['auth_token'] = $this->login($contestData['director']); $response = ContestController::apiUsers($r); $this->assertEquals(1, count($response['users'])); // Remove user $r = new Request(); $r['contest_alias'] = $contestData['request']['alias']; $r['usernameOrEmail'] = $user->getUsername(); $r['auth_token'] = $this->login($contestData['director']); ContestController::apiRemoveUser($r); // Validate 0 users in contest $r = new Request(); $r['contest_alias'] = $contestData['request']['alias']; $r['auth_token'] = $this->login($contestData['director']); $response = ContestController::apiUsers($r); $this->assertEquals(0, count($response['users'])); }
public function testContestUsersValid() { // Get a contest $contestData = ContestsFactory::createContest(); // Create 10 users $n = 10; $users = array(); for ($i = 0; $i < $n; $i++) { // Create a user $users[$i] = UserFactory::createUser(); // Add it to the contest ContestsFactory::addUser($contestData, $users[$i]); } // Create a n+1 user who will just join to the contest withot being // added via API. For public contests, by entering to the contest, the user should be in // the list of contest's users. $nonRegisteredUser = UserFactory::createUser(); ContestsFactory::openContest($contestData, $nonRegisteredUser); // Prepare request $r = new Request(); $r["contest_alias"] = $contestData["request"]["alias"]; // Log in with the admin of the contest $r["auth_token"] = $this->login($contestData["director"]); // Call API $response = ContestController::apiUsers($r); // Check that we have n+1 users $this->assertEquals($n + 1, count($response["users"])); }
public function testCoderOfTheMonthCalc() { $user = UserFactory::createUser(); $contest = ContestsFactory::createContest(); $problem = ProblemsFactory::createProblem(); ContestsFactory::addProblemToContest($problem, $contest); ContestsFactory::addUser($contest, $user); // Creating 10 AC runs for our user in the last month $n = 10; $lastMonth = intval(date('m')) - 1; $runCreationDate = null; if ($lastMonth == 0) { $runCreationDate = date(intval(date('Y') - 1) . '-12-01'); } else { $runCreationDate = date('Y-' . $lastMonth . '-01'); } for ($i = 0; $i < $n; $i++) { $runData = RunsFactory::createRun($problem, $contest, $user); RunsFactory::gradeRun($runData); // Force the run to be in last month $run = RunsDAO::getByAlias($runData['response']['guid']); $run->setTime($runCreationDate); RunsDAO::save($run); } $response = UserController::apiCoderOfTheMonth(new Request()); $this->assertEquals($user->getUsername(), $response['userinfo']['username']); }
/** * Prepares the context to submit a run to a problem. Creates the contest, * problem and opens them. * * @return Request */ private function setValidRequest($contest_public = 1) { // Get a problem $problemData = ProblemsFactory::createProblem(); // Get a contest $this->contestData = ContestsFactory::createContest(null, $contest_public); // Add the problem to the contest ContestsFactory::addProblemToContest($problemData, $this->contestData); // Create our contestant $this->contestant = UserFactory::createUser(); // If the contest is private, add the user if ($contest_public === 0) { ContestsFactory::addUser($this->contestData, $this->contestant); } // Our contestant has to open the contest before sending a run ContestsFactory::openContest($this->contestData, $this->contestant); // Then we need to open the problem ContestsFactory::openProblemInContest($this->contestData, $problemData, $this->contestant); // Create an empty request $r = new Request(); // Log in as contest director $r["auth_token"] = $this->login($this->contestant); // Build request $r["contest_alias"] = $this->contestData["request"]["alias"]; $r["problem_alias"] = $problemData["request"]["alias"]; $r["language"] = "c"; $r["source"] = "#include <stdio.h>\nint main() { printf(\"3\"); return 0; }"; //PHPUnit does not set IP address, doing it manually $_SERVER["REMOTE_ADDR"] = "127.0.0.1"; return $r; }
/** * Prepares the context to submit a run to a problem. Creates the contest, * problem and opens them. * * @return Request */ private function setValidRequest($contest_public = 1) { // Get a problem $problemData = ProblemsFactory::createProblem(); // Get a contest $this->contestData = ContestsFactory::createContest(null, $contest_public); // Add the problem to the contest ContestsFactory::addProblemToContest($problemData, $this->contestData); // Create our contestant $this->contestant = UserFactory::createUser(); // If the contest is private, add the user if ($contest_public === 0) { ContestsFactory::addUser($this->contestData, $this->contestant); } // Our contestant has to open the contest before sending a run ContestsFactory::openContest($this->contestData, $this->contestant); // Then we need to open the problem ContestsFactory::openProblemInContest($this->contestData, $problemData, $this->contestant); // Create an empty request $r = new Request(); // Log in as contest director $r['auth_token'] = $this->login($this->contestant); // Build request $r['contest_alias'] = $this->contestData['request']['alias']; $r['problem_alias'] = $problemData['request']['alias']; $r['language'] = 'c'; $r['source'] = "#include <stdio.h>\nint main() { printf(\"3\"); return 0; }"; return $r; }
/** * */ public function testPrivateContestForNonInvitedUser() { $r = new Request(); // Create new private contest $contestData = ContestsFactory::createContest(null, false); // Get a user for our scenario $contestant = UserFactory::createUser(); // Add user to our private contest ContestsFactory::addUser($contestData, $contestant); $r["auth_token"] = $this->login(UserFactory::createUser()); $response = ContestController::apiList($r); // Assert our contest is not there $this->assertTitleInList($response, $contestData, true); }
public function testProblemsSolved() { $user = UserFactory::createUser(); $contest = ContestsFactory::createContest(); $problemOne = ProblemsFactory::createProblem(); $problemTwo = ProblemsFactory::createProblem(); ContestsFactory::addProblemToContest($problemOne, $contest); ContestsFactory::addProblemToContest($problemTwo, $contest); ContestsFactory::addUser($contest, $user); $runs = array(); $runs[0] = RunsFactory::createRun($problemOne, $contest, $user); $runs[1] = RunsFactory::createRun($problemTwo, $contest, $user); $runs[2] = RunsFactory::createRun($problemOne, $contest, $user); RunsFactory::gradeRun($runs[0]); RunsFactory::gradeRun($runs[1]); RunsFactory::gradeRun($runs[2]); $r = new Request(array("auth_token" => self::login($user))); $response = UserController::apiProblemsSolved($r); $this->assertEquals(2, count($response["problems"])); }
/** * Basic tests for shareable scoreboard url */ public function testScoreboardUrlNoLogin() { // Get a private contest with 0% of scoreboard show percentage $contestData = ContestsFactory::createContest(null, 0); ContestsFactory::setScoreboardPercentage($contestData, 0); // Create problem $problemData = ProblemsFactory::createProblem(); ContestsFactory::addProblemToContest($problemData, $contestData); // Create our contestant, will submit 1 run $contestant = UserFactory::createUser(); ContestsFactory::addUser($contestData, $contestant); $runData = RunsFactory::createRun($problemData, $contestData, $contestant); RunsFactory::gradeRun($runData); // Get the scoreboard url by using the MyList api being the // contest director $response = ContestController::apiMyList(new Request(array('auth_token' => $this->login($contestData['director'])))); // Look for our contest from the list and save the scoreboard tokens $scoreboard_url = null; $scoreboard_admin_url = null; foreach ($response['results'] as $c) { if ($c['alias'] === $contestData['request']['alias']) { $scoreboard_url = $c['scoreboard_url']; $scoreboard_admin_url = $c['scoreboard_url_admin']; break; } } $this->assertNotNull($scoreboard_url); $this->assertNotNull($scoreboard_admin_url); // Call scoreboard api from the user $scoreboardResponse = ContestController::apiScoreboard(new Request(array('contest_alias' => $contestData['request']['alias'], 'token' => $scoreboard_url))); $this->assertEquals('0', $scoreboardResponse['ranking'][0]['total']['points']); // Call scoreboard api from the user with admin token $scoreboardResponse = ContestController::apiScoreboard(new Request(array('contest_alias' => $contestData['request']['alias'], 'token' => $scoreboard_admin_url))); $this->assertEquals('100', $scoreboardResponse['ranking'][0]['total']['points']); }
/** * First access time should not change */ public function testAccessTimeIsAlwaysFirstAccessForPrivate() { // Get a contest $contestData = ContestsFactory::createContest(null, 0); // Get a user for our scenario $contestant = UserFactory::createUser(); // Add user to our private contest ContestsFactory::addUser($contestData, $contestant); // Prepare our request $r = new Request(); $r['contest_alias'] = $contestData['request']['alias']; // Log in the user $r['auth_token'] = $this->login($contestant); // Call api $response = ContestController::apiDetails($r); // We need to grab the access time from the ContestUsers table $contest = ContestsDAO::getByAlias($contestData['request']['alias']); $contest_user = ContestsUsersDAO::getByPK($contestant->getUserId(), $contest->getContestId()); $firstAccessTime = $contest_user->getAccessTime(); // Call API again, access time should not change $response = ContestController::apiDetails($r); $contest_user = ContestsUsersDAO::getByPK($contestant->getUserId(), $contest->getContestId()); $this->assertEquals($firstAccessTime, $contest_user->getAccessTime()); }