Back to index

plone3  3.1.7
test_DublinCore.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Unit tests for DublinCore module.
00014 
00015 $Id: test_DublinCore.py 77186 2007-06-28 19:06:19Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from AccessControl.SecurityManagement import newSecurityManager
00022 from Acquisition import Implicit
00023 from DateTime.DateTime import DateTime
00024 from zope.testing.cleanup import cleanUp
00025 
00026 from Products.CMFCore.tests.base.dummy import DummySite
00027 from Products.CMFCore.tests.base.dummy import DummyUserFolder
00028 from Products.CMFCore.tests.base.testcase import SecurityTest
00029 from Products.CMFDefault.MembershipTool import MembershipTool
00030 
00031 
00032 def _DateIndexConvert(value):
00033     # Duplicate date conversion done by DateIndex._convert
00034     t_tup = value.toZone('UTC').parts()
00035     yr = t_tup[0]
00036     mo = t_tup[1]
00037     dy = t_tup[2]
00038     hr = t_tup[3]
00039     mn = t_tup[4]
00040     t_val = ((((yr * 12 + mo) * 31 + dy) * 24 + hr) * 60 + mn)
00041 
00042     if isinstance(t_val, long):
00043         # t_val must be IntType, not LongType
00044         raise OverflowError("Date too big: %s" % `value`)
00045 
00046     return t_val
00047 
00048 
00049 class DummyMetadataTool(Implicit):
00050 
00051     def __init__(self, publisher):
00052         self._publisher = publisher
00053 
00054     def getPublisher(self):
00055         return self._publisher
00056 
00057 
00058 class DublinCoreTests(SecurityTest):
00059 
00060     def _makeDummyContent(self, id, *args, **kw):
00061         from Products.CMFCore.PortalContent import PortalContent
00062         from Products.CMFDefault.DublinCore import DefaultDublinCoreImpl
00063 
00064         class DummyContent(PortalContent, DefaultDublinCoreImpl):
00065             pass
00066 
00067         return DummyContent(id, *args, **kw)
00068 
00069     def tearDown(self):
00070         cleanUp()
00071         SecurityTest.tearDown(self)
00072 
00073     def test_z2interfaces(self):
00074         from Interface.Verify import verifyClass
00075         from Products.CMFCore.interfaces.DublinCore \
00076                 import CatalogableDublinCore as ICatalogableDublinCore
00077         from Products.CMFCore.interfaces.DublinCore \
00078                 import DublinCore as IDublinCore
00079         from Products.CMFCore.interfaces.DublinCore \
00080                 import MutableDublinCore as IMutableDublinCore
00081         from Products.CMFDefault.DublinCore import DefaultDublinCoreImpl
00082 
00083         verifyClass(ICatalogableDublinCore, DefaultDublinCoreImpl)
00084         verifyClass(IDublinCore, DefaultDublinCoreImpl)
00085         verifyClass(IMutableDublinCore, DefaultDublinCoreImpl)
00086 
00087     def test_z3interfaces(self):
00088         from zope.interface.verify import verifyClass
00089         from Products.CMFCore.interfaces import ICatalogableDublinCore
00090         from Products.CMFCore.interfaces import IDublinCore
00091         from Products.CMFCore.interfaces import IMutableDublinCore
00092         from Products.CMFDefault.DublinCore import DefaultDublinCoreImpl
00093 
00094         verifyClass(ICatalogableDublinCore, DefaultDublinCoreImpl)
00095         verifyClass(IDublinCore, DefaultDublinCoreImpl)
00096         verifyClass(IMutableDublinCore, DefaultDublinCoreImpl)
00097 
00098     def test_notifyModified(self):
00099         site = DummySite('site').__of__(self.root)
00100         acl_users = site._setObject( 'acl_users', DummyUserFolder() )
00101         site._setObject( 'portal_membership', MembershipTool() )
00102         newSecurityManager(None, acl_users.user_foo)
00103         item = self._makeDummyContent('item').__of__(site)
00104         self.assertEqual( item.listCreators(), () )
00105         item.setModificationDate(0)
00106         initial_date = item.ModificationDate()
00107 
00108         item.notifyModified()
00109         self.assertEqual( item.listCreators(), ('user_foo',) )
00110         self.assertNotEqual( item.ModificationDate(), initial_date )
00111 
00112     def test_creators_methods(self):
00113         site = DummySite('site').__of__(self.root)
00114         acl_users = site._setObject( 'acl_users', DummyUserFolder() )
00115         site._setObject( 'portal_membership', MembershipTool() )
00116         newSecurityManager(None, acl_users.user_foo)
00117         item = self._makeDummyContent('item').__of__(site)
00118         self.assertEqual( item.listCreators(), () )
00119 
00120         item.addCreator()
00121         self.assertEqual( item.listCreators(), ('user_foo',) )
00122         newSecurityManager(None, acl_users.user_bar)
00123         item.addCreator()
00124         self.assertEqual( item.listCreators(), ('user_foo', 'user_bar') )
00125         item.addCreator()
00126         self.assertEqual( item.listCreators(), ('user_foo', 'user_bar') )
00127         item.addCreator('user_baz')
00128         self.assertEqual( item.listCreators(),
00129                           ('user_foo', 'user_bar', 'user_baz') )
00130         item.setCreators('user_bar')
00131         self.assertEqual( item.listCreators(), ('user_bar',) )
00132         item.setCreators( ('user_baz',) )
00133         self.assertEqual( item.listCreators(), ('user_baz',) )
00134 
00135     def test_creators_upgrade(self):
00136         site = DummySite('site').__of__(self.root)
00137         acl_users = site._setObject( 'acl_users', DummyUserFolder() )
00138         site._setObject( 'portal_membership', MembershipTool() )
00139         newSecurityManager(None, acl_users.user_foo)
00140         item = self._makeDummyContent('item').__of__(site)
00141         item.manage_fixupOwnershipAfterAdd()
00142         # fake an old object < CMF 1.5 without creators
00143         delattr(item, 'creators')
00144         self.assertEqual(item.Creator(), 'user_foo')
00145         newSecurityManager(None, acl_users.user_bar)
00146         item.addCreator()
00147         self.assertEqual(item.Creator(), 'user_foo')
00148         self.assertEqual(item.listCreators(), ('user_foo', 'user_bar'))
00149         # or if added directly
00150         delattr(item, 'creators')
00151         item.addCreator()
00152         self.assertEqual(item.Creator(), 'user_foo')
00153         self.assertEqual(item.listCreators(), ('user_foo', 'user_bar'))
00154 
00155     def test_ceiling_parsable(self):
00156         # Test that a None ceiling date will be parsable by a DateIndex
00157         site = DummySite('site').__of__(self.root)
00158         item = self._makeDummyContent('item').__of__(site)
00159         self.assertEqual(item.expiration_date, None)
00160         self.assert_(_DateIndexConvert(item.expires()))
00161 
00162     def test_publisher_no_metadata_tool(self):
00163         site = DummySite('site').__of__(self.root)
00164         item = self._makeDummyContent('item').__of__(site)
00165         self.assertEqual(item.Publisher(), 'No publisher')
00166 
00167     def test_publisher_with_metadata_tool(self):
00168         PUBLISHER = 'Some Publisher'
00169         site = DummySite('site').__of__(self.root)
00170         site.portal_metadata = DummyMetadataTool(publisher=PUBLISHER)
00171         item = self._makeDummyContent('item').__of__(site)
00172         self.assertEqual(item.Publisher(), PUBLISHER)
00173 
00174     def test_timezone_metadata(self):
00175         # http://www.zope.org/Collectors/CMF/325
00176         # If an item's timestamp(s) are stored in another timezone,
00177         # e.g. 4 hours further away from UTC, the DC date methods
00178         # should still return it in the local timezone so that all
00179         # user-visible dates can be compared to each other by eye.
00180         site = DummySite('site').__of__(self.root)
00181         item = self._makeDummyContent('item').__of__(site)
00182         dates_and_methods = (
00183             ('modification_date', 'ModificationDate'),
00184             ('effective_date', 'EffectiveDate'),
00185             ('effective_date', 'Date'),
00186             ('expiration_date', 'ExpirationDate'),
00187             ('creation_date', 'CreationDate'))
00188         offset = 4  # arbitrary, any value should work.
00189         for datename, dc_methodname in dates_and_methods:
00190             orig = getattr(item, datename)
00191             # Some default to None, fix that.
00192             if orig is None:
00193                 orig = DateTime()
00194                 setattr(item, datename, orig)
00195             orig_DC = getattr(item, dc_methodname)()
00196             # Change the timezone of the date.
00197             local_offset = orig.tzoffset() % (3600*24)
00198             other_offset = (local_offset + offset) % 24
00199             otherzone = 'GMT+%d' % other_offset
00200             setattr(item, datename, orig.toZone(otherzone))
00201             # Finally, verify that display has not changed.
00202             new_DC = getattr(item, dc_methodname)()
00203             self.assertEqual(orig_DC, new_DC)
00204 
00205     def test_Date_with_explicit_timezone(self):
00206         item = self._makeDummyContent('item')
00207         item.effective_date = DateTime('2007-01-01T12:00:00Z')
00208         self.assertEqual(item.Date('US/Eastern'),
00209                          '2007-01-01 07:00:00')
00210 
00211     def test_CreationDate_with_explicit_timezone(self):
00212         item = self._makeDummyContent('item')
00213         item.creation_date = DateTime('2007-01-01T12:00:00Z')
00214         self.assertEqual(item.CreationDate('US/Eastern'),
00215                          '2007-01-01 07:00:00')
00216 
00217     def test_ModificationDate_with_explicit_timezone(self):
00218         item = self._makeDummyContent('item')
00219         item.modification_date = DateTime('2007-01-01T12:00:00Z')
00220         self.assertEqual(item.ModificationDate('US/Eastern'),
00221                          '2007-01-01 07:00:00')
00222 
00223     def test_EffectiveDate_with_explicit_timezone(self):
00224         item = self._makeDummyContent('item')
00225         item.effective_date = DateTime('2007-01-01T12:00:00Z')
00226         self.assertEqual(item.EffectiveDate('US/Eastern'),
00227                          '2007-01-01 07:00:00')
00228 
00229     def test_ExpirationDate_with_explicit_timezone(self):
00230         item = self._makeDummyContent('item')
00231         item.expiration_date = DateTime('2007-01-01T12:00:00Z')
00232         self.assertEqual(item.ExpirationDate('US/Eastern'),
00233                          '2007-01-01 07:00:00')
00234 
00235 def test_suite():
00236     return unittest.TestSuite((
00237         unittest.makeSuite(DublinCoreTests),
00238         ))
00239 
00240 if __name__ == '__main__':
00241     unittest.main(defaultTest='test_suite')