Skip to content

Commit

Permalink
Hide samples for interactive problems.
Browse files Browse the repository at this point in the history
Fixes #926
  • Loading branch information
nickygerritsen committed Aug 11, 2023
1 parent 37043cd commit 0d5184c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
7 changes: 5 additions & 2 deletions webapp/src/Service/DOMJudgeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,10 @@ public function printFile(
*/
public function getSamplesZipContent(ContestProblem $contestProblem): string
{
if ($contestProblem->getProblem()->getCombinedRunCompare()) {
throw new NotFoundHttpException(sprintf('Problem p%d has no downloadable samples', $contestProblem->getProbid()));
}

$zip = new ZipArchive();
if (!($tempFilename = tempnam($this->getDomjudgeTmpDir(), "export-"))) {
throw new ServiceUnavailableHttpException(null, 'Could not create temporary file.');
Expand Down Expand Up @@ -964,7 +968,7 @@ public function getTwigDataForProblemsAction(
->join('cp.problem', 'p')
->leftJoin('p.testcases', 'tc')
->leftJoin('p.attachments', 'a')
->select('partial p.{probid,name,externalid,problemtext_type,timelimit,memlimit}', 'cp', 'a')
->select('partial p.{probid,name,externalid,problemtext_type,timelimit,memlimit,combined_run_compare}', 'cp', 'a')
->andWhere('cp.contest = :contest')
->andWhere('cp.allowSubmit = 1')
->setParameter('contest', $contest)
Expand All @@ -976,7 +980,6 @@ public function getTwigDataForProblemsAction(
->from(ContestProblem::class, 'cp')
->join('cp.problem', 'p')
->leftJoin('p.testcases', 'tc')
->leftJoin('p.attachments', 'a')
->select('p.probid', 'SUM(tc.sample) AS numsamples')
->andWhere('cp.contest = :contest')
->andWhere('cp.allowSubmit = 1')
Expand Down
3 changes: 3 additions & 0 deletions webapp/templates/partials/problem_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
{% for problem in problems %}
<div class="col">
{% set numsamples = samples[problem.probid] %}
{% if problem.problem.combinedRunCompare %}
{% set numsamples = 0 %}
{% endif %}
<div class="card">
<div class="card-body">
<h2 class="card-title">
Expand Down
36 changes: 36 additions & 0 deletions webapp/tests/Unit/Controller/Team/ProblemControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,40 @@ public function testSamples(): void
// Does not contain more than these 4 files.
self::assertCount(4, $content);
}

/**
* Test that the problems page does not show sample data for interactive problems.
*/
public function testInteractiveSamples(): void
{
// First, enable a sample for the interactive boolfind problem.
$em = self::getContainer()->get(EntityManagerInterface::class);
/** @var Problem $problem */
$problem = $em->getRepository(Problem::class)->findOneBy(['externalid' => 'boolfind']);

/** @var Testcase $sample */
$sample = $problem->getTestcases()->get(0);
$sample->setSample(true);
$em->flush();

$this->logIn();

$crawler = $this->client->request('GET', '/team/problems');

// Get the card bodies.
$cardBodies = $crawler->filter('.card-body');

// The last card should not have any samples.
self::assertSame(0,
$cardBodies->eq(2)->filter('.list-group .list-group-item')->count());

// Check the link to download all samples.
$link = $cardBodies->eq(2)->filter('a')->eq(1);
self::assertNotSame('samples', $link->text(null, true));

// Download the sample and make sure the contents are correct.
$this->client->request('GET', '/team/'.$problem->getProbid().'/samples.zip');
$response = $this->client->getResponse();
self::assertEquals(404, $response->getStatusCode());
}
}

0 comments on commit 0d5184c

Please sign in to comment.