diff --git a/addons/crm/tests/common.py b/addons/crm/tests/common.py new file mode 100644 index 00000000000..7156d1a0844 --- /dev/null +++ b/addons/crm/tests/common.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +from openerp.addons.mail.tests.common import TestMail + + +class TestCrm(TestMail): + + @classmethod + def setUpClass(cls): + super(TestCrm, cls).setUpClass() + + user_group_employee = cls.env.ref('base.group_user') + user_group_salesman_all = cls.env.ref('base.group_sale_salesman_all_leads') + + # Test users to use through the various tests + Users = cls.env['res.users'].with_context({'no_reset_password': True, 'mail_create_nosubscribe': True}) + cls.user_salesman_all = Users.create({ + 'name': 'Riton La Chignole', + 'login': 'riton', + 'alias_name': 'riton', + 'email': 'riton.salesman_all@example.com', + 'groups_id': [(6, 0, [user_group_employee.id, user_group_salesman_all.id])]}) + + cls.sales_team_1 = cls.env['crm.team'].create({ + 'name': 'Test Sales Team', + 'alias_name': 'test_sales_team', + }) diff --git a/addons/crm/tests/test_new_lead_notification.py b/addons/crm/tests/test_new_lead_notification.py index 8eefc00df19..36c28767acb 100644 --- a/addons/crm/tests/test_new_lead_notification.py +++ b/addons/crm/tests/test_new_lead_notification.py @@ -1,43 +1,37 @@ # -*- coding: utf-8 -*- -import unittest -from openerp.tests.common import TransactionCase -class NewLeadNotificationTest(TransactionCase): - @unittest.skip("Broken in 9.0. auto_subscribe does not handle channels") +from .common import TestCrm + + +class NewLeadNotification(TestCrm): + def test_new_lead_notification(self): - # Create a new user - groups = [ - self.env.ref('base.group_user').id, - self.env.ref('base.group_sale_salesman').id, - ] - user = self.env["res.users"].create({ - "name": __file__, - "login": __file__, - 'groups_id': [(6, 0, groups)], - }) - - chan = self.env['mail.channel'].create({ - 'name': 'Follow leads', - 'group_ids': [(6, 0, groups)], - }) - - # Subscribe him to sales department - team = self.env.ref("sales_team.team_sales_department") + """ Test newly create leads like from the website. People and channels + subscribed to the sales team shoud be notified. """ + # subscribe a partner and a channel to the sales team with new lead subtype subtype = self.env.ref("crm.mt_salesteam_lead") - team.message_subscribe(channel_ids=[chan.id], subtype_ids=[subtype.id]) + self.sales_team_1.message_subscribe(partner_ids=[self.user_salesman_all.partner_id.id], subtype_ids=[subtype.id]) + self.sales_team_1.message_subscribe(channel_ids=[self.group_pigs.id], subtype_ids=[subtype.id]) # Imitate what happens in the controller when somebody creates a new # lead from the website form - lead = (self.env["crm.lead"] - .with_context(mail_create_nosubscribe=True) - .sudo() - .create({"contact_name": "Somebody", - "description": "Some question", - "email_from": "somemail@example.com", - "name": "Some subject", - "partner_name": "Some company", - "team_id": team.id, - "phone": "+0000000000"})) + lead = self.env["crm.lead"].with_context(mail_create_nosubscribe=True).sudo().create({ + "contact_name": "Somebody", + "description": "Some question", + "email_from": "somemail@example.com", + "name": "Some subject", + "partner_name": "Some company", + "team_id": self.sales_team_1.id, + "phone": "+0000000000" + }) + # partner and channel should be auto subscribed + self.assertIn(self.user_salesman_all.partner_id, lead.message_partner_ids) + self.assertIn(self.group_pigs, lead.message_channel_ids) + + msg = lead.message_ids[0] + self.assertIn(self.user_salesman_all.partner_id, msg.needaction_partner_ids) + self.assertIn(self.group_pigs, msg.channel_ids) # The user should have a new unread message - self.assertTrue(lead.sudo(user).message_unread) + lead_user = lead.sudo(self.user_salesman_all) + self.assertTrue(lead_user.message_needaction) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 7aa0efbae31..e0ae94a07da 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -169,7 +169,7 @@ class MailThread(models.AbstractModel): for record in self: record.message_needaction_counter = res.get(record.id, 0) - record.message_needaction = bool(record.message_unread_counter) + record.message_needaction = bool(record.message_needaction_counter) @api.model def _search_message_needaction(self, operator, operand): @@ -242,13 +242,13 @@ class MailThread(models.AbstractModel): # Perform write result = super(MailThread, self).write(values) + # update followers + self.message_auto_subscribe(values.keys(), values=values) + # Perform the tracking if tracked_fields: track_self.message_track(tracked_fields, initial_values) - # update followers - self.message_auto_subscribe(values.keys(), values=values) - return result @api.multi @@ -1827,7 +1827,7 @@ class MailThread(models.AbstractModel): to get the values. Added after releasing 7.0, therefore not merged with updated_fields argumment. """ - new_followers = dict() + new_partners, new_channels = dict(), dict() # fetch auto_follow_fields: res.users relation fields whose changes are tracked for subscription user_field_lst = self._message_get_auto_subscribe_fields(updated_fields) @@ -1862,19 +1862,29 @@ class MailThread(models.AbstractModel): for header_follower in self.env['mail.followers'].sudo().search(header_domain): for subtype in header_follower.subtype_ids: if subtype.parent_id and subtype.parent_id.res_model == self._name: - new_followers.setdefault(header_follower.partner_id.id, set()).add(subtype.parent_id.id) + new_subtype = subtype.parent_id elif subtype.res_model is False: - new_followers.setdefault(header_follower.partner_id.id, set()).add(subtype.id) + new_subtype = subtype + else: + continue + if header_follower.partner_id: + new_partners.setdefault(header_follower.partner_id.id, set()).add(new_subtype.id) + else: + new_channels.setdefault(header_follower.channel_id.id, set()).add(new_subtype.id) # add followers coming from res.users relational fields that are tracked user_ids = [values[name] for name in user_field_lst if values.get(name)] user_pids = [user.partner_id.id for user in self.env['res.users'].sudo().browse(user_ids)] for partner_id in user_pids: - new_followers.setdefault(partner_id, None) + new_partners.setdefault(partner_id, None) - for pid, subtypes in new_followers.items(): + for pid, subtypes in new_partners.items(): subtypes = list(subtypes) if subtypes is not None else None - self.message_subscribe([pid], subtype_ids=subtypes) + self.message_subscribe(partner_ids=[pid], subtype_ids=subtypes) + for cid, subtypes in new_channels.items(): + subtypes = list(subtypes) if subtypes is not None else None + self.message_subscribe(channel_ids=[cid], subtype_ids=subtypes) + # remove the current user from the needaction partner to avoid to notify the author of the message user_pids = [user_pid for user_pid in user_pids if user_pid != self.env.user.partner_id.id] self._message_auto_subscribe_notify(user_pids)