Files
odoo_source/addons/website_crm/tests/test_website_visitor.py
T
Thibault Delavallée 52ac7f2ddb [FIX] website(_crm): fix email and phone computation of visitor
Currently there are several limitations in visitor email and phone
computation, notably phone of partner is not taken into account (only mobile)
and computation does not work in website_crm if there are no leads.

In this commit we

  * support both mobile and phone fields of partner. Indeed generally all
    SMS flows check for those two fields and not only for mobile;
  * replace the SQL by standard ORM computation. As those fields are not stored
    and displayed in list views ORM should be capable of managing them;
  * avoid crash when creating visitors through the UI (no ids -> don't go
    into raw SQL);
  * correctly keep partner-based values when no lead is linked to the visitor;

Tests are also added in order to ensure behavior.

Task ID 2196869

closes odoo/odoo#48369

X-original-commit: 02ceb42e204be2b40b334b4e554eef6dfaffd228
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
2020-03-25 15:51:25 +00:00

68 lines
2.7 KiB
Python

# -*- 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 users
class TestWebsiteVisitor(TestCrmCommon):
def setUp(self):
super(TestWebsiteVisitor, self).setUp()
self.test_partner = self.env['res.partner'].create({
'name': 'Test Customer',
'email': '"Test Customer" <test@test.example.com>',
'country_id': self.env.ref('base.be').id,
'mobile': '+32456001122'
})
@users('user_sales_manager')
def test_compute_email_phone(self):
visitor_sudo = self.env['website.visitor'].sudo().create({
'name': 'Mega Visitor',
})
visitor = visitor_sudo.with_user(self.env.user) # as of 13.0 salesmen cannot create visitors, only read them
customer = self.test_partner.with_user(self.env.user)
self.assertFalse(visitor.email)
self.assertFalse(visitor.mobile)
# partner information copied on visitor -> behaves like related
visitor_sudo.write({'partner_id': self.test_partner.id})
self.assertEqual(visitor.email, customer.email_normalized)
self.assertEqual(visitor.mobile, customer.mobile)
# if reset -> behaves like a related, also reset on visitor
visitor_sudo.write({'partner_id': False})
self.assertFalse(visitor.email)
self.assertFalse(visitor.mobile)
# first lead created -> updates email
lead_1 = self.env['crm.lead'].create({
'name': 'Test Lead 1',
'email_from': 'Rambeau Fort <beaufort@test.example.com',
'visitor_ids': [(4, visitor.id)],
})
self.assertEqual(visitor.email, lead_1.email_normalized)
self.assertFalse(visitor.mobile)
# second lead created -> keep first email but takes mobile as not defined before
lead_2 = self.env['crm.lead'].create({
'name': 'Test Lead 1',
'email_from': 'Martino Brie <brie@test.example.com',
'country_id': self.env.ref('base.be').id,
'mobile': '+32456001122',
'visitor_ids': [(4, visitor.id)],
})
self.assertEqual(visitor.email, lead_1.email_normalized)
self.assertEqual(visitor.mobile, lead_2.mobile)
# partner win on leads
visitor_sudo.write({'partner_id': self.test_partner.id})
self.assertEqual(visitor.email, customer.email_normalized)
self.assertEqual(visitor.mobile, customer.mobile)
# partner updated -> fallback on leads
customer.write({'mobile': False})
self.assertEqual(visitor.email, customer.email_normalized)
self.assertEqual(visitor.mobile, lead_2.mobile)