diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index fec7751f873..c68205db283 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -442,7 +442,7 @@ class Lead(models.Model): 'partner_name': partner_name or self.partner_name, 'contact_name': False if partner.is_company else partner.name or self.contact_name, }) - return values + return self._convert_to_write(values) # ------------------------------------------------------------ # ORM diff --git a/addons/crm/tests/test_crm_lead.py b/addons/crm/tests/test_crm_lead.py index f275612be70..19c2746ea03 100644 --- a/addons/crm/tests/test_crm_lead.py +++ b/addons/crm/tests/test_crm_lead.py @@ -7,6 +7,34 @@ from odoo.tests.common import users class TestCRMLead(TestCrmCommon): + @classmethod + def setUpClass(cls): + super(TestCRMLead, cls).setUpClass() + cls.country_ref = cls.env.ref('base.be') + cls.test_email = '"Test Email" ' + cls.test_phone = '0485112233' + + @users('user_sales_leads') + def test_crm_lead_creation_no_partner(self): + lead_data = { + 'name': 'Test', + 'country_id': self.country_ref.id, + 'email_from': self.test_email, + 'phone': self.test_phone, + } + lead = self.env['crm.lead'].new(lead_data) + # get the street should not trigger cache miss + lead.street + # Create the lead and the write partner_id = False: country should remain + lead = self.env['crm.lead'].create(lead_data) + self.assertEqual(lead.country_id, self.country_ref, "Country should be set on the lead") + self.assertEqual(lead.email_from, self.test_email) + self.assertEqual(lead.phone, self.test_phone) + lead.partner_id = False + self.assertEqual(lead.country_id, self.country_ref, "Country should still be set on the lead") + self.assertEqual(lead.email_from, self.test_email) + self.assertEqual(lead.phone, self.test_phone) + @users('user_sales_manager') def test_crm_lead_creation_partner(self): lead = self.env['crm.lead'].create({ @@ -31,42 +59,23 @@ class TestCRMLead(TestCrmCommon): self.assertEqual(lead.zip, self.contact_1.zip) self.assertEqual(lead.country_id, self.contact_1.country_id) - def test_crm_lead_creation_no_partner(self): - belgium = self.env.ref('base.be') - lead_data = { - 'name': 'Test', - 'country_id': belgium.id, - 'email_from': 'test@odoo.com', - 'phone': '04584579646', - } - lead = self.env['crm.lead'].new(lead_data) - # get the street should not trigger cache miss - lead.street - # Create the lead and the write partner_id = False - # Country should remain - lead = self.env['crm.lead'].create(lead_data) - self.assertEqual(lead.country_id, belgium, "Country should be set on the lead") - lead.partner_id = False - self.assertEqual(lead.country_id, belgium, "Country should still be set on the lead") - def test_crm_lead_creation_partner_no_info(self): - belgium = self.env.ref('base.be') empty_partner = self.env['res.partner'].create({ 'name': 'Empty partner', 'is_company': True }) lead_data = { 'name': 'Test', - 'country_id': belgium.id, - 'email_from': 'test@odoo.com', - 'phone': '04584579646', + 'country_id': self.country_ref.id, + 'email_from': self.test_email, + 'phone': self.test_phone, } lead = self.env['crm.lead'].create(lead_data) lead.partner_id = empty_partner - self.assertEqual(bool(lead.country_id), False, "Country should be empty") + self.assertEqual(lead.country_id, self.env['res.country'], "Country should be empty") self.assertEqual(lead.contact_name, False, "Contact name should be empty") - self.assertEqual(lead.email_from, lead_data['email_from'], "Email From should keep its initial value") - self.assertEqual(lead.phone, lead_data['phone'], "Phone should keep its initial value") + self.assertEqual(lead.email_from, self.test_email, "Email From should keep its initial value") + self.assertEqual(lead.phone, self.test_phone, "Phone should keep its initial value") self.assertEqual(lead.partner_name, empty_partner.name, "Partner name should be set as contact is a company") @users('user_sales_manager') @@ -111,6 +120,40 @@ class TestCRMLead(TestCrmCommon): self.assertEqual(lead.probability, 100.0) self.assertEqual(lead.stage_id, self.stage_gen_won) # generic won stage has lower sequence than team won stage + @users('user_sales_leads') + def test_crm_lead_update_contact(self): + # ensure initial data, especially for corner cases + self.assertFalse(self.contact_company_1.phone) + self.assertEqual(self.contact_company_1.country_id.code, "US") + lead = self.env['crm.lead'].create({ + 'name': 'Test', + 'country_id': self.country_ref.id, + 'email_from': self.test_email, + 'phone': self.test_phone, + }) + self.assertEqual(lead.country_id, self.country_ref, "Country should be set on the lead") + lead.partner_id = False + self.assertEqual(lead.country_id, self.country_ref, "Country should still be set on the lead") + self.assertEqual(lead.email_from, self.test_email) + self.assertEqual(lead.phone, self.test_phone) + self.assertEqual(lead.email_state, 'correct') + self.assertEqual(lead.phone_state, 'correct') + + lead.partner_id = self.contact_company_1 + self.assertEqual(lead.country_id, self.contact_company_1.country_id, "Country should still be the one set on partner") + self.assertEqual(lead.email_from, self.contact_company_1.email) + self.assertEqual(lead.phone, self.test_phone) + self.assertEqual(lead.email_state, 'correct') + # currently we keep phone as partner as a void one -> may lead to inconsistencies + self.assertEqual(lead.phone_state, 'incorrect', "Belgian phone with US country -> considered as incorrect") + + lead.email_from = 'broken' + lead.phone = 'alsobroken' + self.assertEqual(lead.email_state, 'incorrect') + self.assertEqual(lead.phone_state, 'incorrect') + self.assertEqual(self.contact_company_1.email, 'broken') + self.assertEqual(self.contact_company_1.phone, 'alsobroken') + @users('user_sales_manager') def test_crm_team_alias(self): new_team = self.env['crm.team'].create({