Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add test for assigning menu item as parent to itself

NyanKiyoshi0436a5348908
diff --git a/saleor/graphql/menu/mutations.py b/saleor/graphql/menu/mutations.py
index b8282ad783..be502844ef 100644
--- a/saleor/graphql/menu/mutations.py
+++ b/saleor/graphql/menu/mutations.py
@@ -215,45 +215,50 @@ class MenuItemMove(BaseMutation):
description = 'Moves items of menus'
@staticmethod
- def validate_move(
- operation: _MenuMoveOperation):
- """Validate if the given move is actually possible."""
-
- if operation.parent:
- if operation.menuItem.parent:
+ def pre_validate_move(move):
+ """Validate if the given move could be possibly possible."""
+ if move.parent_id:
+ if move.item_id == move.parent_id:
raise ValidationError({
- 'parent': (
- 'Cannot assign a parent to a leaf.')
- })
+ 'parent': 'Cannot assign a node to itself.'})
+
+ @staticmethod
+ def post_validate_move(operation: _MenuMoveOperation):
+ """Validate if the given move will be actually possible."""
+ if operation.parent:
if operation.menuItem.is_ancestor_of(operation.parent):
raise ValidationError({
'parent': (
- 'Cannot assign a parent that is an ancestor '
- 'of the target menu item.')
- })
+ 'Cannot assign a node as child of '
+ 'one of its descendants.')})
+
+ @classmethod
+ def move_to_operation(cls, info, move) -> _MenuMoveOperation:
+ menu_item = cls.get_node_or_error(
+ info, move.item_id,
+ field='item_id', only_type=MenuItem)
+ parent_node = None
+
+ if move.parent_id is not None:
+ parent_node = cls.get_node_or_error(
+ info, move.parent_id,
+ field='parent_id', only_type=MenuItem)
+
+ return _MenuMoveOperation(
+ menuItem=menu_item,
+ parent=parent_node,
+ sort_order=move.sort_order)
@classmethod
def clean_move(
cls, info, move_operations: List) -> List[_MenuMoveOperation]:
+
operations = []
for move in move_operations:
- menu_item = cls.get_node_or_error(
- info, move.item_id,
- field='item_id', only_type=MenuItem)
- parent_node = None
-
- if move.parent_id is not None:
- parent_node = cls.get_node_or_error(
- info, move.parent_id,
- field='parent_id', only_type=MenuItem)
-
- operation = _MenuMoveOperation(
- menuItem=menu_item,
- parent=parent_node,
- sort_order=move.sort_order)
-
- cls.validate_move(operation)
+ cls.pre_validate_move(move)
+ operation = cls.move_to_operation(info, move)
+ cls.post_validate_move(operation)
operations.append(operation)
return operations
diff --git a/tests/api/test_menu.py b/tests/api/test_menu.py
index d2ef1316be..7a5d4d1c8f 100644
--- a/tests/api/test_menu.py
+++ b/tests/api/test_menu.py
@@ -461,24 +461,51 @@ def test_menu_reorder_assign_parent_to_top_level(
assert root.parent is None
-def test_menu_reorder_assign_parent_to_leaf(
+def test_menu_reorder_cannot_assign_to_ancestor(
staff_api_client, permission_manage_menus, menu_item_list):
root = menu_item_list[0]
root_node_id = graphene.Node.to_global_id('MenuItem', root.pk)
- # Give to the item menu a parent
- root.move_to(menu_item_list[1])
- root.save()
+ child = menu_item_list[2]
+ child_node_id = graphene.Node.to_global_id('MenuItem', child.pk)
- assert root.parent
+ # Give the child an ancestor
+ child.move_to(root)
+ child.save()
+
+ # Give the child an ancestor
+ child.move_to(root)
+ child.save()
+
+ assert not root.parent
+ assert child.parent
move = [{
"itemId": root_node_id,
- "parentId": root_node_id,
+ "parentId": child_node_id,
+ "sortOrder": None
+ }]
+
+ response = get_graphql_content(staff_api_client.post_graphql(
+ QUERY_REORDER_MENU, {'move': move}, [permission_manage_menus]))
+ assert response['data']['menuItemMove']['errors'] == [
+ {'field': 'parent',
+ 'message': 'Cannot assign a node as child of '
+ 'one of its descendants.'}]
+
+
+def test_menu_reorder_cannot_assign_to_itself(
+ staff_api_client, permission_manage_menus, menu_item):
+ node_id = graphene.Node.to_global_id('MenuItem', menu_item.pk)
+ move = [{
+ "itemId": node_id,
+ "parentId": node_id,
"sortOrder": None
}]
response = get_graphql_content(staff_api_client.post_graphql(
QUERY_REORDER_MENU, {'move': move}, [permission_manage_menus]))
- assert response['data']['menuItemMove']['errors']
+ assert response['data']['menuItemMove']['errors'] == [
+ {'field': 'parent',
+ 'message': 'Cannot assign a node to itself.'}]