2011-03-09 04:07:29 +01:00
|
|
|
# coding=utf-8
|
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from Acquisition import aq_base
|
|
|
|
|
|
|
|
from zope.component import createObject
|
|
|
|
from zope.component import getSiteManager
|
2010-07-12 15:47:53 +02:00
|
|
|
from zope.component import queryUtility
|
2010-02-11 22:05:28 +01:00
|
|
|
|
|
|
|
from Products.PloneTestCase.ptc import PloneTestCase
|
|
|
|
|
|
|
|
from Products.MailHost.interfaces import IMailHost
|
|
|
|
from Products.CMFPlone.tests.utils import MockMailHost
|
|
|
|
|
|
|
|
from plone.registry.interfaces import IRegistry
|
|
|
|
|
2010-07-13 12:45:53 +02:00
|
|
|
from plone.app.discussion.interfaces import IConversation
|
2010-10-30 17:02:05 +02:00
|
|
|
from plone.app.discussion.interfaces import IDiscussionSettings
|
2010-02-11 22:05:28 +01:00
|
|
|
from plone.app.discussion.tests.layer import DiscussionLayer
|
|
|
|
|
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
class TestUserNotificationUnit(PloneTestCase):
|
2010-03-11 20:23:53 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
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
|
2011-03-09 04:07:46 +01:00
|
|
|
# Archetypes content types store data as utf-8 encoded strings
|
|
|
|
# The missing u in front of a string is therefor not missing
|
|
|
|
self.portal.doc1.title = 'Kölle Alaaf' # What is "Fasching"?
|
2010-10-30 17:02:05 +02:00
|
|
|
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)
|
2010-12-16 00:52:56 +01:00
|
|
|
sm.registerUtility(aq_base(self.portal._original_MailHost),
|
2010-10-30 17:02:05 +02:00
|
|
|
provided=IMailHost)
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
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.text = 'Comment text'
|
|
|
|
comment.user_notification = True
|
|
|
|
comment.author_email = "john@plone.test"
|
|
|
|
self.conversation.addComment(comment)
|
|
|
|
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
self.conversation.addComment(comment)
|
|
|
|
self.assertEquals(len(self.mailhost.messages), 1)
|
|
|
|
self.failUnless(self.mailhost.messages[0])
|
2010-10-31 11:48:45 +01:00
|
|
|
msg = str(self.mailhost.messages[0])
|
2010-10-30 17:02:05 +02:00
|
|
|
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
|
2010-12-16 00:52:56 +01:00
|
|
|
# # The output should be encoded in a reasonable manner
|
2010-10-30 17:02:05 +02:00
|
|
|
# (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.text = 'Comment text'
|
|
|
|
comment.user_notification = True
|
|
|
|
comment.author_email = "john@plone.test"
|
|
|
|
self.conversation.addComment(comment)
|
|
|
|
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
self.conversation.addComment(comment)
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
self.assertEquals(len(self.mailhost.messages), 0)
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
def test_do_not_notify_user_when_email_address_is_given(self):
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
comment.user_notification = True
|
|
|
|
self.conversation.addComment(comment)
|
|
|
|
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
self.conversation.addComment(comment)
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
self.assertEquals(len(self.mailhost.messages), 0)
|
|
|
|
|
|
|
|
def test_do_not_notify_user_when_no_sender_is_available(self):
|
|
|
|
# Set sender mail address to none and make sure no email is send to
|
|
|
|
# the moderator.
|
|
|
|
self.portal.email_from_address = None
|
|
|
|
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
comment.user_notification = True
|
|
|
|
comment.author_email = "john@plone.test"
|
|
|
|
self.conversation.addComment(comment)
|
|
|
|
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
self.conversation.addComment(comment)
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
self.assertEquals(len(self.mailhost.messages), 0)
|
|
|
|
|
2010-12-16 00:41:57 +01:00
|
|
|
def test_notify_only_once(self):
|
|
|
|
# When a user has added two comments in a conversation and has
|
|
|
|
# both times requested email notification, do not send him two
|
|
|
|
# emails when another comment has been added.
|
|
|
|
|
|
|
|
# Comment 1
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
comment.user_notification = True
|
|
|
|
comment.author_email = "john@plone.test"
|
|
|
|
self.conversation.addComment(comment)
|
|
|
|
|
|
|
|
# Comment 2
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
comment.user_notification = True
|
|
|
|
comment.author_email = "john@plone.test"
|
|
|
|
self.conversation.addComment(comment)
|
|
|
|
# Note that we might want to get rid of this message, as the
|
|
|
|
# new comment is added by the same user.
|
|
|
|
self.assertEquals(len(self.mailhost.messages), 1)
|
|
|
|
self.mailhost.reset()
|
|
|
|
self.assertEquals(len(self.mailhost.messages), 0)
|
|
|
|
|
|
|
|
# Comment 3
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
self.conversation.addComment(comment)
|
|
|
|
self.assertEquals(len(self.mailhost.messages), 1)
|
|
|
|
self.failUnless(self.mailhost.messages[0])
|
|
|
|
msg = str(self.mailhost.messages[0])
|
|
|
|
self.failUnless('To: john@plone.test' in msg)
|
|
|
|
self.failUnless('From: portal@plone.test' in msg)
|
|
|
|
|
2010-02-13 22:31:17 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
class TestModeratorNotificationUnit(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
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
# Enable comment moderation
|
|
|
|
self.portal.portal_types['Document'].allow_discussion = True
|
|
|
|
self.portal.portal_workflow.setChainForPortalTypes(
|
|
|
|
('Discussion Item',),
|
|
|
|
('comment_review_workflow',))
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
# Enable moderator notification setting
|
2010-07-12 15:47:53 +02:00
|
|
|
registry = queryUtility(IRegistry)
|
2010-08-31 19:28:07 +02:00
|
|
|
registry['plone.app.discussion.interfaces.IDiscussionSettings.' +
|
|
|
|
'moderator_notification_enabled'] = True
|
2010-02-11 22:05:28 +01:00
|
|
|
|
2010-02-13 22:31:17 +01:00
|
|
|
# Create test content
|
2010-02-11 22:05:28 +01:00
|
|
|
self.loginAsPortalOwner()
|
|
|
|
self.portal.invokeFactory('Document', 'doc1')
|
|
|
|
self.portal_discussion = self.portal.portal_discussion
|
2011-03-09 04:07:29 +01:00
|
|
|
# Archetypes content types store data as utf-8 encoded strings
|
|
|
|
# The missing u in front of a string is therefor not missing
|
|
|
|
self.portal.doc1.title = 'Kölle Alaaf' # What is "Fasching"?
|
2010-02-11 22:05:28 +01:00
|
|
|
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)
|
2010-12-16 00:52:56 +01:00
|
|
|
sm.registerUtility(aq_base(self.portal._original_MailHost),
|
2010-08-31 19:28:07 +02:00
|
|
|
provided=IMailHost)
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
def test_notify_moderator(self):
|
|
|
|
# Add a comment and make sure an email is send to the moderator.
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
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]
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-03-11 20:23:53 +01:00
|
|
|
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)
|
2010-12-16 00:52:56 +01:00
|
|
|
self.failUnless('From: portal@plone.test' in msg)
|
2010-02-11 22:05:28 +01:00
|
|
|
|
|
|
|
#We expect the headers to be properly header encoded (7-bit):
|
|
|
|
#>>> 'Subject: =?utf-8?q?Some_t=C3=A4st_subject=2E?=' in msg
|
|
|
|
#True
|
|
|
|
|
2010-12-16 00:52:56 +01:00
|
|
|
#The output should be encoded in a reasonable manner (in this case
|
2010-08-31 19:28:07 +02:00
|
|
|
# quoted-printable):
|
2010-02-11 22:05:28 +01:00
|
|
|
#>>> msg
|
2010-12-16 00:52:56 +01:00
|
|
|
#'...Another t=C3=A4st message...You are receiving this mail because
|
|
|
|
# T=C3=A4st user\ntest@plone.test...is sending feedback about the site
|
2010-08-31 19:28:07 +02:00
|
|
|
# you administer at...
|
2010-02-11 22:05:28 +01:00
|
|
|
|
|
|
|
def test_do_not_notify_moderator_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.text = 'Comment text'
|
|
|
|
self.conversation.addComment(comment)
|
|
|
|
self.assertEquals(len(self.mailhost.messages), 0)
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
def test_do_not_notify_moderator_when_notification_is_disabled(self):
|
2010-12-16 00:52:56 +01:00
|
|
|
# Disable moderator notification setting and make sure no email is send
|
2010-02-11 22:05:28 +01:00
|
|
|
# to the moderator.
|
2010-07-12 15:47:53 +02:00
|
|
|
registry = queryUtility(IRegistry)
|
2010-08-31 19:28:07 +02:00
|
|
|
registry['plone.app.discussion.interfaces.IDiscussionSettings.' +
|
|
|
|
'moderator_notification_enabled'] = False
|
2010-02-11 22:05:28 +01:00
|
|
|
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
self.conversation.addComment(comment)
|
|
|
|
self.assertEquals(len(self.mailhost.messages), 0)
|
|
|
|
|
|
|
|
def test_suite():
|
|
|
|
return unittest.defaultTestLoader.loadTestsFromName(__name__)
|