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. *