Exemplo n.º 1
0
 /**
  * Test 'page', 'order_by' and 'mode' parametes of the apiList() method, and search by title.
  */
 public function testProblemListPager()
 {
     // Create a user and some problems with submissions for the tests.
     $contestant = UserFactory::createUser();
     for ($i = 0; $i < 6; $i++) {
         $problemData[$i] = ProblemsFactory::createProblem(null, null, 1);
         $runs = $i / 2;
         for ($r = 0; $r < $runs; $r++) {
             $runData = RunsFactory::createRunToProblem($problemData[$i], $contestant);
             $points = rand(0, 100);
             $verdict = 'WA';
             if ($points > 0) {
                 $verdict = $points == 100 ? 'AC' : 'PA';
             }
             RunsFactory::gradeRun($runData, $points / 100, $verdict);
         }
     }
     $request = new Request();
     $request['auth_token'] = $this->login($contestant);
     $response = ProblemController::apiList($request);
     // Test search by title
     $titles = array();
     foreach ($response['results'] as $problem) {
         array_push($titles, $problem['title']);
     }
     foreach ($titles as $title) {
         $request['query'] = $title;
         $response = ProblemController::apiList($request);
         $this->assertTrue(count($response['results']) == 1);
         $this->assertTrue($title === $response['results'][0]['title']);
     }
     $request['query'] = null;
     $response = ProblemController::apiList($request);
     $total = $response['total'];
     $pages = intval(($total + PROBLEMS_PER_PAGE - 1) / PROBLEMS_PER_PAGE);
     // The following tests will try the different scenarios that can occur
     // with the additions of the three features to apiList(), that is, paging,
     // order by column and order mode: Call apiList() with and without
     // pagination, for each allowed ordering and each possible order mode.
     $modes = array('asc', 'desc');
     $columns = array('title', 'submissions', 'accepted', 'ratio', 'points', 'score');
     $counter = 0;
     for ($paging = 0; $paging <= 1; $paging++) {
         foreach ($columns as $col) {
             foreach ($modes as $mode) {
                 $first = null;
                 $last = null;
                 $request['mode'] = $mode;
                 $request['order_by'] = $col;
                 if ($paging == 1) {
                     // Clear offset and rowcount if set.
                     if (isset($request['offset'])) {
                         unset($request['offset']);
                     }
                     if (isset($request['rowcount'])) {
                         unset($request['rowcount']);
                     }
                     $request['page'] = 1;
                     $response = ProblemController::apiList($request);
                     $first = $response['results'];
                     $request['page'] = $pages;
                     $response = ProblemController::apiList($request);
                     $last = $response['results'];
                     // Test number of problems per page
                     $this->assertEquals(PROBLEMS_PER_PAGE, count($first));
                 } else {
                     $request['page'] = null;
                     $response = ProblemController::apiList($request);
                     $first = $response['results'];
                     $last = $first;
                 }
                 $i = 0;
                 $j = count($last) - 1;
                 if ($col === 'title') {
                     $comp = strcmp($first[$i]['title'], $last[$j]['title']);
                     if ($mode === 'asc') {
                         $this->assertTrue($comp <= 0);
                     } else {
                         $this->assertTrue($comp >= 0);
                     }
                 } else {
                     if ($mode === 'asc') {
                         $this->assertTrue($first[$i][$col] <= $last[$j][$col]);
                     } else {
                         $this->assertTrue($first[$i][$col] >= $last[$j][$col]);
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
<?php

require_once "../server/bootstrap.php";
$r = new Request();
$mode = isset($_GET['mode']) ? $_GET['mode'] : 'asc';
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$order_by = isset($_GET['order_by']) ? $_GET['order_by'] : 'title';
$r['page'] = $page;
$r['order_by'] = $order_by;
$r['mode'] = $mode;
if (!empty($_GET['tag'])) {
    $r['tag'] = $_GET['tag'];
}
$keyword = '';
if (!empty($_GET['query']) && strlen($_GET['query']) > 0) {
    $keyword = substr($_GET['query'], 0, 256);
    $r['query'] = $keyword;
}
$response = ProblemController::apiList($r);
$params = array('query' => $keyword, 'order_by' => $order_by, 'mode' => $mode);
if (!empty($_GET['tag'])) {
    $params['tag'] = $_GET['tag'];
}
$pager_items = Pager::paginate($response['total'], $page, '/problem/list/', 5, $params);
$smarty->assign('KEYWORD', $keyword);
$smarty->assign('MODE', $mode);
$smarty->assign('ORDER_BY', $order_by);
$smarty->assign('problems', $response['results']);
$smarty->assign('pager_items', $pager_items);
$smarty->display('../templates/problems.tpl');