Skip to content

Commit

Permalink
Extend unit add tests
Browse files Browse the repository at this point in the history
We now also test for boolean values and enumarations. When lists are
summarized as counts we can indicate this with another field.
  • Loading branch information
vmcj committed Aug 10, 2021
1 parent 7ef4a1f commit 9f98d5d
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 62 deletions.
34 changes: 33 additions & 1 deletion webapp/tests/Unit/Controller/Jury/ContestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
namespace App\Tests\Unit\Controller\Jury;

use App\DataFixtures\Test\AddJudgehostRestrictionFixture;
use App\DataFixtures\Test\SampleJudgehostRestrictionFixture;
use App\Entity\JudgehostRestriction;

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'],
Expand All @@ -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);
}
}
121 changes: 71 additions & 50 deletions webapp/tests/Unit/Controller/Jury/JuryControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '')
{
Expand Down Expand Up @@ -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
*/
Expand All @@ -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);
}
}
}
Expand All @@ -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 . '")');
}

/**
Expand All @@ -266,7 +287,7 @@ public function provideDeleteEntity(): Generator
}
}
} else {
yield ['nothing', 'toDelete'];
self::markTestSkipped("No deletable entity.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
1 change: 1 addition & 0 deletions webapp/tests/Unit/Controller/Jury/TeamControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 9f98d5d

Please sign in to comment.