From 0e12edd5742c77be08c49643c405dc2ff8851949 Mon Sep 17 00:00:00 2001 From: as6325400 Date: Fri, 25 Oct 2024 01:32:02 +0800 Subject: [PATCH] add unittest to test that the problems page does not show page, statement, sample data before contest start. --- .../Controller/Team/ProblemControllerTest.php | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/webapp/tests/Unit/Controller/Team/ProblemControllerTest.php b/webapp/tests/Unit/Controller/Team/ProblemControllerTest.php index 1817a6136c..3661088aa4 100644 --- a/webapp/tests/Unit/Controller/Team/ProblemControllerTest.php +++ b/webapp/tests/Unit/Controller/Team/ProblemControllerTest.php @@ -4,6 +4,7 @@ use App\Entity\Problem; use App\Entity\Testcase; +use App\Entity\Contest; use App\Tests\Unit\BaseTestCase; use Doctrine\ORM\EntityManagerInterface; use Generator; @@ -183,4 +184,40 @@ public function testInteractiveSamples(): void $response = $this->client->getResponse(); self::assertEquals(404, $response->getStatusCode()); } -} + + /** + * Test that the problems page does not show page, statement, sample data before contest start. + */ + public function testAccessProblemBeforeContestStarts(): void + { + /** @var EntityManagerInterface $em */ + $em = self::getContainer()->get(EntityManagerInterface::class); + + $contest = $em->getRepository(Contest::class)->findOneBy(['shortname' => 'demo']); + $originalStartTime = $contest->getStarttimeString(); + + $lastSpacePosition = strrpos($originalStartTime, ' '); + $datetime = substr($originalStartTime, 0, $lastSpacePosition); + $timezone = substr($originalStartTime, $lastSpacePosition + 1); + + $date = new \DateTime($datetime, new \DateTimeZone($timezone)); + $date->modify('+1 hour'); + $newTimeString = $date->format('Y-m-d H:i:s') . ' ' . $timezone; + $contest->setStarttimeString($newTimeString)->updateTimes(); + + $this->withChangedConfiguration('public_access_before_contest', true, function () { + $this->client->request('GET', '/public/problems'); + static::assertSelectorTextContains('.nav-item .nav-link.disabled', 'Problems'); + static::assertSelectorTextContains('.alert.alert-secondary', 'No problem texts available at this point.'); + + $this->client->request('GET', '/team/problems/2/statement'); + $statusCode = $this->client->getResponse()->getStatusCode(); + static::assertSame(404, $statusCode, 'Expected status code 404, got ' . $statusCode); + + $this->client->request('GET', '/team/problems/2/samples.zip'); + $statusCode = $this->client->getResponse()->getStatusCode(); + static::assertSame(404, $statusCode, 'Expected status code 404, got ' . $statusCode); + }); + $contest->setStarttimeString($originalStartTime)->updateTimes(); + } +} \ No newline at end of file