Use configured portal timezone in tests

This commit is contained in:
Jon Pentland 2022-10-22 11:48:10 +02:00
parent b28d6d1f91
commit 194f2d9f76
2 changed files with 20 additions and 12 deletions

View File

@ -20,6 +20,7 @@ from Products.CMFCore.utils import getToolByName
from zope import interface from zope import interface
from zope.annotation.interfaces import IAnnotations from zope.annotation.interfaces import IAnnotations
from zope.component import createObject from zope.component import createObject
from zope.component import getUtility
from zope.component import queryUtility from zope.component import queryUtility
import unittest import unittest
@ -34,6 +35,11 @@ class ConversationTest(unittest.TestCase):
setRoles(self.portal, TEST_USER_ID, ["Manager"]) setRoles(self.portal, TEST_USER_ID, ["Manager"])
interface.alsoProvides(self.portal.REQUEST, IDiscussionLayer) interface.alsoProvides(self.portal.REQUEST, IDiscussionLayer)
# Set the portal timezone to something non-utc
reg_key = "plone.portal_timezone"
registry = getUtility(IRegistry)
registry[reg_key] = "Europe/Berlin"
self.typetool = self.portal.portal_types self.typetool = self.portal.portal_types
self.portal_discussion = getToolByName( self.portal_discussion = getToolByName(
self.portal, self.portal,
@ -90,23 +96,25 @@ class ConversationTest(unittest.TestCase):
new_id = conversation.addComment(comment) new_id = conversation.addComment(comment)
# Check that comments have timezones # Check that comments have the correct portal timezones
self.assertTrue(comment.creation_date.tzinfo) self.assertTrue(comment.creation_date.tzinfo,
self.assertTrue(comment.modification_date.tzinfo) tz.gettz("Europe/Berlin"))
self.assertTrue(comment.modification_date.tzinfo,
tz.gettz("Europe/Berlin"))
# Remove the timezone from the comment dates # Remove the timezone from the comment dates
comment.creation_date = comment.creation_date.replace(tzinfo=None) comment.creation_date = datetime.utcnow()
comment.modification_date = comment.modification_date.replace(tzinfo=None) comment.modification_date = datetime.utcnow()
# Check that the date is still correct # Check that the timezone naive date is converted to UTC
# See https://github.com/plone/plone.app.discussion/pull/204
self.assertTrue( self.assertTrue(
conversation.last_comment_date conversation.last_comment_date
- datetime.now().astimezone(tz.gettz(default_timezone())) - datetime.now().astimezone(timezone.utc)
< timedelta(seconds=1), < timedelta(seconds=1),
) )
# Check that comments still have timezones self.assertTrue(comment.creation_date.tzinfo, timezone.utc)
self.assertTrue(comment.creation_date.tzinfo) self.assertTrue(comment.modification_date.tzinfo, timezone.utc)
self.assertTrue(comment.modification_date.tzinfo)
def test_private_comment(self): def test_private_comment(self):
conversation = IConversation(self.portal.doc1) conversation = IConversation(self.portal.doc1)

View File

@ -138,7 +138,7 @@ class CommentIndexersTest(unittest.TestCase):
# Add a comment. Note: in real life, we always create comments via the # Add a comment. Note: in real life, we always create comments via the
# factory to allow different factories to be swapped in # factory to allow different factories to be swapped in
# Get the default timezone from the portal # Set the portal timezone to something non-utc
reg_key = "plone.portal_timezone" reg_key = "plone.portal_timezone"
registry = getUtility(IRegistry) registry = getUtility(IRegistry)
registry[reg_key] = "Europe/Berlin" registry[reg_key] = "Europe/Berlin"