Merge branch 'MDL-87516-500' of https://github.com/NashTechOpenUniversity/moodle into MOODLE_500_STABLE

This commit is contained in:
Mihail Geshoski
2026-03-13 15:36:10 +08:00
3 changed files with 56 additions and 5 deletions
+1
View File
@@ -941,6 +941,7 @@ ORDER BY
$record = new stdClass();
$record->id = $qa->get_database_id();
$record->slot = $qa->get_slot();
$record->behaviour = $qa->get_behaviour_name();
$record->questionid = $qa->get_question(false)->id;
$record->variant = $qa->get_variant();
$record->maxmark = $qa->get_max_mark();
+45
View File
@@ -279,6 +279,51 @@ final class datalib_test extends \qbehaviour_walkthrough_test_base {
question_engine::save_questions_usage_by_activity($quba);
}
/**
* Test replacing an old question attempt with a new question of a different behaviour.
* Ensures the slot is updated and the behaviour changes as expected.
*
* @covers ::add_question_in_place_of_other
*/
public function test_replace_old_attempt_with_a_question_have_different_behaviour(): void {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
$course = $this->getDataGenerator()->create_course();
$qbank = $this->getDataGenerator()->create_module('qbank', ['course' => $course->id]);
$category = $generator->create_question_category(
['contextid' => \context_module::instance($qbank->cmid)->id]
);
// Create and add the initial question (true/false) to a new usage.
$truefalse = $generator->create_question('truefalse', null, ['category' => $category->id]);
$usage = \question_engine::make_questions_usage_by_activity('test', \context_module::instance($qbank->cmid));
$usage->set_preferred_behaviour('deferredfeedback');
$slot = $usage->add_question(question_bank::load_question($truefalse->id));
$usage->start_all_questions();
\question_engine::save_questions_usage_by_activity($usage);
// Load the usage and get the current question in the slot.
$loadedusage = \question_engine::load_questions_usage_by_activity($usage->get_id());
$originalquestion = $loadedusage->get_question_attempt($slot)->get_question();
// Create a new replacement question (description type).
$replacementquestion = \test_question_maker::make_question('description');
// Replace the question in the slot with the new one.
$newslot = $loadedusage->add_question_in_place_of_other($slot, $replacementquestion, null, false);
$loadedusage->start_question($newslot);
$loadedusage->finish_all_questions();
\question_engine::save_questions_usage_by_activity($loadedusage);
// Get the new question in the slot after replacement.
$newquestion = $loadedusage->get_question_attempt($newslot)->get_question();
// Check the replacement and behaviour.
$this->assertEquals($replacementquestion->name, $newquestion->name);
$this->assertNotEquals($originalquestion->id, $newquestion->id);
$this->assertEquals($loadedusage->get_question_attempt($newslot)->get_behaviour_name(), 'informationitem');
}
/**
* Test cases for {@see test_get_file_area_name()}.
*
@@ -562,9 +562,16 @@ final class questionusage_autosave_test extends \qbehaviour_walkthrough_test_bas
$transaction->allow_commit();
// Now commit the other transaction.
$this->expectException('dml_write_exception');
$this->save_quba($DB2);
$transaction2->allow_commit();
try {
$this->save_quba($DB2);
// Should never get here.
$transaction2->allow_commit();
} catch (\dml_write_exception $expected) {
// Testing the exception catch manually, because
// we want to assert some more things below.
} finally {
$DB2->dispose();
}
// Now re-load and check how that is re-displayed.
$this->load_quba();
@@ -574,8 +581,6 @@ final class questionusage_autosave_test extends \qbehaviour_walkthrough_test_bas
$this->render();
$this->check_output_contains_text_input('answer', 'autosaved response 1');
$this->check_output_contains_hidden_input(':sequencecheck', 1);
$DB2->dispose();
}
public function test_autosave_with_wrong_seq_number_ignored(): void {