test_crm_lead_merge.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import base64
  4. from datetime import timedelta
  5. from odoo.addons.crm.tests.common import TestLeadConvertMassCommon
  6. from odoo.fields import Datetime
  7. from odoo.tests.common import tagged, users
  8. from odoo.tools import mute_logger
  9. class TestLeadMergeCommon(TestLeadConvertMassCommon):
  10. """ During a mixed merge (involving leads and opps), data should be handled a certain way following their type
  11. (m2o, m2m, text, ...). """
  12. @classmethod
  13. def setUpClass(cls):
  14. super(TestLeadMergeCommon, cls).setUpClass()
  15. cls.leads = cls.lead_1 + cls.lead_w_partner + cls.lead_w_contact + cls.lead_w_email + cls.lead_w_partner_company + cls.lead_w_email_lost
  16. # reset some assigned users to test salesmen assign
  17. (cls.lead_w_partner | cls.lead_w_email_lost).write({
  18. 'user_id': False,
  19. })
  20. cls.lead_w_partner.write({'stage_id': False})
  21. cls.lead_w_contact.write({'description': 'lead_w_contact'})
  22. cls.lead_w_email.write({'description': 'lead_w_email'})
  23. cls.lead_1.write({'description': 'lead_1'})
  24. cls.lead_w_partner.write({'description': 'lead_w_partner'})
  25. cls.assign_users = cls.user_sales_manager + cls.user_sales_leads_convert + cls.user_sales_salesman
  26. @tagged('lead_manage')
  27. class TestLeadMerge(TestLeadMergeCommon):
  28. def _run_merge_wizard(self, leads):
  29. res = self.env['crm.merge.opportunity'].with_context({
  30. 'active_model': 'crm.lead',
  31. 'active_ids': leads.ids,
  32. 'active_id': False,
  33. }).create({
  34. 'team_id': False,
  35. 'user_id': False,
  36. }).action_merge()
  37. return self.env['crm.lead'].browse(res['res_id'])
  38. def test_initial_data(self):
  39. """ Ensure initial data to avoid spaghetti test update afterwards
  40. Original order:
  41. lead_w_contact ----------lead---seq=3----proba=15
  42. lead_w_email ------------lead---seq=3----proba=15
  43. lead_1 ------------------lead---seq=1----proba=?
  44. lead_w_partner ----------lead---seq=False---proba=10
  45. lead_w_partner_company --lead---seq=False---proba=15
  46. """
  47. self.assertFalse(self.lead_1.date_conversion)
  48. self.assertEqual(self.lead_1.date_open, Datetime.from_string('2020-01-15 11:30:00'))
  49. self.assertEqual(self.lead_1.user_id, self.user_sales_leads)
  50. self.assertEqual(self.lead_1.team_id, self.sales_team_1)
  51. self.assertEqual(self.lead_1.stage_id, self.stage_team1_1)
  52. self.assertEqual(self.lead_w_partner.stage_id, self.env['crm.stage'])
  53. self.assertEqual(self.lead_w_partner.user_id, self.env['res.users'])
  54. self.assertEqual(self.lead_w_partner.team_id, self.sales_team_1)
  55. self.assertEqual(self.lead_w_partner_company.stage_id, self.stage_team1_1)
  56. self.assertEqual(self.lead_w_partner_company.user_id, self.user_sales_manager)
  57. self.assertEqual(self.lead_w_partner_company.team_id, self.sales_team_1)
  58. self.assertEqual(self.lead_w_contact.stage_id, self.stage_gen_1)
  59. self.assertEqual(self.lead_w_contact.user_id, self.user_sales_salesman)
  60. self.assertEqual(self.lead_w_contact.team_id, self.sales_team_convert)
  61. self.assertEqual(self.lead_w_email.stage_id, self.stage_gen_1)
  62. self.assertEqual(self.lead_w_email.user_id, self.user_sales_salesman)
  63. self.assertEqual(self.lead_w_email.team_id, self.sales_team_convert)
  64. self.assertEqual(self.lead_w_email_lost.stage_id, self.stage_team1_2)
  65. self.assertEqual(self.lead_w_email_lost.user_id, self.env['res.users'])
  66. self.assertEqual(self.lead_w_email_lost.team_id, self.sales_team_1)
  67. @users('user_sales_manager')
  68. @mute_logger('odoo.models.unlink')
  69. def test_lead_merge_address_not_propagated(self):
  70. """All addresses have the same number of non-empty address fields, take the first one (lead_w_contact)
  71. because it's the lead that has the best confidence level after being sorted with '_sort_by_confidence_level'"""
  72. initial_address = {
  73. 'street': 'Test street',
  74. 'street2': 'Test street2',
  75. 'city': 'Test City',
  76. 'zip': '5000',
  77. 'state_id': False,
  78. 'country_id': self.env.ref('base.be'),
  79. }
  80. self.lead_w_contact.write(initial_address)
  81. (self.leads - self.lead_w_contact).write({
  82. 'street': 'Other street',
  83. 'street2': 'Other street2',
  84. 'city': 'Other City',
  85. 'zip': '6666',
  86. 'state_id': self.env.ref('base.state_us_1'),
  87. 'country_id': False,
  88. })
  89. leads = self.env['crm.lead'].browse(self.leads.ids)._sort_by_confidence_level(reverse=True)
  90. with self.assertLeadMerged(self.lead_w_contact, leads, **initial_address):
  91. leads._merge_opportunity(auto_unlink=False, max_length=None)
  92. @users('user_sales_manager')
  93. @mute_logger('odoo.models.unlink')
  94. def test_lead_merge_address_propagated(self):
  95. """Test that the address with the most non-empty fields is propagated.
  96. Should take the address of "lead_w_partner" (maximum number of non-empty address
  97. fields and with an highest rank than "lead_w_email_lost")
  98. """
  99. self.leads.write({
  100. 'street': 'Original street',
  101. 'street2': False,
  102. 'city': False,
  103. 'zip': False,
  104. 'state_id': False,
  105. 'country_id': False,
  106. })
  107. new_address = {
  108. 'street': 'New street',
  109. 'street2': False,
  110. 'city': 'New City',
  111. 'zip': False,
  112. 'state_id': False,
  113. 'country_id': False,
  114. }
  115. self.lead_w_partner.write(new_address)
  116. self.lead_w_email_lost.write({
  117. 'street': 'Other street',
  118. 'street2': False,
  119. 'city': 'Other City',
  120. 'zip': False,
  121. 'state_id': False,
  122. 'country_id': False,
  123. })
  124. leads = self.env['crm.lead'].browse(self.leads.ids)._sort_by_confidence_level(reverse=True)
  125. with self.assertLeadMerged(self.lead_w_contact, leads, **new_address):
  126. leads._merge_opportunity(auto_unlink=False, max_length=None)
  127. @users('user_sales_manager')
  128. @mute_logger('odoo.models.unlink')
  129. def test_lead_merge_internals(self):
  130. """ Test internals of merge wizard. In this test leads are ordered as
  131. lead_w_contact --lead---seq=3---probability=25
  132. lead_w_email ----lead---seq=3---probability=15
  133. lead_1 ----------lead---seq=1
  134. lead_w_partner --lead---seq=False
  135. """
  136. # ensure initial data
  137. self.lead_w_partner_company.action_set_won() # won opps should be excluded
  138. merge = self.env['crm.merge.opportunity'].with_context({
  139. 'active_model': 'crm.lead',
  140. 'active_ids': self.leads.ids,
  141. 'active_id': False,
  142. }).create({
  143. 'user_id': self.user_sales_leads_convert.id,
  144. })
  145. self.assertEqual(merge.team_id, self.sales_team_convert)
  146. # TDE FIXME: not sure the browse in default get of wizard intended to exlude lost, as it browse ids
  147. # and exclude inactive leads, but that's not written anywhere ... intended ??
  148. self.assertEqual(merge.opportunity_ids, self.leads - self.lead_w_partner_company - self.lead_w_email_lost)
  149. ordered_merge = self.lead_w_contact + self.lead_w_email + self.lead_1 + self.lead_w_partner
  150. ordered_merge_description = '<br><br>'.join(l.description for l in ordered_merge)
  151. # merged opportunity: in this test, all input are leads. Confidence is based on stage
  152. # sequence -> lead_w_contact has a stage sequence of 3 and probability is greater
  153. result = merge.action_merge()
  154. merge_opportunity = self.env['crm.lead'].browse(result['res_id'])
  155. self.assertFalse((ordered_merge - merge_opportunity).exists())
  156. self.assertEqual(merge_opportunity, self.lead_w_contact)
  157. self.assertEqual(merge_opportunity.type, 'lead')
  158. self.assertEqual(merge_opportunity.description, ordered_merge_description)
  159. # merged opportunity has updated salesman / team / stage is ok as generic
  160. self.assertEqual(merge_opportunity.user_id, self.user_sales_leads_convert)
  161. self.assertEqual(merge_opportunity.team_id, self.sales_team_convert)
  162. self.assertEqual(merge_opportunity.stage_id, self.stage_gen_1)
  163. @users('user_sales_manager')
  164. @mute_logger('odoo.models.unlink')
  165. def test_lead_merge_mixed(self):
  166. """ In case of mix, opportunities are on top, and result is an opportunity
  167. lead_1 -------------------opp----seq=1---probability=60
  168. lead_w_partner_company ---opp----seq=1---probability=50
  169. lead_w_contact -----------lead---seq=3---probability=25
  170. lead_w_email -------------lead---seq=3---probability=15
  171. lead_w_partner -----------lead---seq=False
  172. """
  173. # ensure initial data
  174. (self.lead_w_partner_company | self.lead_1).write({'type': 'opportunity'})
  175. self.lead_1.write({'probability': 60})
  176. self.assertEqual(self.lead_w_partner_company.stage_id.sequence, 1)
  177. self.assertEqual(self.lead_1.stage_id.sequence, 1)
  178. merge = self.env['crm.merge.opportunity'].with_context({
  179. 'active_model': 'crm.lead',
  180. 'active_ids': self.leads.ids,
  181. 'active_id': False,
  182. }).create({
  183. 'team_id': self.sales_team_convert.id,
  184. 'user_id': False,
  185. })
  186. # TDE FIXME: see aa44700dccdc2618e0b8bc94252789264104047c -> no user, no team -> strange
  187. merge.write({'team_id': self.sales_team_convert.id})
  188. # TDE FIXME: not sure the browse in default get of wizard intended to exlude lost, as it browse ids
  189. # and exclude inactive leads, but that's not written anywhere ... intended ??
  190. self.assertEqual(merge.opportunity_ids, self.leads - self.lead_w_email_lost)
  191. ordered_merge = self.lead_w_partner_company + self.lead_w_contact + self.lead_w_email + self.lead_w_partner
  192. result = merge.action_merge()
  193. merge_opportunity = self.env['crm.lead'].browse(result['res_id'])
  194. self.assertFalse((ordered_merge - merge_opportunity).exists())
  195. self.assertEqual(merge_opportunity, self.lead_1)
  196. self.assertEqual(merge_opportunity.type, 'opportunity')
  197. # merged opportunity has same salesman (not updated in wizard)
  198. self.assertEqual(merge_opportunity.user_id, self.user_sales_leads)
  199. # TDE FIXME: as same uer_id is enforced, team is updated through onchange and therefore stage
  200. self.assertEqual(merge_opportunity.team_id, self.sales_team_convert)
  201. # self.assertEqual(merge_opportunity.team_id, self.sales_team_1)
  202. # TDE FIXME: BUT team_id is computed after checking stage, based on wizard's team_id
  203. self.assertEqual(merge_opportunity.stage_id, self.stage_team_convert_1)
  204. @users('user_sales_manager')
  205. @mute_logger('odoo.models.unlink')
  206. def test_lead_merge_probability_auto(self):
  207. """ Check master lead keeps its automated probability when merged. """
  208. self.lead_1.write({'type': 'opportunity', 'probability': self.lead_1.automated_probability})
  209. self.assertTrue(self.lead_1.is_automated_probability)
  210. leads = self.env['crm.lead'].browse((self.lead_1 + self.lead_w_partner + self.lead_w_partner_company).ids)
  211. merged_lead = self._run_merge_wizard(leads)
  212. self.assertEqual(merged_lead, self.lead_1)
  213. self.assertTrue(merged_lead.is_automated_probability, "lead with Auto proba should remain with auto probability")
  214. @users('user_sales_manager')
  215. @mute_logger('odoo.models.unlink')
  216. def test_lead_merge_probability_auto_empty(self):
  217. """ Check master lead keeps its automated probability when merged
  218. even if its probability is 0. """
  219. self.lead_1.write({'type': 'opportunity', 'probability': 0, 'automated_probability': 0})
  220. self.assertTrue(self.lead_1.is_automated_probability)
  221. leads = self.env['crm.lead'].browse((self.lead_1 + self.lead_w_partner + self.lead_w_partner_company).ids)
  222. merged_lead = self._run_merge_wizard(leads)
  223. self.assertEqual(merged_lead, self.lead_1)
  224. self.assertTrue(merged_lead.is_automated_probability, "lead with Auto proba should remain with auto probability")
  225. @users('user_sales_manager')
  226. @mute_logger('odoo.models.unlink')
  227. def test_lead_merge_probability_manual(self):
  228. """ Check master lead keeps its manual probability when merged. """
  229. self.lead_1.write({'probability': 60})
  230. self.assertFalse(self.lead_1.is_automated_probability)
  231. leads = self.env['crm.lead'].browse((self.lead_1 + self.lead_w_partner + self.lead_w_partner_company).ids)
  232. merged_lead = self._run_merge_wizard(leads)
  233. self.assertEqual(merged_lead, self.lead_1)
  234. self.assertEqual(merged_lead.probability, 60, "Manual Probability should remain the same after the merge")
  235. self.assertFalse(merged_lead.is_automated_probability)
  236. @users('user_sales_manager')
  237. @mute_logger('odoo.models.unlink')
  238. def test_lead_merge_probability_manual_empty(self):
  239. """ Check master lead keeps its manual probability when merged even if
  240. its probability is 0. """
  241. self.lead_1.write({'type': 'opportunity', 'probability': 0})
  242. leads = self.env['crm.lead'].browse((self.lead_1 + self.lead_w_partner + self.lead_w_partner_company).ids)
  243. merged_lead = self._run_merge_wizard(leads)
  244. self.assertEqual(merged_lead, self.lead_1)
  245. self.assertEqual(merged_lead.probability, 0, "Manual Probability should remain the same after the merge")
  246. self.assertFalse(merged_lead.is_automated_probability)
  247. @users('user_sales_manager')
  248. @mute_logger('odoo.models.unlink')
  249. def test_merge_method(self):
  250. """ In case of mix, opportunities are on top, and result is an opportunity
  251. lead_1 -------------------opp----seq=1---probability=50
  252. lead_w_partner_company ---opp----seq=1---probability=50 (ID greater)
  253. lead_w_contact -----------lead---seq=3
  254. lead_w_email -------------lead---seq=3
  255. lead_w_partner -----------lead---seq=False
  256. """
  257. # ensure initial data
  258. (self.lead_w_partner_company | self.lead_1).write({'type': 'opportunity', 'probability': 50})
  259. leads = self.env['crm.lead'].browse(self.leads.ids)._sort_by_confidence_level(reverse=True)
  260. # lead_w_partner is lost, check that the "lost_reason" is not propagated
  261. # because "lead_1" is not lost
  262. lost_reason = self.env['crm.lost.reason'].create({'name': 'Test Reason'})
  263. self.lead_w_partner.write({
  264. 'lost_reason_id': lost_reason,
  265. 'probability': 0,
  266. })
  267. all_tags = self.leads.mapped('tag_ids')
  268. with self.assertLeadMerged(self.lead_1, leads,
  269. name='Nibbler Spacecraft Request',
  270. partner_id=self.contact_company_1,
  271. priority='2',
  272. lost_reason_id=False,
  273. tag_ids=all_tags):
  274. leads._merge_opportunity(auto_unlink=False, max_length=None)
  275. @users('user_sales_manager')
  276. @mute_logger('odoo.models.unlink')
  277. def test_merge_method_propagate_lost_reason(self):
  278. """Check that the lost reason is propagated to the final lead if it's lost."""
  279. self.leads.write({
  280. 'probability': 0,
  281. 'automated_probability': 50, # Do not automatically update the probability
  282. })
  283. lost_reason = self.env['crm.lost.reason'].create({'name': 'Test Reason'})
  284. self.lead_w_partner.lost_reason_id = lost_reason
  285. leads = self.env['crm.lead'].browse(self.leads.ids)._sort_by_confidence_level(reverse=True)
  286. with self.assertLeadMerged(leads[0], leads, lost_reason_id=lost_reason):
  287. leads._merge_opportunity(auto_unlink=False, max_length=None)
  288. @users('user_sales_manager')
  289. def test_lead_merge_properties_formatting(self):
  290. lead = self.lead_1
  291. partners = self.env['res.partner'].create([{'name': 'Alice'}, {'name': 'Bob'}])
  292. lead.lead_properties = [{
  293. 'type': 'many2one',
  294. 'comodel': 'res.partner',
  295. 'name': 'test_many2one',
  296. 'string': 'My Partner',
  297. 'value': partners[0].id,
  298. 'definition_changed': True,
  299. }, {
  300. 'type': 'many2many',
  301. 'comodel': 'res.partner',
  302. 'name': 'test_many2many',
  303. 'string': 'My Partners',
  304. 'value': partners.ids,
  305. }, {
  306. 'type': 'selection',
  307. 'selection': [['a', 'A'], ['b', 'B']],
  308. 'name': 'test_selection',
  309. 'string': 'My Selection',
  310. 'value': 'a',
  311. }, {
  312. 'type': 'tags',
  313. 'tags': [['a', 'A', 1], ['b', 'B', 2], ['c', 'C', 3]],
  314. 'name': 'test_tags',
  315. 'string': 'My Tags',
  316. 'value': ['a', 'c'],
  317. }, {
  318. 'type': 'boolean',
  319. 'name': 'test_boolean',
  320. 'string': 'My Boolean',
  321. 'value': True,
  322. }, {
  323. 'type': 'integer',
  324. 'name': 'test_integer',
  325. 'string': 'My Integer',
  326. 'value': 1337,
  327. }, {
  328. 'type': 'datetime',
  329. 'name': 'test_datetime',
  330. 'string': 'My Datetime',
  331. 'value': '2022-02-21 16:11:42',
  332. }]
  333. expected = [{
  334. 'label': 'My Partner',
  335. 'value': 'Alice',
  336. }, {
  337. 'label': 'My Partners',
  338. 'values': [
  339. {'name': 'Alice'},
  340. {'name': 'Bob'},
  341. ],
  342. }, {
  343. 'label': 'My Selection',
  344. 'value': 'A',
  345. }, {
  346. 'label': 'My Tags',
  347. 'values': [
  348. {'name': 'A', 'color': 1},
  349. {'name': 'C', 'color': 3},
  350. ],
  351. }, {
  352. 'label': 'My Boolean',
  353. 'value': 'Yes',
  354. }, {
  355. 'label': 'My Integer',
  356. 'value': 1337,
  357. }, {
  358. 'label': 'My Datetime',
  359. # datetime are stored as string because they are not JSONifiable
  360. 'value': '2022-02-21 16:11:42',
  361. }]
  362. self.assertEqual(expected, lead._format_properties())
  363. # check the rendered template
  364. result = self.env['ir.qweb']._render(
  365. 'crm.crm_lead_merge_summary',
  366. {'opportunities': lead, 'is_html_empty': lambda x: True})
  367. self.assertIn("o_tag_color_1", result)
  368. self.assertIn("Alice", result)
  369. self.assertIn("Bob", result)
  370. @users('user_sales_manager')
  371. def test_merge_method_dependencies(self):
  372. """ Test if dependences for leads are not lost while merging leads. In
  373. this test leads are ordered as
  374. lead_w_partner_company ---opp----seq=1 (ID greater)
  375. lead_w_contact -----------lead---seq=3
  376. lead_w_email -------------lead---seq=3----------------attachments
  377. lead_1 -------------------lead---seq=1----------------activity+meeting
  378. lead_w_partner -----------lead---seq=False
  379. """
  380. self.env['crm.lead'].browse(self.lead_w_partner_company.ids).write({'type': 'opportunity'})
  381. # create side documents
  382. attachments = self.env['ir.attachment'].create([
  383. {'name': '%02d.txt' % idx,
  384. 'datas': base64.b64encode(b'Att%02d' % idx),
  385. 'res_model': 'crm.lead',
  386. 'res_id': self.lead_w_email.id,
  387. } for idx in range(4)
  388. ])
  389. lead_1 = self.env['crm.lead'].browse(self.lead_1.ids)
  390. activity = lead_1.activity_schedule('crm.lead_test_activity_1')
  391. calendar_event = self.env['calendar.event'].create({
  392. 'name': 'Meeting with partner',
  393. 'activity_ids': [(4, activity.id)],
  394. 'start': '2021-06-12 21:00:00',
  395. 'stop': '2021-06-13 00:00:00',
  396. 'res_model_id': self.env['ir.model']._get('crm.crm_lead').id,
  397. 'res_id': lead_1.id,
  398. 'opportunity_id': lead_1.id,
  399. })
  400. # run merge and check documents are moved to the master record
  401. merge = self.env['crm.merge.opportunity'].with_context({
  402. 'active_model': 'crm.lead',
  403. 'active_ids': self.leads.ids,
  404. 'active_id': False,
  405. }).create({
  406. 'team_id': self.sales_team_convert.id,
  407. 'user_id': False,
  408. })
  409. result = merge.action_merge()
  410. master_lead = self.leads.filtered(lambda lead: lead.id == result['res_id'])
  411. # check result of merge process
  412. self.assertEqual(master_lead, self.lead_w_partner_company)
  413. # records updated
  414. self.assertEqual(calendar_event.opportunity_id, master_lead)
  415. self.assertEqual(calendar_event.res_id, master_lead.id)
  416. self.assertTrue(all(att.res_id == master_lead.id for att in attachments))
  417. # 2many accessors updated
  418. self.assertEqual(master_lead.activity_ids, activity)
  419. self.assertEqual(master_lead.calendar_event_ids, calendar_event)
  420. @users('user_sales_manager')
  421. @mute_logger('odoo.models.unlink')
  422. def test_merge_method_followers(self):
  423. """ Test that the followers of the leads are added in the destination lead.
  424. They should be added if:
  425. - The related partner was active on the lead (posted a message in the last 30 days)
  426. - The related partner is not already following the destination lead
  427. Leads Followers Info
  428. ---------------------------------------------------------------------------------
  429. lead_w_contact contact_1 OK (destination lead)
  430. lead_w_email contact_1 KO (already following the destination lead)
  431. contact_2 OK (active on lead_w_email)
  432. contact_company KO (most recent message on lead_w_email is 35 days ago, message
  433. on lead_w_partner is not counted as they don't follow it)
  434. lead_w_partner contact_2 KO (already added with lead_w_email)
  435. lead_w_partner_company
  436. """
  437. self.leads.message_follower_ids.unlink()
  438. self.leads.message_ids.unlink()
  439. self.lead_w_contact.message_subscribe([self.contact_1.id])
  440. self.lead_w_email.message_subscribe([self.contact_1.id, self.contact_2.id, self.contact_company.id])
  441. self.lead_w_partner.message_subscribe([self.contact_2.id])
  442. self.env['mail.message'].create([{
  443. 'author_id': self.contact_1.id,
  444. 'model': 'crm.lead',
  445. 'res_id': self.lead_w_contact.id,
  446. 'date': Datetime.now() - timedelta(days=1),
  447. 'subtype_id': self.ref('mail.mt_comment'),
  448. 'reply_to': False,
  449. 'body': 'Test follower',
  450. }, {
  451. 'author_id': self.contact_1.id,
  452. 'model': 'crm.lead',
  453. 'res_id': self.lead_w_email.id,
  454. 'date': Datetime.now() - timedelta(days=20),
  455. 'subtype_id': self.ref('mail.mt_comment'),
  456. 'reply_to': False,
  457. 'body': 'Test follower',
  458. }, {
  459. 'author_id': self.contact_2.id,
  460. 'model': 'crm.lead',
  461. 'res_id': self.lead_w_email.id,
  462. 'date': Datetime.now() - timedelta(days=15),
  463. 'subtype_id': self.ref('mail.mt_comment'),
  464. 'reply_to': False,
  465. 'body': 'Test follower',
  466. }, {
  467. 'author_id': self.contact_2.id,
  468. 'model': 'crm.lead',
  469. 'res_id': self.lead_w_partner.id,
  470. 'date': Datetime.now() - timedelta(days=29),
  471. 'subtype_id': self.ref('mail.mt_comment'),
  472. 'reply_to': False,
  473. 'body': 'Test follower',
  474. }, {
  475. 'author_id': self.contact_company.id,
  476. 'model': 'crm.lead',
  477. 'res_id': self.lead_w_email.id,
  478. 'date': Datetime.now() - timedelta(days=35),
  479. 'subtype_id': self.ref('mail.mt_comment'),
  480. 'reply_to': False,
  481. 'body': 'Test follower',
  482. }, {
  483. 'author_id': self.contact_company.id,
  484. 'model': 'crm.lead',
  485. 'res_id': self.lead_w_partner.id,
  486. 'date': Datetime.now(),
  487. 'subtype_id': self.ref('mail.mt_comment'),
  488. 'reply_to': False,
  489. 'body': 'Test follower',
  490. }])
  491. initial_followers = self.lead_w_contact.message_follower_ids
  492. leads = self.env['crm.lead'].browse(self.leads.ids)._sort_by_confidence_level(reverse=True)
  493. master_lead = leads._merge_opportunity(max_length=None)
  494. self.assertEqual(master_lead, self.lead_w_contact)
  495. # Check followers of the destination lead
  496. new_partner_followers = (master_lead.message_follower_ids - initial_followers).partner_id
  497. self.assertIn(self.contact_2, new_partner_followers,
  498. 'The partner must follow the destination lead')
  499. # "contact_company" posted a message 35 days ago on lead_2, so it's considered as inactive
  500. # "contact_company" posted a message now on lead_3, but they don't follow lead_3
  501. # so this message is just ignored
  502. self.assertNotIn(self.contact_company, new_partner_followers,
  503. 'The partner was not active on the lead')
  504. self.assertIn(self.contact_1, master_lead.message_follower_ids.partner_id,
  505. 'Should not have removed follower of the destination lead')