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.
This commit is contained in:
Mark Johnson
2025-07-11 10:07:29 +01:00
parent 9addea9f0a
commit bea32ccfa0
2 changed files with 32 additions and 10 deletions
@@ -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"
@@ -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,