|
| 1 | +# Part of OpenSPP. See LICENSE file for full copyright and licensing details. |
| 2 | +"""Tests for spp_hide_menus_base — hide/show menu visibility logic. |
| 3 | +
|
| 4 | +The module patches ``ir.module.module`` to hide a curated list of stock |
| 5 | +Odoo menus (Project, Calendar, Stock, ...) from the OpenSPP user group |
| 6 | +when an install/upgrade completes. The tests exercise the ``hide_menu`` |
| 7 | +and ``show_menu`` round-trip on ``spp.hide.menu`` directly so we cover |
| 8 | +the model's state transition without depending on a real "Apps install" |
| 9 | +flow. |
| 10 | +""" |
| 11 | + |
| 12 | +from odoo.tests import TransactionCase, tagged |
| 13 | + |
| 14 | + |
| 15 | +@tagged("post_install", "-at_install") |
| 16 | +class TestSppHideMenu(TransactionCase): |
| 17 | + """Exercise the hide / show round-trip on a sample menu.""" |
| 18 | + |
| 19 | + @classmethod |
| 20 | + def setUpClass(cls): |
| 21 | + super().setUpClass() |
| 22 | + # Pick any existing menu we can safely toggle in a test transaction. |
| 23 | + cls.menu = cls.env["ir.ui.menu"].search([], limit=1) |
| 24 | + if not cls.menu: |
| 25 | + raise AssertionError("No ir.ui.menu records found to test against") |
| 26 | + |
| 27 | + def test_module_is_installed(self): |
| 28 | + module = self.env["ir.module.module"].search([("name", "=", "spp_hide_menus_base")], limit=1) |
| 29 | + self.assertEqual(module.state, "installed") |
| 30 | + |
| 31 | + def test_group_hide_menus_user_seed(self): |
| 32 | + """security/groups.xml must declare the hide-menus-user group.""" |
| 33 | + group = self.env.ref("spp_hide_menus_base.group_hide_menus_user", raise_if_not_found=False) |
| 34 | + self.assertTrue( |
| 35 | + group, |
| 36 | + "group_hide_menus_user must exist — hide_menu() falls back on it", |
| 37 | + ) |
| 38 | + |
| 39 | + def test_hide_menu_transition(self): |
| 40 | + """hide_menu() flips state show → hide and snapshots original groups.""" |
| 41 | + original_groups = self.env["ir.ui.menu"].browse(self.menu.id).group_ids |
| 42 | + record = self.env["spp.hide.menu"].create({"menu_id": self.menu.id, "xml_id": "test.hide_menu_target"}) |
| 43 | + self.assertEqual(record.state, "show") |
| 44 | + |
| 45 | + record.hide_menu() |
| 46 | + |
| 47 | + self.assertEqual(record.state, "hide") |
| 48 | + # Original groups were saved on the record so show_menu can restore them. |
| 49 | + self.assertEqual(record.default_group_ids, original_groups) |
| 50 | + |
| 51 | + def test_show_menu_restores_original_groups(self): |
| 52 | + """show_menu() restores the snapshot taken at hide time.""" |
| 53 | + original_groups = self.env["ir.ui.menu"].browse(self.menu.id).group_ids |
| 54 | + record = self.env["spp.hide.menu"].create({"menu_id": self.menu.id, "xml_id": "test.hide_menu_target"}) |
| 55 | + record.hide_menu() |
| 56 | + # Menu is now restricted to the hide-menus-user group only. |
| 57 | + self.assertNotEqual(self.menu.group_ids, original_groups) |
| 58 | + |
| 59 | + record.show_menu() |
| 60 | + self.assertEqual(record.state, "show") |
| 61 | + self.assertEqual(self.menu.group_ids, original_groups) |
| 62 | + |
| 63 | + def test_hide_menu_noop_when_already_hidden(self): |
| 64 | + """Calling hide_menu twice doesn't change state or groups again.""" |
| 65 | + record = self.env["spp.hide.menu"].create({"menu_id": self.menu.id, "xml_id": "test.hide_menu_target"}) |
| 66 | + record.hide_menu() |
| 67 | + snapshot = record.default_group_ids |
| 68 | + record.hide_menu() # second call — guarded by state == "show" |
| 69 | + self.assertEqual(record.state, "hide") |
| 70 | + # Original snapshot must not be overwritten by the second call. |
| 71 | + self.assertEqual(record.default_group_ids, snapshot) |
| 72 | + |
| 73 | + def test_menu_app_catalog_is_well_formed(self): |
| 74 | + """ir.module.module.MENU_APP entries must point to a menu xml_id.""" |
| 75 | + IrModuleModule = self.env["ir.module.module"] |
| 76 | + for module_name, info in IrModuleModule.MENU_APP.items(): |
| 77 | + self.assertIn( |
| 78 | + "menu_xml_id", |
| 79 | + info, |
| 80 | + f"MENU_APP[{module_name!r}] missing required 'menu_xml_id'", |
| 81 | + ) |
| 82 | + self.assertTrue( |
| 83 | + info["menu_xml_id"], |
| 84 | + f"MENU_APP[{module_name!r}].menu_xml_id is empty", |
| 85 | + ) |
0 commit comments