diff --git a/addons/mail/tests/common.py b/addons/mail/tests/common.py index 3912398d5c3..675779e19e9 100644 --- a/addons/mail/tests/common.py +++ b/addons/mail/tests/common.py @@ -19,7 +19,7 @@ from odoo.addons.mail.models.mail_mail import MailMail from odoo.addons.mail.models.mail_message import Message from odoo.addons.mail.models.mail_notification import MailNotification from odoo.tests import common, new_test_user -from odoo.tools import formataddr, pycompat +from odoo.tools import formataddr, mute_logger, pycompat from odoo.tools.translate import code_translations mail_new_test_user = partial(new_test_user, context={'mail_create_nolog': True, @@ -1102,9 +1102,9 @@ class MailCommon(common.TransactionCase, MailCase): * 'English Layout for' -> Spanish Layout para * model * description: English: Lang Chatter Model (depends on test_record._name) - translated: Spanish description + translated: Spanish Model Description * module - * _('TestStuff') -> TestSpanishStuff (used as link button name in layout) + * _('NotificationButtonTitle') -> SpanishNotificationButtonTitle (used as link button name in layout) * _('View %s') -> SpanishView %s * template * body: English:
EnglishBody for
Body for
EnglishBody for
Body for %s
' % record.name) + subject = composer._render_field('subject', source.ids)[source.id] + self.assertEqual(subject, f'EnglishSubject for {source.name}') + body = composer._render_field('body', source.ids)[source.id] + self.assertEqual(body, f'EnglishBody for {source.name}
') @users("employee") - def test_rendering(self): - record = self.env['mail.test.composer.mixin'].create({ - 'name': 'Invite', - 'subject': 'Subject for {{ object.name }}', - 'body': 'Content from
Description for
SpecificBody from
Content from
SpecificBody from
Content from %s
' % self.env.user.name) - description = record._render_field('description', record.ids)[record.id] - self.assertEqual(description, 'Description for Invite
') + subject = composer._render_field('subject', source.ids)[source.id] + self.assertEqual(subject, f'SpecificSubject for {source.name}') + body = composer._render_field('body', source.ids)[source.id] + self.assertEqual(body, f'SpecificBody from {self.env.user.name}
') + description = composer._render_field('description', source.ids)[source.id] + self.assertEqual(description, f'Description for {source.name}
') + + @users("employee") + def test_rendering_lang(self): + """ Test rendering with language involved """ + template = self.mail_template.with_env(self.env) + customer = self.partner_1.with_env(self.env) + customer.lang = 'es_ES' + source = self.test_record.with_env(self.env) + composer = self.env['mail.test.composer.mixin'].create({ + 'description': 'Description for
EnglishBody for {source.name}
') + description = composer._render_field('description', source.ids)[source.id] + self.assertEqual(description, f'Description for {source.name}
') + + # ask for dynamic language computation + subject = composer._render_field('subject', source.ids, compute_lang=True)[source.id] + self.assertEqual(subject, f'EnglishSubject for {source.name}', + 'Fixme: translations are not done, as taking composer translations and not template one') + body = composer._render_field('body', source.ids, compute_lang=True)[source.id] + self.assertEqual(body, f'EnglishBody for {source.name}
', + 'Fixme: translations are not done, as taking composer translations and not template one' + ) + description = composer._render_field('description', source.ids)[source.id] + self.assertEqual(description, f'Description for {source.name}
') diff --git a/addons/test_mail/tests/test_mail_template.py b/addons/test_mail/tests/test_mail_template.py index fb1bec1977e..678df090b6e 100644 --- a/addons/test_mail/tests/test_mail_template.py +++ b/addons/test_mail/tests/test_mail_template.py @@ -128,7 +128,7 @@ class TestMailTemplateLanguages(TestMailTemplateCommon): mail_id = test_template.send_mail(test_record.id, email_layout_xmlid='mail.test_layout') mail = self.env['mail.mail'].sudo().browse(mail_id) self.assertEqual(mail.body_html, - 'SpanishBody for %s
Spanish Layout para Spanish description' % self.test_record.name) + 'SpanishBody for %s
Spanish Layout para Spanish Model Description' % self.test_record.name) self.assertEqual(mail.subject, 'SpanishSubject for %s' % self.test_record.name) @mute_logger('odoo.addons.mail.models.mail_mail') @@ -147,5 +147,5 @@ class TestMailTemplateLanguages(TestMailTemplateCommon): mail_id = test_template.send_mail(test_record.id, email_layout_xmlid='mail.test_layout') mail = self.env['mail.mail'].sudo().browse(mail_id) self.assertEqual(mail.body_html, - 'SpanishBody for %s
Spanish Layout para Spanish description' % self.test_record.name) + 'SpanishBody for %s
Spanish Layout para Spanish Model Description' % self.test_record.name) self.assertEqual(mail.subject, 'SpanishSubject for %s' % self.test_record.name) diff --git a/addons/test_mail/tests/test_message_post.py b/addons/test_mail/tests/test_message_post.py index 7341c9fd44a..b23052cc450 100644 --- a/addons/test_mail/tests/test_message_post.py +++ b/addons/test_mail/tests/test_message_post.py @@ -1210,7 +1210,49 @@ class TestMessagePostLang(TestMailCommon, TestRecipients): @users('employee') @mute_logger('odoo.addons.mail.models.mail_mail') - def test_composer_lang_template(self): + def test_composer_lang_template_comment(self): + test_record = self.test_records[0].with_user(self.env.user) + test_template = self.test_template.with_user(self.env.user) + + with self.mock_mail_gateway(): + test_record.message_post_with_template( + test_template.id, + composition_mode='comment', + email_layout_xmlid='mail.test_layout', + message_type='comment', + subtype_id=self.env.ref('mail.mt_comment').id, + ) + + record0_customer = self.env['res.partner'].search([('email_normalized', '=', 'test.record.1@test.customer.com')], limit=1) + self.assertTrue(record0_customer, 'Template usage should have created a contact based on record email') + + customer_email = self._find_sent_mail_wemail(record0_customer.email_formatted) + self.assertTrue(customer_email) + body = customer_email['body'] + # check content + self.assertIn(f'SpanishBody for {test_record.name}', body, + 'Body based on template should be translated') + # check subject + self.assertEqual(f'SpanishSubject for {test_record.name}', customer_email['subject'], + 'Subject based on template should be translated') + # check notification layout content + self.assertIn('Spanish Layout para', body, 'Layout content should be translated') + self.assertNotIn('English Layout for', body) + self.assertIn('Spanish Layout para Spanish Model Description', body, 'Model name should be translated') + # check notification layout strings + self.assertIn('View Lang Chatter Model', body, + 'Fixme: "View document" should be translated') + # self.assertIn('SpanishView Spanish Model Description', body, + # '"View document" should be translated') + # self.assertNotIn(f'View {test_record._description}', body, + # '"View document" should be translated') + # self.assertIn('SpanishButtonTitle', body, + # 'Groups-based action names should be translated') + self.assertIn('NotificationButtonTitle', body, 'Fixme: Groups-based action names should be translated') + + @users('employee') + @mute_logger('odoo.addons.mail.models.mail_mail') + def test_composer_lang_template_mass(self): test_records = self.test_records.with_user(self.env.user) test_template = self.test_template.with_user(self.env.user) @@ -1231,11 +1273,15 @@ class TestMessagePostLang(TestMailCommon, TestRecipients): self.assertTrue(customer_email) body = customer_email['body'] # check content - # self.assertIn('SpanishBody for %s' % record.name, body, 'Body based on template should be translated') - self.assertIn('EnglishBody for %s' % record.name, body, 'Fixme: this should be translated') + # self.assertIn(f'SpanishBody for {record.name}', body, + # 'Body based on template should be translated') + self.assertIn(f'EnglishBody for {record.name}', body, + 'Fixme: Body based on template should be translated') # check subject - # self.assertEqual('SpanishSubject for %s' % record.name, customer_email['subject'], 'Subject based on template should be translated') - self.assertEqual('EnglishSubject for %s' % record.name, customer_email['subject'], 'Fixme: this should be translated') + # self.assertEqual(f'SpanishSubject for {record.name}', customer_email['subject'], + # 'Subject based on template should be translated') + self.assertEqual(f'EnglishSubject for {record.name}', customer_email['subject'], + 'Fixme: Subject based on template should be translated') @users('employee') @mute_logger('odoo.addons.mail.models.mail_mail') @@ -1255,16 +1301,20 @@ class TestMessagePostLang(TestMailCommon, TestRecipients): customer_email = self._find_sent_mail_wemail(self.partner_2.email_formatted) self.assertTrue(customer_email) body = customer_email['body'] - # check notification layout translation - self.assertIn('Spanish Layout para', body, 'Layout content should be translated') - self.assertNotIn('English Layout for', body) - self.assertIn('Spanish Layout para Spanish description', body, 'Model name should be translated') - self.assertIn('SpanishView Spanish description', body, '"View document" should be translated') - self.assertNotIn('View %s' % test_records[1]._description, body) - self.assertIn('TestSpanishStuff', body, 'Groups-based action names should be translated') - self.assertNotIn('TestStuff', body) # check content - self.assertIn('Hello', body, 'Body of posted message should be present') + self.assertIn('Hello
', body, 'Body of posted message should be present') + # check notification layout content + self.assertIn('Spanish Layout para', body, + 'Layout content should be translated') + self.assertNotIn('English Layout for', body) + self.assertIn('Spanish Layout para Spanish Model Description', body, + 'Model name should be translated') + # check notification layout strings + self.assertIn('SpanishView Spanish Model Description', body, + '"View document" should be translated') + self.assertNotIn(f'View {test_records[1]._description}', body) + self.assertIn('SpanishButtonTitle', body, 'Groups-based action names should be translated') + self.assertNotIn('NotificationButtonTitle', body) @users('employee') @mute_logger('odoo.addons.mail.models.mail_mail') @@ -1288,15 +1338,23 @@ class TestMessagePostLang(TestMailCommon, TestRecipients): customer_email = self._find_sent_mail_wemail(customer.email_formatted) self.assertTrue(customer_email) body = customer_email['body'] - # check notification layout translation - self.assertIn('Spanish Layout para', body, 'Layout content should be translated') - self.assertNotIn('English Layout for', body) - self.assertIn('Spanish Layout para Spanish description', body, 'Model name should be translated') - # self.assertIn('SpanishView Spanish description', body, '"View document" should be translated') - self.assertIn('View %s' % test_records[1]._description, body, 'Fixme: this should be translated') - # self.assertIn('TestSpanishStuff', body, 'Groups-based action names should be translated') - self.assertIn('TestStuff', body, 'Fixme: groups-based action names should be translated') # check content - self.assertIn('SpanishBody for %s' % record.name, body, 'Body based on template should be translated') + self.assertIn(f'SpanishBody for {record.name}', body, + 'Body based on template should be translated') # check subject - self.assertEqual('SpanishSubject for %s' % record.name, customer_email['subject'], 'Subject based on template should be translated') + self.assertEqual(f'SpanishSubject for {record.name}', customer_email['subject'], + 'Subject based on template should be translated') + # check notification layout translation + self.assertIn('Spanish Layout para', body, + 'Layout content should be translated') + self.assertNotIn('English Layout for', body) + self.assertIn('Spanish Layout para Spanish Model Description', body, + 'Model name should be translated') + # self.assertIn('SpanishView Spanish Model Description', body, + # '"View document" should be translated') + self.assertIn(f'View {test_records[1]._description}', body, + 'Fixme: "View document" should be translated') + # self.assertIn('NotificationButtonTitle', body, + # 'Groups-based action names should be translated') + self.assertIn('NotificationButtonTitle', body, + 'Fixme: groups-based action names should be translated')