MDL-45023 quiz statistics report : refactoring of tests for code reuse

This commit is contained in:
James Pratt
2014-04-11 15:07:08 +07:00
parent 1a727e121e
commit ea6db479dd
2 changed files with 80 additions and 62 deletions
@@ -72,48 +72,13 @@ class quiz_report_statistics_from_steps_testcase extends mod_quiz_attempt_walkth
*/
public function test_walkthrough_from_csv($quizsettings, $csvdata) {
// CSV data files for these tests were generated using :
// https://github.com/jamiepratt/moodle-quiz-tools/tree/master/responsegenerator
$this->create_quiz_simulate_attempts_and_check_results($quizsettings, $csvdata);
$this->resetAfterTest(true);
question_bank::get_qtype('random')->clear_caches_before_testing();
$this->create_quiz($quizsettings, $csvdata['questions']);
$attemptids = $this->walkthrough_attempts($csvdata['steps']);
if (isset($csvdata['results'])) {
$this->check_attempts_results($csvdata['results'], $attemptids);
}
$this->report = new quiz_statistics_report();
$whichattempts = QUIZ_GRADEAVERAGE; // All attempts.
$whichtries = question_attempt::ALL_TRIES;
$groupstudents = array();
$questions = $this->report->load_and_initialise_questions_for_calculations($this->quiz);
list($quizstats, $questionstats) = $this->report->get_all_stats_and_analysis($this->quiz,
$whichattempts,
$whichtries,
$groupstudents,
$questions);
$qubaids = quiz_statistics_qubaids_condition($this->quiz->id, $groupstudents, $whichattempts);
// We will create some quiz and question stat calculator instances and some response analyser instances, just in order
// to check the last analysed time then returned.
$quizcalc = new \quiz_statistics\calculator();
// Should not be a delay of more than one second between the calculation of stats above and here.
$this->assertTimeCurrent($quizcalc->get_last_calculated_time($qubaids));
$qcalc = new \core_question\statistics\questions\calculator($questions);
$this->assertTimeCurrent($qcalc->get_last_calculated_time($qubaids));
if (isset($csvdata['responsecounts'])) {
$this->check_response_counts($csvdata['responsecounts'], $qubaids, $questions, $whichtries);
}
if (isset($csvdata['qstats'])) {
$this->check_question_stats($csvdata['qstats'], $questionstats);
}
list($questions, $quizstats, $questionstats, $qubaids) =
$this->check_stats_calculations_and_response_analysis($csvdata, $whichattempts, $whichtries, $groupstudents);
if ($quizsettings['testnumber'] === '00') {
$this->check_variants_count_for_quiz_00($questions, $questionstats, $whichtries, $qubaids);
$this->check_quiz_stats_for_quiz_00($quizstats);
@@ -389,4 +354,44 @@ class quiz_report_statistics_from_steps_testcase extends mod_quiz_attempt_walkth
}
}
/**
* Check the question stats and the response counts used in the statistics report. If the appropriate files exist in fixtures/.
*
* @param PHPUnit_Extensions_Database_DataSet_ITable[] $csvdata Data loaded from csv files for this test.
* @param string $whichattempts
* @param string $whichtries
* @param int[] $groupstudents
* @return array with contents 0 => $questions, 1 => $quizstats, 2=> $questionstats, 3=> $qubaids Might be needed for further
* testing.
*/
protected function check_stats_calculations_and_response_analysis($csvdata, $whichattempts, $whichtries, $groupstudents) {
$this->report = new quiz_statistics_report();
$questions = $this->report->load_and_initialise_questions_for_calculations($this->quiz);
list($quizstats, $questionstats) = $this->report->get_all_stats_and_analysis($this->quiz,
$whichattempts,
$whichtries,
$groupstudents,
$questions);
$qubaids = quiz_statistics_qubaids_condition($this->quiz->id, $groupstudents, $whichattempts);
// We will create some quiz and question stat calculator instances and some response analyser instances, just in order
// to check the last analysed time then returned.
$quizcalc = new \quiz_statistics\calculator();
// Should not be a delay of more than one second between the calculation of stats above and here.
$this->assertTimeCurrent($quizcalc->get_last_calculated_time($qubaids));
$qcalc = new \core_question\statistics\questions\calculator($questions);
$this->assertTimeCurrent($qcalc->get_last_calculated_time($qubaids));
if (isset($csvdata['responsecounts'])) {
$this->check_response_counts($csvdata['responsecounts'], $qubaids, $questions, $whichtries);
}
if (isset($csvdata['qstats'])) {
$this->check_question_stats($csvdata['qstats'], $questionstats);
return array($questions, $quizstats, $questionstats, $qubaids);
}
return array($questions, $quizstats, $questionstats, $qubaids);
}
}
@@ -53,6 +53,23 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
*/
protected $randqids;
/**
* The only test in this class. This is run multiple times depending on how many sets of files there are in fixtures/
* directory.
*
* @param array $quizsettings of settings read from csv file quizzes.csv
* @param PHPUnit_Extensions_Database_DataSet_ITable[] $csvdata of data read from csv file "questionsXX.csv",
* "stepsXX.csv" and "resultsXX.csv".
* @dataProvider get_data_for_walkthrough
*/
public function test_walkthrough_from_csv($quizsettings, $csvdata) {
// CSV data files for these tests were generated using :
// https://github.com/jamiepratt/moodle-quiz-tools/tree/master/responsegenerator
$this->create_quiz_simulate_attempts_and_check_results($quizsettings, $csvdata);
}
public function create_quiz($quizsettings, $qs) {
global $SITE, $DB;
$this->setAdminUser();
@@ -103,7 +120,7 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
// Settings from param override defaults.
$aggregratedsettings = $quizsettings + array('course'=>$SITE->id,
$aggregratedsettings = $quizsettings + array('course' => $SITE->id,
'questionsperpage' => 0,
'grade' => 100.0,
'sumgrades' => $sumofgrades);
@@ -121,6 +138,25 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
}
}
/**
* Create quiz, simulate attempts and check results (if resultsXX.csv exists).
*
* @param array $quizsettings Quiz overrides for this quiz.
* @param PHPUnit_Extensions_Database_DataSet_ITable[] $csvdata Data loaded from csv files for this test.
*/
protected function create_quiz_simulate_attempts_and_check_results($quizsettings, $csvdata) {
$this->resetAfterTest(true);
question_bank::get_qtype('random')->clear_caches_before_testing();
$this->create_quiz($quizsettings, $csvdata['questions']);
$attemptids = $this->walkthrough_attempts($csvdata['steps']);
if (isset($csvdata['results'])) {
$this->check_attempts_results($csvdata['results'], $attemptids);
}
}
/**
* Get full path of CSV file.
*
@@ -192,29 +228,6 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
return $datasets;
}
/**
* Create a quiz add questions to it, walk through quiz attempts and then check results.
*
* @param $quizsettings array of settings read from csv file quizzes.csv
* @param $csvdata \PHPUnit_Extensions_Database_DataSet_ITable[] of data read from csv file "questionsXX.csv",
* "stepsXX.csv" and "resultsXX.csv".
* @dataProvider get_data_for_walkthrough
*/
public function test_walkthrough_from_csv($quizsettings, $csvdata) {
// CSV data files for these tests were generated using :
// https://github.com/jamiepratt/moodle-quiz-tools/tree/master/responsegenerator
$this->resetAfterTest(true);
question_bank::get_qtype('random')->clear_caches_before_testing();
$this->create_quiz($quizsettings, $csvdata['questions']);
$attemptids = $this->walkthrough_attempts($csvdata['steps']);
$this->check_attempts_results($csvdata['results'], $attemptids);
}
/**
* @param $steps PHPUnit_Extensions_Database_DataSet_ITable the step data from the csv file.
* @return array attempt no as in csv file => the id of the quiz_attempt as stored in the db.