From 7df6e571f87ad82b22aa21c014db2c9508fa3a55 Mon Sep 17 00:00:00 2001 From: Mark Johnson Date: Tue, 4 Nov 2025 10:15:21 +0000 Subject: [PATCH] MDL-86099 questions: Check for empty bank names If we call `question_bank_helper::create_default_open_instance` with an empty `$bankname` parameter, it will currently create a new instance with no name. This leads to exceptions being thrown when we try to load the course it belongs to. This adds some validation to ensure the name being passed is not empty. Once the issue is resolved, and the bank is created with a proper name, the course will work as normal. --- .../local/bank/question_bank_helper.php | 6 +++++ .../local/bank/question_bank_helper_test.php | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/public/question/classes/local/bank/question_bank_helper.php b/public/question/classes/local/bank/question_bank_helper.php index d9ee76ce17d..9a67bcf479b 100644 --- a/public/question/classes/local/bank/question_bank_helper.php +++ b/public/question/classes/local/bank/question_bank_helper.php @@ -626,6 +626,12 @@ class question_bank_helper { ); } + if ($bankname === '') { + throw new \coding_exception( + 'The provided bankname is empty. You must provide a name for the question bank.', + ); + } + $data = new stdClass(); $data->section = 0; $data->visible = 0; diff --git a/public/question/tests/local/bank/question_bank_helper_test.php b/public/question/tests/local/bank/question_bank_helper_test.php index 83b692209df..c877de22060 100644 --- a/public/question/tests/local/bank/question_bank_helper_test.php +++ b/public/question/tests/local/bank/question_bank_helper_test.php @@ -16,6 +16,7 @@ namespace core_question; +use core\exception\coding_exception; use core_question\local\bank\question_bank_helper; /** @@ -355,6 +356,30 @@ final class question_bank_helper_test extends \advanced_testcase { $this->assertEquals($bankname, $cminfo->get_name()); } + /** + * Attempting to create a default bank with an empty name throws an exception and does not create the bank. + */ + public function test_create_default_open_instance_with_empty_name(): void { + $this->resetAfterTest(); + self::setAdminUser(); + + $course = self::getDataGenerator()->create_course(); + $bankname = ''; + + try { + question_bank_helper::create_default_open_instance($course, $bankname); + } catch (coding_exception $e) { + $this->assertStringEndsWith( + 'The provided bankname is empty. You must provide a name for the question bank.', + $e->getMessage(), + ); + } + + $modinfo = get_fast_modinfo($course); + $cminfos = $modinfo->get_instances_of('qbank'); + $this->assertCount(0, $cminfos); + } + /** * Assert that viewing a question bank logs the view for that user up to a maximum of 5 unique bank views. *