Skip to content

Commit

Permalink
MDL-81521 mod_quiz: Fix attempt walkthrough tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Nov 15, 2024
1 parent a6acb01 commit 9adfbcd
Show file tree
Hide file tree
Showing 9 changed files with 688 additions and 578 deletions.
19 changes: 19 additions & 0 deletions .upgradenotes/MDL-81521-2024101701160681.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
issueNumber: MDL-81521
notes:
mod_quiz:
- message: >
The `\mod_quiz\attempt_walkthrough_from_csv_test` unit test has been
marked as final and should not be extended by other tests.
All shared functionality has been moved to a new autoloadable test-case:
`\mod_quiz\tests\attempt_walkthrough_testcase`.
To support this testcase the existing `$files` instance property should be replaced with a
new static method, `::get_test_files`.
Both the existing instance property and the new static method can co-exist.
type: changed
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@
use mod_quiz\quiz_attempt;
use question_bank;

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot . '/mod/quiz/tests/attempt_walkthrough_from_csv_test.php');
require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');

/**
* Quiz attempt walk through using data from csv file.
*
Expand All @@ -35,16 +28,21 @@
* @author Jamie Pratt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class responses_from_steps_walkthrough_test extends \mod_quiz\attempt_walkthrough_from_csv_test {
protected function get_full_path_of_csv_file(string $setname, string $test): string {
// Overridden here so that __DIR__ points to the path of this file.
return __DIR__."/fixtures/{$setname}{$test}.csv";
final class responses_from_steps_walkthrough_test extends \mod_quiz\tests\attempt_walkthrough_testcase {
#[\Override]
public static function setUpBeforeClass(): void {
global $CFG;

parent::setUpBeforeClass();

require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
}

/**
* @var string[] names of the files which contain the test data.
*/
protected $files = ['questions', 'steps', 'responses'];
#[\Override]
protected static function get_test_files(): array {
return ['questions', 'steps', 'responses'];
}

/**
* Create a quiz add questions to it, walk through quiz attempts and then check results.
Expand All @@ -54,7 +52,6 @@ protected function get_full_path_of_csv_file(string $setname, string $test): str
* @dataProvider get_data_for_walkthrough
*/
public function test_walkthrough_from_csv($quizsettings, $csvdata): void {

$this->resetAfterTest(true);
question_bank::get_qtype('random')->clear_caches_before_testing();

Expand All @@ -72,7 +69,14 @@ public function test_walkthrough_from_csv($quizsettings, $csvdata): void {
}
}

protected function assert_response_test($quizattemptid, $responses) {
/**
* Helper to assert a response.
*
* @param mixed $quizattemptid
* @param mixed $responses
* @throws \coding_exception
*/
protected function assert_response_test($quizattemptid, $responses): void {
$quizattempt = quiz_attempt::create($quizattemptid);

foreach ($responses['slot'] as $slot => $tests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@
use question_finder;
use quiz_statistics_report;

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot . '/mod/quiz/tests/attempt_walkthrough_from_csv_test.php');
require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');

/**
* Quiz attempt walk through using data from csv file.
*
Expand All @@ -45,22 +38,26 @@
* @author Jamie Pratt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class stats_from_steps_walkthrough_test extends \mod_quiz\attempt_walkthrough_from_csv_test {

final class stats_from_steps_walkthrough_test extends \mod_quiz\tests\attempt_walkthrough_testcase {
/**
* @var quiz_statistics_report object to do stats calculations.
*/
protected $report;

protected function get_full_path_of_csv_file(string $setname, string $test): string {
// Overridden here so that __DIR__ points to the path of this file.
return __DIR__."/fixtures/{$setname}{$test}.csv";
#[\Override]
public static function setUpBeforeClass(): void {
global $CFG;

parent::setUpBeforeClass();

require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
}

/**
* @var string[] names of the files which contain the test data.
*/
protected $files = ['questions', 'steps', 'results', 'qstats', 'responsecounts'];
#[\Override]
protected static function get_test_files(): array {
return ['questions', 'steps', 'results', 'qstats', 'responsecounts'];
}

/**
* Create a quiz add questions to it, walk through quiz attempts and then check results.
Expand All @@ -69,7 +66,6 @@ protected function get_full_path_of_csv_file(string $setname, string $test): str
* @dataProvider get_data_for_walkthrough
*/
public function test_walkthrough_from_csv($quizsettings, $csvdata): void {

$this->create_quiz_simulate_attempts_and_check_results($quizsettings, $csvdata);

$whichattempts = QUIZ_GRADEAVERAGE; // All attempts.
Expand Down
Loading

0 comments on commit 9adfbcd

Please sign in to comment.