Skip to content

Commit

Permalink
add tests for managing challenges
Browse files Browse the repository at this point in the history
  • Loading branch information
Nakiami committed May 11, 2018
1 parent 5ff089b commit 418cc2a
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 5 deletions.
4 changes: 4 additions & 0 deletions htdocs/admin/edit_challenge.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 12,10 @@
array('id' => $_GET['id'])
);

if (empty($challenge)) {
message_error('No challenge found with this ID');
}

head('Site management');
menu_management();

Expand Down
2 changes: 1 addition & 1 deletion tests/codeception/_data/sql/acceptance/challenges.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 49,7 @@ INSERT INTO challenges (
UNIX_TIMESTAMP(),
1, -- added_by
'Editable CI Challenge',
2, -- category
1, -- category
'This is the editable CI Challenge',
1, -- exposed
1451635200,
Expand Down
34 changes: 34 additions & 0 deletions tests/codeception/_support/AcceptanceTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 90,27 @@ public function amOnListNews() {
$I->seeInCurrentUrl('/list_news');
}

public function amOnCategory($name) {
$I = $this;

$I->amOnPage('/challenges');
$I->click($name);
}

public function amOnEditCategory($id) {
$I = $this;

$I->amOnAdminHome();
$I->amOnPage('/admin/edit_category?id=' . $id);
}

public function amOnEditChallenge($id) {
$I = $this;

$I->amOnAdminHome();
$I->amOnPage('/admin/edit_challenge?id=' . $id);
}

public function makeCategoryAvailable($id) {
$I = $this;

Expand All @@ -117,6 131,26 @@ public function makeCategoryAvailable($id) {
}
}

public function makeChallengeAvailable($id) {
$I = $this;

$I->amOnEditChallenge($id);

$exposed = $I->grabFromDatabase('categories', 'exposed', array('id' => $id));
$available_from = strtotime($I->grabValueFrom('available_from'));
$available_until = strtotime($I->grabValueFrom('available_until'));

if (!$exposed || $available_from > time() || $available_until < time()) {
$I->checkOption('#exposed');
$from = date_time(time() - 10000);
$until = date_time(time() 10000);
$I->fillField('available_from', $from);
$I->fillField('available_until', $until);

$I->click('Save changes');
}
}

public function amAnAdmin() {
$I = $this;

Expand Down
7 changes: 5 additions & 2 deletions tests/codeception/acceptance/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 10,13 @@
const CI_DEFAULT_CATEGORY_ID = 1;
const CI_DEFAULT_CATEGORY_TITLE = 'Default CI Category';

const CI_EDITABLE_CATEGORY_TITLE = 'Editable CI Category';
const CI_EDITABLE_CATEGORY_ID = 2;

const CI_DEFAULT_CHALLENGE_ID = 1;
const CI_DEFAULT_CHALLENGE_TITLE = 'Default CI Challenge';
const CI_DEFAULT_CHALLENGE_DESCRIPTION = 'This is the default CI Challenge';
const CI_DEFAULT_CHALLENGE_FLAG = 'abc123';

const CI_EDITABLE_CATEGORY_TITLE = 'Editable CI Category';
const CI_EDITABLE_CATEGORY_ID = 2;
const CI_EDITABLE_CHALLENGE_TITLE = 'Editable CI Challenge';
const CI_EDITABLE_CHALLENGE_ID = 2;
129 changes: 127 additions & 2 deletions tests/codeception/acceptance/admin/ManageChallengeCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 36,135 @@ public function shouldBeAbleToCreateANewChallenge(AcceptanceTester $I) {

$I->amOnAdminHome();
$I->see($title);
$I->amOnPage('/challenges');
$I->click(CI_DEFAULT_CATEGORY_TITLE);
$I->amOnCategory(CI_DEFAULT_CATEGORY_TITLE);
$I->see($title);
$I->amOnPage('/scores');
$I->see($title);
}

public function whenSettingAChallengeToBeNotExposedItShouldNotBeVisible(AcceptanceTester $I) {
$I->logInAsAnAdmin();
$I->amOnEditChallenge(CI_EDITABLE_CHALLENGE_ID);

$title = time().'title';
$description = time().'body';
$from = date_time();
$until = date_time(time() 10000);

$I->fillField('title', $title);
$I->fillField('description', $description);
$I->uncheckOption('#exposed');
$I->fillField('available_from', $from);
$I->fillField('available_until', $until);
$I->selectOption('form select[name=category]', CI_DEFAULT_CATEGORY_TITLE);
$I->click('Save changes');

$I->waitForText('Edit challenge');
$I->seeInCurrentUrl('/edit_challenge');
$I->seeInField('title', $title);
$I->seeInField('description', $description);
$I->dontSeeCheckboxIsChecked('#exposed');
$I->seeInField('available_from', $from);
$I->seeInField('available_until', $until);

$I->amOnAdminHome();
$I->see($title);
$I->amOnCategory(CI_DEFAULT_CATEGORY_TITLE);
$I->dontSee($title);
$I->amOnPage('/scores');
$I->dontSee($title);
}

public function whenSettingAChallengeToBeExposedAtADateInTheFutureItShouldNotBeVisible(AcceptanceTester $I) {
$I->logInAsAnAdmin();
$I->amOnEditChallenge(CI_EDITABLE_CHALLENGE_ID);

$title = time().'title';

$I->fillField('title', $title);
$I->checkOption('#exposed');
$from = date_time(time() - 10000);
$until = date_time(time() - 100);
$I->fillField('available_from', $from);
$I->fillField('available_until', $until);
$I->selectOption('form select[name=category]', CI_DEFAULT_CATEGORY_TITLE);
$I->click('Save changes');

$I->waitForText('Edit challenge');
$I->seeCheckboxIsChecked('#exposed');
$I->seeInField('available_from', $from);
$I->seeInField('available_until', $until);

$I->amOnCategory(CI_DEFAULT_CATEGORY_TITLE);
$I->see($title);
$I->dontSee('Please enter flag for challenge: ' . $title);

$I->amOnAdminHome();
$I->see($title);
$I->amOnPage('/scores');
$I->see($title);
}

public function shouldGetAnErrorWhenTryingToDeleteAChallengeWithoutTickingTheConfirmationBox(AcceptanceTester $I) {
$I->logInAsAnAdmin();

$I->makeChallengeAvailable(CI_EDITABLE_CHALLENGE_ID);

$I->amOnEditChallenge(CI_EDITABLE_CHALLENGE_ID);
$title = $I->grabValueFrom('title');

$I->amOnAdminHome();
$I->see($title);
$I->amOnCategory(CI_DEFAULT_CATEGORY_TITLE);
$I->see($title);
$I->amOnPage('/scores');
$I->see($title);

$I->amOnEditChallenge(CI_EDITABLE_CHALLENGE_ID);
$I->click('Delete challenge');

$I->see('Error');
$I->see('Please confirm delete');

$I->amOnAdminHome();
$I->see($title);
$I->amOnCategory(CI_DEFAULT_CATEGORY_TITLE);
$I->see($title);
$I->amOnPage('/scores');
$I->see($title);
}

/**
* @depends shouldGetAnErrorWhenTryingToDeleteAChallengeWithoutTickingTheConfirmationBox
*/
public function shouldDeleteAChallengeWhenTheConfirmationBoxIsTicked(AcceptanceTester $I) {
$I->logInAsAnAdmin();

$I->makeChallengeAvailable(CI_EDITABLE_CHALLENGE_ID);

$I->amOnEditChallenge(CI_EDITABLE_CHALLENGE_ID);
$title = $I->grabValueFrom('title');

$I->amOnAdminHome();
$I->see($title);
$I->amOnCategory(CI_DEFAULT_CATEGORY_TITLE);
$I->see($title);
$I->amOnPage('/scores');
$I->see($title);

$I->amOnEditChallenge(CI_EDITABLE_CHALLENGE_ID);
$I->checkOption('#delete_confirmation');
$I->click('Delete challenge');

$I->amOnAdminHome();
$I->dontSee($title);
$I->amOnCategory(CI_DEFAULT_CATEGORY_TITLE);
$I->dontSee($title);
$I->amOnPage('/scores');
$I->dontSee($title);

$I->amOnEditChallenge(CI_EDITABLE_CHALLENGE_ID);
$I->see('Error');
$I->see('No challenge found with this ID');
}
}

0 comments on commit 418cc2a

Please sign in to comment.