[FIX] tests on mails

bzr revid: fp@openerp.com-20121001210653-ss4uqv7ij1mobuwc
This commit is contained in:
Fabien Pinckaers 2012-10-01 23:06:53 +02:00
parent 486055417d
commit ee5f1e57ff
3 changed files with 25 additions and 13 deletions

View File

@ -94,13 +94,16 @@ class mail_notification(osv.Model):
return self.write(cr, uid, notif_ids, {'read': read}, context=context)
def get_partners_to_notify(self, cr, uid, partner_ids, message, context=None):
def get_partners_to_notify(self, cr, uid, message, context=None):
""" Return the list of partners to notify, based on their preferences.
:param browse_record message: mail.message to notify
"""
notify_pids = []
for partner in self.pool.get('res.partner').browse(cr, SUPERUSER_ID, partner_ids, context=context):
for notification in message.notification_ids:
if notification.read:
continue
partner = notification.partner_id
# Do not send an email to the writer
if partner.user_ids and partner.user_ids[0].id == uid:
continue
@ -119,15 +122,15 @@ class mail_notification(osv.Model):
notify_pids.append(partner.id)
return notify_pids
def notify(self, cr, uid, partner_ids, msg_id, context=None):
def _notify(self, cr, uid, msg_id, context=None):
""" Send by email the notification depending on the user preferences """
context = context or {}
# mail_noemail (do not send email) or no partner_ids: do not send, return
if context.get('mail_noemail') or not partner_ids:
if context.get('mail_noemail'):
return True
msg = self.pool.get('mail.message').browse(cr, uid, msg_id, context=context)
notify_partner_ids = self.get_partners_to_notify(cr, uid, partner_ids, msg, context=context)
notify_partner_ids = self.get_partners_to_notify(cr, uid, msg, context=context)
if not notify_partner_ids:
return True

View File

@ -450,7 +450,7 @@ class mail_message(osv.Model):
if not values.get('message_id') and values.get('res_id') and values.get('model'):
values['message_id'] = tools.generate_tracking_message_id('%(model)s-%(res_id)s' % values)
newid = super(mail_message, self).create(cr, uid, values, context)
self.notify(cr, uid, newid, context=context)
self._notify(cr, 1, newid, context=context)
return newid
def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
@ -472,7 +472,7 @@ class mail_message(osv.Model):
self.pool.get('ir.attachment').unlink(cr, uid, attachments_to_delete, context=context)
return super(mail_message, self).unlink(cr, uid, ids, context=context)
def notify(self, cr, uid, newid, context=None):
def _notify(self, cr, uid, newid, context=None):
""" Add the related record followers to the destination partner_ids.
Call mail_notification.notify to manage the email sending
"""
@ -501,13 +501,10 @@ class mail_message(osv.Model):
# unless remove myself author
if ((message.model=="res.partner" and message.res_id==message.author_id.id)):
self.write(cr, SUPERUSER_ID, [newid], {'partner_ids': [(4, message.author_id.id)]}, context=context)
# add myself if this message have a parent message and I recive parent message
# ! subtype_id: pure log message => do this in read_message
# or (message.parent_id and self.pool.get('mail.notification').search(cr, uid, [('partner_id','=',message.author_id.id),('message_id','=',message.parent_id)])):
else:
self.write(cr, SUPERUSER_ID, [newid], {'partner_ids': [(3, message.author_id.id)]}, context=context)
self.pool.get('mail.notification').notify(cr, uid, list(partners_to_notify), newid, context=context)
self.pool.get('mail.notification')._notify(cr, uid, newid, context=context)
def copy(self, cr, uid, id, default=None, context=None):
"""Overridden to avoid duplicating fields that are unique to each email"""

View File

@ -135,6 +135,8 @@ class test_mail(TestMailMockups):
self.res_users = self.registry('res.users')
self.res_partner = self.registry('res.partner')
self.user_demo = self.registry('ir.model.data').get_object_reference(self.cr, self.uid, 'base', 'user_demo')[1]
# Mock send_get_mail_body to test its functionality without other addons override
self._send_get_mail_body = self.registry('mail.mail').send_get_mail_body
self.registry('mail.mail').send_get_mail_body = self._mock_send_get_mail_body
@ -634,6 +636,7 @@ class test_mail(TestMailMockups):
""" Tests for mail.message needaction. """
cr, uid = self.cr, self.uid
group_pigs = self.mail_group.browse(cr, uid, self.group_pigs_id)
group_pigs_demo = self.mail_group.browse(cr, self.user_demo, self.group_pigs_id)
user_admin = self.res_users.browse(cr, uid, uid)
# Demo values: check unread notification = needaction on mail.message
@ -644,9 +647,12 @@ class test_mail(TestMailMockups):
na_count = self.mail_message._needaction_count(cr, uid, domain=[])
self.assertEqual(len(notif_ids), na_count, 'unread notifications count does not match needaction count')
# Post 4 message on group_pigs
for dummy in range(4):
na_count1 = self.mail_message._needaction_count(cr, uid, domain=[('model', '=', 'mail.group'), ('res_id', '=', self.group_pigs_id)])
# Post 2 message on group_pigs as admin, 3 messages as demo user
for dummy in range(2):
group_pigs.message_post(body='My Body', subtype='mt_comment')
for dummy in range(3):
group_pigs_demo.message_post(body='My Demo Body', subtype='mt_comment')
# Check there are 4 new needaction on mail.message
notif_ids = self.mail_notification.search(cr, uid, [
@ -666,6 +672,12 @@ class test_mail(TestMailMockups):
])
self.assertEqual(len(notif_ids), na_count, 'posted message count does not match needaction count')
na_count3 = self.mail_message._needaction_count(cr, self.user_demo, domain=[('model', '=', 'mail.group'), ('res_id', '=', self.group_pigs_id)])
self.assertEqual(na_count3-na_count1, 0, 'demo has 0 message: not a follower and do not follow his own messages')
na_count2 = self.mail_message._needaction_count(cr, uid, domain=[('model', '=', 'mail.group'), ('res_id', '=', self.group_pigs_id)])
self.assertEqual(na_count2-na_count1, 3, 'admin has 3 messages: 0 from itself as they are marked as read, 3 from demo')
def test_50_thread_parent_resolution(self):
"""Verify parent/child relationships are correctly established when processing incoming mails"""
cr, uid = self.cr, self.uid