diff --git a/api/controller/TestController.php b/api/controller/TestController.php
index ccf2ad7c14532aba5f7381d82fd70df091542845..1f60dada4d8af6cbf1ddcb7a9cdb0dc594ca156b 100644
--- a/api/controller/TestController.php
+++ b/api/controller/TestController.php
@@ -194,7 +194,7 @@ class TestController {
$passes = 1;
} else {
$fails = 1;
- $message = stack_string('default_test_fail');
+ $message = stack_string('defaulttestfail');
}
}
diff --git a/api/public/bulktest.php b/api/public/bulktest.php
index 08c1b84e63d8719113457a2b6f530bcd47a51348..a50e7f4b48bf7a62d756f09e6bf89844e5b3195e 100644
--- a/api/public/bulktest.php
+++ b/api/public/bulktest.php
@@ -160,7 +160,7 @@ require_login();
}
}
if (!json.istests && json.results[seed].passes == 1) {
- resultHtml += '<p class="feedback passed">' + '<?php echo stack_string('default_test_pass')?>' + '</p>';
+ resultHtml += '<p class="feedback passed">' + '<?php echo stack_string('defaulttestpass')?>' + '</p>';
}
// Display seed level messages - will be exceptions.
if (json.results[seed].messages) {
diff --git a/lang/en/qtype_stack.php b/lang/en/qtype_stack.php
index 72816403c0d994da1cb6037c12ac04078193b4dd..072b01484a10fe381de5408786bfb6e65310549e 100644
--- a/lang/en/qtype_stack.php
+++ b/lang/en/qtype_stack.php
@@ -512,7 +512,8 @@ $string['testallincategory'] = 'Test all questions in this category';
$string['overallresult'] = 'Overall result';
$string['seedx'] = 'Seed {$a}';
$string['testpassesandfails'] = '{$a->passes} passes and {$a->fails} failures.';
-
+$string['defaulttestpass'] = 'Default test using model answers returns a score of 1.';
+$string['defaulttestfail'] = 'Default test using model answers does not return a score of 1.';
// Strings used by the question test script.
$string['addanothertestcase'] = 'Add another test case...';
$string['addatestcase'] = 'Add a test case...';
@@ -578,6 +579,8 @@ $string['runquestiontests_help'] = 'The dashboard runs question tests which unit
$string['runquestiontests_alert'] = 'Question is missing tests or variants.';
$string['runquestiontests_auto'] = 'Automatically adding one test case assuming the teacher\'s input gets full marks. Please check the answer note carefully.';
$string['runquestiontests_autoprompt'] = 'Add test case assuming the teacher\'s input gets full marks.';
+$string['runquestiontests_explanation'] = 'If you add the test, its output will look like this:';
+$string['runquestiontests_example'] = 'example';
$string['autotestcase'] = 'Test case assuming the teacher\'s input gets full marks.';
$string['showingundeployedvariant'] = 'Showing undeployed variant: {$a}';
$string['switchtovariant'] = 'Switch to variant: ';
@@ -1527,8 +1530,6 @@ $string['castext_error_unevaluated'] = 'This text content was never evaluated.';
-$string['default_test_fail'] = 'Default test using the model answers does not return a score of 1.';
-$string['default_test_pass'] = 'Default test using the model answers returns a score of 1.';
// API strings.
$string['api_choose_file'] = 'Please select a question file';
$string['api_choose_folder'] = 'Choose a STACK folder';
diff --git a/questiontestrun.php b/questiontestrun.php
index af54fcb8da9206355a3d2a307bc80c299d789faa..8f0259292e841db10db0faf6dc8dbfb94dc4e6ee 100644
--- a/questiontestrun.php
+++ b/questiontestrun.php
@@ -176,27 +176,12 @@ $questionvariablevalues = $question->get_question_session_keyval_representation(
// Load the list of test cases.
$testscases = question_bank::get_qtype('stack')->load_question_tests($question->id);
// Create the default test case.
+$defaulttest = null;
+$defaulttestresult = null;
+
if (optional_param('defaulttestcase', null, PARAM_INT) && $canedit) {
- $inputs = [];
- foreach ($question->inputs as $inputname => $input) {
- $inputs[$inputname] = $input->get_teacher_answer_testcase();
- }
- $qtest = new stack_question_test(stack_string('autotestcase'), $inputs);
- $response = stack_question_test::compute_response($question, $inputs);
-
- foreach ($question->prts as $prtname => $prt) {
- $result = $question->get_prt_result($prtname, $response, false);
- // For testing purposes we just take the last note.
- $answernotes = $result->get_answernotes();
- $answernote = [end($answernotes)];
- // Here we hard-wire 1 mark and 0 penalty. This is what we normally want for the
- // teacher's answer. If the question does not give full marks to the teacher's answer then
- // the test case will fail, and the user can confirm the failing behaviour if they really intended this.
- // Normally we'd want a failing test case with the teacher's answer not getting full marks!
- $qtest->add_expected_result($prtname, new stack_potentialresponse_tree_state(
- 1, true, 1, 0, '', $answernote));
- }
- question_bank::get_qtype('stack')->save_question_test($questionid, $qtest);
+ $defaulttest = stack_bulk_tester::create_default_test($question);
+ question_bank::get_qtype('stack')->save_question_test($questionid, $defaulttest);
$testscases = question_bank::get_qtype('stack')->load_question_tests($question->id);
echo html_writer::tag('p', stack_string_error('runquestiontests_auto'));
@@ -217,6 +202,13 @@ if (empty($testscases) && $canedit) {
echo html_writer::end_tag('form');
}
+if (empty($testscases)) {
+ $defaulttest = stack_bulk_tester::create_default_test($question);
+ $defaulttestresult = $defaulttest->test_question($questionid, $seed, $context);
+ echo stack_string('runquestiontests_explanation');
+ echo $defaulttestresult->html_output($question, stack_string('runquestiontests_example'));
+}
+
$deployfeedback = optional_param('deployfeedback', null, PARAM_TEXT);
if (!is_null($deployfeedback)) {
echo html_writer::tag('p', $deployfeedback, ['class' => 'overallresult pass']);
diff --git a/stack/bulktester.class.php b/stack/bulktester.class.php
index 8dae8fd8313b63a108aec839387bb210a5da48c6..b9152c5e84f1ac2d7834fd722230afcf0cf45516 100644
--- a/stack/bulktester.class.php
+++ b/stack/bulktester.class.php
@@ -450,6 +450,18 @@ class stack_bulk_tester {
return [false, "Attempting to start the question threw an exception!"];
}
+ if (!$tests) {
+ $defaulttest = stack_bulk_tester::create_default_test($question);
+ $defaulttestresult = $defaulttest->test_question($qid, $seed, $context);
+ if ($defaulttestresult->passed()) {
+ $ok = true;
+ $message = stack_string('defaulttestpass');
+ } else {
+ $ok = false;
+ $message = stack_string('defaulttestfail');
+ }
+ }
+
// Prepare the display options.
$options = new question_display_options();
$options->readonly = true;
@@ -582,4 +594,41 @@ class stack_bulk_tester {
echo html_writer::tag('p', html_writer::link(new moodle_url('/question/type/stack/adminui/bulktestindex.php'),
get_string('back')));
}
+
+ /**
+ * Create a default test for a question.
+ *
+ * Expected score of 1 and penalty of 0 using the model answers.
+ * The answer note will be set to whatever answer note the model
+ * answer returns.
+ *
+ * Question usage by attempt needs to have already been set up
+ * and the question started.
+ *
+ * @param object $question
+ * @return stack_question_test
+ */
+ public static function create_default_test($question) {
+ $inputs = [];
+ foreach ($question->inputs as $inputname => $input) {
+ $inputs[$inputname] = $input->get_teacher_answer_testcase();
+ }
+ $qtest = new stack_question_test(stack_string('autotestcase'), $inputs);
+ $response = stack_question_test::compute_response($question, $inputs);
+
+ foreach ($question->prts as $prtname => $prt) {
+ $result = $question->get_prt_result($prtname, $response, false);
+ // For testing purposes we just take the last note.
+ $answernotes = $result->get_answernotes();
+ $answernote = [end($answernotes)];
+ // Here we hard-wire 1 mark and 0 penalty. This is what we normally want for the
+ // teacher's answer. If the question does not give full marks to the teacher's answer then
+ // the test case will fail, and the user can confirm the failing behaviour if they really intended this.
+ // Normally we'd want a failing test case with the teacher's answer not getting full marks!
+ $qtest->add_expected_result($prtname, new stack_potentialresponse_tree_state(
+ 1, true, 1, 0, '', $answernote));
+ }
+
+ return $qtest;
+ }
}
diff --git a/tests/api_controller_test.php b/tests/api_controller_test.php
index 348d76d05977c55e0ca7972fe12a22eae7f508fd..f1e95fee86f948b24a9ae8723da4fbad55ee234c 100644
--- a/tests/api_controller_test.php
+++ b/tests/api_controller_test.php
@@ -330,7 +330,7 @@ class api_controller_test extends qtype_stack_testcase {
$this->assertEquals(false, $results->isdeployedseeds);
$this->assertEquals(false, $results->israndomvariants);
$this->assertEquals(false, $results->istests);
- $this->assertEquals(stack_string('default_test_fail'), $results->results->noseed->messages);
+ $this->assertEquals(stack_string('defaulttestfail'), $results->results->noseed->messages);
$this->assertEquals(1, $results->results->noseed->fails);
$this->assertEquals(0, $results->results->noseed->passes);
}