|
| 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 | + ) |
| 86 | + |
| 87 | + def test_hide_menus_processes_catalog(self): |
| 88 | + """``ir.module.module.hide_menus()`` walks MENU_APP and creates a |
| 89 | + ``spp.hide.menu`` record (state=hide) for every entry whose menu |
| 90 | + xml_id resolves in the current DB. |
| 91 | + """ |
| 92 | + IrModuleModule = self.env["ir.module.module"] |
| 93 | + HideMenu = self.env["spp.hide.menu"] |
| 94 | + |
| 95 | + # Figure out which catalog entries are actually resolvable here — |
| 96 | + # most stock Odoo modules in MENU_APP (mail, contacts, ...) are |
| 97 | + # present in any test DB, but a few (mass_mailing, survey, ...) |
| 98 | + # may not be installed. |
| 99 | + resolvable = [] |
| 100 | + for module_name, info in IrModuleModule.MENU_APP.items(): |
| 101 | + menu = self.env.ref(info["menu_xml_id"], raise_if_not_found=False) |
| 102 | + module = IrModuleModule.search([("name", "=", module_name)], limit=1) |
| 103 | + if menu and module: |
| 104 | + resolvable.append((module_name, menu.id)) |
| 105 | + |
| 106 | + if not resolvable: |
| 107 | + self.skipTest("No MENU_APP entries are resolvable in this test DB") |
| 108 | + |
| 109 | + # Wipe any pre-existing spp.hide.menu so the test's assertions are |
| 110 | + # clearly about hide_menus()'s effect, not the install hook. |
| 111 | + HideMenu.search([]).unlink() |
| 112 | + |
| 113 | + IrModuleModule.hide_menus() |
| 114 | + |
| 115 | + for module_name, menu_id in resolvable: |
| 116 | + record = HideMenu.search([("menu_id", "=", menu_id)], limit=1) |
| 117 | + self.assertTrue( |
| 118 | + record, |
| 119 | + f"hide_menus() didn't create a spp.hide.menu for {module_name!r}", |
| 120 | + ) |
| 121 | + self.assertEqual( |
| 122 | + record.state, |
| 123 | + "hide", |
| 124 | + f"spp.hide.menu for {module_name!r} expected state=hide, got {record.state}", |
| 125 | + ) |
| 126 | + |
| 127 | + def test_hide_menus_is_idempotent(self): |
| 128 | + """Calling hide_menus() twice doesn't double-hide already-hidden menus. |
| 129 | +
|
| 130 | + After the first pass every resolvable entry is in state=hide. A |
| 131 | + second pass must leave them in state=hide (the inner guard |
| 132 | + ``elif hidden_menus.state == "show"`` skips them). |
| 133 | + """ |
| 134 | + IrModuleModule = self.env["ir.module.module"] |
| 135 | + HideMenu = self.env["spp.hide.menu"] |
| 136 | + |
| 137 | + HideMenu.search([]).unlink() |
| 138 | + IrModuleModule.hide_menus() |
| 139 | + after_first = HideMenu.search([]) |
| 140 | + self.assertTrue( |
| 141 | + after_first, |
| 142 | + "hide_menus() didn't create any records — nothing to check idempotency against", |
| 143 | + ) |
| 144 | + |
| 145 | + IrModuleModule.hide_menus() |
| 146 | + after_second = HideMenu.search([]) |
| 147 | + # No duplicates created and every record stayed in state=hide. |
| 148 | + self.assertEqual(set(after_first.ids), set(after_second.ids)) |
| 149 | + for record in after_second: |
| 150 | + self.assertEqual(record.state, "hide") |
| 151 | + |
| 152 | + def test_hide_menus_skips_unknown_modules(self): |
| 153 | + """An ir.module.module record whose name isn't in MENU_APP must be |
| 154 | + ignored by hide_menus() — no spp.hide.menu record is created for it. |
| 155 | + """ |
| 156 | + IrModuleModule = self.env["ir.module.module"] |
| 157 | + HideMenu = self.env["spp.hide.menu"] |
| 158 | + |
| 159 | + # ``base`` is always installed and is NOT in MENU_APP. |
| 160 | + self.assertNotIn("base", IrModuleModule.MENU_APP) |
| 161 | + |
| 162 | + before = HideMenu.search([]).ids |
| 163 | + IrModuleModule.hide_menus() |
| 164 | + after = HideMenu.search([]).ids |
| 165 | + |
| 166 | + # Whatever new records appeared, none should belong to the ``base`` menu. |
| 167 | + new_ids = set(after) - set(before) |
| 168 | + for record in HideMenu.browse(list(new_ids)): |
| 169 | + self.assertNotEqual( |
| 170 | + record.menu_id.id, |
| 171 | + self.env.ref("base.menu_administration").id |
| 172 | + if self.env.ref("base.menu_administration", raise_if_not_found=False) |
| 173 | + else 0, |
| 174 | + "hide_menus() shouldn't touch base.menu_administration", |
| 175 | + ) |
0 commit comments