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.
This commit is contained in:
Mark Johnson
2025-11-14 16:52:50 +00:00
parent 982a10e9cf
commit 7df6e571f8
2 changed files with 31 additions and 0 deletions
@@ -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;
@@ -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.
*