2022-05-02 00:39:34 +02:00
|
|
|
from ..interfaces import IConversation
|
|
|
|
from ..interfaces import IReplies
|
|
|
|
from ..testing import PLONE_APP_DISCUSSION_INTEGRATION_TESTING
|
2015-05-03 08:16:39 +02:00
|
|
|
from plone.app.testing import setRoles
|
|
|
|
from plone.app.testing import TEST_USER_ID
|
2016-02-05 01:39:53 +01:00
|
|
|
from Zope2.App import zcml
|
2017-07-28 17:58:35 +02:00
|
|
|
from zope.component import createObject
|
2021-09-08 13:02:34 +02:00
|
|
|
from zope.event import notify
|
|
|
|
from zope.lifecycleevent import ObjectModifiedEvent
|
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
|
|
|
|
|
|
|
|
2022-05-01 23:14:41 +02:00
|
|
|
class EventsRegistry:
|
2022-05-01 23:14:09 +02:00
|
|
|
"""Fake registry to be used while testing discussion events"""
|
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
commentAdded = False
|
2021-09-08 13:02:34 +02:00
|
|
|
commentModified = False
|
2014-05-12 19:20:18 +02:00
|
|
|
commentRemoved = False
|
|
|
|
replyAdded = False
|
2021-09-08 13:02:34 +02:00
|
|
|
replyModified = False
|
2014-05-12 19:20:18 +02:00
|
|
|
replyRemoved = False
|
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
2021-09-08 13:02:34 +02:00
|
|
|
def comment_modified(doc, evt):
|
|
|
|
EventsRegistry.commentModified = True
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-09-08 13:02:34 +02:00
|
|
|
def reply_modified(doc, evt):
|
|
|
|
EventsRegistry.replyModified = True
|
|
|
|
|
|
|
|
|
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):
|
2022-05-01 23:14:09 +02:00
|
|
|
"""Test custom comments events"""
|
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
|
|
|
# Setup sandbox
|
2022-05-01 23:14:09 +02:00
|
|
|
self.portal = self.layer["portal"]
|
|
|
|
self.request = self.layer["request"]
|
2014-05-12 19:20:18 +02:00
|
|
|
self.registry = EventsRegistry
|
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
setRoles(self.portal, TEST_USER_ID, ["Manager"])
|
|
|
|
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"
|
|
|
|
/>
|
|
|
|
|
2021-09-08 13:02:34 +02:00
|
|
|
<subscriber
|
|
|
|
for="plone.app.discussion.interfaces.IComment
|
|
|
|
zope.lifecycleevent.interfaces.IObjectModifiedEvent"
|
|
|
|
handler="plone.app.discussion.tests.test_events.comment_modified"
|
|
|
|
/>
|
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
<subscriber
|
|
|
|
for="OFS.interfaces.ISimpleItem
|
|
|
|
plone.app.discussion.interfaces.ICommentRemovedEvent"
|
|
|
|
handler="plone.app.discussion.tests.test_events.comment_removed"
|
|
|
|
/>
|
|
|
|
|
|
|
|
</configure>
|
|
|
|
"""
|
2022-05-01 23:14:09 +02: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)
|
2022-05-01 23:14:09 +02:00
|
|
|
comment = createObject("plone.Comment")
|
2014-05-12 19:20:18 +02:00
|
|
|
conversation = IConversation(self.document)
|
|
|
|
conversation.addComment(comment)
|
|
|
|
self.assertTrue(self.registry.commentAdded)
|
|
|
|
|
2021-09-08 13:02:34 +02:00
|
|
|
def test_modifyEvent(self):
|
|
|
|
self.assertFalse(self.registry.commentModified)
|
2022-05-01 23:14:09 +02:00
|
|
|
comment = createObject("plone.Comment")
|
2021-09-08 13:02:34 +02:00
|
|
|
conversation = IConversation(self.document)
|
|
|
|
new_id = conversation.addComment(comment)
|
|
|
|
comment = self.document.restrictedTraverse(
|
2022-05-01 23:14:41 +02:00
|
|
|
f"++conversation++default/{new_id}",
|
2021-09-08 13:02:34 +02:00
|
|
|
)
|
|
|
|
comment.text = "foo"
|
|
|
|
notify(ObjectModifiedEvent(comment))
|
|
|
|
self.assertTrue(self.registry.commentModified)
|
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
def test_removedEvent(self):
|
|
|
|
self.assertFalse(self.registry.commentRemoved)
|
2022-05-01 23:14:09 +02:00
|
|
|
comment = createObject("plone.Comment")
|
2014-05-12 19:20:18 +02:00
|
|
|
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):
|
2022-05-01 23:14:09 +02:00
|
|
|
"""Test custom replies events"""
|
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING
|
|
|
|
|
|
|
|
def setUp(self):
|
2022-05-01 23:14:09 +02:00
|
|
|
self.portal = self.layer["portal"]
|
|
|
|
self.request = self.layer["request"]
|
2014-05-12 19:20:18 +02:00
|
|
|
self.registry = EventsRegistry
|
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
setRoles(self.portal, TEST_USER_ID, ["Manager"])
|
|
|
|
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"
|
|
|
|
/>
|
|
|
|
|
2021-09-08 13:02:34 +02:00
|
|
|
<subscriber
|
|
|
|
for="plone.app.discussion.interfaces.IComment
|
|
|
|
zope.lifecycleevent.interfaces.IObjectModifiedEvent"
|
|
|
|
handler="plone.app.discussion.tests.test_events.reply_modified"
|
|
|
|
/>
|
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
<subscriber
|
|
|
|
for="OFS.interfaces.ISimpleItem
|
|
|
|
plone.app.discussion.interfaces.IReplyRemovedEvent"
|
|
|
|
handler="plone.app.discussion.tests.test_events.reply_removed"
|
|
|
|
/>
|
|
|
|
|
|
|
|
</configure>
|
|
|
|
"""
|
2022-05-01 23:14:09 +02: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)
|
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
comment = createObject("plone.Comment")
|
|
|
|
comment.text = "Comment text"
|
2014-05-12 19:20:18 +02:00
|
|
|
new_id = replies.addComment(comment)
|
|
|
|
comment = self.document.restrictedTraverse(
|
2022-05-01 23:14:41 +02:00
|
|
|
f"++conversation++default/{new_id}",
|
2016-02-05 01:39:53 +01:00
|
|
|
)
|
2014-05-12 19:20:18 +02:00
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
re_comment = createObject("plone.Comment")
|
|
|
|
re_comment.text = "Comment text"
|
2014-05-12 19:20:18 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-09-08 13:02:34 +02:00
|
|
|
def test_modifyEvent(self):
|
|
|
|
self.assertFalse(self.registry.replyModified)
|
|
|
|
|
|
|
|
conversation = IConversation(self.document)
|
|
|
|
replies = IReplies(conversation)
|
2022-05-01 23:14:09 +02:00
|
|
|
comment = createObject("plone.Comment")
|
|
|
|
comment.text = "Comment text"
|
2021-09-08 13:02:34 +02:00
|
|
|
comment_id = replies.addComment(comment)
|
|
|
|
comment = self.document.restrictedTraverse(
|
2022-05-01 23:14:41 +02:00
|
|
|
f"++conversation++default/{comment_id}",
|
2021-09-08 13:02:34 +02:00
|
|
|
)
|
2022-05-01 23:14:09 +02:00
|
|
|
re_comment = createObject("plone.Comment")
|
|
|
|
re_comment.text = "Comment text"
|
2021-09-08 13:02:34 +02:00
|
|
|
replies = IReplies(comment)
|
|
|
|
new_id = replies.addComment(re_comment)
|
|
|
|
reply = replies[new_id]
|
|
|
|
reply.text = "Another text"
|
|
|
|
notify(ObjectModifiedEvent(reply))
|
|
|
|
self.assertTrue(self.registry.replyModified)
|
|
|
|
|
2014-05-12 19:20:18 +02:00
|
|
|
def test_removedEvent(self):
|
|
|
|
self.assertFalse(self.registry.replyRemoved)
|
|
|
|
|
|
|
|
conversation = IConversation(self.portal.doc1)
|
|
|
|
replies = IReplies(conversation)
|
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
comment = createObject("plone.Comment")
|
|
|
|
comment.text = "Comment text"
|
2014-05-12 19:20:18 +02:00
|
|
|
new_id = replies.addComment(comment)
|
|
|
|
comment = self.portal.doc1.restrictedTraverse(
|
2022-05-01 23:14:41 +02:00
|
|
|
f"++conversation++default/{new_id}",
|
2016-02-05 01:39:53 +01:00
|
|
|
)
|
2014-05-12 19:20:18 +02:00
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
re_comment = createObject("plone.Comment")
|
|
|
|
re_comment.text = "Comment text"
|
2014-05-12 19:20:18 +02:00
|
|
|
replies = IReplies(comment)
|
|
|
|
new_re_id = replies.addComment(re_comment)
|
|
|
|
|
|
|
|
del replies[new_re_id]
|
|
|
|
self.assertTrue(self.registry.replyRemoved)
|