-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX]doument_page_portal: added test cases
- Loading branch information
Davit Lomauridze
committed
Aug 13, 2024
1 parent
9e38a69
commit c8a0d76
Showing
1 changed file
with
34 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,183 +1,41 @@ | ||
# Copyright (C) 2020 - TODAY, Marcel Savegnago - Escodoo). | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
from odoo.tests.common import HttpCase | ||
|
||
from odoo import fields as odoo_fields, http | ||
from odoo.exceptions import AccessError, MissingError | ||
from odoo.http import request | ||
from odoo.osv.expression import OR | ||
from odoo.tools.translate import _ | ||
|
||
from odoo.addons.portal.controllers.portal import CustomerPortal, pager as portal_pager | ||
|
||
|
||
class CustomerPortal(CustomerPortal): | ||
def _prepare_portal_layout_values(self): | ||
values = super(CustomerPortal, self)._prepare_portal_layout_values() | ||
values["document_page_count"] = request.env["document.page"].search_count( | ||
[("type", "=", "content")] | ||
class TestPortalController(HttpCase): | ||
def setUp(self): | ||
super(TestPortalController, self).setUp() | ||
self.document_page_model = self.env["document.page"] | ||
self.portal_controller = self.env["ir.http"].get( | ||
"portal.my.knowledge.document.pages" | ||
) | ||
return values | ||
|
||
def _get_archive_groups( | ||
self, | ||
model, | ||
domain=None, | ||
fields=None, | ||
groupby="create_date", | ||
order="create_date desc", | ||
): | ||
if not model: | ||
return [] | ||
if domain is None: | ||
domain = [] | ||
if fields is None: | ||
fields = ["name", "create_date"] | ||
groups = [] | ||
for group in request.env[model]._read_group_raw( | ||
domain, fields=fields, groupby=groupby, orderby=order | ||
): | ||
dates, label = group[groupby] | ||
date_begin, date_end = dates.split("/") | ||
groups.append( | ||
{ | ||
"date_begin": odoo_fields.Date.to_string( | ||
odoo_fields.Date.from_string(date_begin) | ||
), | ||
"date_end": odoo_fields.Date.to_string( | ||
odoo_fields.Date.from_string(date_end) | ||
), | ||
"name": label, | ||
"item_count": group[groupby + "_count"], | ||
} | ||
) | ||
return groups | ||
def test_prepare_portal_layout_values(self): | ||
# Test if document_page_count is correctly set in the portal layout values | ||
self.document_page_model.create({"name": "Test Page 1", "type": "content"}) | ||
response = self.portal_controller()._prepare_portal_layout_values() | ||
self.assertIn("document_page_count", response) | ||
self.assertEqual(response["document_page_count"], 1) | ||
|
||
def _document_page_get_page_view_values( | ||
self, document_page, access_token, **kwargs | ||
): | ||
values = { | ||
"page_name": "document_page", | ||
"document_page": document_page, | ||
} | ||
return self._get_page_view_values( | ||
document_page, | ||
access_token, | ||
values, | ||
"my_document_pages_history", | ||
False, | ||
**kwargs | ||
) | ||
|
||
@http.route( | ||
["/my/knowledge/documents/", "/my/knowledge/documents/page/<int:page>"], | ||
type="http", | ||
auth="user", | ||
website=True, | ||
) | ||
def portal_my_knowledge_document_pages( | ||
self, | ||
page=1, | ||
date_begin=None, | ||
date_end=None, | ||
sortby=None, | ||
search=None, | ||
search_in="content", | ||
**kw | ||
): | ||
values = self._prepare_portal_layout_values() | ||
def test_get_archive_groups(self): | ||
# Test if archive groups are correctly retrieved | ||
self.document_page_model.create({"name": "Test Page 1", "type": "content"}) | ||
domain = [("type", "=", "content")] | ||
|
||
searchbar_sortings = { | ||
"date": {"label": _("Newest"), "order": "create_date desc"}, | ||
"name": {"label": _("Name"), "order": "name"}, | ||
"parent": {"label": _("Category"), "order": "parent_id"}, | ||
} | ||
searchbar_inputs = { | ||
"content": { | ||
"input": "content", | ||
"label": _('Search <span class="nolabel"> (in Content)</span>'), | ||
}, | ||
"all": {"input": "all", "label": _("Search in All")}, | ||
} | ||
|
||
# default sort by value | ||
if not sortby: | ||
sortby = "date" | ||
order = searchbar_sortings[sortby]["order"] | ||
|
||
# archive groups - Default Group By 'create_date' | ||
archive_groups = self._get_archive_groups("document.page", domain) | ||
if date_begin and date_end: | ||
domain += [ | ||
("create_date", ">", date_begin), | ||
("create_date", "<=", date_end), | ||
] | ||
|
||
# search | ||
if search and search_in: | ||
search_domain = [] | ||
if search_in in ("content", "all"): | ||
search_domain = OR( | ||
[ | ||
search_domain, | ||
["|", ("name", "ilike", search), ("content", "ilike", search)], | ||
] | ||
) | ||
domain += search_domain | ||
|
||
# pager | ||
document_pages_count = request.env["document.page"].search_count(domain) | ||
pager = portal_pager( | ||
url="/my/knowledge/documents", | ||
url_args={"date_begin": date_begin, "date_end": date_end, "sortby": sortby}, | ||
total=document_pages_count, | ||
page=page, | ||
step=self._items_per_page, | ||
) | ||
|
||
document_pages = request.env["document.page"].search( | ||
domain, order=order, limit=self._items_per_page, offset=pager["offset"] | ||
) | ||
request.session["my_document_pages_history"] = document_pages.ids[:100] | ||
|
||
values.update( | ||
{ | ||
"date": date_begin, | ||
"document_pages": document_pages, | ||
"page_name": "document_page", | ||
"default_url": "/my/knowledge/s", | ||
"pager": pager, | ||
"archive_groups": archive_groups, | ||
"searchbar_sortings": searchbar_sortings, | ||
"searchbar_inputs": searchbar_inputs, | ||
"sortby": sortby, | ||
"search_in": search_in, | ||
"search": search, | ||
} | ||
) | ||
return request.render( | ||
"document_page_portal.portal_my_knowledge_document_pages", values | ||
) | ||
|
||
@http.route( | ||
[ | ||
"/knowledge/document/<int:document_page_id>", | ||
"/knowledge/document/<int:document_page_id>/<token>", | ||
"/my/knowledge/document/<int:document_page_id>", | ||
], | ||
type="http", | ||
auth="public", | ||
website=True, | ||
) | ||
def document_pages_followup(self, document_page_id=None, access_token=None, **kw): | ||
try: | ||
document_page_sudo = self._document_check_access( | ||
"document.page", document_page_id, access_token | ||
) | ||
except (AccessError, MissingError): | ||
return request.redirect("/my") | ||
|
||
values = self._document_page_get_page_view_values( | ||
document_page_sudo, access_token, **kw | ||
groups = self.portal_controller()._get_archive_groups("document.page", domain) | ||
self.assertTrue(groups) | ||
self.assertEqual(groups[0]["name"], "Test Page 1") | ||
|
||
def test_portal_my_knowledge_document_pages(self): | ||
# Test if the portal route for knowledge document pages works correctly | ||
self.document_page_model.create({"name": "Test Page 1", "type": "content"}) | ||
response = self.url_open("/my/knowledge/documents/") | ||
self.assertEqual(response.status_code, 200) | ||
self.assertIn(b"Test Page 1", response.data) | ||
|
||
def test_document_pages_followup(self): | ||
# Test if the follow-up route for a specific document page works correctly | ||
document_page = self.document_page_model.create( | ||
{"name": "Test Page 1", "type": "content"} | ||
) | ||
return request.render("document_page_portal.document_pages_followup", values) | ||
response = self.url_open(f"/knowledge/document/{document_page.id}") | ||
self.assertEqual(response.status_code, 200) | ||
self.assertIn(b"Test Page 1", response.data) | ||