From 76c6b1769f8c6237da6f96025a1ca24c4aac79f1 Mon Sep 17 00:00:00 2001 From: Deniz Gallo Date: Thu, 26 Nov 2026 12:05:20 +0100 Subject: [PATCH 1/3] [FIX] document_page_approval: make Back to draft reopen pending requests --- document_page_approval/tests/__init__.py | 1 + .../tests/test_document_page_history.py | 116 ++++++++++++++++++ .../views/document_page_approval.xml | 12 +- 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 document_page_approval/tests/test_document_page_history.py diff --git a/document_page_approval/tests/__init__.py b/document_page_approval/tests/__init__.py index 6f4fbfc92fc..fd8bd358480 100644 --- a/document_page_approval/tests/__init__.py +++ b/document_page_approval/tests/__init__.py @@ -1 +1,2 @@ from . import test_document_page_approval +from . import test_document_page_history diff --git a/document_page_approval/tests/test_document_page_history.py b/document_page_approval/tests/test_document_page_history.py new file mode 100644 index 00000000000..e369c20f34e --- /dev/null +++ b/document_page_approval/tests/test_document_page_history.py @@ -0,0 +1,116 @@ +# Copyright 2026 NuoBiT Solutions SL - Deniz Gallo +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from lxml import etree + +from odoo.tests import new_test_user +from odoo.tools.safe_eval import safe_eval + +from odoo.addons.base.tests.common import BaseCommon + + +class TestDocumentPageHistory(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.history_obj = cls.env["document.page.history"] + cls.page_approvers = cls.env["res.groups"].create({"name": "Page approvers"}) + cls.owner = new_test_user( + cls.env, + login="test_owner", + groups="document_knowledge.group_document_user," + "document_page.group_document_editor", + name="Test owner", + ) + cls.other_approver = new_test_user( + cls.env, + login="test_other_approver", + groups="document_page_approval.group_document_approver_user", + name="Test other approver", + ) + cls.manager = new_test_user( + cls.env, + login="test_manager", + groups="document_page.group_document_manager", + name="Test manager", + ) + category = cls.env["document.page"].create( + { + "name": "This category requires approval", + "type": "category", + "approval_required": True, + "approver_gid": cls.page_approvers.id, + } + ) + cls.page = cls.env["document.page"].create( + {"name": "This page requires approval", "parent_id": category.id} + ) + + def _create_change_request(self, user): + return self.history_obj.with_user(user).create( + { + "page_id": self.page.id, + "summary": "Changed something", + "content": "

New content

", + } + ) + + def _with_user(self, record, user): + # a request of that user starts with an empty cache + self.env.invalidate_all() + return record.with_user(user) + + def _visible_buttons(self, record, string): + """Methods of the buttons labelled `string` that the record's user sees.""" + arch = record.get_views([(False, "form")])["views"]["form"]["arch"] + values = { + "am_i_owner": record.am_i_owner, + "am_i_approver": record.am_i_approver, + "is_approval_required": record.is_approval_required, + "state": record.state, + } + names = [] + for button in etree.fromstring(arch).iter("button"): + if button.get("string") == string and not safe_eval( + button.get("invisible", "False"), values + ): + names.append(button.get("name")) + return names + + def _click(self, record, string): + names = self._visible_buttons(record, string) + self.assertEqual(len(names), 1, names) + return getattr(record, names[0])() + + def test_manager_owner_back_to_draft_pending(self): + """Ensure a manager sends their own pending request back to draft""" + chreq = self._create_change_request(self.manager) + chreq.action_to_approve() + self._click(chreq, "Back to draft") + self.assertEqual(chreq.state, "draft") + self.assertIn("has been cancelled by Test manager.", chreq.message_ids[0].body) + + def test_manager_back_to_draft_pending(self): + """Ensure a manager sends someone else's pending request back to draft""" + chreq = self._create_change_request(self.owner) + chreq.action_to_approve() + chreq = self._with_user(chreq, self.manager) + self._click(chreq, "Back to draft") + self.assertEqual(chreq.state, "draft") + self.assertIn("has been cancelled by Test manager.", chreq.message_ids[0].body) + + def test_other_approver_no_back_to_draft_pending(self): + """Ensure Back to draft is hidden from who is neither owner nor approver""" + chreq = self._create_change_request(self.owner) + chreq.action_to_approve() + self.assertEqual(chreq.state, "to approve") + chreq = self._with_user(chreq, self.other_approver) + self.assertEqual(self._visible_buttons(chreq, "Back to draft"), []) + + def test_manager_back_to_draft_cancelled(self): + """Ensure Back to draft still reopens a cancelled request""" + chreq = self._create_change_request(self.manager) + chreq.action_to_approve() + chreq.action_cancel() + self._click(chreq, "Back to draft") + self.assertEqual(chreq.state, "draft") diff --git a/document_page_approval/views/document_page_approval.xml b/document_page_approval/views/document_page_approval.xml index 87cdf3c4dc4..40887c8709d 100644 --- a/document_page_approval/views/document_page_approval.xml +++ b/document_page_approval/views/document_page_approval.xml @@ -1,4 +1,6 @@ + @@ -44,7 +46,15 @@ name="action_draft" type="object" string="Back to draft" - invisible="(not am_i_owner or not am_i_approver) and (state not in ('cancelled', 'to approve'))" + invisible="(not am_i_owner and not am_i_approver) or state != 'cancelled'" + groups="document_page.group_document_manager" + /> + +