Esempio n. 1
0
 public function testEditProblem()
 {
     // Login
     $author = $this->createUserAndLogin();
     // Create a problem
     $problemData = ProblemsFactory::createProblem(null, null, 1, $author);
     // Open problem create
     $this->open('/problemedit.php');
     sleep(1);
     $this->type('name=edit-problem-list', $problemData["request"]["alias"]);
     $this->waitForValue('name=title', $problemData["request"]["title"]);
     $problemNewData = ProblemsFactory::getRequest();
     $this->type('name=title', $problemNewData["request"]["title"]);
     $this->type('source', $problemNewData["request"]["source"]);
     $this->type('time_limit', '666');
     $this->type('memory_limit', '1234');
     $this->type('validator', 'token-caseless');
     $this->type('public', '1');
     // Click inicia sesion
     $this->clickAndWait("//input[@value='Actualizar problema']");
     $this->assertElementContainsText('//*[@id="content"]/div[2]/div', "Problem updated succesfully!");
     // Verify data in DB
     $problem_mask = new Problems();
     $problem_mask->setTitle($problemNewData["request"]["title"]);
     $problems = ProblemsDAO::search($problem_mask);
     // Check that we only retreived 1 element
     $this->assertEquals(1, count($problems));
     $this->assertEquals($problemNewData["request"]["source"], $problems[0]->getSource());
     $this->assertEquals(666, $problems[0]->getTimeLimit());
     $this->assertEquals(1234, $problems[0]->getMemoryLimit());
     $this->assertEquals('token-caseless', $problems[0]->getValidator());
     $this->assertEquals('1', $problems[0]->getPublic());
 }
 /**
  * Test that we can produce a valid alias from the title
  */
 public function testConstructAliasFromTitle()
 {
     // Get the problem data
     $problemData = ProblemsFactory::getRequest();
     $r = $problemData['request'];
     $problemAuthor = $problemData['author'];
     // Set a valid "complex" title
     $r['title'] = 'Lá Venganza Del Malvado Dr. Liraaa';
     // Login user
     $r['auth_token'] = $this->login($problemAuthor);
     // Get File Uploader Mock and tell Omegaup API to use it
     FileHandler::SetFileUploader($this->createFileUploaderMock());
     // Call the API
     $response = ProblemController::apiCreate($r);
     // Validate
     // Verify response
     $this->assertEquals('ok', $response['status']);
     $this->assertEquals('testplan', $response['uploaded_files'][10]);
     // Verify data in DB
     $problem_mask = new Problems();
     $problem_mask->setTitle($r['title']);
     $problems = ProblemsDAO::search($problem_mask);
     // Check that we only retreived 1 element
     $this->assertEquals(1, count($problems));
     $problem = $problems[0];
     // Verify contest was found
     $this->assertNotNull($problem);
     $this->assertNotNull($problem->getProblemId());
     // Verify DB data
     $this->assertEquals($r['title'], $problem->getTitle());
     // Verify problem contents were copied
     $targetpath = PROBLEMS_PATH . DIRECTORY_SEPARATOR . $problem->getAlias() . DIRECTORY_SEPARATOR;
     $this->assertFileExists($targetpath . 'testplan');
     $this->assertFileExists($targetpath . 'cases');
     $this->assertFileExists($targetpath . 'statements' . DIRECTORY_SEPARATOR . 'en.html');
 }
 /**
  * Create a new problem
  *
  * @throws ApiException
  * @throws DuplicatedEntryInDatabaseException
  * @throws InvalidDatabaseOperationException
  */
 public static function apiCreate(Request $r)
 {
     self::authenticateRequest($r);
     // Validates request
     self::validateCreateOrUpdate($r);
     // Populate a new Problem object
     $problem = new Problems();
     $problem->setPublic($r['public']);
     /* private by default */
     $problem->setTitle($r['title']);
     $problem->setValidator($r['validator']);
     $problem->setTimeLimit($r['time_limit']);
     $problem->setValidatorTimeLimit($r['validator_time_limit']);
     $problem->setOverallWallTimeLimit($r['overall_wall_time_limit']);
     $problem->setExtraWallTime($r['extra_wall_time']);
     $problem->setMemoryLimit($r['memory_limit']);
     $problem->setOutputLimit($r['output_limit']);
     $problem->setVisits(0);
     $problem->setSubmissions(0);
     $problem->setAccepted(0);
     $problem->setDifficulty(0);
     $problem->setSource($r['source']);
     $problem->setOrder('normal');
     /* defaulting to normal */
     $problem->setAuthorId($r['current_user_id']);
     $problem->setAlias($r['alias']);
     $problem->setLanguages($r['languages']);
     $problem->setStackLimit($r['stack_limit']);
     $problem->setEmailClarifications($r['email_clarifications']);
     if (file_exists(PROBLEMS_PATH . DIRECTORY_SEPARATOR . $r['alias'])) {
         throw new DuplicatedEntryInDatabaseException('problemExists');
     }
     $problemDeployer = new ProblemDeployer($r['alias'], ProblemDeployer::CREATE);
     // Insert new problem
     try {
         ProblemsDAO::transBegin();
         // Create file after we know that alias is unique
         $problemDeployer->deploy();
         if ($problemDeployer->hasValidator) {
             $problem->validator = 'custom';
         } elseif ($problem->validator == 'custom') {
             throw new ProblemDeploymentFailedException('problemDeployerValidatorRequired');
         }
         $problem->slow = $problemDeployer->isSlow($problem);
         // Calculate output limit.
         $output_limit = $problemDeployer->getOutputLimit();
         if ($output_limit != -1) {
             $problem->setOutputLimit($output_limit);
         }
         // Save the contest object with data sent by user to the database
         ProblemsDAO::save($problem);
         ProblemsDAO::transEnd();
         // Commit at the very end
         $problemDeployer->commit('Initial commit', $r['current_user']);
     } catch (ApiException $e) {
         // Operation failed in something we know it could fail, rollback transaction
         ProblemsDAO::transRollback();
         throw $e;
     } catch (Exception $e) {
         self::$log->error('Failed to upload problem');
         self::$log->error($e);
         // Operation failed unexpectedly, rollback transaction
         ProblemsDAO::transRollback();
         // Alias may be duplicated, 1062 error indicates that
         if (strpos($e->getMessage(), '1062') !== false) {
             throw new DuplicatedEntryInDatabaseException('problemTitleExists');
         } else {
             throw new InvalidDatabaseOperationException($e);
         }
     } finally {
         $problemDeployer->cleanup();
     }
     // Adding unzipped files to response
     $result['uploaded_files'] = $problemDeployer->filesToUnzip;
     $result['status'] = 'ok';
     $result['alias'] = $r['alias'];
     return $result;
 }
 /**
  * Tests removed problem admin can't edit a problem anymore
  *
  * @expectedException ForbiddenAccessException
  */
 public function testUpdateProblemWithRemovedProblemAdmin()
 {
     // Get a problem
     $problemData = ProblemsFactory::createProblem();
     // Create our new admin
     $problemAdmin = UserFactory::createUser();
     // Add admin to the problem
     $response = ProblemController::apiAddAdmin(new Request(array('usernameOrEmail' => $problemAdmin->username, 'problem_alias' => $problemData['request']['alias'], 'auth_token' => $this->login($problemData['author']))));
     $this->assertEquals('ok', $response['status']);
     // Then remove the user
     $response = ProblemController::apiRemoveAdmin(new Request(array('usernameOrEmail' => $problemAdmin->username, 'problem_alias' => $problemData['request']['alias'], 'auth_token' => $this->login($problemData['author']))));
     $this->assertEquals('ok', $response['status']);
     //Call API
     $newTitle = 'new title coadmin';
     $response = ProblemController::apiUpdate(new Request(array('problem_alias' => $problemData['request']['alias'], 'title' => $newTitle, 'message' => 'Non-admin powers', 'auth_token' => $this->login($problemAdmin))));
     // Verify data in DB
     $problem_mask = new Problems();
     $problem_mask->setTitle($newTitle);
     $problems = ProblemsDAO::search($problem_mask);
 }
