From bea32ccfa007308d401ec2d5f709f627b6a0eddb Mon Sep 17 00:00:00 2001 From: Mark Johnson Date: Thu, 12 Jun 2025 15:21:54 +0100 Subject: [PATCH] MDL-79540 qbank_managecategories: Don't allow own child as parent When we build the options for a question category selector, the "nochildrenof" option isn't correctly compared to the category ID. This means we can select a category's own children as its new parent. --- .../behat/move_question_categories.feature | 39 ++++++++++++++----- .../output/question_category_selector.php | 3 +- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/question/bank/managecategories/tests/behat/move_question_categories.feature b/question/bank/managecategories/tests/behat/move_question_categories.feature index 9e5c6aa43cc..86dde644d74 100644 --- a/question/bank/managecategories/tests/behat/move_question_categories.feature +++ b/question/bank/managecategories/tests/behat/move_question_categories.feature @@ -14,6 +14,7 @@ Feature: A teacher can reorder question categories And the following "activities" exist: | activity | name | intro | course | idnumber | | qbank | Qbank 1 | Question bank 1 | C1 | qbank1 | + | qbank | Qbank 2 | Question bank 2 | C1 | qbank2 | And the following "course enrolments" exist: | user | course | role | | teacher1 | C1 | editingteacher | @@ -22,14 +23,14 @@ Feature: A teacher can reorder question categories | teacher1 | editingteacher | System | And the following "question categories" exist: | contextlevel | reference | name | idnumber | - | Course | C1 | Course category 1 | questioncat1 | - | Course | C1 | Course category 2 | questioncat2 | - | Course | C1 | Course category 3 | questioncat3 | - And I am on the "Course 1" "core_question > course question categories" page logged in as "teacher1" + | Activity module | qbank1 | Course category 1 | questioncat1 | + | Activity module | qbank1 | Course category 2 | questioncat2 | + | Activity module | qbank1 | Course category 3 | questioncat3 | + And I am on the "Qbank 1" "core_question > question categories" page logged in as "teacher1" Scenario: Teacher cannot move or delete single category under context - Given I am on the "Qbank 1" "core_question > question categories" page logged in as "teacher1" - When I open the action menu in "Default for Qbank 1" "list_item" + And I am on the "Qbank 2" "core_question > question categories" page logged in as "teacher1" + When I open the action menu in "Default for Qbank 2" "list_item" Then I should not see "Delete" Scenario: Teacher can see complete edit menu if multiples categories exist under context @@ -67,8 +68,28 @@ Feature: A teacher can reorder question categories And "Course category 3" "list_item" should exist in the "Course category 1" "list_item" Scenario: Teacher can display and hide category descriptions - Given I am on the "Qbank 1" "core_question > question categories" page logged in as "teacher1" + Given I am on the "Qbank 2" "core_question > question categories" page logged in as "teacher1" When I click on "Show descriptions" "checkbox" - Then I should see "The default category for questions shared in context 'Qbank 1'." + Then I should see "The default category for questions shared in context 'Qbank 2'." And I click on "Show descriptions" "checkbox" - And I should not see "The default category for questions shared in context 'Qbank 1'." + And I should not see "The default category for questions shared in context 'Qbank 2'." + + Scenario: Teacher can move a category via the edit form + Given "Course category 1" "list_item" should appear before "Course category 2" "list_item" + And "Course category 2" "list_item" should appear before "Course category 3" "list_item" + When I open the action menu in "Course category 3" "list_item" + And I choose "Edit" in the open action menu + And I should see "Top for Qbank 1" in the "Parent category" "select" + And I should see "Course category 1" in the "Parent category" "field" + And I should see "Course category 2" in the "Parent category" "field" + And I should not see "Course category 3" in the "Parent category" "field" + And I set the field "Parent category" to "Course category 1" + And I press "Save changes" + Then "Course category 1" "list_item" should appear before "Course category 3" "list_item" + And "Course category 3" "list_item" should appear before "Course category 2" "list_item" + And I open the action menu in "Course category 1" "list_item" + And I choose "Edit" in the open action menu + And I should see "Top for Qbank 1" in the "Parent category" "select" + And I should not see "Course category 1" in the "Parent category" "field" + And I should see "Course category 2" in the "Parent category" "field" + And I should not see "Course category 3" in the "Parent category" "field" diff --git a/question/classes/output/question_category_selector.php b/question/classes/output/question_category_selector.php index db064f30c2b..c53407a66fb 100644 --- a/question/classes/output/question_category_selector.php +++ b/question/classes/output/question_category_selector.php @@ -314,7 +314,8 @@ class question_category_selector implements renderable, templatable { // Recursively indent the children. foreach ($categories[$id]->childids as $childid) { - if ($childid != $nochildrenof) { + [$childcategory, ] = explode(',', $childid); + if ($childcategory != $nochildrenof) { $newcategories = $newcategories + self::flatten_category_tree( $categories, $childid,