1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
import json
from hashlib import sha256
from lxml import html
from unittest.mock import Mock, patch
from werkzeug.urls import url_parse
from odoo.addons.website.tools import MockRequest
from odoo.tests import common
class TestMenu(common.TransactionCase):
def setUp(self):
super(TestMenu, self).setUp()
self.nb_website = self.env['website'].search_count([])
def test_01_menu_got_duplicated(self):
Menu = self.env['website.menu']
total_menu_items = Menu.search_count([])
self.menu_root = Menu.create({
'name': 'Root',
})
self.menu_child = Menu.create({
'name': 'Child',
'parent_id': self.menu_root.id,
})
self.assertEqual(total_menu_items + self.nb_website * 2, Menu.search_count([]), "Creating a menu without a website_id should create this menu for every website_id")
def test_02_menu_count(self):
Menu = self.env['website.menu']
total_menu_items = Menu.search_count([])
top_menu = self.env['website'].get_current_website().menu_id
data = [
{
'id': 'new-1',
'parent_id': top_menu.id,
'name': 'New Menu Specific 1',
'url': '/new-specific-1',
'is_mega_menu': False,
},
{
'id': 'new-2',
'parent_id': top_menu.id,
'name': 'New Menu Specific 2',
'url': '/new-specific-2',
'is_mega_menu': False,
}
]
Menu.save(1, {'data': data, 'to_delete': []})
self.assertEqual(total_menu_items + 2, Menu.search_count([]), "Creating 2 new menus should create only 2 menus records")
def test_03_default_menu_for_new_website(self):
Website = self.env['website']
Menu = self.env['website.menu']
total_menu_items = Menu.search_count([])
# Simulating website.menu created on module install (blog, shop, forum..) that will be created on default menu tree
default_menu = self.env.ref('website.main_menu')
Menu.create({
'name': 'Sub Default Menu',
'parent_id': default_menu.id,
})
self.assertEqual(total_menu_items + 1 + self.nb_website, Menu.search_count([]), "Creating a default child menu should create it as such and copy it on every website")
# Ensure new website got a top menu
total_menus = Menu.search_count([])
Website.create({'name': 'new website'})
self.assertEqual(total_menus + 4, Menu.search_count([]), "New website's bootstraping should have duplicate default menu tree (Top/Home/Contactus/Sub Default Menu)")
def test_04_specific_menu_translation(self):
IrModuleModule = self.env['ir.module.module']
if self.env['website'].search_count([]) == 1:
self.env['website'].create({
'name': 'My Website 2',
'domain': '',
'sequence': 20,
})
Menu = self.env['website.menu']
existing_menus = Menu.search([])
default_menu = self.env.ref('website.main_menu')
template_menu = Menu.create({
'parent_id': default_menu.id,
'name': 'Menu in english',
'url': 'turlututu',
})
new_menus = Menu.search([]) - existing_menus
specific1, specific2 = new_menus.with_context(lang='fr_FR') - template_menu
# create fr_FR translation for template menu
self.env.ref('base.lang_fr').active = True
template_menu.with_context(lang='fr_FR').name = 'Menu en français'
self.assertEqual(specific1.name, 'Menu in english',
'Translating template menu does not translate specific menu')
# have different translation for specific website
specific1.name = 'Menu in french'
# loading translation add missing specific translation
IrModuleModule._load_module_terms(['website'], ['fr_FR'])
Menu.invalidate_model(['name'])
self.assertEqual(specific1.name, 'Menu in french',
'Load translation without overwriting keep existing translation')
self.assertEqual(specific2.name, 'Menu en français',
'Load translation add missing translation from template menu')
# loading translation with overwrite sync all translations from menu template
IrModuleModule._load_module_terms(['website'], ['fr_FR'], overwrite=True)
Menu.invalidate_model(['name'])
self.assertEqual(specific1.name, 'Menu en français',
'Load translation with overwriting update existing menu from template')
def test_05_default_menu_unlink(self):
Menu = self.env['website.menu']
total_menu_items = Menu.search_count([])
default_menu = self.env.ref('website.main_menu')
default_menu.child_id[0].unlink()
self.assertEqual(total_menu_items - 1 - self.nb_website, Menu.search_count([]), "Deleting a default menu item should delete its 'copies' (same URL) from website's menu trees. In this case, the default child menu and its copies on website 1 and website 2")
def test_06_menu_active(self):
Menu = self.env['website.menu']
website_1 = self.env['website'].browse(1)
menu = Menu.create({
'name': 'Page Specific menu',
'url': '/contactus',
'website_id': website_1.id,
})
def url_parse_mock(s):
if isinstance(s, Mock):
# We end up in this case when `url_parse` is actually called on
# `request.httprequest.url`. This is simulating as if we were
# really calling the `_is_active()` method from this endpoint
# url.
return url_parse(self.request_url_mock)
return url_parse(s)
def test_full_case(a_menu):
""" This method is testing all the possible flows about URL
matching:
- Same domain:
- no qs & no anchor:
- Same path -> Active
- Not same path -> Not active
- qs:
- same qs
- Same path -> Active
- Not same path -> Not active
- not same qs -> Not active
- Anchor
- Same path -> Active
- Not same path -> Not active
- Not same domain: -> Not active
It should receives a URL with no query string and no anchor.
"""
url = a_menu.url
self.request_url_mock = 'http://localhost:8069' + url
with MockRequest(self.env, website=website_1), \
patch('odoo.addons.website.models.website_menu.url_parse', new=url_parse_mock):
self.assertTrue(a_menu._is_active(), "Same path, no domain, no qs, should match")
a_menu.url = f'{url}#anchor'
self.assertTrue(a_menu._is_active(), "Same path, no domain, no qs, should match (anchor should be ignored)")
a_menu.url = f'{url}?qs=1'
self.assertFalse(a_menu._is_active(), "Same path, no domain, qs mismatch, should not match")
self.request_url_mock = f'http://localhost:8069{url}?qs=2'
self.assertFalse(a_menu._is_active(), "Same path, no domain, qs mismatch (not the same val), should not match")
self.request_url_mock = f'http://localhost:8069{url}?qs=1'
self.assertTrue(a_menu._is_active(), "Same path, no domain, qs match, should match")
self.request_url_mock = f'http://localhost:8069{url}?qs=1&qs_extra=1'
self.assertTrue(a_menu._is_active(), "Same path, no domain, qs subset match, should match")
a_menu.url = f'http://localhost.com:8069{url}'
self.request_url_mock = f'http://example.com:8069{url}'
self.assertFalse(a_menu._is_active(), "Same path, domain mismatch, should not match")
self.request_url_mock = f'http://localhost.com:8069{url}'
self.assertTrue(a_menu._is_active(), "Same path, same domain, should match")
# First, test the full cases with a normal top menu (no child)
test_full_case(menu)
# Create the following menu structure:
# - 2 menus without children: `/` and `#`
# - 2 menus with children: `/` and `#`, both with a `/contactus` child
#
# menu (/) menu2 (/) menu3 (#) menu4 (#)
# - submenu (/contactus) - submenu2 (/contactus)
menu.url = '/'
menu2 = menu.copy()
menu3 = menu.copy({'url': '#'})
menu4 = menu3.copy()
submenu = Menu.create({
'name': 'Page Specific menu',
'url': '/contactus',
'website_id': website_1.id,
'parent_id': menu.id
})
submenu2 = submenu.copy({'parent_id': menu3.id})
# Second, test a nested menu configuration (simple URL, no qs/anchor)
self.request_url_mock = 'http://localhost:8069/'
with MockRequest(self.env, website=website_1), \
patch('odoo.addons.website.models.website_menu.url_parse', new=url_parse_mock):
self.assertFalse(menu._is_active(), "Same path but it's a container menu, its URL shouldn't be considered")
self.assertTrue(menu2._is_active(), "Same path and no child -> Should be active")
self.assertFalse(menu3._is_active(), "Not same path + children")
# Anchor menus are a mistake (unless for container menu) (and
# shouldn't even be possible to create from frontend), the user
# forgot to add the path (the menu won't work on pages without the
# anchor) so the system will prefix it by `/` for the check. This
# will then become `/#` and since anchors are ignored for the check,
# this will match.
self.assertTrue(menu4._is_active(), "Should match, see comment in code")
self.assertFalse(submenu._is_active(), "Not same path (2)")
self.assertFalse(submenu2._is_active(), "Not same path (3)")
self.request_url_mock = 'http://localhost:8069/contactus'
self.assertTrue(menu._is_active(), "A child is active (submenu)")
self.assertFalse(menu2._is_active(), "Not same path (4)")
self.assertTrue(menu3._is_active(), "A child is active (submenu2)")
self.assertFalse(menu4._is_active(), "Not same path (5)")
self.assertTrue(submenu._is_active(), "Same path")
self.assertTrue(submenu2._is_active(), "Same path (2)")
# Third, do the same test as the first one but with a child menu, to
# ensure the behavior remains the same regardless if it is a top menu or
# a child menu
test_full_case(submenu)
# Fourth, do the same test again with a slugified URL, to be sure the
# anchor and query string are not messing with the slug url compare
submenu.url = '/sub/slug-3'
test_full_case(submenu)
class TestMenuHttp(common.HttpCase):
def setUp(self):
super().setUp()
self.page_url = '/page_specific'
self.page = self.env['website.page'].create({
'url': self.page_url,
'website_id': 1,
# ir.ui.view properties
'name': 'Base',
'type': 'qweb',
'arch': '<div>Specific View</div>',
'key': 'test.specific_view',
})
self.menu = self.env['website.menu'].create({
'name': 'Page Specific menu',
'page_id': self.page.id,
'url': self.page_url,
'website_id': 1,
})
def simulate_rpc_save_menu(self, data, to_delete=None):
self.authenticate("admin", "admin")
# `Menu.save(1, {'data': [data], 'to_delete': []})` would have been
# ideal but need a full frontend context to generate routing maps,
# router and registry, even MockRequest is not enough
self.url_open('/web/dataset/call_kw', data=json.dumps({
"params": {
'model': 'website.menu',
'method': 'save',
'args': [1, {'data': [data], 'to_delete': to_delete or []}],
'kwargs': {},
},
}), headers={"Content-Type": "application/json", "Referer": self.page.get_base_url() + self.page_url})
def test_01_menu_page_m2o(self):
# Ensure that the M2o relation tested later in the test is properly set.
self.assertTrue(self.menu.page_id, "M2o should have been set by the setup")
# Edit the menu URL to a 'reserved' URL
data = {
'id': self.menu.id,
'parent_id': self.menu.parent_id.id,
'name': self.menu.name,
'url': '/website/info',
}
self.simulate_rpc_save_menu(data)
self.assertFalse(self.menu.page_id, "M2o should have been unset as this is a reserved URL.")
self.assertEqual(self.menu.url, '/website/info', "Menu URL should have changed.")
self.assertEqual(self.page.url, self.page_url, "Page's URL shouldn't have changed.")
# 3. Edit the menu URL back to the page URL
data['url'] = self.page_url
self.env['website.menu'].save(1, {'data': [data], 'to_delete': []})
self.assertEqual(self.menu.page_id, self.page,
"M2o should have been set back, as there was a page found with the new URL set on the menu.")
self.assertTrue(self.page.url == self.menu.url == self.page_url)
def test_02_menu_anchors(self):
# Ensure that the M2o relation tested later in the test is properly set.
self.assertTrue(self.menu.page_id, "M2o should have been set by the setup")
# Edit the menu URL to an anchor
data = {
'id': self.menu.id,
'parent_id': self.menu.parent_id.id,
'name': self.menu.name,
'url': '#anchor',
}
self.simulate_rpc_save_menu(data)
self.assertFalse(self.menu.page_id, "M2o should have been unset as this is an anchor URL.")
self.assertEqual(self.menu.url, self.page_url + '#anchor', "Page URL should have been properly prefixed with the referer url")
self.assertEqual(self.page.url, self.page_url, "Page URL should not have changed")
def test_03_mega_menu_translate(self):
# Setup
fr = self.env['res.lang']._activate_lang('fr_FR')
Menu = self.env['website.menu']
website = self.env['website'].browse(1)
website.language_ids += fr
menu = Menu.create({
'name': 'Test Mega Menu Content Translation Edit Mode',
'mega_menu_content': '<p>something</p>',
'parent_id': website.menu_id.id,
'website_id': website.id,
})
self.env['ir.module.module']._load_module_terms(['website'], [fr.code])
# Load cache
self.url_open('/%s' % fr.url_code)
self.url_open('/%s?edit_translations=1' % fr.url_code)
# Translate
root = html.fromstring(menu.mega_menu_content)
to_translate = root.text_content()
sha = sha256(to_translate.encode()).hexdigest()
menu.web_update_field_translations('mega_menu_content', {fr.code: {sha: 'french_mega_menu_content'}})
self.assertIn("french_mega_menu_content",
menu.with_context(lang=fr.code, website_id=website.id).mega_menu_content)
# Checks
page = self.url_open('/%s' % fr.url_code)
self.assertIn(b"french_mega_menu_content", page.content)
page = self.url_open('/%s?edit_translations=1' % fr.url_code)
self.assertIn(b"french_mega_menu_content", page.content)
def test_menu_empty_url(self):
website = self.env['website'].browse(1)
menu = self.env['website.menu'].create({
'name': 'Test Empty URL menu',
'parent_id': website.menu_id.id,
'website_id': website.id,
})
self.assertFalse(menu.url, "Menu URL should be empty")
# this should not crash
website.is_menu_cache_disabled()
|