diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py
index 0b1a9ca5fdf..315b0f0d568 100644
--- a/addons/mail/models/mail_thread.py
+++ b/addons/mail/models/mail_thread.py
@@ -2326,9 +2326,10 @@ class MailThread(models.AbstractModel):
if add_sign:
signature = "
--
%s
" % author.name
- # use company switcher company if author is current user
- user_company = self.env.company if self.env.user == user else user.company_id
- company = self.company_id.sudo() if self and 'company_id' in self else user_company
+ # company value should fall back on env.company if:
+ # - no company_id field on record
+ # - company_id field available but not set
+ company = self.company_id.sudo() if self and 'company_id' in self and self.company_id else self.env.company
if company.website:
website_url = 'http://%s' % company.website if not company.website.lower().startswith(('http:', 'https:')) else company.website
else:
diff --git a/addons/test_mail/tests/test_message_post.py b/addons/test_mail/tests/test_message_post.py
index 6ef40ab3c09..9b715b6d2f6 100644
--- a/addons/test_mail/tests/test_message_post.py
+++ b/addons/test_mail/tests/test_message_post.py
@@ -12,6 +12,7 @@ from odoo.api import call_kw
from odoo.exceptions import AccessError
from odoo.tests import tagged
from odoo.tools import mute_logger, formataddr
+from odoo.tests.common import users
@tagged('mail_post')
@@ -35,6 +36,43 @@ class TestMessagePost(TestMailCommon, TestRecipients):
messageId = call_kw(self.env['mail.channel'], 'message_post', [test_channel.id], {'body': 'test'})
self.assertTrue(isinstance(messageId, int))
+ @users('employee')
+ def test_notify_prepare_template_context_company_value(self):
+ """ Verify that the template context company value is right
+ after switching the env company or if a company_id is set
+ on mail record.
+ """
+ current_user = self.env.user
+ main_company = current_user.company_id
+ other_company = self.env['res.company'].with_user(self.user_admin).create({'name': 'Company B'})
+ current_user.sudo().write({'company_ids': [(4, other_company.id)]})
+ test_record = self.env['mail.test.multi.company'].with_user(self.user_admin).create({
+ 'name': 'Multi Company Record',
+ 'company_id': False,
+ })
+
+ # self.env.company.id = Main Company AND test_record.company_id = False
+ self.assertEqual(self.env.company.id, main_company.id)
+ self.assertEqual(test_record.company_id.id, False)
+ template_values = test_record._notify_prepare_template_context(test_record.message_ids, {})
+ self.assertEqual(template_values.get('company').id, self.env.company.id)
+
+ # self.env.company.id = Other Company AND test_record.company_id = False
+ current_user.company_id = other_company
+ test_record = self.env['mail.test.multi.company'].browse(test_record.id)
+ self.assertEqual(self.env.company.id, other_company.id)
+ self.assertEqual(test_record.company_id.id, False)
+ template_values = test_record._notify_prepare_template_context(test_record.message_ids, {})
+ self.assertEqual(template_values.get('company').id, self.env.company.id)
+
+ # self.env.company.id = Other Company AND test_record.company_id = Main Company
+ test_record.company_id = main_company
+ test_record = self.env['mail.test.multi.company'].browse(test_record.id)
+ self.assertEqual(self.env.company.id, other_company.id)
+ self.assertEqual(test_record.company_id.id, main_company.id)
+ template_values = test_record._notify_prepare_template_context(test_record.message_ids, {})
+ self.assertEqual(template_values.get('company').id, main_company.id)
+
def test_notify_recipients_internals(self):
pdata = self._generate_notify_recipients(self.partner_1 | self.partner_employee)
msg_vals = {