diff --git a/question/bank/managecategories/classes/helper.php b/question/bank/managecategories/classes/helper.php index c9e9e8b45ac..d1a8458aa7b 100644 --- a/question/bank/managecategories/classes/helper.php +++ b/question/bank/managecategories/classes/helper.php @@ -324,7 +324,7 @@ class helper { foreach ($categories as $category) { if ($category->contextid == $contextid) { $cid = $category->id; - if ($currentcat != $cid || $currentcat == 0) { + if ("{$currentcat},{$contextid}" != $cid || $currentcat == 0) { $a = new \stdClass; $a->name = format_string($category->indentedname, true, ['context' => $context]); diff --git a/question/bank/managecategories/tests/helper_test.php b/question/bank/managecategories/tests/helper_test.php index d9a6d499bce..e7e332deaf6 100644 --- a/question/bank/managecategories/tests/helper_test.php +++ b/question/bank/managecategories/tests/helper_test.php @@ -253,6 +253,39 @@ final class helper_test extends \advanced_testcase { } } + /** + * Test that question_category_options function does not include the current category. + * + * @covers ::question_category_options + */ + public function test_question_category_options_exclude_current(): void { + $this->setAdminUser(); + $this->resetAfterTest(); + + // Create categories. + $qcategory1 = $this->qgenerator->create_question_category(['contextid' => $this->context->id]); + $qcategory2 = $this->qgenerator->create_question_category(['contextid' => $this->context->id, 'parent' => $qcategory1->id]); + $qcategory3 = $this->qgenerator->create_question_category(['contextid' => $this->context->id]); + + $contexts = new \core_question\local\bank\question_edit_contexts($this->context); + + $categorycontexts = helper::question_category_options($contexts->having_cap('moodle/question:add')); + // We get all categories without the currentcat parameter. + $categorycontext = $categorycontexts['Quiz: Quiz 1']; + $this->assertCount(3, $categorycontext); + + // The currentcat category is excluded. + $newcategorycontexts = helper::question_category_options( + $contexts->having_cap('moodle/question:add'), + currentcat: $qcategory2->id, + ); + $newcategorycontext = $newcategorycontexts['Quiz: Quiz 1']; + $this->assertCount(2, $newcategorycontext); + $this->assertContains($qcategory1->name, $newcategorycontext); + $this->assertNotContains($qcategory2->name, $newcategorycontext); + $this->assertContains($qcategory3->name, $newcategorycontext); + } + /** * Test that get_categories_for_contexts function returns the correct question count number. *