MDL-77462 core_question: avoid passing null to preg_match

This commit is contained in:
Tim Hunt 2023-03-03 08:46:00 +00:00
parent 556208417d
commit de086efd41
2 changed files with 4 additions and 3 deletions

View File

@ -1982,7 +1982,7 @@ function core_question_find_next_unused_idnumber(?string $oldidnumber, int $cate
global $DB;
// The the old idnumber is not of the right form, bail now.
if (!preg_match('~\d+$~', $oldidnumber, $matches)) {
if ($oldidnumber === null || !preg_match('~\d+$~', $oldidnumber, $matches)) {
return null;
}

View File

@ -1998,6 +1998,7 @@ class questionlib_test extends \advanced_testcase {
*/
public function find_next_unused_idnumber_cases(): array {
return [
[null, null],
['id', null],
['id1a', null],
['id001', 'id002'],
@ -2020,10 +2021,10 @@ class questionlib_test extends \advanced_testcase {
* Test core_question_find_next_unused_idnumber in the case when there are no other questions.
*
* @dataProvider find_next_unused_idnumber_cases
* @param string $oldidnumber value to pass to core_question_find_next_unused_idnumber.
* @param string|null $oldidnumber value to pass to core_question_find_next_unused_idnumber.
* @param string|null $expectednewidnumber expected result.
*/
public function test_core_question_find_next_unused_idnumber(string $oldidnumber, ?string $expectednewidnumber) {
public function test_core_question_find_next_unused_idnumber(?string $oldidnumber, ?string $expectednewidnumber) {
$this->assertSame($expectednewidnumber, core_question_find_next_unused_idnumber($oldidnumber, 0));
}