Skip to content

Commit

Permalink
MDL-56881 mod_choice: add unit tests for choice_user_submit_response
Browse files Browse the repository at this point in the history
Tests cover option limits and multiple option responses.
  • Loading branch information
snake committed Jul 9, 2018
1 parent 0357796 commit 5a87913
Showing 1 changed file with 194 additions and 0 deletions.
194 changes: 194 additions & 0 deletions mod/choice/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -785,4 +785,198 @@ public function test_mod_choice_core_calendar_get_valid_event_timestart_range_cl
$this->assertNull($min);
$this->assertNull($max);
}

/**
* Test choice_user_submit_response for a choice with specific options.
* Options:
* allowmultiple: false
* limitanswers: false
*/
public function test_choice_user_submit_response_no_multiple_no_limits() {
global $DB;
$this->resetAfterTest(true);

$generator = $this->getDataGenerator();
$course = $generator->create_course();
$user = $generator->create_user();
$user2 = $generator->create_user();

// User must be enrolled in the course for choice limits to be honoured properly.
$role = $DB->get_record('role', ['shortname' => 'student']);
$this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $role->id);

// Create choice, with updates allowed and a two options both limited to 1 response each.
$choice = $generator->get_plugin_generator('mod_choice')->create_instance([
'course' => $course->id,
'allowupdate' => false,
'limitanswers' => false,
'allowmultiple' => false,
'option' => ['red', 'green'],
]);
$cm = get_coursemodule_from_instance('choice', $choice->id);

// Get the choice, with options and limits included.
$choicewithoptions = choice_get_choice($choice->id);
$optionids = array_keys($choicewithoptions->option);

// Now, save an response which includes the first option.
$this->assertNull(choice_user_submit_response($optionids[0], $choicewithoptions, $user->id, $course, $cm));

// Confirm that saving again without changing the selected option will not throw a 'choice full' exception.
$this->assertNull(choice_user_submit_response($optionids[1], $choicewithoptions, $user->id, $course, $cm));

// Confirm that saving a response for student 2 including the first option is allowed.
$this->assertNull(choice_user_submit_response($optionids[0], $choicewithoptions, $user2->id, $course, $cm));

// Confirm that trying to save multiple options results in an exception.
$this->expectException('moodle_exception');
choice_user_submit_response([$optionids[1], $optionids[1]], $choicewithoptions, $user->id, $course, $cm);
}

/**
* Test choice_user_submit_response for a choice with specific options.
* Options:
* allowmultiple: true
* limitanswers: false
*/
public function test_choice_user_submit_response_multiples_no_limits() {
global $DB;
$this->resetAfterTest(true);

$generator = $this->getDataGenerator();
$course = $generator->create_course();
$user = $generator->create_user();
$user2 = $generator->create_user();

// User must be enrolled in the course for choice limits to be honoured properly.
$role = $DB->get_record('role', ['shortname' => 'student']);
$this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $role->id);

// Create choice, with updates allowed and a two options both limited to 1 response each.
$choice = $generator->get_plugin_generator('mod_choice')->create_instance([
'course' => $course->id,
'allowupdate' => false,
'allowmultiple' => true,
'limitanswers' => false,
'option' => ['red', 'green'],
]);
$cm = get_coursemodule_from_instance('choice', $choice->id);

// Get the choice, with options and limits included.
$choicewithoptions = choice_get_choice($choice->id);
$optionids = array_keys($choicewithoptions->option);

// Save a response which includes the first option only.
$this->assertNull(choice_user_submit_response([$optionids[0]], $choicewithoptions, $user->id, $course, $cm));

// Confirm that adding an option to the response is allowed.
$this->assertNull(choice_user_submit_response([$optionids[0], $optionids[1]], $choicewithoptions, $user->id, $course, $cm));

// Confirm that saving a response for student 2 including the first option is allowed.
$this->assertNull(choice_user_submit_response($optionids[0], $choicewithoptions, $user2->id, $course, $cm));

