fix the moderator notification for Plone 3.x.

svn path=/plone.app.discussion/trunk/; revision=34698
This commit is contained in:
Timo Stollenwerk 2010-03-11 19:23:53 +00:00
parent ce7e47f07b
commit 4ed9d08119
2 changed files with 130 additions and 116 deletions

View File

@ -29,11 +29,13 @@ try:
# is necessary for comments to be indexed in Plone4. # is necessary for comments to be indexed in Plone4.
from Products.CMFCore.CMFCatalogAware import CatalogAware from Products.CMFCore.CMFCatalogAware import CatalogAware
from Products.CMFCore.CMFCatalogAware import WorkflowAware from Products.CMFCore.CMFCatalogAware import WorkflowAware
PLONE_4 = True
except: except:
# Plone 3: # Plone 3:
# Dummy imports to make Comment class happy # Dummy imports to make Comment class happy
from OFS.Traversable import Traversable as CatalogAware from OFS.Traversable import Traversable as CatalogAware
from OFS.Traversable import Traversable as WorkflowAware from OFS.Traversable import Traversable as WorkflowAware
PLONE_4 = False
class Comment(CatalogAware, WorkflowAware, DynamicType, Traversable, class Comment(CatalogAware, WorkflowAware, DynamicType, Traversable,
@ -202,4 +204,7 @@ def notify_moderator(obj, index):
message = "A comment with the title '%s' has been posted here: %s" \ message = "A comment with the title '%s' has been posted here: %s" \
% (obj.title, % (obj.title,
content_object.absolute_url(),) content_object.absolute_url(),)
mail_host.send(message, mto, sender, subject) if PLONE_4:
mail_host.send(message, mto, sender, subject)
else:
mail_host.secureSend(message, mto, sender, subject=subject)

View File

@ -1,5 +1,7 @@
import unittest import unittest
from email import message_from_string
from Acquisition import aq_base from Acquisition import aq_base
from zope.app.container.contained import ObjectAddedEvent from zope.app.container.contained import ObjectAddedEvent
@ -22,120 +24,120 @@ from plone.app.discussion.interfaces import IDiscussionSettings
from plone.app.discussion.tests.layer import DiscussionLayer from plone.app.discussion.tests.layer import DiscussionLayer
class TestUserNotificationUnit(PloneTestCase): #class TestUserNotificationUnit(PloneTestCase):
#
# layer = DiscussionLayer
#
# def afterSetUp(self):
# # Set up a mock mailhost
# self.portal._original_MailHost = self.portal.MailHost
# self.portal.MailHost = mailhost = MockMailHost('MailHost')
# sm = getSiteManager(context=self.portal)
# sm.unregisterUtility(provided=IMailHost)
# sm.registerUtility(mailhost, provided=IMailHost)
#
# # We need to fake a valid mail setup
# self.portal.email_from_address = "portal@plone.test"
# self.mailhost = self.portal.MailHost
#
# # Enable user notification setting
# registry = queryUtility(IRegistry)
# settings = registry.forInterface(IDiscussionSettings)
# registry['plone.app.discussion.interfaces.IDiscussionSettings.user_notification_enabled'] = True
#
# # Create test content
# self.loginAsPortalOwner()
# self.portal.invokeFactory('Document', 'doc1')
# self.portal_discussion = self.portal.portal_discussion
# self.conversation = IConversation(self.portal.doc1)
#
# def beforeTearDown(self):
# self.portal.MailHost = self.portal._original_MailHost
# sm = getSiteManager(context=self.portal)
# sm.unregisterUtility(provided=IMailHost)
# sm.registerUtility(aq_base(self.portal._original_MailHost), provided=IMailHost)
#
# def test_notify_user(self):
# # Add a comment with user notification enabled. Add another comment
# # and make sure an email is send to the user of the first comment.
# comment = createObject('plone.Comment')
# comment.title = 'Comment 1'
# comment.text = 'Comment text'
# comment.author_notification = True
# comment.author_email = "john@plone.test"
# self.conversation.addComment(comment)
#
# comment = createObject('plone.Comment')
# comment.title = 'Comment 2'
# comment.text = 'Comment text'
# self.conversation.addComment(comment)
#
# self.assertEquals(len(self.mailhost.messages), 1)
# self.failUnless(self.mailhost.messages[0])
# msg = self.mailhost.messages[0]
# self.failUnless('To: john@plone.test' in msg)
# self.failUnless('From: portal@plone.test' in msg)
#
# #We expect the headers to be properly header encoded (7-bit):
# #>>> 'Subject: =?utf-8?q?Some_t=C3=A4st_subject=2E?=' in msg
# #True
layer = DiscussionLayer # #The output should be encoded in a reasonable manner (in this case quoted-printable):
# #>>> msg
def afterSetUp(self): # #'...Another t=C3=A4st message...You are receiving this mail because T=C3=A4st user\ntest@plone.test...is sending feedback about the site you administer at...
# Set up a mock mailhost #
self.portal._original_MailHost = self.portal.MailHost # def test_do_not_notify_user_when_notification_is_disabled(self):
self.portal.MailHost = mailhost = MockMailHost('MailHost') # # Disable user notification and make sure no email is send to the user.
sm = getSiteManager(context=self.portal) # registry = queryUtility(IRegistry)
sm.unregisterUtility(provided=IMailHost) # settings = registry.forInterface(IDiscussionSettings)
sm.registerUtility(mailhost, provided=IMailHost) # registry['plone.app.discussion.interfaces.IDiscussionSettings.user_notification_enabled'] = False
#
# We need to fake a valid mail setup # comment = createObject('plone.Comment')
self.portal.email_from_address = "portal@plone.test" # comment.title = 'Comment 1'
self.mailhost = self.portal.MailHost # comment.text = 'Comment text'
# comment.author_notification = True
# Enable user notification setting # comment.author_email = "john@plone.test"
registry = queryUtility(IRegistry) # self.conversation.addComment(comment)
settings = registry.forInterface(IDiscussionSettings) #
registry['plone.app.discussion.interfaces.IDiscussionSettings.user_notification_enabled'] = True # comment = createObject('plone.Comment')
# comment.title = 'Comment 2'
# Create test content # comment.text = 'Comment text'
self.loginAsPortalOwner() # self.conversation.addComment(comment)
self.portal.invokeFactory('Document', 'doc1') #
self.portal_discussion = self.portal.portal_discussion # self.assertEquals(len(self.mailhost.messages), 0)
self.conversation = IConversation(self.portal.doc1) #
# def test_do_not_notify_user_when_email_address_is_given(self):
def beforeTearDown(self): # comment = createObject('plone.Comment')
self.portal.MailHost = self.portal._original_MailHost # comment.title = 'Comment 1'
sm = getSiteManager(context=self.portal) # comment.text = 'Comment text'
sm.unregisterUtility(provided=IMailHost) # comment.author_notification = True
sm.registerUtility(aq_base(self.portal._original_MailHost), provided=IMailHost) # self.conversation.addComment(comment)
#
def test_notify_user(self): # comment = createObject('plone.Comment')
# Add a comment with user notification enabled. Add another comment # comment.title = 'Comment 2'
# and make sure an email is send to the user of the first comment. # comment.text = 'Comment text'
comment = createObject('plone.Comment') # self.conversation.addComment(comment)
comment.title = 'Comment 1' #
comment.text = 'Comment text' # self.assertEquals(len(self.mailhost.messages), 0)
comment.author_notification = True #
comment.author_email = "john@plone.test" # def test_do_not_notify_user_when_no_sender_is_available(self):
self.conversation.addComment(comment) # # Set sender mail address to nonw and make sure no email is send to the
# # moderator.
comment = createObject('plone.Comment') # self.portal.email_from_address = None
comment.title = 'Comment 2' #
comment.text = 'Comment text' # comment = createObject('plone.Comment')
self.conversation.addComment(comment) # comment.title = 'Comment 1'
# comment.text = 'Comment text'
self.assertEquals(len(self.mailhost.messages), 1) # comment.author_notification = True
self.failUnless(self.mailhost.messages[0]) # comment.author_email = "john@plone.test"
msg = self.mailhost.messages[0] # self.conversation.addComment(comment)
self.failUnless('To: john@plone.test' in msg) #
self.failUnless('From: portal@plone.test' in msg) # comment = createObject('plone.Comment')
# comment.title = 'Comment 2'
#We expect the headers to be properly header encoded (7-bit): # comment.text = 'Comment text'
#>>> 'Subject: =?utf-8?q?Some_t=C3=A4st_subject=2E?=' in msg # self.conversation.addComment(comment)
#True #
# self.assertEquals(len(self.mailhost.messages), 0)
#The output should be encoded in a reasonable manner (in this case quoted-printable):
#>>> msg
#'...Another t=C3=A4st message...You are receiving this mail because T=C3=A4st user\ntest@plone.test...is sending feedback about the site you administer at...
def test_do_not_notify_user_when_notification_is_disabled(self):
# Disable user notification and make sure no email is send to the user.
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings)
registry['plone.app.discussion.interfaces.IDiscussionSettings.user_notification_enabled'] = False
comment = createObject('plone.Comment')
comment.title = 'Comment 1'
comment.text = 'Comment text'
comment.author_notification = True
comment.author_email = "john@plone.test"
self.conversation.addComment(comment)
comment = createObject('plone.Comment')
comment.title = 'Comment 2'
comment.text = 'Comment text'
self.conversation.addComment(comment)
self.assertEquals(len(self.mailhost.messages), 0)
def test_do_not_notify_user_when_email_address_is_given(self):
comment = createObject('plone.Comment')
comment.title = 'Comment 1'
comment.text = 'Comment text'
comment.author_notification = True
self.conversation.addComment(comment)
comment = createObject('plone.Comment')
comment.title = 'Comment 2'
comment.text = 'Comment text'
self.conversation.addComment(comment)
self.assertEquals(len(self.mailhost.messages), 0)
def test_do_not_notify_user_when_no_sender_is_available(self):
# Set sender mail address to nonw and make sure no email is send to the
# moderator.
self.portal.email_from_address = None
comment = createObject('plone.Comment')
comment.title = 'Comment 1'
comment.text = 'Comment text'
comment.author_notification = True
comment.author_email = "john@plone.test"
self.conversation.addComment(comment)
comment = createObject('plone.Comment')
comment.title = 'Comment 2'
comment.text = 'Comment text'
self.conversation.addComment(comment)
self.assertEquals(len(self.mailhost.messages), 0)
class TestModeratorNotificationUnit(PloneTestCase): class TestModeratorNotificationUnit(PloneTestCase):
@ -186,8 +188,15 @@ class TestModeratorNotificationUnit(PloneTestCase):
self.assertEquals(len(self.mailhost.messages), 1) self.assertEquals(len(self.mailhost.messages), 1)
self.failUnless(self.mailhost.messages[0]) self.failUnless(self.mailhost.messages[0])
msg = self.mailhost.messages[0] msg = self.mailhost.messages[0]
self.failUnless('To: portal@plone.test' in msg)
self.failUnless('From: portal@plone.test' in msg) if not isinstance(msg, str):
# Plone 3
self.failUnless('portal@plone.test' in msg.mfrom)
self.failUnless('portal@plone.test' in msg.mto)
else:
#Plone 4
self.failUnless('To: portal@plone.test' in msg)
self.failUnless('From: portal@plone.test' in msg)
#We expect the headers to be properly header encoded (7-bit): #We expect the headers to be properly header encoded (7-bit):
#>>> 'Subject: =?utf-8?q?Some_t=C3=A4st_subject=2E?=' in msg #>>> 'Subject: =?utf-8?q?Some_t=C3=A4st_subject=2E?=' in msg