diff --git a/webapp/tests/Unit/Controller/Jury/ContestControllerTest.php b/webapp/tests/Unit/Controller/Jury/ContestControllerTest.php index 8d99f4cebd..a4f224b4e1 100644 --- a/webapp/tests/Unit/Controller/Jury/ContestControllerTest.php +++ b/webapp/tests/Unit/Controller/Jury/ContestControllerTest.php @@ -21,6 +21,7 @@ class ContestControllerTest extends JuryControllerTest 'selector' => 'Boolean switch search', 'fixture' => NULL]; protected static $addForm = 'contest['; + protected static $addPlus = 'problems'; protected static $addEntitiesShown = ['shortname','name']; protected static $addEntities = [['shortname' => 'nc', 'name' => 'New Contest', @@ -81,7 +82,38 @@ class ContestControllerTest extends JuryControllerTest 'name' => 'Timezones', 'activatetimeString' => '1990-07-17 16:00:00 Africa/Douala', 'starttimeString' => '1990-07-17 16:00:00 Etc/GMT+2', - 'freezetimeString' => '1990-07-17 16:00:00 America/Paramaribo']]; + 'freezetimeString' => '1990-07-17 16:00:00 America/Paramaribo'], + ['shortname' => 'prob', + 'problems' => ['0' => ['shortname' => 'boolfind', + 'points' => '1', + 'allowSubmit' => '1', + 'allowJudge' => '1', + 'color' => '#ffffff', + 'lazyEvalResults' => '0', + 'problem' => '2']]], + ['shortname' => 'multprob', + 'name' => 'Contest with problems', + 'problems' => ['0' => ['problem' => '2', + 'shortname' => 'fcmp', + 'points' => '2', + 'allowSubmit' => '1', + 'allowJudge' => '1', + 'color' => '#000000', + 'lazyEvalResults' => '0'], + '1' => ['problem' => '1', + 'shortname' => 'hw', + 'points' => '1', + 'allowSubmit' => '0', + 'allowJudge' => '1', + 'color' => '#000000', + 'lazyEvalResults' => '0'], + '2' => ['problem' => '3', + 'shortname' => 'p3', + 'points' => '1', + 'allowSubmit' => '1', + 'allowJudge' => '0', + 'color' => 'yellow', + 'lazyEvalResults' => '1']]]]; public function testCheckAddEntityAdmin(): void { diff --git a/webapp/tests/Unit/Controller/Jury/JudgehostRestrictionsControllerTest.php b/webapp/tests/Unit/Controller/Jury/JudgehostRestrictionsControllerTest.php index 2aafd0933d..c0cce8bfc1 100644 --- a/webapp/tests/Unit/Controller/Jury/JudgehostRestrictionsControllerTest.php +++ b/webapp/tests/Unit/Controller/Jury/JudgehostRestrictionsControllerTest.php @@ -3,6 +3,7 @@ namespace App\Tests\Unit\Controller\Jury; use App\DataFixtures\Test\AddJudgehostRestrictionFixture; +use App\DataFixtures\Test\SampleJudgehostRestrictionFixture; use App\Entity\JudgehostRestriction; class JudgehostRestrictionsControllerTest extends JuryControllerTest @@ -10,12 +11,13 @@ class JudgehostRestrictionsControllerTest extends JuryControllerTest protected static $baseUrl = '/jury/judgehost-restrictions'; protected static $exampleEntries = ['No']; protected static $shortTag = 'judgehost restriction'; - protected static $deleteEntities = ['description' => ['adb']]; + protected static $deleteEntities = ['name' => ['TestRestriction']]; protected static $getIDFunc = 'getRestrictionid'; protected static $className = JudgehostRestriction::class; protected static $DOM_elements = ['h1' => ['Judgehost restrictions']]; - protected static $delete = ''; //TODO: When insert works this can be reset. + protected static $deleteFixtures = [AddJudgehostRestrictionFixture::class]; protected static $addForm = 'judgehost_restriction['; + protected static $addEntitiesCount = ['contests','problems','languages']; protected static $addEntitiesShown = ['name']; protected static $addEntities = [['name' => 'Restriction', 'contests' => ['0' => '1', '1' => '2'], @@ -26,15 +28,9 @@ class JudgehostRestrictionsControllerTest extends JuryControllerTest 'contests' => [], 'problems' => [], 'languages' => []], + ['name' => 'HighCountNumbers', + 'languages' => ['0' => 'adb', '1' => 'awk', '2' => 'swift', + '3' => 'r', '4' => 'rb', '5' => 'plg']], ['name' => 'NoJudgeOwn', 'rejudge_own' => '0']]; - - /** - * @dataProvider provideDeleteEntity - */ - public function testDeleteEntity(string $identifier, string $entityShortName): void - { - $this->loadFixture(AddJudgehostRestrictionFixture::class); - parent::testDeleteEntity($identifier, $entityShortName); - } } diff --git a/webapp/tests/Unit/Controller/Jury/JuryControllerTest.php b/webapp/tests/Unit/Controller/Jury/JuryControllerTest.php index d297832287..cac2193aac 100644 --- a/webapp/tests/Unit/Controller/Jury/JuryControllerTest.php +++ b/webapp/tests/Unit/Controller/Jury/JuryControllerTest.php @@ -25,10 +25,14 @@ abstract class JuryControllerTest extends BaseTest protected static $add = '/add'; protected static $edit = '/edit'; protected static $delete = '/delete'; + protected static $deleteEntities = []; + protected static $deleteFixtures = []; protected static $shortTag = ''; + protected static $addPlus = null; protected static $addForm = ''; - protected static $deleteExtra = NULL; + protected static $deleteExtra = null; protected static $addEntities = []; + protected static $addEntitiesCount = []; public function __construct($name = null, array $data = [], $dataName = '') { @@ -158,6 +162,19 @@ public function testCheckAddEntityJury(): void } } + public function helperCheckExistance(string $id, $value, array $element): void { + if (in_array($id, static::$addEntitiesShown)) { + $tmpValue = $element[$id]; + if (is_bool($value)) { + $tmpValue = $value ? 'yes' : 'no'; + } + self::assertSelectorExists('body:contains("' . $tmpValue . '")'); + } + if (in_array($id, static::$addEntitiesCount)) { + self::assertSelectorExists('body:contains("' . count($element[$id]) . '")'); + } + } + /** * Test that admin can add a new entity for this controller */ @@ -172,55 +189,60 @@ public function testCheckAddEntityAdmin(): void foreach (static::$addEntities as $element) { $formFields = []; // First fill with default values, the 0th item of the array - foreach (static::$addEntities[0] as $id=>$field) { - // We can not set checkboxes directly, so skip them for now - if (is_bool($field)) { - continue; - } - $formId = str_replace('.', '][', $id); - $formFields[static::$addForm . $formId . "]"] = $field; - } // Overwrite with data to test with. - foreach ($element as $id=>$field) { - // We can not set checkboxes directly, so skip them for now - if (is_bool($field)) { - continue; + foreach ([static::$addEntities[0], $element] as $item) { + foreach ($item as $id=>$field) { + // Skip elements which we cannot set yet + // We can not set checkboxes directly + // We can not set the fields set by JS directly + if (is_bool($field) || $id === static::$addPlus) { + continue; + } + $formId = str_replace('.', '][', $id); + $formFields[static::$addForm . $formId . "]"] = $field; } - $formId = str_replace('.', '][', $id); - $formFields[static::$addForm . $formId . "]"] = $field; } $this->verifyPageResponse('GET', static::$baseUrl . static::$add, 200); $button = $this->client->getCrawler()->selectButton('Save'); $form = $button->form($formFields, 'POST'); $formName = str_replace('[', '', static::$addForm); - // Set checkboxes - foreach (static::$addEntities[0] as $id=>$field) { - if (!is_bool($field)) { - continue; - } - if ($field) { - $form[$formName][$id]->tick(); - } else { - $form[$formName][$id]->untick(); + // Get the underlying object to inject elements not currently in the DOM. + $rawValues = $form->getPhpValues(); + foreach ([static::$addEntities[0], $element] as $item) { + if (key_exists(static::$addPlus, $item)) { + $rawValues[$formName . static::$addPlus . ']'] = $item[static::$addPlus]; } } - foreach ($element as $id=>$field) { - if (!is_bool($field)) { - continue; - } - if ($field) { - $form[$formName][$id]->tick(); - } else { - $form[$formName][$id]->untick(); + // Set checkboxes + foreach ([static::$addEntities[0], $element] as $item) { + foreach ($item as $id => $field) { + if (!is_bool($field)) { + continue; + } + if ($field) { + $form[$formName][$id]->tick(); + } else { + $form[$formName][$id]->untick(); + } } } $this->client->submit($form); - } + } $this->verifyPageResponse('GET', static::$baseUrl, 200); foreach (static::$addEntities as $element) { foreach ($element as $id=>$value) { - if (in_array($id, static::$addEntitiesShown)) { - self::assertSelectorExists('body:contains("' . $element[$id] . '")'); + if (is_array($value)) { + if (in_array($id, static::$addEntitiesCount)) { + self::assertSelectorExists('body:contains("' . count($element[$id]) . '")'); + } else { + foreach ($value as $id2=>$value2) { + if (is_array($value2)) { + $this->helperCheckExistance((string)$id, $value2, $element); + } + } + } + } else { + $this->helperCheckExistance($id, $value, $element); } } } @@ -237,21 +259,20 @@ public function testDeleteEntity(string $identifier, string $entityShortName): v $this->roles = ['admin']; $this->logOut(); $this->logIn(); + $this->loadFixtures(static::$deleteFixtures); $this->verifyPageResponse('GET', static::$baseUrl, 200); - if (static::$delete !== '') { - // Find a CID we can delete - $em = self::$container->get('doctrine')->getManager(); - $ent = $em->getRepository(static::$className)->findOneBy([$identifier => $entityShortName]); - self::assertSelectorExists('i[class*=fa-trash-alt]'); - self::assertSelectorExists('body:contains("' . $entityShortName . '")'); - $this->verifyPageResponse( - 'GET', - static::$baseUrl . '/' . $ent->{static::$getIDFunc}() . static::$delete, - 200 - ); - $this->client->submitForm('Delete', []); - self::assertSelectorNotExists('body:contains("' . $entityShortName . '")'); - } + // Find a CID we can delete + $em = self::$container->get('doctrine')->getManager(); + $ent = $em->getRepository(static::$className)->findOneBy([$identifier => $entityShortName]); + self::assertSelectorExists('i[class*=fa-trash-alt]'); + self::assertSelectorExists('body:contains("' . $entityShortName . '")'); + $this->verifyPageResponse( + 'GET', + static::$baseUrl . '/' . $ent->{static::$getIDFunc}() . static::$delete, + 200 + ); + $this->client->submitForm('Delete', []); + self::assertSelectorNotExists('body:contains("' . $entityShortName . '")'); } /** @@ -266,7 +287,7 @@ public function provideDeleteEntity(): Generator } } } else { - yield ['nothing', 'toDelete']; + self::markTestSkipped("No deletable entity."); } } diff --git a/webapp/tests/Unit/Controller/Jury/LanguagesControllerTest.php b/webapp/tests/Unit/Controller/Jury/LanguagesControllerTest.php index 288a68885b..e98f9cdcfb 100644 --- a/webapp/tests/Unit/Controller/Jury/LanguagesControllerTest.php +++ b/webapp/tests/Unit/Controller/Jury/LanguagesControllerTest.php @@ -13,6 +13,7 @@ class LanguagesControllerTest extends JuryControllerTest protected static $getIDFunc = 'getLangid'; protected static $className = Language::class; protected static $DOM_elements = ['h1' => ['Languages']]; + protected static $addPlus = 'extensions'; protected static $addForm = 'language['; protected static $addEntitiesShown = ['langid','externalid','name','timefactor']; protected static $addEntities = []; diff --git a/webapp/tests/Unit/Controller/Jury/TeamControllerTest.php b/webapp/tests/Unit/Controller/Jury/TeamControllerTest.php index 3f9c2cfe60..03d918461d 100644 --- a/webapp/tests/Unit/Controller/Jury/TeamControllerTest.php +++ b/webapp/tests/Unit/Controller/Jury/TeamControllerTest.php @@ -15,6 +15,7 @@ class TeamControllerTest extends JuryControllerTest protected static $DOM_elements = ['h1' => ['Teams']]; protected static $addForm = 'team['; protected static $addEntitiesShown = ['name','icpcid','displayName','room']; + protected static $addEntitiesCount = ['contests']; protected static $addEntities = [['name' => 'New Team', 'displayName' => 'New Team Display Name', 'category' => '3',