plone.app.discussion/plone/app/discussion/tests/test_events.py

242 lines
6.9 KiB
Python
Raw Normal View History

from ..interfaces import IConversation
from ..interfaces import IReplies
from ..testing import PLONE_APP_DISCUSSION_INTEGRATION_TESTING
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from Zope2.App import zcml
2017-07-28 17:58:35 +02:00
from zope.component import createObject
from zope.event import notify
from zope.lifecycleevent import ObjectModifiedEvent
import Products.Five
import unittest
#
# Fake events registry
#
class EventsRegistry:
2022-05-01 23:14:09 +02:00
"""Fake registry to be used while testing discussion events"""
commentAdded = False
commentModified = False
commentRemoved = False
replyAdded = False
replyModified = False
replyRemoved = False
2022-05-01 23:14:09 +02:00
#
# Fake event handlers
#
def comment_added(doc, evt):
EventsRegistry.commentAdded = True
def comment_modified(doc, evt):
EventsRegistry.commentModified = True
def comment_removed(doc, evt):
EventsRegistry.commentRemoved = True
def reply_added(doc, evt):
EventsRegistry.replyAdded = True
def reply_modified(doc, evt):
EventsRegistry.replyModified = True
def reply_removed(doc, evt):
EventsRegistry.replyRemoved = True
#
# Tests
#
class CommentEventsTest(unittest.TestCase):
2022-05-01 23:14:09 +02:00
"""Test custom comments events"""
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"]
self.registry = EventsRegistry
2022-05-01 23:14:09 +02:00
setRoles(self.portal, TEST_USER_ID, ["Manager"])
self.document = self.portal["doc1"]
#
# 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="plone.app.discussion.interfaces.IComment
zope.lifecycleevent.interfaces.IObjectModifiedEvent"
handler="plone.app.discussion.tests.test_events.comment_modified"
/>
<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)
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")
conversation = IConversation(self.document)
conversation.addComment(comment)
self.assertTrue(self.registry.commentAdded)
def test_modifyEvent(self):
self.assertFalse(self.registry.commentModified)
2022-05-01 23:14:09 +02:00
comment = createObject("plone.Comment")
conversation = IConversation(self.document)
new_id = conversation.addComment(comment)
comment = self.document.restrictedTraverse(
f"++conversation++default/{new_id}",
)
comment.text = "foo"
notify(ObjectModifiedEvent(comment))
self.assertTrue(self.registry.commentModified)
def test_removedEvent(self):
self.assertFalse(self.registry.commentRemoved)
2022-05-01 23:14:09 +02:00
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
class RepliesEventsTest(unittest.TestCase):
2022-05-01 23:14:09 +02:00
"""Test custom replies events"""
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"]
self.registry = EventsRegistry
2022-05-01 23:14:09 +02:00
setRoles(self.portal, TEST_USER_ID, ["Manager"])
self.document = self.portal["doc1"]
#
# 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="plone.app.discussion.interfaces.IComment
zope.lifecycleevent.interfaces.IObjectModifiedEvent"
handler="plone.app.discussion.tests.test_events.reply_modified"
/>
<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)
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"
new_id = replies.addComment(comment)
comment = self.document.restrictedTraverse(
f"++conversation++default/{new_id}",
)
2022-05-01 23:14:09 +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)
self.assertTrue(self.registry.replyAdded)
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"
comment_id = replies.addComment(comment)
comment = self.document.restrictedTraverse(
f"++conversation++default/{comment_id}",
)
2022-05-01 23:14:09 +02:00
re_comment = createObject("plone.Comment")
re_comment.text = "Comment text"
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)
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"
new_id = replies.addComment(comment)
comment = self.portal.doc1.restrictedTraverse(
f"++conversation++default/{new_id}",
)
2022-05-01 23:14:09 +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)