plone.app.discussion/plone/app/discussion/tests/test_comments_viewlet.py

783 lines
28 KiB
Python
Raw Normal View History

from .. import interfaces
from ..browser.comment import EditCommentForm
from ..browser.comments import CommentForm
from ..browser.comments import CommentsViewlet
from ..interfaces import IConversation
from ..interfaces import IDiscussionSettings
from ..testing import PLONE_APP_DISCUSSION_INTEGRATION_TESTING
from AccessControl import Unauthorized
from datetime import datetime
from OFS.Image import Image
from plone.app.testing import login
from plone.app.testing import logout
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.app.testing import TEST_USER_NAME
from plone.registry.interfaces import IRegistry
from Products.CMFCore.utils import getToolByName
from z3c.form.interfaces import IFormLayer
from zope import interface
from zope.annotation.interfaces import IAttributeAnnotatable
from zope.component import createObject
from zope.component import getMultiAdapter
from zope.component import provideAdapter
from zope.component import queryUtility
from zope.interface import alsoProvides
from zope.interface import Interface
from zope.publisher.browser import TestRequest
from zope.publisher.interfaces.browser import IBrowserRequest
from ZPublisher.HTTPRequest import FileUpload
import time
import unittest
import io
TEXT = b"file data"
class DummyFile(FileUpload):
"""Dummy upload object
Used to fake uploaded files.
"""
__allow_access_to_unprotected_subobjects__ = 1
filename = "dummy.txt"
data = TEXT
headers = {}
def __init__(self, filename=None, data=None, headers=None):
if filename is not None:
self.filename = filename
if data is not None:
self.data = data
if headers is not None:
self.headers = headers
self.file = io.BytesIO(self.data)
def seek(self, *args):
pass
def tell(self, *args):
return 1
def read(self, *args):
return self.data
class TestCommentForm(unittest.TestCase):
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"]
setRoles(self.portal, TEST_USER_ID, ["Manager"])
self.portal.invokeFactory("Folder", "test-folder")
self.folder = self.portal["test-folder"]
2012-01-09 16:43:54 +01:00
interface.alsoProvides(
2013-04-17 19:34:17 +02:00
self.portal.REQUEST,
interfaces.IDiscussionLayer,
)
2022-05-01 23:14:09 +02:00
wftool = getToolByName(self.portal, "portal_workflow")
wftool.doActionFor(self.portal.doc1, action="publish")
2013-10-10 15:36:18 +02:00
self.portal.doc1.allow_discussion = True
2022-05-01 23:14:09 +02:00
self.membershipTool = getToolByName(self.folder, "portal_membership")
self.memberdata = self.portal.portal_memberdata
2022-05-01 23:14:09 +02:00
self.context = getattr(self.portal, "doc1")
# Allow discussion
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings)
settings.globally_enabled = True
def test_add_comment(self):
2022-05-01 23:14:09 +02:00
"""Post a comment as logged-in user."""
# Allow discussion
2013-10-10 15:36:18 +02:00
self.portal.doc1.allow_discussion = True
self.viewlet = CommentsViewlet(self.context, self.request, None, None)
def make_request(form={}):
request = TestRequest()
request.form.update(form)
alsoProvides(request, IFormLayer)
alsoProvides(request, IAttributeAnnotatable)
return request
2013-04-17 19:34:17 +02:00
provideAdapter(
adapts=(Interface, IBrowserRequest),
provides=Interface,
factory=CommentForm,
name="comment-form",
2013-04-17 19:34:17 +02:00
)
# The form should return an error if the comment text field is empty
request = make_request(form={})
2013-04-17 19:34:17 +02:00
commentForm = getMultiAdapter(
(self.context, request),
name="comment-form",
2013-04-17 19:34:17 +02:00
)
commentForm.update()
data, errors = commentForm.extractData() # pylint: disable-msg=W0612
self.assertEqual(len(errors), 1)
2022-05-01 23:14:09 +02:00
self.assertFalse(commentForm.handleComment(commentForm, "foo"))
# The form is submitted successfully, if the required text field is
# filled out
request = make_request(form={"form.widgets.text": "bar"})
2013-04-17 19:34:17 +02:00
commentForm = getMultiAdapter(
(self.context, request),
name="comment-form",
2013-04-17 19:34:17 +02:00
)
commentForm.update()
data, errors = commentForm.extractData() # pylint: disable-msg=W0612
self.assertEqual(len(errors), 0)
2022-05-01 23:14:09 +02:00
self.assertFalse(commentForm.handleComment(commentForm, "foo"))
2013-11-04 16:30:14 +01:00
comments = IConversation(commentForm.context).getComments()
2015-05-03 08:25:19 +02:00
comments = [comment for comment in comments] # consume iterator
2013-11-04 16:30:14 +01:00
self.assertEqual(len(comments), 1)
for comment in comments:
self.assertEqual(comment.text, "bar")
2022-05-01 23:14:09 +02:00
self.assertEqual(comment.creator, "test_user_1_")
self.assertEqual(comment.getOwner().getUserName(), "test-user")
2013-11-04 16:30:14 +01:00
local_roles = comment.get_local_roles()
self.assertEqual(len(local_roles), 1)
userid, roles = local_roles[0]
2022-05-01 23:14:09 +02:00
self.assertEqual(userid, "test_user_1_")
2013-11-04 16:30:14 +01:00
self.assertEqual(len(roles), 1)
2022-05-01 23:14:09 +02:00
self.assertEqual(roles[0], "Owner")
2013-11-04 16:30:14 +01:00
2013-09-17 14:03:46 +02:00
def test_edit_comment(self):
2022-05-01 23:14:09 +02:00
"""Edit a comment as logged-in user."""
2013-09-17 14:03:46 +02:00
# Allow discussion
2014-03-03 15:09:30 +01:00
self.portal.doc1.allow_discussion = True
2013-09-17 14:03:46 +02:00
self.viewlet = CommentsViewlet(self.context, self.request, None, None)
def make_request(form={}):
request = TestRequest()
request.form.update(form)
alsoProvides(request, IFormLayer)
alsoProvides(request, IAttributeAnnotatable)
return request
provideAdapter(
adapts=(Interface, IBrowserRequest),
provides=Interface,
factory=CommentForm,
name="comment-form",
2013-09-17 14:03:46 +02:00
)
provideAdapter(
adapts=(Interface, IBrowserRequest),
provides=Interface,
factory=EditCommentForm,
name="edit-comment-form",
2013-09-17 14:03:46 +02:00
)
# The form is submitted successfully, if the required text field is
# filled out
request = make_request(form={"form.widgets.text": "bar"})
2013-09-17 14:03:46 +02:00
commentForm = getMultiAdapter(
(self.context, request),
name="comment-form",
2013-09-17 14:03:46 +02:00
)
commentForm.update()
data, errors = commentForm.extractData() # pylint: disable-msg=W0612
self.assertEqual(len(errors), 0)
2022-05-01 23:14:09 +02:00
self.assertFalse(commentForm.handleComment(commentForm, "foo"))
2013-09-17 14:03:46 +02:00
# Edit the last comment
conversation = IConversation(self.context)
comment = [x for x in conversation.getComments()][-1]
request = make_request(form={"form.widgets.text": "foobar"})
2013-09-17 14:03:46 +02:00
editForm = getMultiAdapter(
(comment, request),
name="edit-comment-form",
2013-09-17 14:03:46 +02:00
)
editForm.update()
data, errors = editForm.extractData() # pylint: disable-msg=W0612
self.assertEqual(len(errors), 0)
2022-05-01 23:14:09 +02:00
self.assertFalse(editForm.handleComment(editForm, "foo"))
2013-09-17 14:03:46 +02:00
comment = [x for x in conversation.getComments()][-1]
self.assertEqual(comment.text, "foobar")
2013-09-17 14:03:46 +02:00
2014-02-04 11:35:20 +01:00
comments = IConversation(commentForm.context).getComments()
2015-05-03 08:42:52 +02:00
comments = [c for c in comments] # consume iterator
2014-02-04 11:35:20 +01:00
self.assertEqual(len(comments), 1)
for comment in comments:
self.assertEqual(comment.text, "foobar")
2022-05-01 23:14:09 +02:00
self.assertEqual(comment.creator, "test_user_1_")
2014-03-26 18:22:59 +01:00
2022-05-01 23:14:09 +02:00
self.assertEqual(comment.getOwner().getUserName(), "test-user")
2014-02-04 11:35:20 +01:00
local_roles = comment.get_local_roles()
self.assertEqual(len(local_roles), 1)
2014-02-04 11:35:20 +01:00
userid, roles = local_roles[0]
2022-05-01 23:14:09 +02:00
self.assertEqual(userid, "test_user_1_")
2014-02-04 11:35:20 +01:00
self.assertEqual(len(roles), 1)
2022-05-01 23:14:09 +02:00
self.assertEqual(roles[0], "Owner")
2014-02-04 11:35:20 +01:00
def test_delete_comment(self):
2022-05-01 23:14:09 +02:00
"""Delete a comment as logged-in user."""
# Allow discussion
self.portal.doc1.allow_discussion = True
self.viewlet = CommentsViewlet(self.context, self.request, None, None)
def make_request(form={}):
request = TestRequest()
request.form.update(form)
alsoProvides(request, IFormLayer)
alsoProvides(request, IAttributeAnnotatable)
return request
provideAdapter(
adapts=(Interface, IBrowserRequest),
provides=Interface,
factory=CommentForm,
name="comment-form",
)
# The form is submitted successfully, if the required text field is
# filled out
form_request = make_request(form={"form.widgets.text": "bar"})
commentForm = getMultiAdapter(
(self.context, form_request),
name="comment-form",
)
commentForm.update()
data, errors = commentForm.extractData() # pylint: disable-msg=W0612
self.assertEqual(len(errors), 0)
2022-05-01 23:14:09 +02:00
self.assertFalse(commentForm.handleComment(commentForm, "foo"))
# Delete the last comment
conversation = IConversation(self.context)
comment = [x for x in conversation.getComments()][-1]
deleteView = getMultiAdapter(
(comment, self.request),
name="moderate-delete-comment",
)
# try to delete last comment without 'Delete comments' permission
2022-05-01 23:14:09 +02:00
setRoles(self.portal, TEST_USER_ID, ["Member"])
2015-05-03 08:27:03 +02:00
self.assertRaises(
Unauthorized,
comment.restrictedTraverse,
2022-05-01 23:14:09 +02:00
"@@moderate-delete-comment",
2015-05-03 08:27:03 +02:00
)
deleteView()
self.assertEqual(1, len([x for x in conversation.getComments()]))
# try to delete last comment with 'Delete comments' permission
2022-05-01 23:14:09 +02:00
setRoles(self.portal, TEST_USER_ID, ["Reviewer"])
deleteView()
self.assertEqual(0, len([x for x in conversation.getComments()]))
2022-05-01 23:14:09 +02:00
setRoles(self.portal, TEST_USER_ID, ["Manager"])
2014-09-20 16:21:55 +02:00
def test_delete_own_comment(self):
2022-05-01 23:14:09 +02:00
"""Delete own comment as logged-in user."""
2014-09-20 16:21:55 +02:00
# Allow discussion
self.portal.doc1.allow_discussion = True
self.viewlet = CommentsViewlet(self.context, self.request, None, None)
def make_request(form={}):
request = TestRequest()
request.form.update(form)
alsoProvides(request, IFormLayer)
alsoProvides(request, IAttributeAnnotatable)
return request
provideAdapter(
adapts=(Interface, IBrowserRequest),
provides=Interface,
factory=CommentForm,
name="comment-form",
2014-09-20 16:21:55 +02:00
)
# The form is submitted successfully, if the required text field is
# filled out
form_request = make_request(form={"form.widgets.text": "bar"})
2014-09-20 16:21:55 +02:00
commentForm = getMultiAdapter(
(self.context, form_request),
name="comment-form",
2014-09-20 16:21:55 +02:00
)
commentForm.update()
data, errors = commentForm.extractData() # pylint: disable-msg=W0612
self.assertEqual(len(errors), 0)
2022-05-01 23:14:09 +02:00
self.assertFalse(commentForm.handleComment(commentForm, "foo"))
2014-09-20 16:21:55 +02:00
# Delete the last comment
conversation = IConversation(self.context)
comment = [x for x in conversation.getComments()][-1]
deleteView = getMultiAdapter(
(comment, self.request),
name="delete-own-comment",
2014-09-20 16:21:55 +02:00
)
# try to delete last comment with johndoe
2022-05-01 23:14:09 +02:00
setRoles(self.portal, "johndoe", ["Member"])
login(self.portal, "johndoe")
2015-05-03 08:27:03 +02:00
self.assertRaises(
Unauthorized,
comment.restrictedTraverse,
2022-05-01 23:14:09 +02:00
"@@delete-own-comment",
2015-05-03 08:27:03 +02:00
)
2014-09-20 16:21:55 +02:00
self.assertEqual(1, len([x for x in conversation.getComments()]))
# try to delete last comment with the same user that created it
login(self.portal, TEST_USER_NAME)
2022-05-01 23:14:09 +02:00
setRoles(self.portal, TEST_USER_ID, ["Member"])
2014-09-20 16:21:55 +02:00
deleteView()
self.assertEqual(0, len([x for x in conversation.getComments()]))
def test_add_anonymous_comment(self):
2013-10-10 15:36:18 +02:00
self.portal.doc1.allow_discussion = True
self.viewlet = CommentsViewlet(self.context, self.request, None, None)
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False)
settings.anonymous_comments = True
# Logout
logout()
def make_request(form={}):
request = TestRequest()
request.form.update(form)
alsoProvides(request, IFormLayer)
alsoProvides(request, IAttributeAnnotatable)
return request
2022-05-01 23:14:09 +02:00
provideAdapter(
adapts=(Interface, IBrowserRequest),
provides=Interface,
factory=CommentForm,
name="comment-form",
2022-05-01 23:14:09 +02:00
)
# Post an anonymous comment and provide a name
2022-05-01 23:14:09 +02:00
request = make_request(
form={
"form.widgets.name": "john doe",
"form.widgets.text": "bar",
2022-05-01 23:14:09 +02:00
}
)
2013-04-17 19:34:17 +02:00
commentForm = getMultiAdapter(
(self.context, request),
name="comment-form",
2013-04-17 19:34:17 +02:00
)
commentForm.update()
data, errors = commentForm.extractData() # pylint: disable-msg=W0612
self.assertEqual(len(errors), 0)
2022-05-01 23:14:09 +02:00
self.assertFalse(commentForm.handleComment(commentForm, "action"))
2013-11-04 16:30:14 +01:00
comments = IConversation(commentForm.context).getComments()
comments = [comment for comment in comments] # consume itertor
self.assertEqual(len(comments), 1)
for comment in IConversation(commentForm.context).getComments():
self.assertEqual(comment.text, "bar")
2013-11-04 16:30:14 +01:00
self.assertIsNone(comment.creator)
roles = comment.get_local_roles()
self.assertEqual(len(roles), 0)
def test_can_not_add_comments_if_discussion_is_not_allowed(self):
2022-05-01 23:14:09 +02:00
"""Make sure that comments can't be posted if discussion is disabled."""
2013-10-10 15:36:18 +02:00
# Disable discussion
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings)
settings.globally_enabled = False
def make_request(form={}):
request = TestRequest()
request.form.update(form)
alsoProvides(request, IFormLayer)
alsoProvides(request, IAttributeAnnotatable)
return request
2022-05-01 23:14:09 +02:00
provideAdapter(
adapts=(Interface, IBrowserRequest),
provides=Interface,
factory=CommentForm,
name="comment-form",
2022-05-01 23:14:09 +02:00
)
request = make_request(form={"form.widgets.text": "bar"})
2013-04-17 19:34:17 +02:00
commentForm = getMultiAdapter(
(self.context, request),
name="comment-form",
2013-04-17 19:34:17 +02:00
)
commentForm.update()
data, errors = commentForm.extractData() # pylint: disable-msg=W0612
# No form errors, but raise unauthorized because discussion is not
# allowed
self.assertEqual(len(errors), 0)
2013-10-10 15:36:18 +02:00
2022-05-01 23:14:09 +02:00
self.assertRaises(Unauthorized, commentForm.handleComment, commentForm, "foo")
def test_anonymous_can_not_add_comments_if_discussion_is_not_allowed(self):
"""Make sure that anonymous users can't post comments if anonymous
2022-05-01 23:14:09 +02:00
comments are disabled.
"""
# Anonymous comments are disabled by default
logout()
def make_request(form={}):
request = TestRequest()
request.form.update(form)
alsoProvides(request, IFormLayer)
alsoProvides(request, IAttributeAnnotatable)
return request
2022-05-01 23:14:09 +02:00
provideAdapter(
adapts=(Interface, IBrowserRequest),
provides=Interface,
factory=CommentForm,
name="comment-form",
2022-05-01 23:14:09 +02:00
)
request = make_request(form={"form.widgets.text": "bar"})
commentForm = getMultiAdapter((self.context, request), name="comment-form")
commentForm.update()
data, errors = commentForm.extractData() # pylint: disable-msg=W0612
self.assertEqual(len(errors), 0)
2013-04-17 19:34:17 +02:00
self.assertRaises(
Unauthorized,
commentForm.handleComment,
commentForm,
2022-05-01 23:14:09 +02:00
"foo",
2013-04-17 19:34:17 +02:00
)
class TestCommentsViewlet(unittest.TestCase):
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"]
setRoles(self.portal, TEST_USER_ID, ["Manager"])
self.portal.invokeFactory("Folder", "test-folder")
self.folder = self.portal["test-folder"]
interface.alsoProvides(
2013-04-17 19:34:17 +02:00
self.request,
interfaces.IDiscussionLayer,
2013-04-17 19:34:17 +02:00
)
2012-01-09 16:43:54 +01:00
2022-05-01 23:14:09 +02:00
self.workflowTool = getToolByName(self.portal, "portal_workflow")
self.workflowTool.setDefaultChain("comment_one_state_workflow")
2012-01-09 16:43:54 +01:00
2022-05-01 23:14:09 +02:00
self.membershipTool = getToolByName(self.folder, "portal_membership")
self.memberdata = self.portal.portal_memberdata
2022-05-01 23:14:09 +02:00
context = getattr(self.portal, "doc1")
self.viewlet = CommentsViewlet(context, self.request, None, None)
# Allow discussion
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings)
settings.globally_enabled = True
def test_can_reply(self):
# Portal owner can reply
self.assertTrue(self.viewlet.can_reply())
logout()
# Anonymous users can not reply
self.assertFalse(self.viewlet.can_reply())
def test_can_review(self):
# Portal owner has 'can review' permission
self.assertTrue(self.viewlet.can_review())
logout()
# Anonymous has no 'can review' permission
self.assertFalse(self.viewlet.can_review())
# The reviewer role has the 'Review comments' permission
2022-05-01 23:14:09 +02:00
self.portal.acl_users._doAddUser("reviewer", "secret", ["Reviewer"], [])
login(self.portal, "reviewer")
self.assertTrue(self.viewlet.can_review())
def test_can_manage(self):
"""We keep this method for backward compatibility. This method has been
2022-05-01 23:14:09 +02:00
removed in version 1.0b9 and added again in 1.0b11 because we don't
do API changes in beta releases.
"""
# Portal owner has 'can review' permission
self.assertTrue(self.viewlet.can_manage())
logout()
# Anonymous has no 'can review' permission
self.assertFalse(self.viewlet.can_manage())
# The reviewer role has the 'Review comments' permission
2022-05-01 23:14:09 +02:00
self.portal.acl_users._doAddUser("reviewer", "secret", ["Reviewer"], [])
login(self.portal, "reviewer")
self.assertTrue(self.viewlet.can_manage())
def test_is_discussion_allowed(self):
# By default, discussion is disabled
self.assertFalse(self.viewlet.is_discussion_allowed())
# Enable discussion
2013-10-10 15:36:18 +02:00
self.portal.doc1.allow_discussion = True
# Test if discussion has been enabled
self.assertTrue(self.viewlet.is_discussion_allowed())
def test_comment_transform_message(self):
# Default transform is plain/text and comment moderation disabled
self.assertTrue(self.viewlet.comment_transform_message())
self.assertEqual(
self.viewlet.comment_transform_message(),
2022-05-01 23:14:09 +02:00
"You can add a comment by filling out the form below. Plain "
+ "text formatting.",
)
# Set text transform to intelligent text
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False)
2022-05-01 23:14:09 +02:00
settings.text_transform = "text/x-web-intelligent"
# Make sure the comment description changes accordingly
self.assertEqual(
self.viewlet.comment_transform_message(),
2022-05-01 23:14:09 +02:00
"You can add a comment by filling out the form below. "
+ "Plain text formatting. Web and email addresses are transformed "
+ "into clickable links.",
2013-04-17 19:34:17 +02:00
)
# Enable moderation workflow
self.workflowTool.setChainForPortalTypes(
2022-05-01 23:14:09 +02:00
("Discussion Item",), ("comment_review_workflow,")
)
# Make sure the comment description shows that comments are moderated
self.assertEqual(
self.viewlet.comment_transform_message(),
2022-05-01 23:14:09 +02:00
"You can add a comment by filling out the form below. "
+ "Plain text formatting. Web and email addresses are transformed "
+ "into clickable links. Comments are moderated.",
)
def test_has_replies(self):
self.assertEqual(self.viewlet.has_replies(), False)
2022-05-01 23:14:09 +02:00
comment = createObject("plone.Comment")
comment.text = "Comment text"
conversation = IConversation(self.portal.doc1)
conversation.addComment(comment)
self.assertEqual(self.viewlet.has_replies(), True)
def test_get_replies(self):
self.assertFalse(self.viewlet.get_replies())
2022-05-01 23:14:09 +02:00
comment = createObject("plone.Comment")
comment.text = "Comment text"
conversation = IConversation(self.portal.doc1)
conversation.addComment(comment)
conversation.addComment(comment)
replies = self.viewlet.get_replies()
self.assertEqual(len(tuple(replies)), 2)
replies = self.viewlet.get_replies()
2018-01-25 13:04:11 +01:00
next(replies)
next(replies)
2018-06-12 14:25:01 +02:00
with self.assertRaises(StopIteration):
next(replies)
def test_get_replies_on_non_annotatable_object(self):
2022-05-01 23:14:09 +02:00
context = self.portal.MailHost # the mail host is not annotatable
viewlet = CommentsViewlet(context, self.request, None, None)
replies = viewlet.get_replies()
self.assertEqual(len(tuple(replies)), 0)
replies = viewlet.get_replies()
2018-06-12 14:25:01 +02:00
with self.assertRaises(StopIteration):
next(replies)
def test_get_replies_with_workflow_actions(self):
self.assertFalse(self.viewlet.get_replies(workflow_actions=True))
2022-05-01 23:14:09 +02:00
comment = createObject("plone.Comment")
comment.text = "Comment text"
conversation = IConversation(self.portal.doc1)
c1 = conversation.addComment(comment)
self.assertEqual(
2013-04-17 19:34:17 +02:00
len(tuple(self.viewlet.get_replies(workflow_actions=True))),
1,
2013-04-17 19:34:17 +02:00
)
# Enable moderation workflow
self.workflowTool.setChainForPortalTypes(
2022-05-01 23:14:09 +02:00
("Discussion Item",),
("comment_review_workflow,"),
2013-04-17 19:34:17 +02:00
)
# Check if workflow actions are available
2018-01-25 13:04:11 +01:00
reply = next(self.viewlet.get_replies(workflow_actions=True))
2022-05-01 23:14:09 +02:00
self.assertTrue("actions" in reply)
2013-04-17 19:34:17 +02:00
self.assertEqual(
2022-05-01 23:14:09 +02:00
reply["actions"][0]["id"],
"mark_as_spam",
)
expected_url = (
"http://nohost/plone/doc1/++conversation++default/{0}"
"/content_status_modify?workflow_action=mark_as_spam"
2013-04-17 19:34:17 +02:00
)
self.assertEqual(
2022-05-01 23:14:09 +02:00
reply["actions"][0]["url"],
expected_url.format(int(c1)),
2013-04-17 19:34:17 +02:00
)
def test_get_commenter_home_url(self):
2022-05-01 23:14:09 +02:00
comment = createObject("plone.Comment")
comment.text = "Comment text"
IConversation(self.portal.doc1)
2022-05-01 23:14:09 +02:00
portal_membership = getToolByName(self.portal, "portal_membership")
m = portal_membership.getAuthenticatedMember()
2013-04-17 19:34:17 +02:00
self.assertEqual(
self.viewlet.get_commenter_home_url(m.getUserName()),
2022-05-01 23:14:09 +02:00
"http://nohost/plone/author/test-user",
2013-04-17 19:34:17 +02:00
)
def test_get_commenter_home_url_is_none(self):
self.assertFalse(self.viewlet.get_commenter_home_url())
def test_get_commenter_portrait(self):
# Add a user with a member image
2022-05-01 23:14:09 +02:00
self.membershipTool.addMember("jim", "Jim", ["Member"], [])
self.memberdata._setPortrait(
Image(
id="jim",
file=DummyFile(),
2022-05-01 23:14:09 +02:00
title="",
),
"jim",
)
2013-04-17 19:34:17 +02:00
self.assertEqual(
2022-05-01 23:14:09 +02:00
self.memberdata._getPortrait("jim").getId(),
"jim",
2013-04-17 19:34:17 +02:00
)
self.assertEqual(
2022-05-01 23:14:09 +02:00
self.memberdata._getPortrait("jim").meta_type,
"Image",
2013-04-17 19:34:17 +02:00
)
# Add a conversation with a comment
conversation = IConversation(self.portal.doc1)
2022-05-01 23:14:09 +02:00
comment = createObject("plone.Comment")
comment.text = "Comment text"
comment.Creator = "Jim"
comment.author_username = "jim"
conversation.addComment(comment)
# Call get_commenter_portrait method of the viewlet
self.viewlet.update()
2022-05-01 23:14:09 +02:00
portrait_url = self.viewlet.get_commenter_portrait("jim")
# Check if the correct member image URL is returned
2013-04-17 19:34:17 +02:00
self.assertEqual(
portrait_url,
2022-05-01 23:14:09 +02:00
"http://nohost/plone/portal_memberdata/portraits/jim",
2013-04-17 19:34:17 +02:00
)
def test_get_commenter_portrait_is_none(self):
2013-09-17 14:03:46 +02:00
self.assertTrue(
2022-05-01 23:14:09 +02:00
self.viewlet.get_commenter_portrait()
in (
"defaultUser.png",
"defaultUser.gif",
),
2013-04-17 19:34:17 +02:00
)
def test_get_commenter_portrait_without_userimage(self):
# Create a user without a user image
2022-05-01 23:14:09 +02:00
self.membershipTool.addMember("jim", "Jim", ["Member"], [])
# Add a conversation with a comment
conversation = IConversation(self.portal.doc1)
2022-05-01 23:14:09 +02:00
comment = createObject("plone.Comment")
comment.text = "Comment text"
comment.Creator = "Jim"
comment.author_username = "jim"
conversation.addComment(comment)
# Call get_commenter_portrait method of the viewlet
self.viewlet.update()
2022-05-01 23:14:09 +02:00
portrait_url = self.viewlet.get_commenter_portrait("jim")
# Check if the correct default member image URL is returned.
# Note that Products.PlonePAS 4.0.5 and later have .png and
# earlier versions have .gif.
2013-04-17 19:34:17 +02:00
self.assertTrue(
2022-05-01 23:14:09 +02:00
portrait_url
in (
"http://nohost/plone/defaultUser.png",
"http://nohost/plone/defaultUser.gif",
),
2013-04-17 19:34:17 +02:00
)
def test_anonymous_discussion_allowed(self):
# Anonymous discussion is not allowed by default
self.assertFalse(self.viewlet.anonymous_discussion_allowed())
# Allow anonymous discussion
registry = queryUtility(IRegistry)
2013-04-17 19:34:17 +02:00
registry[
2022-05-01 23:14:09 +02:00
"plone.app.discussion.interfaces.IDiscussionSettings."
+ "anonymous_comments"
2013-04-17 19:34:17 +02:00
] = True
# Test if anonymous discussion is allowed for the viewlet
self.assertTrue(self.viewlet.anonymous_discussion_allowed())
def test_show_commenter_image(self):
self.assertTrue(self.viewlet.show_commenter_image())
registry = queryUtility(IRegistry)
2013-04-17 19:34:17 +02:00
registry[
2022-05-01 23:14:09 +02:00
"plone.app.discussion.interfaces.IDiscussionSettings."
+ "show_commenter_image"
2013-04-17 19:34:17 +02:00
] = False
self.assertFalse(self.viewlet.show_commenter_image())
def test_is_anonymous(self):
self.assertFalse(self.viewlet.is_anonymous())
logout()
self.assertTrue(self.viewlet.is_anonymous())
def test_login_action(self):
self.viewlet.update()
2013-04-17 19:34:17 +02:00
self.assertEqual(
self.viewlet.login_action(),
2022-05-01 23:14:09 +02:00
"http://nohost/plone/login_form?came_from=http%3A//nohost",
2013-04-17 19:34:17 +02:00
)
def test_format_time(self):
2018-03-18 17:01:27 +01:00
python_time = datetime(2009, 2, 1, 23, 32, 3, 57)
# Python Time must be utc time. There seems to be no too simple way
# to tell datetime to be of utc time.
# therefor, we convert the time to seconds since epoch, which seems
# to assume, that the datetime was given in local time, and does the
# correction to the seconds since epoch. Then time.gmtime returns
# a correct utc time that can be used to make datetime set the utc
# time of the local time given above. That way, the time for the
# example below is correct within each time zone, independent of DST
2022-05-01 23:14:09 +02:00
python_time = datetime(*time.gmtime(time.mktime(python_time.timetuple()))[:7])
localized_time = self.viewlet.format_time(python_time)
self.assertTrue(
2022-05-01 23:14:09 +02:00
localized_time in ["Feb 01, 2009 11:32 PM", "2009-02-01 23:32"],
)