mirror of
https://github.com/moodle/moodle.git
synced 2025-08-09 02:46:40 +02:00
MDL-79073 question: detect missing context when deleting question.
This commit is contained in:
parent
e998f14061
commit
3b525fa234
2 changed files with 29 additions and 1 deletions
|
@ -352,11 +352,12 @@ function question_delete_question($questionid): void {
|
||||||
qv.version,
|
qv.version,
|
||||||
qbe.id as entryid,
|
qbe.id as entryid,
|
||||||
qc.id as categoryid,
|
qc.id as categoryid,
|
||||||
qc.contextid as contextid
|
ctx.id as contextid
|
||||||
FROM {question} q
|
FROM {question} q
|
||||||
LEFT JOIN {question_versions} qv ON qv.questionid = q.id
|
LEFT JOIN {question_versions} qv ON qv.questionid = q.id
|
||||||
LEFT JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
|
LEFT JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
|
||||||
LEFT JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
|
LEFT JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
|
||||||
|
LEFT JOIN {context} ctx ON ctx.id = qc.contextid
|
||||||
WHERE q.id = ?';
|
WHERE q.id = ?';
|
||||||
$questiondata = $DB->get_record_sql($sql, [$question->id]);
|
$questiondata = $DB->get_record_sql($sql, [$question->id]);
|
||||||
|
|
||||||
|
|
|
@ -326,6 +326,33 @@ class questionlib_test extends \advanced_testcase {
|
||||||
$this->assertFalse($DB->record_exists('question', ['id' => $q1->id]));
|
$this->assertFalse($DB->record_exists('question', ['id' => $q1->id]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test deleting a broken question whose category refers to a missing context
|
||||||
|
*/
|
||||||
|
public function test_question_delete_question_missing_context() {
|
||||||
|
global $DB;
|
||||||
|
|
||||||
|
$coursecategory = $this->getDataGenerator()->create_category();
|
||||||
|
$context = $coursecategory->get_context();
|
||||||
|
|
||||||
|
/** @var \core_question_generator $generator */
|
||||||
|
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
||||||
|
$questioncategory = $generator->create_question_category(['contextid' => $context->id]);
|
||||||
|
$question = $generator->create_question('shortanswer', null, ['category' => $questioncategory->id]);
|
||||||
|
|
||||||
|
// Now delete the context, to simulate what happens in old sites where
|
||||||
|
// referential integrity has failed.
|
||||||
|
$DB->delete_records('context', ['id' => $context->id]);
|
||||||
|
|
||||||
|
question_delete_question($question->id);
|
||||||
|
|
||||||
|
$this->assertDebuggingCalled('Deleting question ' . $question->id .
|
||||||
|
' which is no longer linked to a context. Assuming system context ' .
|
||||||
|
'to avoid errors, but this may mean that some data like ' .
|
||||||
|
'files, tags, are not cleaned up.');
|
||||||
|
$this->assertFalse($DB->record_exists('question', ['id' => $question->id]));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function tests the question_category_delete_safe function.
|
* This function tests the question_category_delete_safe function.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue