Use plone.app.testing in test_comments_viewlet.

svn path=/plone.app.discussion/trunk/; revision=48899
This commit is contained in:
Timo Stollenwerk 2011-04-16 09:25:16 +00:00
parent 38b70a28e5
commit f8019b0c10

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import unittest import unittest2 as unittest
import time import time
from datetime import datetime from datetime import datetime
@ -26,34 +26,41 @@ from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.tests import dummy from Products.CMFPlone.tests import dummy
from Products.PloneTestCase.ptc import PloneTestCase from plone.app.testing import TEST_USER_ID, setRoles
from plone.app.testing import logout
from plone.app.testing import login
from plone.app.discussion.browser.comments import CommentsViewlet from plone.app.discussion.browser.comments import CommentsViewlet
from plone.app.discussion.browser.comments import CommentForm from plone.app.discussion.browser.comments import CommentForm
from plone.app.discussion import interfaces from plone.app.discussion import interfaces
from plone.app.discussion.interfaces import IConversation from plone.app.discussion.interfaces import IConversation
from plone.app.discussion.tests.layer import DiscussionLayer from plone.app.discussion.testing import PLONE_APP_DISCUSSION_INTEGRATION_TESTING
from plone.app.discussion.interfaces import IDiscussionSettings from plone.app.discussion.interfaces import IDiscussionSettings
class TestCommentForm(PloneTestCase): class TestCommentForm(unittest.TestCase):
layer = DiscussionLayer layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING
def setUp(self):
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']
def afterSetUp(self):
interface.alsoProvides( interface.alsoProvides(
self.portal.REQUEST, interfaces.IDiscussionLayer) self.portal.REQUEST, interfaces.IDiscussionLayer)
self.loginAsPortalOwner()
typetool = self.portal.portal_types typetool = self.portal.portal_types
typetool.constructContent('Document', self.portal, 'doc1') typetool.constructContent('Document', self.portal, 'doc1')
self.dtool = getToolByName(self.portal, self.discussionTool = getToolByName(self.portal,
'portal_discussion', 'portal_discussion',
None) None)
self.dtool.overrideDiscussionFor(self.portal.doc1, False) self.discussionTool.overrideDiscussionFor(self.portal.doc1, False)
self.mtool = getToolByName(self.folder, 'portal_membership', None) self.membershipTool = getToolByName(self.folder, 'portal_membership', None)
self.memberdata = self.portal.portal_memberdata self.memberdata = self.portal.portal_memberdata
self.request = self.app.REQUEST
self.context = getattr(self.portal, 'doc1') self.context = getattr(self.portal, 'doc1')
# Allow discussion # Allow discussion
@ -66,7 +73,7 @@ class TestCommentForm(PloneTestCase):
""" """
# Allow discussion # Allow discussion
self.dtool.overrideDiscussionFor(self.portal.doc1, True) self.discussionTool.overrideDiscussionFor(self.portal.doc1, True)
self.viewlet = CommentsViewlet(self.context, self.request, None, None) self.viewlet = CommentsViewlet(self.context, self.request, None, None)
def make_request(form={}): def make_request(form={}):
@ -105,21 +112,16 @@ class TestCommentForm(PloneTestCase):
self.assertFalse(commentForm.handleComment(commentForm, "foo")) self.assertFalse(commentForm.handleComment(commentForm, "foo"))
def test_add_anonymous_comment(self): def test_add_anonymous_comment(self):
"""Add a comment as anonymous. self.discussionTool.overrideDiscussionFor(self.portal.doc1, True)
"""
# Allow discussion
self.dtool.overrideDiscussionFor(self.portal.doc1, True)
self.viewlet = CommentsViewlet(self.context, self.request, None, None) self.viewlet = CommentsViewlet(self.context, self.request, None, None)
registry = queryUtility(IRegistry) registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False) settings = registry.forInterface(IDiscussionSettings, check=False)
settings.anonymous_comments = True settings.anonymous_comments = True
self.portal.portal_workflow.doActionFor(self.context, 'publish')
# Logout # Logout
self.logout() logout()
def make_request(form={}): def make_request(form={}):
request = TestRequest() request = TestRequest()
@ -185,7 +187,7 @@ class TestCommentForm(PloneTestCase):
# Anonymous comments are disabled by default # Anonymous comments are disabled by default
self.logout() logout()
def make_request(form={}): def make_request(form={}):
request = TestRequest() request = TestRequest()
@ -213,25 +215,31 @@ class TestCommentForm(PloneTestCase):
"foo") "foo")
class TestCommentsViewlet(PloneTestCase): class TestCommentsViewlet(unittest.TestCase):
layer = DiscussionLayer layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING
def afterSetUp(self): def setUp(self):
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( interface.alsoProvides(
self.portal.REQUEST, interfaces.IDiscussionLayer) self.request, interfaces.IDiscussionLayer)
self.workflowTool = getToolByName(self.portal, 'portal_workflow')
self.workflowTool.setDefaultChain('one_state_workflow')
self.loginAsPortalOwner()
typetool = self.portal.portal_types typetool = self.portal.portal_types
typetool.constructContent('Document', self.portal, 'doc1') typetool.constructContent('Document', self.portal, 'doc1')
self.portal_discussion = getToolByName(self.portal, self.portal_discussion = getToolByName(self.portal,
'portal_discussion', 'portal_discussion',
None) None)
self.mtool = getToolByName(self.folder, 'portal_membership') self.membershipTool = getToolByName(self.folder, 'portal_membership')
self.memberdata = self.portal.portal_memberdata self.memberdata = self.portal.portal_memberdata
request = self.app.REQUEST
context = getattr(self.portal, 'doc1') context = getattr(self.portal, 'doc1')
self.viewlet = CommentsViewlet(context, request, None, None) self.viewlet = CommentsViewlet(context, self.request, None, None)
# Allow discussion # Allow discussion
registry = queryUtility(IRegistry) registry = queryUtility(IRegistry)
@ -241,19 +249,19 @@ class TestCommentsViewlet(PloneTestCase):
def test_can_reply(self): def test_can_reply(self):
# Portal owner can reply # Portal owner can reply
self.assertTrue(self.viewlet.can_reply()) self.assertTrue(self.viewlet.can_reply())
self.logout() logout()
# Anonymous users can not reply # Anonymous users can not reply
self.assertFalse(self.viewlet.can_reply()) self.assertFalse(self.viewlet.can_reply())
def test_can_review(self): def test_can_review(self):
# Portal owner has 'can review' permission # Portal owner has 'can review' permission
self.assertTrue(self.viewlet.can_review()) self.assertTrue(self.viewlet.can_review())
self.logout() logout()
# Anonymous has no 'can review' permission # Anonymous has no 'can review' permission
self.assertFalse(self.viewlet.can_review()) self.assertFalse(self.viewlet.can_review())
# The reviewer role has the 'Review comments' permission # The reviewer role has the 'Review comments' permission
self.portal.acl_users._doAddUser('reviewer', 'secret', ['Reviewer'], []) self.portal.acl_users._doAddUser('reviewer', 'secret', ['Reviewer'], [])
self.login('reviewer') login(self.portal, 'reviewer')
self.assertTrue(self.viewlet.can_review()) self.assertTrue(self.viewlet.can_review())
def test_can_manage(self): def test_can_manage(self):
@ -263,12 +271,12 @@ class TestCommentsViewlet(PloneTestCase):
""" """
# Portal owner has 'can review' permission # Portal owner has 'can review' permission
self.assertTrue(self.viewlet.can_manage()) self.assertTrue(self.viewlet.can_manage())
self.logout() logout()
# Anonymous has no 'can review' permission # Anonymous has no 'can review' permission
self.assertFalse(self.viewlet.can_manage()) self.assertFalse(self.viewlet.can_manage())
# The reviewer role has the 'Review comments' permission # The reviewer role has the 'Review comments' permission
self.portal.acl_users._doAddUser('reviewer', 'secret', ['Reviewer'], []) self.portal.acl_users._doAddUser('reviewer', 'secret', ['Reviewer'], [])
self.login('reviewer') login(self.portal, 'reviewer')
self.assertTrue(self.viewlet.can_manage()) self.assertTrue(self.viewlet.can_manage())
def test_is_discussion_allowed(self): def test_is_discussion_allowed(self):
@ -302,7 +310,7 @@ class TestCommentsViewlet(PloneTestCase):
"into clickable links.") "into clickable links.")
# Enable moderation workflow # Enable moderation workflow
self.portal.portal_workflow.setChainForPortalTypes( self.workflowTool.setChainForPortalTypes(
('Discussion Item',), ('Discussion Item',),
('comment_review_workflow,')) ('comment_review_workflow,'))
@ -344,7 +352,7 @@ class TestCommentsViewlet(PloneTestCase):
self.assertEqual( self.assertEqual(
len(tuple(self.viewlet.get_replies(workflow_actions=True))), 1) len(tuple(self.viewlet.get_replies(workflow_actions=True))), 1)
# Enable moderation workflow # Enable moderation workflow
self.portal.portal_workflow.setChainForPortalTypes( self.workflowTool.setChainForPortalTypes(
('Discussion Item',), ('Discussion Item',),
('comment_review_workflow,')) ('comment_review_workflow,'))
# Check if workflow actions are available # Check if workflow actions are available
@ -363,7 +371,7 @@ class TestCommentsViewlet(PloneTestCase):
portal_membership = getToolByName(self.portal, 'portal_membership') portal_membership = getToolByName(self.portal, 'portal_membership')
m = portal_membership.getAuthenticatedMember() m = portal_membership.getAuthenticatedMember()
self.assertEqual(self.viewlet.get_commenter_home_url(m.getUserName()), self.assertEqual(self.viewlet.get_commenter_home_url(m.getUserName()),
'http://nohost/plone/author/portal_owner') 'http://nohost/plone/author/test-user')
def test_get_commenter_home_url_is_none(self): def test_get_commenter_home_url_is_none(self):
self.assertFalse(self.viewlet.get_commenter_home_url()) self.assertFalse(self.viewlet.get_commenter_home_url())
@ -371,7 +379,7 @@ class TestCommentsViewlet(PloneTestCase):
def test_get_commenter_portrait(self): def test_get_commenter_portrait(self):
# Add a user with a member image # Add a user with a member image
self.mtool.addMember('jim', 'Jim', ['Member'], []) self.membershipTool.addMember('jim', 'Jim', ['Member'], [])
self.memberdata._setPortrait(Image(id='jim', self.memberdata._setPortrait(Image(id='jim',
file=dummy.File(), file=dummy.File(),
title=''), 'jim') title=''), 'jim')
@ -403,7 +411,7 @@ class TestCommentsViewlet(PloneTestCase):
def test_get_commenter_portrait_without_userimage(self): def test_get_commenter_portrait_without_userimage(self):
# Create a user without a user image # Create a user without a user image
self.mtool.addMember('jim', 'Jim', ['Member'], []) self.membershipTool.addMember('jim', 'Jim', ['Member'], [])
# Add a conversation with a comment # Add a conversation with a comment
conversation = IConversation(self.portal.doc1) conversation = IConversation(self.portal.doc1)
@ -443,7 +451,7 @@ class TestCommentsViewlet(PloneTestCase):
def test_is_anonymous(self): def test_is_anonymous(self):
self.assertFalse(self.viewlet.is_anonymous()) self.assertFalse(self.viewlet.is_anonymous())
self.logout() logout()
self.assertTrue(self.viewlet.is_anonymous()) self.assertTrue(self.viewlet.is_anonymous())
def test_login_action(self): def test_login_action(self):