Esempio n. 5
0
 /** 
  * Tests removed problem admin can't edit a problem anymore
  * 
  * @expectedException ForbiddenAccessException
  */
 public function testUpdateProblemWithRemovedProblemAdmin()
 {
     // Get a problem
     $problemData = ProblemsFactory::createProblem();
     // Create our new admin
     $problemAdmin = UserFactory::createUser();
     // Add admin to the problem
     $response = ProblemController::apiAddAdmin(new Request(array("usernameOrEmail" => $problemAdmin->username, "problem_alias" => $problemData["request"]["alias"], "auth_token" => $this->login($problemData["author"]))));
     $this->assertEquals("ok", $response["status"]);
     // Then remove the user
     $response = ProblemController::apiRemoveAdmin(new Request(array("usernameOrEmail" => $problemAdmin->username, "problem_alias" => $problemData["request"]["alias"], "auth_token" => $this->login($problemData["author"]))));
     $this->assertEquals("ok", $response["status"]);
     //Call API
     $newTitle = "new title coadmin";
     $response = ProblemController::apiUpdate(new Request(array("problem_alias" => $problemData["request"]["alias"], "title" => $newTitle, "message" => 'Non-admin powers', "auth_token" => $this->login($problemAdmin))));
     // Verify data in DB
     $problem_mask = new Problems();
     $problem_mask->setTitle($newTitle);
     $problems = ProblemsDAO::search($problem_mask);
 }