2010-09-16 16:58:11 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from plone.app.discussion.browser.moderation import BulkActionsView
|
2019-12-05 21:55:23 +01:00
|
|
|
from plone.app.discussion.browser.moderation import CommentTransition
|
2022-05-01 23:14:00 +02:00
|
|
|
from plone.app.discussion.browser.moderation import DeleteComment
|
2015-05-03 08:16:39 +02:00
|
|
|
from plone.app.discussion.browser.moderation import View
|
2010-07-13 12:45:53 +02:00
|
|
|
from plone.app.discussion.interfaces import IConversation
|
2016-09-19 17:06:17 +02:00
|
|
|
from plone.app.discussion.interfaces import IDiscussionSettings
|
2022-05-01 23:14:00 +02:00
|
|
|
from plone.app.discussion.testing import ( # noqa
|
|
|
|
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-09-19 17:06:17 +02:00
|
|
|
from plone.registry.interfaces import IRegistry
|
2016-02-05 01:39:53 +01:00
|
|
|
from Products.CMFCore.utils import getToolByName
|
2015-05-03 08:16:39 +02:00
|
|
|
from zope.component import createObject
|
2016-09-19 17:06:17 +02:00
|
|
|
from zope.component import queryUtility
|
2015-05-03 08:16:39 +02:00
|
|
|
|
|
|
|
import unittest
|
2009-06-18 22:57:47 +02:00
|
|
|
|
|
|
|
|
2011-04-17 10:50:34 +02:00
|
|
|
class ModerationViewTest(unittest.TestCase):
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-17 10:50:34 +02:00
|
|
|
layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-17 10:50:34 +02:00
|
|
|
def setUp(self):
|
2022-05-01 23:14:09 +02:00
|
|
|
self.app = self.layer["app"]
|
|
|
|
self.portal = self.layer["portal"]
|
|
|
|
self.request = self.layer["request"]
|
|
|
|
setRoles(self.portal, TEST_USER_ID, ["Manager"])
|
|
|
|
self.portal_discussion = getToolByName(self.portal, "portal_discussion", None)
|
|
|
|
self.membership_tool = getToolByName(self.portal, "portal_membership")
|
2009-06-18 22:57:47 +02:00
|
|
|
self.memberdata = self.portal.portal_memberdata
|
|
|
|
request = self.app.REQUEST
|
2022-05-01 23:14:09 +02:00
|
|
|
context = getattr(self.portal, "doc1")
|
2009-06-18 22:57:47 +02:00
|
|
|
self.view = View(context, request)
|
2009-10-18 15:12:52 +02:00
|
|
|
self.portal.portal_workflow.setChainForPortalTypes(
|
2022-05-01 23:14:09 +02:00
|
|
|
("Discussion Item",), "comment_review_workflow"
|
|
|
|
)
|
2009-06-27 11:35:52 +02:00
|
|
|
self.wf_tool = self.portal.portal_workflow
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2009-10-18 15:12:52 +02:00
|
|
|
def test_moderation_enabled(self):
|
2010-12-16 00:52:56 +01:00
|
|
|
"""Make sure that moderation_enabled returns true if the comment
|
2022-05-01 23:14:09 +02:00
|
|
|
workflow implements a 'pending' state.
|
2010-10-08 12:22:40 +02:00
|
|
|
"""
|
2011-04-27 19:41:07 +02:00
|
|
|
# If workflow is not set, enabled must return False
|
2022-05-01 23:14:09 +02:00
|
|
|
self.wf_tool.setChainForPortalTypes(("Discussion Item",), ())
|
2011-04-27 19:41:07 +02:00
|
|
|
self.assertEqual(self.view.moderation_enabled(), False)
|
2017-01-10 18:09:01 +01:00
|
|
|
# The comment_one_state_workflow does not have a 'pending' state
|
2022-05-01 23:14:09 +02:00
|
|
|
self.wf_tool.setChainForPortalTypes(
|
|
|
|
("Discussion Item",), ("comment_one_state_workflow,")
|
|
|
|
)
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertEqual(self.view.moderation_enabled(), False)
|
2010-10-08 12:22:40 +02:00
|
|
|
# The comment_review_workflow does have a 'pending' state
|
2022-05-01 23:14:09 +02:00
|
|
|
self.wf_tool.setChainForPortalTypes(
|
|
|
|
("Discussion Item",), ("comment_review_workflow,")
|
|
|
|
)
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertEqual(self.view.moderation_enabled(), True)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
|
|
|
|
2011-04-17 10:50:34 +02:00
|
|
|
class ModerationBulkActionsViewTest(unittest.TestCase):
|
2010-09-16 16:58:11 +02:00
|
|
|
|
2011-04-17 10:50:34 +02:00
|
|
|
layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING
|
2010-09-16 16:58:11 +02:00
|
|
|
|
2011-04-17 10:50:34 +02:00
|
|
|
def setUp(self):
|
2022-05-01 23:14:09 +02:00
|
|
|
self.app = self.layer["app"]
|
|
|
|
self.portal = self.layer["portal"]
|
|
|
|
self.request = self.layer["request"]
|
|
|
|
setRoles(self.portal, TEST_USER_ID, ["Manager"])
|
|
|
|
self.wf = getToolByName(self.portal, "portal_workflow", None)
|
2010-09-16 16:58:11 +02:00
|
|
|
self.context = self.portal
|
|
|
|
self.portal.portal_workflow.setChainForPortalTypes(
|
2022-05-01 23:14:09 +02:00
|
|
|
("Discussion Item",),
|
|
|
|
"comment_review_workflow",
|
2018-06-18 17:04:41 +02:00
|
|
|
)
|
2010-09-16 16:58:11 +02:00
|
|
|
self.wf_tool = self.portal.portal_workflow
|
|
|
|
# Add a conversation with three comments
|
|
|
|
conversation = IConversation(self.portal.doc1)
|
2022-05-01 23:14:09 +02:00
|
|
|
comment1 = createObject("plone.Comment")
|
|
|
|
comment1.title = "Comment 1"
|
|
|
|
comment1.text = "Comment text"
|
|
|
|
comment1.Creator = "Jim"
|
2010-09-16 16:58:11 +02:00
|
|
|
new_id_1 = conversation.addComment(comment1)
|
2013-04-17 19:04:45 +02:00
|
|
|
self.comment1 = self.portal.doc1.restrictedTraverse(
|
2022-05-01 23:14:09 +02:00
|
|
|
"++conversation++default/{0}".format(new_id_1),
|
2013-04-17 19:04:45 +02:00
|
|
|
)
|
2022-05-01 23:14:09 +02:00
|
|
|
comment2 = createObject("plone.Comment")
|
|
|
|
comment2.title = "Comment 2"
|
|
|
|
comment2.text = "Comment text"
|
|
|
|
comment2.Creator = "Joe"
|
2010-09-16 16:58:11 +02:00
|
|
|
new_id_2 = conversation.addComment(comment2)
|
2013-04-17 19:04:45 +02:00
|
|
|
self.comment2 = self.portal.doc1.restrictedTraverse(
|
2022-05-01 23:14:09 +02:00
|
|
|
"++conversation++default/{0}".format(new_id_2),
|
2013-04-17 19:04:45 +02:00
|
|
|
)
|
2022-05-01 23:14:09 +02:00
|
|
|
comment3 = createObject("plone.Comment")
|
|
|
|
comment3.title = "Comment 3"
|
|
|
|
comment3.text = "Comment text"
|
|
|
|
comment3.Creator = "Emma"
|
2010-09-16 16:58:11 +02:00
|
|
|
new_id_3 = conversation.addComment(comment3)
|
2013-04-17 19:04:45 +02:00
|
|
|
self.comment3 = self.portal.doc1.restrictedTraverse(
|
2022-05-01 23:14:09 +02:00
|
|
|
"++conversation++default/{0}".format(new_id_3),
|
2013-04-17 19:04:45 +02:00
|
|
|
)
|
2010-09-16 16:58:11 +02:00
|
|
|
self.conversation = conversation
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-09-16 17:42:27 +02:00
|
|
|
def test_default_bulkaction(self):
|
2010-09-16 17:44:07 +02:00
|
|
|
# Make sure no error is raised when no bulk actions has been supplied
|
2022-05-01 23:14:09 +02:00
|
|
|
self.request.set("form.select.BulkAction", "-1")
|
|
|
|
self.request.set("paths", ["/".join(self.comment1.getPhysicalPath())])
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-17 10:50:34 +02:00
|
|
|
view = BulkActionsView(self.portal, self.request)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertFalse(view())
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-09-16 16:58:11 +02:00
|
|
|
def test_publish(self):
|
2022-05-01 23:14:09 +02:00
|
|
|
self.request.set("form.select.BulkAction", "publish")
|
|
|
|
self.request.set("paths", ["/".join(self.comment1.getPhysicalPath())])
|
2011-04-17 10:50:34 +02:00
|
|
|
view = BulkActionsView(self.portal, self.request)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-09-16 17:12:59 +02:00
|
|
|
view()
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-09-16 17:12:59 +02:00
|
|
|
# Count published comments
|
|
|
|
published_comments = 0
|
|
|
|
for r in self.conversation.getThreads():
|
2022-05-01 23:14:09 +02:00
|
|
|
comment_obj = r["comment"]
|
|
|
|
workflow_status = self.wf.getInfoFor(comment_obj, "review_state")
|
|
|
|
if workflow_status == "published":
|
2010-09-16 17:12:59 +02:00
|
|
|
published_comments += 1
|
2010-09-16 17:42:27 +02:00
|
|
|
# Make sure the comment has been published
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertEqual(published_comments, 1)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-09-16 16:58:11 +02:00
|
|
|
def test_delete(self):
|
|
|
|
# Initially we have three comments
|
2012-06-13 13:17:22 +02:00
|
|
|
self.assertEqual(len(self.conversation.objectIds()), 3)
|
2010-09-16 16:58:11 +02:00
|
|
|
# Delete two comments with bulk actions
|
2022-05-01 23:14:09 +02:00
|
|
|
self.request.set("form.select.BulkAction", "delete")
|
|
|
|
self.request.set(
|
|
|
|
"paths",
|
|
|
|
[
|
|
|
|
"/".join(self.comment1.getPhysicalPath()),
|
|
|
|
"/".join(self.comment3.getPhysicalPath()),
|
|
|
|
],
|
|
|
|
)
|
2011-04-17 10:50:34 +02:00
|
|
|
view = BulkActionsView(self.app, self.request)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-09-16 16:58:11 +02:00
|
|
|
view()
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-09-16 16:58:11 +02:00
|
|
|
# Make sure that the two comments have been deleted
|
2012-06-13 13:17:22 +02:00
|
|
|
self.assertEqual(len(self.conversation.objectIds()), 1)
|
2018-01-25 13:04:11 +01:00
|
|
|
comment = next(self.conversation.getComments())
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertTrue(comment)
|
|
|
|
self.assertEqual(comment, self.comment2)
|
2016-09-19 17:06:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RedirectionTest(unittest.TestCase):
|
|
|
|
|
|
|
|
layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# Update settings.
|
2022-05-01 23:14:09 +02:00
|
|
|
self.portal = self.layer["portal"]
|
|
|
|
self.request = self.layer["request"]
|
|
|
|
setRoles(self.portal, TEST_USER_ID, ["Manager"])
|
2016-09-19 17:06:17 +02:00
|
|
|
# applyProfile(self.portal, 'plone.app.discussion:default')
|
|
|
|
registry = queryUtility(IRegistry)
|
|
|
|
settings = registry.forInterface(IDiscussionSettings)
|
|
|
|
settings.globally_enabled = True
|
|
|
|
self.portal.portal_workflow.setChainForPortalTypes(
|
2022-05-01 23:14:09 +02:00
|
|
|
("Discussion Item",),
|
|
|
|
("comment_review_workflow",),
|
2018-06-18 17:04:41 +02:00
|
|
|
)
|
2016-09-19 17:06:17 +02:00
|
|
|
# Create page plus comment.
|
|
|
|
self.portal.invokeFactory(
|
2022-05-01 23:14:09 +02:00
|
|
|
id="page",
|
|
|
|
title="Page 1",
|
|
|
|
type_name="Document",
|
2016-09-19 17:06:17 +02:00
|
|
|
)
|
|
|
|
self.page = self.portal.page
|
|
|
|
self.conversation = IConversation(self.page)
|
2022-05-01 23:14:09 +02:00
|
|
|
comment = createObject("plone.Comment")
|
|
|
|
comment.text = "Comment text"
|
2016-09-19 17:06:17 +02:00
|
|
|
self.comment_id = self.conversation.addComment(comment)
|
|
|
|
self.comment = list(self.conversation.getComments())[0]
|
|
|
|
|
|
|
|
def test_regression(self):
|
|
|
|
page_url = self.page.absolute_url()
|
2022-05-01 23:14:09 +02:00
|
|
|
self.request["HTTP_REFERER"] = page_url
|
2019-12-05 21:55:23 +01:00
|
|
|
for Klass in (DeleteComment, CommentTransition):
|
2016-09-19 17:06:17 +02:00
|
|
|
view = Klass(self.comment, self.request)
|
|
|
|
view.__parent__ = self.comment
|
|
|
|
self.assertEqual(page_url, view())
|
|
|
|
|
|
|
|
def test_valid_next_url(self):
|
2022-05-01 23:14:09 +02:00
|
|
|
self.request["HTTP_REFERER"] = "http://attacker.com"
|
2019-12-05 21:55:23 +01:00
|
|
|
for Klass in (DeleteComment, CommentTransition):
|
2016-09-19 17:06:17 +02:00
|
|
|
view = Klass(self.comment, self.request)
|
|
|
|
view.__parent__ = self.comment
|
2022-05-01 23:14:09 +02:00
|
|
|
self.assertNotEqual("http://attacker.com", view())
|