Files
odoo_source/addons/test_website/tests/test_menu.py
T
Guillaume-gdi a911014cd3 [FIX] website, test_website: clear menu cache correctly
[This commit] fixed an issue with the menu cache. Unfortunately, during
the forward port, we missed updating the cache key according to what has
been done in [this other commit]. This commit updates the cache key and
improves the test.

Steps to reproduce the bug fixed by this commit:
- Render a website without a record URL in the menu (to the cache)
- Edit the website's menu
- Add a link to a product page (e.g., customizable-desk)
- Add a link to another product (e.g., chair-floor-protection)
- Save the menu
- Click on the menu link to go to customizable-desk
=> At this point, the active menu element is correct
- Click on the menu link to go to chair-floor-protection
=> The active menu element does not update

This issue does not occur if there is a record like URL in the menu
before the first render.

[This commit]: https://github.com/odoo/odoo/commit/970c173530e5523d0e3242ad84dae6fe5e332d68
[this other commit]:https://github.com/odoo/odoo/commit/595aa248433246959a5fa9288e477091701c6a35

opw-3694651
opw-3750925
opw-3781668

closes odoo/odoo#159464

X-original-commit: 9b5647f2951cdd7bde214f860b5ea95d42d2a501
Signed-off-by: Romain Derie (rde) <rde@odoo.com>
Signed-off-by: Guillaume Dieleman (gdi) <gdi@odoo.com>
2024-03-27 17:44:59 +00:00

53 lines
2.0 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
from lxml import html
from odoo.addons.website.tools import MockRequest
from odoo.tests import tagged, HttpCase
@tagged('post_install', '-at_install')
class TestWebsiteMenu(HttpCase):
def test_menu_active_element(self):
records = self.env['test.model'].create([{
'name': "Record 1",
'is_published': True,
}, {
'name': "Record 2",
'is_published': True,
}])
controller_url = '/test_website/model_item/'
website = self.env['website'].browse(1)
# First render to fill the cache.
with MockRequest(self.env, website=website, url_root='', path=f"{controller_url}{records[0].id}"):
html.fromstring(self.env['ir.qweb']._render('test_website.model_item', {
'record': records[0],
'main_object': records[0],
}))
self.env['website.menu'].create([{
'name': records[0].name,
'url': f"{controller_url}{records[0].id}",
'parent_id': website.menu_id.id,
'website_id': website.id,
'sequence': 10,
}, {
'name': records[1].name,
'url': f"{controller_url}{records[1].id}",
'parent_id': website.menu_id.id,
'website_id': website.id,
'sequence': 20,
}])
for record in records:
record_url = f"{controller_url}{record.id}"
with MockRequest(self.env, website=website, url_root='', path=record_url):
tree = html.fromstring(self.env['ir.qweb']._render('test_website.model_item', {
'record': record,
'main_object': record,
}))
menu_link_el = tree.xpath(".//*[@id='top_menu']//a[@href='%s' and contains(@class, 'active')]" % record_url)
self.assertEqual(len(menu_link_el), 1, "The menu link related to the current record should be active")