2014-05-12 19:20:18 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-05-03 08:16:39 +02:00
|
|
|
from plone.app.discussion.interfaces import IConversation
|
|
|
|
from plone.app.discussion.interfaces import IReplies
|
2015-05-03 08:41:56 +02:00
|
|
|
from plone.app.discussion.testing import PLONE_APP_DISCUSSION_INTEGRATION_TESTING # noqa
|
2015-05-03 08:16:39 +02:00
|
|
|
from plone.app.testing import setRoles
|
|
|
|
from plone.app.testing import TEST_USER_ID
|
|
|
|
from zope.component import createObject
|
2016-02-05 01:39:53 +01:00
|
|
|
from Zope2.App import zcml
|
2015-05-03 08:16:39 +02:00
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
import Products.Five
|
2017-05-08 09:24:38 +02:00
|
|
|
import unittest
|
2015-05-03 08:16:39 +02:00
|
|
|
|
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
#
|
|
|
|
# Fake events registry
|
|
|
|
#
|
2015-05-03 08:16:39 +02:00
|
|
|
|
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
class EventsRegistry(object):
|
|
|
|
""" Fake registry to be used while testing discussion events
|
|
|
|
"""
|
|
|
|
commentAdded = False
|
|
|
|
commentRemoved = False
|
|
|
|
replyAdded = False
|
|
|
|
replyRemoved = False
|
|
|
|
|
|
|
|
#
|
|
|
|
# Fake event handlers
|
|
|
|
#
|
2015-05-03 08:22:51 +02:00
|
|
|
|
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
def comment_added(doc, evt):
|
|
|
|
EventsRegistry.commentAdded = True
|
|
|
|
|
2015-05-03 08:22:51 +02:00
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
def comment_removed(doc, evt):
|
|
|
|
EventsRegistry.commentRemoved = True
|
|
|
|
|
2015-05-03 08:22:51 +02:00
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
def reply_added(doc, evt):
|
|
|
|
EventsRegistry.replyAdded = True
|
|
|
|
|
2015-05-03 08:22:51 +02:00
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
def reply_removed(doc, evt):
|
|
|
|
EventsRegistry.replyRemoved = True
|
2015-05-03 08:22:51 +02:00
|
|
|
|
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
#
|
|
|
|
# Tests
|
|
|
|
#
|
2015-05-03 08:22:51 +02:00
|
|
|
|
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
class CommentEventsTest(unittest.TestCase):
|
|
|
|
""" Test custom comments events
|
|
|
|
"""
|
|
|
|
layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
|
|
|
# Setup sandbox
|
|
|
|
self.portal = self.layer['portal']
|
|
|
|
self.request = self.layer['request']
|
|
|
|
self.registry = EventsRegistry
|
|
|
|
|
|
|
|
setRoles(self.portal, TEST_USER_ID, ['Manager'])
|
2015-03-10 05:51:45 +01:00
|
|
|
self.document = self.portal['doc1']
|
2014-05-12 19:20:18 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
# Subscribers
|
|
|
|
#
|
|
|
|
configure = """
|
|
|
|
<configure
|
|
|
|
xmlns="http://namespaces.zope.org/zope">
|
|
|
|
|
|
|
|
<subscriber
|
|
|
|
for="OFS.interfaces.ISimpleItem
|
|
|
|
plone.app.discussion.interfaces.ICommentAddedEvent"
|
|
|
|
handler="plone.app.discussion.tests.test_events.comment_added"
|
|
|
|
/>
|
|
|
|
|
|
|
|
<subscriber
|
|
|
|
for="OFS.interfaces.ISimpleItem
|
|
|
|
plone.app.discussion.interfaces.ICommentRemovedEvent"
|
|
|
|
handler="plone.app.discussion.tests.test_events.comment_removed"
|
|
|
|
/>
|
|
|
|
|
|
|
|
</configure>
|
|
|
|
"""
|
2016-02-05 01:39:53 +01:00
|
|
|
zcml.load_config('configure.zcml', Products.Five)
|
2014-05-12 19:20:18 +02:00
|
|
|
zcml.load_string(configure)
|
|
|
|
|
|
|
|
def test_addEvent(self):
|
|
|
|
self.assertFalse(self.registry.commentAdded)
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
conversation = IConversation(self.document)
|
|
|
|
conversation.addComment(comment)
|
|
|
|
self.assertTrue(self.registry.commentAdded)
|
|
|
|
|
|
|
|
def test_removedEvent(self):
|
|
|
|
self.assertFalse(self.registry.commentRemoved)
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
conversation = IConversation(self.document)
|
|
|
|
cid = conversation.addComment(comment)
|
|
|
|
del conversation[cid]
|
|
|
|
self.assertTrue(self.registry.commentRemoved)
|
|
|
|
|
2015-05-03 08:43:22 +02:00
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
class RepliesEventsTest(unittest.TestCase):
|
|
|
|
""" Test custom replies events
|
|
|
|
"""
|
|
|
|
layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.portal = self.layer['portal']
|
|
|
|
self.request = self.layer['request']
|
|
|
|
self.registry = EventsRegistry
|
|
|
|
|
|
|
|
setRoles(self.portal, TEST_USER_ID, ['Manager'])
|
2015-03-10 05:51:45 +01:00
|
|
|
self.document = self.portal['doc1']
|
2014-05-12 19:20:18 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
# Subscribers
|
|
|
|
#
|
|
|
|
configure = """
|
|
|
|
<configure
|
|
|
|
xmlns="http://namespaces.zope.org/zope">
|
|
|
|
|
|
|
|
<subscriber
|
|
|
|
for="OFS.interfaces.ISimpleItem
|
|
|
|
plone.app.discussion.interfaces.IReplyAddedEvent"
|
|
|
|
handler="plone.app.discussion.tests.test_events.reply_added"
|
|
|
|
/>
|
|
|
|
|
|
|
|
<subscriber
|
|
|
|
for="OFS.interfaces.ISimpleItem
|
|
|
|
plone.app.discussion.interfaces.IReplyRemovedEvent"
|
|
|
|
handler="plone.app.discussion.tests.test_events.reply_removed"
|
|
|
|
/>
|
|
|
|
|
|
|
|
</configure>
|
|
|
|
"""
|
2016-02-05 01:39:53 +01:00
|
|
|
zcml.load_config('configure.zcml', Products.Five)
|
2014-05-12 19:20:18 +02:00
|
|
|
zcml.load_string(configure)
|
|
|
|
|
|
|
|
def test_addEvent(self):
|
|
|
|
self.assertFalse(self.registry.replyAdded)
|
|
|
|
|
|
|
|
conversation = IConversation(self.document)
|
|
|
|
replies = IReplies(conversation)
|
|
|
|
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
new_id = replies.addComment(comment)
|
|
|
|
comment = self.document.restrictedTraverse(
|
2016-02-05 01:39:53 +01:00
|
|
|
'++conversation++default/{0}'.format(new_id)
|
|
|
|
)
|
2014-05-12 19:20:18 +02:00
|
|
|
|
|
|
|
re_comment = createObject('plone.Comment')
|
|
|
|
re_comment.text = 'Comment text'
|
|
|
|
|
|
|
|
replies = IReplies(comment)
|
2015-05-03 08:27:21 +02:00
|
|
|
replies.addComment(re_comment)
|
2014-05-12 19:20:18 +02:00
|
|
|
|
|
|
|
self.assertTrue(self.registry.replyAdded)
|
|
|
|
|
|
|
|
def test_removedEvent(self):
|
|
|
|
self.assertFalse(self.registry.replyRemoved)
|
|
|
|
|
|
|
|
conversation = IConversation(self.portal.doc1)
|
|
|
|
replies = IReplies(conversation)
|
|
|
|
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
new_id = replies.addComment(comment)
|
|
|
|
comment = self.portal.doc1.restrictedTraverse(
|
2016-02-05 01:39:53 +01:00
|
|
|
'++conversation++default/{0}'.format(new_id)
|
|
|
|
)
|
2014-05-12 19:20:18 +02:00
|
|
|
|
|
|
|
re_comment = createObject('plone.Comment')
|
|
|
|
re_comment.text = 'Comment text'
|
|
|
|
replies = IReplies(comment)
|
|
|
|
new_re_id = replies.addComment(re_comment)
|
|
|
|
|
|
|
|
del replies[new_re_id]
|
|
|
|
self.assertTrue(self.registry.replyRemoved)
|