From a8fafbf02ea825a7161ae615fb9875450bc861bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Tue, 11 May 2021 14:14:00 +0000 Subject: [PATCH] [FIX] crm: correctly compute phone_sanitized without having crm_sms installed Currently phone_sanitized computation on lead model works only if crm_sms is installed. Indeed an override of ``_phone_get_number_fields`` is missing. However ``_sms_get_number_fields`` coming with ``crm_sms`` and its ``sms`` dependency hides the issue as those modules are auto-install. However if ``crm_sms`` is uninstalled phone_sanitized is not correctly computed anymore. Task ID-2528169 Oversight of odoo/odoo#45315 X-Original-Commit: odoo/odoo@45ae2922e5dc146e27060445137b22ae7f79453d --- addons/crm/models/crm_lead.py | 4 +++ addons/crm/tests/common.py | 10 +++---- addons/crm/tests/test_crm_lead.py | 27 +++++++++++++++-- addons/crm_sms/models/crm_lead.py | 1 + addons/crm_sms/tests/__init__.py | 3 ++ addons/crm_sms/tests/test_crm_lead.py | 29 +++++++++++++++++++ .../models/mailing_contact.py | 1 + addons/sms/models/res_partner.py | 1 + .../test_mail_full/models/test_mail_models.py | 1 + 9 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 addons/crm_sms/tests/__init__.py create mode 100644 addons/crm_sms/tests/test_crm_lead.py diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 442fb7ecfc8..5bb930937e6 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -1778,6 +1778,10 @@ class Lead(models.Model): break return result + def _phone_get_number_fields(self): + """ Use mobile or phone fields to compute sanitized phone number """ + return ['mobile', 'phone'] + @api.model def get_import_templates(self): return [{ diff --git a/addons/crm/tests/common.py b/addons/crm/tests/common.py index 2e299d7eb07..8f1f05c1bfc 100644 --- a/addons/crm/tests/common.py +++ b/addons/crm/tests/common.py @@ -152,12 +152,12 @@ class TestCrmCommon(TestSalesCommon, MailCase): 'philip.j.fry@test.example.com', 'turanga.leela@test.example.com', ] - cls.test_pĥone_data = [ + cls.test_phone_data = [ '+1 202 555 0122', # formatted US number '202 555 0999', # local US number '202 555 0888', # local US number ] - cls.test_pĥone_data_sanitized = [ + cls.test_phone_data_sanitized = [ '+12025550122', '+12025550999', '+12025550888', @@ -176,7 +176,7 @@ class TestCrmCommon(TestSalesCommon, MailCase): cls.contact_1 = cls.env['res.partner'].create({ 'name': 'Philip J Fry', 'email': cls.test_email_data[1], - 'mobile': cls.test_pĥone_data[0], + 'mobile': cls.test_phone_data[0], 'title': cls.env.ref('base.res_partner_title_mister').id, 'function': 'Delivery Boy', 'phone': False, @@ -190,8 +190,8 @@ class TestCrmCommon(TestSalesCommon, MailCase): cls.contact_2 = cls.env['res.partner'].create({ 'name': 'Turanga Leela', 'email': cls.test_email_data[2], - 'mobile': cls.test_pĥone_data[1], - 'phone': cls.test_pĥone_data[2], + 'mobile': cls.test_phone_data[1], + 'phone': cls.test_phone_data[2], 'parent_id': False, 'is_company': False, 'street': 'Cookieville Minimum-Security Orphanarium', diff --git a/addons/crm/tests/test_crm_lead.py b/addons/crm/tests/test_crm_lead.py index 16280da25b5..109ddefdd20 100644 --- a/addons/crm/tests/test_crm_lead.py +++ b/addons/crm/tests/test_crm_lead.py @@ -240,16 +240,16 @@ class TestCRMLead(TestCrmCommon): lead_form = Form(lead) # reset partner phone to a local number and prepare formatted / sanitized values - partner_phone, partner_mobile = self.test_pĥone_data[2], self.test_pĥone_data[1] + partner_phone, partner_mobile = self.test_phone_data[2], self.test_phone_data[1] partner_phone_formatted = phone_format(partner_phone, 'US', '1') partner_phone_sanitized = phone_format(partner_phone, 'US', '1', force_format='E164') partner_mobile_formatted = phone_format(partner_mobile, 'US', '1') partner_mobile_sanitized = phone_format(partner_mobile, 'US', '1', force_format='E164') partner_email, partner_email_normalized = self.test_email_data[2], self.test_email_data_normalized[2] self.assertEqual(partner_phone_formatted, '+1 202-555-0888') - self.assertEqual(partner_phone_sanitized, self.test_pĥone_data_sanitized[2]) + self.assertEqual(partner_phone_sanitized, self.test_phone_data_sanitized[2]) self.assertEqual(partner_mobile_formatted, '+1 202-555-0999') - self.assertEqual(partner_mobile_sanitized, self.test_pĥone_data_sanitized[1]) + self.assertEqual(partner_mobile_sanitized, self.test_phone_data_sanitized[1]) # ensure initial data self.assertEqual(partner.phone, partner_phone) self.assertEqual(partner.mobile, partner_mobile) @@ -516,3 +516,24 @@ class TestCRMLead(TestCrmCommon): self.assertEqual(leads, self.env['crm.lead'].search([ ('phone_mobile_search', 'like', '0499/223 311') ])) + + @users('user_sales_manager') + def test_phone_mobile_update(self): + lead = self.env['crm.lead'].create({ + 'name': 'Lead 1', + 'country_id': self.env.ref('base.us').id, + 'phone': self.test_phone_data[0], + }) + self.assertEqual(lead.phone, self.test_phone_data[0]) + self.assertFalse(lead.mobile) + self.assertEqual(lead.phone_sanitized, self.test_phone_data_sanitized[0]) + + lead.write({'phone': False, 'mobile': self.test_phone_data[1]}) + self.assertFalse(lead.phone) + self.assertEqual(lead.mobile, self.test_phone_data[1]) + self.assertEqual(lead.phone_sanitized, self.test_phone_data_sanitized[1]) + + lead.write({'phone': self.test_phone_data[1], 'mobile': self.test_phone_data[2]}) + self.assertEqual(lead.phone, self.test_phone_data[1]) + self.assertEqual(lead.mobile, self.test_phone_data[2]) + self.assertEqual(lead.phone_sanitized, self.test_phone_data_sanitized[2]) diff --git a/addons/crm_sms/models/crm_lead.py b/addons/crm_sms/models/crm_lead.py index 19e4379339e..76f7301cdfd 100644 --- a/addons/crm_sms/models/crm_lead.py +++ b/addons/crm_sms/models/crm_lead.py @@ -10,4 +10,5 @@ class CrmLead(models.Model): def _sms_get_number_fields(self): """ This method returns the fields to use to find the number to use to send an SMS on a record. """ + # TDE FIXME: to be cleaned in 14.4+ as it conflicts with _phone_get_number_fields return ['mobile', 'phone'] diff --git a/addons/crm_sms/tests/__init__.py b/addons/crm_sms/tests/__init__.py new file mode 100644 index 00000000000..e6f86243ac6 --- /dev/null +++ b/addons/crm_sms/tests/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import test_crm_lead diff --git a/addons/crm_sms/tests/test_crm_lead.py b/addons/crm_sms/tests/test_crm_lead.py new file mode 100644 index 00000000000..f5659da8301 --- /dev/null +++ b/addons/crm_sms/tests/test_crm_lead.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo.addons.crm.tests.common import TestCrmCommon +from odoo.tests.common import Form, users + + +class TestCRMLead(TestCrmCommon): + + @users('user_sales_manager') + def test_phone_mobile_update(self): + lead = self.env['crm.lead'].create({ + 'name': 'Lead 1', + 'country_id': self.env.ref('base.us').id, + 'phone': self.test_phone_data[0], + }) + self.assertEqual(lead.phone, self.test_phone_data[0]) + self.assertFalse(lead.mobile) + self.assertEqual(lead.phone_sanitized, self.test_phone_data_sanitized[0]) + + lead.write({'phone': False, 'mobile': self.test_phone_data[1]}) + self.assertFalse(lead.phone) + self.assertEqual(lead.mobile, self.test_phone_data[1]) + self.assertEqual(lead.phone_sanitized, self.test_phone_data_sanitized[1]) + + lead.write({'phone': self.test_phone_data[1], 'mobile': self.test_phone_data[2]}) + self.assertEqual(lead.phone, self.test_phone_data[1]) + self.assertEqual(lead.mobile, self.test_phone_data[2]) + self.assertEqual(lead.phone_sanitized, self.test_phone_data_sanitized[2]) diff --git a/addons/mass_mailing_sms/models/mailing_contact.py b/addons/mass_mailing_sms/models/mailing_contact.py index 68050b519d3..73a3431e156 100644 --- a/addons/mass_mailing_sms/models/mailing_contact.py +++ b/addons/mass_mailing_sms/models/mailing_contact.py @@ -11,4 +11,5 @@ class MailingContact(models.Model): mobile = fields.Char(string='Mobile') def _sms_get_number_fields(self): + # TDE note: should override _phone_get_number_fields but ok as sms is in dependencies return ['mobile'] diff --git a/addons/sms/models/res_partner.py b/addons/sms/models/res_partner.py index 987791da643..3159cb98575 100644 --- a/addons/sms/models/res_partner.py +++ b/addons/sms/models/res_partner.py @@ -17,4 +17,5 @@ class ResPartner(models.Model): def _sms_get_number_fields(self): """ This method returns the fields to use to find the number to use to send an SMS on a record. """ + # TDE note: should override _phone_get_number_fields but ok as sms override it return ['mobile', 'phone'] diff --git a/addons/test_mail_full/models/test_mail_models.py b/addons/test_mail_full/models/test_mail_models.py index 02f330d8f26..1d05eab7123 100644 --- a/addons/test_mail_full/models/test_mail_models.py +++ b/addons/test_mail_full/models/test_mail_models.py @@ -45,6 +45,7 @@ class MailTestSMSBL(models.Model): return ['customer_id'] def _sms_get_number_fields(self): + # TDE note: should override _phone_get_number_fields but ok as sms in dependencies return ['phone_nbr', 'mobile_nbr']