// Confirm that removing an option from the response is allowed.
$this->assertNull(choice_user_submit_response([$optionids[0]], $choicewithoptions, $user->id, $course, $cm));

// Confirm that removing all options from the response is not allowed via this method.
$this->expectException('moodle_exception');
choice_user_submit_response([], $choicewithoptions, $user->id, $course, $cm);
}

/**
* Test choice_user_submit_response for a choice with specific options.
* Options:
* allowmultiple: false
* limitanswers: true
*/
public function test_choice_user_submit_response_no_multiples_limits() {
global $DB;
$this->resetAfterTest(true);

$generator = $this->getDataGenerator();
$course = $generator->create_course();
$user = $generator->create_user();
$user2 = $generator->create_user();

// User must be enrolled in the course for choice limits to be honoured properly.
$role = $DB->get_record('role', ['shortname' => 'student']);
$this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $role->id);

// Create choice, with updates allowed and a two options both limited to 1 response each.
$choice = $generator->get_plugin_generator('mod_choice')->create_instance([
'course' => $course->id,
'allowupdate' => false,
'allowmultiple' => false,
'limitanswers' => true,
'option' => ['red', 'green'],
'limit' => [1, 1]
]);
$cm = get_coursemodule_from_instance('choice', $choice->id);

// Get the choice, with options and limits included.
$choicewithoptions = choice_get_choice($choice->id);
$optionids = array_keys($choicewithoptions->option);

// Save a response which includes the first option only.
$this->assertNull(choice_user_submit_response($optionids[0], $choicewithoptions, $user->id, $course, $cm));

// Confirm that changing the option in the response is allowed.
$this->assertNull(choice_user_submit_response($optionids[1], $choicewithoptions, $user->id, $course, $cm));

// Confirm that limits are respected by trying to save the same option as another user.
$this->expectException('moodle_exception');
choice_user_submit_response($optionids[1], $choicewithoptions, $user2->id, $course, $cm);
}

/**
* Test choice_user_submit_response for a choice with specific options.
* Options:
* allowmultiple: true
* limitanswers: true
*/
public function test_choice_user_submit_response_multiples_limits() {
global $DB;
$this->resetAfterTest(true);

$generator = $this->getDataGenerator();
$course = $generator->create_course();
$user = $generator->create_user();
$user2 = $generator->create_user();

// User must be enrolled in the course for choice limits to be honoured properly.
$role = $DB->get_record('role', ['shortname' => 'student']);
$this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $role->id);

// Create choice, with updates allowed and a two options both limited to 1 response each.
$choice = $generator->get_plugin_generator('mod_choice')->create_instance([
'course' => $course->id,
'allowupdate' => false,
'allowmultiple' => true,
'limitanswers' => true,
'option' => ['red', 'green'],
'limit' => [1, 1]
]);
$cm = get_coursemodule_from_instance('choice', $choice->id);

// Get the choice, with options and limits included.
$choicewithoptions = choice_get_choice($choice->id);
$optionids = array_keys($choicewithoptions->option);

// Now, save a response which includes the first option only.
$this->assertNull(choice_user_submit_response([$optionids[0]], $choicewithoptions, $user->id, $course, $cm));

// Confirm that changing the option in the response is allowed.
$this->assertNull(choice_user_submit_response([$optionids[1]], $choicewithoptions, $user->id, $course, $cm));

// Confirm that adding an option to the response is allowed.
$this->assertNull(choice_user_submit_response([$optionids[0], $optionids[1]], $choicewithoptions, $user->id, $course, $cm));

// Confirm that limits are respected by trying to save the same option as another user.
$this->expectException('moodle_exception');
choice_user_submit_response($optionids[1], $choicewithoptions, $user2->id, $course, $cm);
}
}

0 comments on commit 5a87913

Please sign in to comment.