Back to index

plone3  3.1.7
testPloneTool.py
Go to the documentation of this file.
00001 #
00002 # Tests the PloneTool
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 from Products.CMFPlone.tests import dummy
00007 
00008 from Products.CMFCore.utils import getToolByName
00009 from Acquisition import Implicit
00010 
00011 default_user = PloneTestCase.default_user
00012 portal_name = PloneTestCase.portal_name
00013 
00014 class DummyTitle(Implicit):
00015     def Title(self):
00016         # Should just return 'portal_catalog'
00017         tool = getToolByName(self, 'portal_catalog')
00018         # Use implicit acquisition even, the horror
00019         tool = self.portal_catalog
00020         return tool.getId()
00021     def getId(self):
00022         return 'foobar'
00023 
00024 
00025 class TestPloneTool(PloneTestCase.PloneTestCase):
00026 
00027     def afterSetUp(self):
00028         self.utils = self.portal.plone_utils
00029 
00030     def testvalidateSingleEmailAddress(self):
00031         # Any RFC 822 email address allowed, but address list must fail
00032         val = self.utils.validateSingleEmailAddress
00033         validInputs = (
00034             'user@example.org',
00035             'user@host.example.org',
00036             'm@t.nu',
00037             'USER@EXAMPLE.ORG',
00038             'USER@HOST.EXAMPLE.ORG',
00039             'USER@hoST.Example.Org',
00040         )
00041         invalidInputs = (
00042             'user@example.org, user2@example.org',   # only single address allowed
00043             'user@example.org,user2@example.org',
00044             'user@example.org\n\nfoo', # double new lines
00045             'user@example.org\n\rfoo',
00046             'user@example.org\r\nfoo',
00047             'user@example.org\r\rfoo',
00048             'user@example.org\nfoo@example.org', # only single address allowed, even if given one per line
00049             'user@example.org\nBcc: user@example.org',
00050             'user@example.org\nCc: user@example.org',
00051             'user@example.org\nSubject: Spam',
00052             # and a full email (note the missing ,!)
00053             'From: foo@example.org\n'
00054             'To: bar@example.org, spam@example.org\n'
00055             'Cc: egg@example.org\n'
00056             'Subject: Spam me plenty\n'
00057             'Spam Spam Spam\n'
00058             'I hate spam',
00059         )
00060         for address in validInputs:
00061             self.failUnless(val(address), '%s should validate' % address)
00062         for address in invalidInputs:
00063             self.failIf(val(address), '%s should fail' % address)
00064 
00065     def testvalidateEmailAddresses(self):
00066         # Any RFC 822 email address allowed and address list allowed
00067         val = self.utils.validateEmailAddresses
00068 
00069         validInputs = (
00070             # 'user',
00071             # 'user@example',
00072             'user@example.org',
00073             #'user@example.org\n user2',
00074             #'user@example.org\r user2',
00075             'user@example.org,\n user2@example.org',
00076             'user@example.org\n user2@example.org', # omitting comma is ok
00077             'USER@EXAMPLE.ORG,\n User2@Example.Org',
00078         )
00079         invalidInputs = (
00080             'user@example.org\n\nfoo', # double new lines
00081             'user@example.org\n\rfoo',
00082             'user@example.org\r\nfoo',
00083             'user@example.org\r\rfoo',
00084             #py stdlib bug? 'user@example.org\nuser2@example.org', # continuation line doesn't begin with white space
00085         )
00086         for address in validInputs:
00087             self.failUnless(val(address), '%s should validate' % address)
00088         for address in invalidInputs:
00089             self.failIf(val(address), '%s should fail' % address)
00090 
00091     def testEditFormatMetadataOfFile(self):
00092         # Test fix for http://dev.plone.org/plone/ticket/1323
00093         # Fixed in CMFDefault.File, not Plone.
00094         self.folder.invokeFactory('File', id='file')
00095         self.folder.file.edit(file=dummy.File('foo.zip'))
00096         self.assertEqual(self.folder.file.Format(), 'application/zip')
00097         self.assertEqual(self.folder.file.getFile().content_type, 'application/zip')
00098         # Changing the format should be reflected in content_type property
00099         self.utils.editMetadata(self.folder.file, format='image/gif')
00100         self.assertEqual(self.folder.file.Format(), 'image/gif')
00101         self.assertEqual(self.folder.file.getFile().content_type, 'image/gif')
00102 
00103     def testEditFormatMetadataOfImage(self):
00104         # Test fix for http://dev.plone.org/plone/ticket/1323
00105         # Fixed in CMFDefault.Image, not Plone.
00106         self.folder.invokeFactory('Image', id='image')
00107         self.folder.image.edit(file=dummy.Image('foo.zip'))
00108         self.assertEqual(self.folder.image.Format(), 'application/zip')
00109         self.assertEqual(self.folder.image.getImage().content_type, 'application/zip')
00110         # Changing the format should be reflected in content_type property
00111         self.utils.editMetadata(self.folder.image, format='image/gif')
00112         self.assertEqual(self.folder.image.Format(), 'image/gif')
00113         self.assertEqual(self.folder.image.getImage().content_type, 'image/gif')
00114 
00115     def testNormalizeStringPunctuation(self):
00116         # Punctuation and spacing is removed and replaced by '-'
00117         self.assertEqual(self.utils.normalizeString("a string with spaces"),
00118                          'a-string-with-spaces')
00119         self.assertEqual(self.utils.normalizeString("p.u,n;c(t)u!a@t#i$o%n"),
00120                          'p-u-n-c-t-u-a-t-i-o-n')
00121 
00122     def testNormalizeStringFileExtensions(self):
00123         # If there is something that looks like a file extensions
00124         # it will be preserved.
00125         self.assertEqual(self.utils.normalizeString("this is a file.gif"),
00126                          'this-is-a-file.gif')
00127         self.assertEqual(self.utils.normalizeString("this is. also. a file.html"),
00128                          'this-is-also-a-file.html')
00129 
00130     def testNormalizeStringIgnoredCharacters(self):
00131         # Some characters should be ignored
00132         self.assertEqual(self.utils.normalizeString("test'test"), 'testtest')
00133 
00134     def testNormalizeStringObject(self):
00135         # Objects should be converted to strings using repr()
00136         self.assertEqual(self.utils.normalizeString(None), 'none')
00137         self.assertEqual(self.utils.normalizeString(True), 'true')
00138         self.assertEqual(self.utils.normalizeString(False), 'false')
00139 
00140     def testNormalizeStringDangerousCharsInExtension(self):
00141         # Punctuation and spacing is removed and replaced by '-'
00142         self.assertEqual(self.utils.normalizeString("A String.a#b"),
00143                          'a-string-a-b')
00144 
00145     def testNormalizeStringRelaxedKeepsUppercaseAndSpaces(self):
00146         # Punctuation and spacing is removed and replaced by '-'
00147         self.assertEqual(self.utils.normalizeString("Capital Letters and Spaces", relaxed=True),
00148                          'Capital Letters and Spaces')
00149 
00150     def testNormalizeStringRelaxedStripsDangerousChars(self):
00151         # Punctuation and spacing is removed and replaced by '-'
00152         self.assertEqual(self.utils.normalizeString("A ?String&/\\foo.x!$#x", relaxed=True),
00153                          'A -String-foo.x-#x')
00154 
00155     def testNormalizeStringRelaxedKeepsOtherSymbols(self):
00156         # Punctuation and spacing is removed and replaced by '-'
00157         self.assertEqual(self.utils.normalizeString("A ?String&/\\.foo_#xx", relaxed=True),
00158                          'A -String-.foo_#xx')
00159 
00160     def testTypesToList(self):
00161         # Make sure typesToList() returns the expected types
00162         wl = self.utils.typesToList()
00163         self.failUnless('Folder' in wl)
00164         self.failUnless('Large Plone Folder' in wl)
00165         self.failUnless('Topic' in wl)
00166         self.failIf('ATReferenceCriterion' in wl)
00167 
00168 
00169 class TestOwnershipStuff(PloneTestCase.PloneTestCase):
00170 
00171     def afterSetUp(self):
00172         self.utils = self.portal.plone_utils
00173         self.membership = self.portal.portal_membership
00174         self.membership.addMember('new_owner', 'secret', ['Member'], [])
00175         self.folder.invokeFactory('Folder', 'folder1')
00176         self.folder1 = self.folder.folder1
00177         self.folder1.invokeFactory('Folder', 'folder2')
00178         self.folder2 = self.folder1.folder2
00179         self.folder2.invokeFactory('Folder', 'folder3')
00180         self.folder3 = self.folder2.folder3
00181 
00182     def assertList(self, result, expect):
00183         # Verifies lists have the same contents
00184         lhs = [r for r in result]
00185         lhs.sort()
00186         rhs = list(expect)
00187         rhs.sort()
00188         self.assertEqual(lhs, rhs)
00189 
00190     def testChangeOwnershipOf(self):
00191         # Should take ownership
00192         self.assertEqual(self.folder1.getOwnerTuple()[1], default_user)
00193         self.assertList(self.folder1.get_local_roles_for_userid(default_user),
00194                         ['Owner'])
00195 
00196         self.utils.changeOwnershipOf(self.folder1, 'new_owner')
00197         self.assertEqual(self.folder1.getOwnerTuple()[1], 'new_owner')
00198         self.assertList(self.folder1.get_local_roles_for_userid('new_owner'),
00199                         ['Owner'])
00200 
00201         # Initial creator no longer has Owner role.
00202         self.assertList(self.folder1.get_local_roles_for_userid(default_user), [])
00203 
00204     def testChangeOwnershipOfWithTopAclUsers(self):
00205         # Should be able to give ownership to a user in the top level
00206         # acl_users folder (even if this is not offered TTW).
00207         self.utils.changeOwnershipOf(self.folder1, 'portal_owner')
00208         self.assertEqual(self.folder1.getOwnerTuple()[1], 'portal_owner')
00209         self.assertList(self.folder1.get_local_roles_for_userid('portal_owner'),
00210                         ['Owner'])
00211 
00212         # Initial creator no longer has Owner role.
00213         self.assertList(self.folder1.get_local_roles_for_userid(default_user), [])
00214 
00215     def testChangeOwnershipOfKeepsOtherRoles(self):
00216         # Should preserve roles other than Owner
00217         self.folder1.manage_addLocalRoles('new_owner', ('Reviewer',))
00218         self.assertList(self.folder1.get_local_roles_for_userid('new_owner'),
00219                         ['Reviewer'])
00220         self.utils.changeOwnershipOf(self.folder1, 'new_owner')
00221         self.assertEqual(self.folder1.getOwnerTuple()[1], 'new_owner')
00222         self.assertList(self.folder1.get_local_roles_for_userid('new_owner'),
00223                         ['Owner', 'Reviewer'])
00224         self.assertList(self.folder1.get_local_roles_for_userid(default_user), [])
00225 
00226     def testChangeOwnershipOfRecursive(self):
00227         # Should take ownership of subobjects as well
00228         self.utils.changeOwnershipOf(self.folder1, 'new_owner', recursive=1)
00229         self.assertEqual(self.folder1.getOwnerTuple()[1], 'new_owner')
00230         self.assertList(self.folder1.get_local_roles_for_userid('new_owner'),
00231                         ['Owner'])
00232         self.assertList(self.folder1.get_local_roles_for_userid(default_user), [])
00233         self.assertEqual(self.folder2.getOwnerTuple()[1], 'new_owner')
00234         self.assertList(self.folder2.get_local_roles_for_userid('new_owner'),
00235                         ['Owner'])
00236         self.assertList(self.folder2.get_local_roles_for_userid(default_user), [])
00237         self.assertEqual(self.folder3.getOwnerTuple()[1], 'new_owner')
00238         self.assertList(self.folder3.get_local_roles_for_userid('new_owner'),
00239                         ['Owner'])
00240         self.assertList(self.folder3.get_local_roles_for_userid(default_user), [])
00241 
00242     def testGetOwnerName(self):
00243         # Test that getOwnerName gets the Owner name
00244         self.assertEqual(self.utils.getOwnerName(self.folder1), default_user)
00245         self.utils.changeOwnershipOf(self.folder1, 'new_owner')
00246         self.assertEqual(self.utils.getOwnerName(self.folder1), 'new_owner')
00247 
00248     def testGetInheritedLocalRoles(self):
00249         # Test basic local roles acquisition is dealt with by
00250         # getInheritedLocalRoles
00251         gILR = self.utils.getInheritedLocalRoles
00252         self.folder1.manage_addLocalRoles('new_owner', ('Reviewer',))
00253         # Test Normal role acquisition is returned
00254         filtered_roles = [r for r in gILR(self.folder2) if r[0] == 'new_owner'][0]
00255         self.assertList(filtered_roles[1], ['Reviewer'])
00256         filtered_roles = [r for r in gILR(self.folder3) if r[0] == 'new_owner'][0]
00257         self.assertList(filtered_roles[1], ['Reviewer'])
00258 
00259     def testGetInheritedLocalRolesMultiLevel(self):
00260         # Test for http://dev.plone.org/plone/ticket/3901
00261         # make sure local roles are picked up from all folders in tree.
00262         gILR = self.utils.getInheritedLocalRoles
00263         self.folder1.manage_addLocalRoles('new_owner', ('Reviewer',))
00264         self.folder2.manage_addLocalRoles('new_owner', ('Owner',))
00265 
00266         # folder2 should have only the inherited role
00267         filtered_roles = [r for r in gILR(self.folder2) if r[0] == 'new_owner'][0]
00268         self.assertList(filtered_roles[1], ['Reviewer'])
00269 
00270         # folder3 should have roles inherited from parent and grand-parent
00271         filtered_roles = [r for r in gILR(self.folder3) if r[0] == 'new_owner'][0]
00272         self.assertList(filtered_roles[1], ['Owner','Reviewer'])
00273 
00274 
00275 class TestEditMetadata(PloneTestCase.PloneTestCase):
00276 
00277     def afterSetUp(self):
00278         self.utils = self.portal.plone_utils
00279         self.folder.invokeFactory('Document', id='doc')
00280         self.doc = self.folder.doc
00281 
00282     def testSetTitle(self):
00283         self.assertEqual(self.doc.Title(), '')
00284         self.utils.editMetadata(self.doc, title='Foo')
00285         self.assertEqual(self.doc.Title(), 'Foo')
00286 
00287     def testClearTitle(self):
00288         self.utils.editMetadata(self.doc, title='Foo')
00289         self.assertEqual(self.doc.Title(), 'Foo')
00290         self.utils.editMetadata(self.doc, title='')
00291         self.assertEqual(self.doc.Title(), '')
00292 
00293     def testSetDescription(self):
00294         self.assertEqual(self.doc.Description(), '')
00295         self.utils.editMetadata(self.doc, description='Foo')
00296         self.assertEqual(self.doc.Description(), 'Foo')
00297 
00298     def testClearDescription(self):
00299         self.utils.editMetadata(self.doc, description='Foo')
00300         self.assertEqual(self.doc.Description(), 'Foo')
00301         self.utils.editMetadata(self.doc, description='')
00302         self.assertEqual(self.doc.Description(), '')
00303 
00304     def testSetSubject(self):
00305         self.assertEqual(self.doc.Subject(), ())
00306         self.utils.editMetadata(self.doc, subject=['Foo'])
00307         self.assertEqual(self.doc.Subject(), ('Foo',))
00308 
00309     def testClearSubject(self):
00310         self.utils.editMetadata(self.doc, subject=['Foo'])
00311         self.assertEqual(self.doc.Subject(), ('Foo',))
00312         self.utils.editMetadata(self.doc, subject=[])
00313         self.assertEqual(self.doc.Subject(), ())
00314 
00315     def testSetContributors(self):
00316         self.assertEqual(self.doc.Contributors(), ())
00317         self.utils.editMetadata(self.doc, contributors=['Foo'])
00318         self.assertEqual(self.doc.Contributors(), ('Foo',))
00319 
00320     def testClearContributors(self):
00321         self.utils.editMetadata(self.doc, contributors=['Foo'])
00322         self.assertEqual(self.doc.Contributors(), ('Foo',))
00323         self.utils.editMetadata(self.doc, contributors=[])
00324         self.assertEqual(self.doc.Contributors(), ())
00325 
00326     def testSetFormat(self):
00327         self.assertEqual(self.doc.Format(), 'text/html')
00328         self.assertEqual(self.doc.text_format, 'text/html')
00329         self.utils.editMetadata(self.doc, format='text/x-rst')
00330         self.assertEqual(self.doc.Format(), 'text/x-rst')
00331         self.assertEqual(self.doc.text_format, 'text/x-rst')
00332 
00333     def testClearFormat(self):
00334         self.utils.editMetadata(self.doc, format='text/x-rst')
00335         self.assertEqual(self.doc.Format(), 'text/x-rst')
00336         self.assertEqual(self.doc.text_format, 'text/x-rst')
00337         self.utils.editMetadata(self.doc, format='')
00338         self.assertEqual(self.doc.Format(), 'text/html')
00339         self.assertEqual(self.doc.text_format, 'text/html')
00340 
00341     def testSetLanguage(self):
00342         self.assertEqual(self.doc.Language(), '')
00343         self.utils.editMetadata(self.doc, language='de')
00344         self.assertEqual(self.doc.Language(), 'de')
00345 
00346     def testClearLanguage(self):
00347         self.utils.editMetadata(self.doc, language='de')
00348         self.assertEqual(self.doc.Language(), 'de')
00349         self.utils.editMetadata(self.doc, language='')
00350         self.assertEqual(self.doc.Language(), '')
00351 
00352     def testSetRights(self):
00353         self.assertEqual(self.doc.Rights(), '')
00354         self.utils.editMetadata(self.doc, rights='Foo')
00355         self.assertEqual(self.doc.Rights(), 'Foo')
00356 
00357     def testClearRights(self):
00358         self.utils.editMetadata(self.doc, rights='Foo')
00359         self.assertEqual(self.doc.Rights(), 'Foo')
00360         self.utils.editMetadata(self.doc, rights='')
00361         self.assertEqual(self.doc.Rights(), '')
00362 
00363     # Also test the various dates
00364 
00365     def testSetEffectiveDate(self):
00366         self.assertEqual(self.doc.EffectiveDate(), 'None')
00367         self.utils.editMetadata(self.doc, effective_date='2001-01-01')
00368         self.assertEqual(self.doc.EffectiveDate(zone='UTC'), '2001-01-01 00:00:00')
00369 
00370     def testClearEffectiveDate(self):
00371         self.utils.editMetadata(self.doc, effective_date='2001-01-01')
00372         self.assertEqual(self.doc.EffectiveDate(zone='UTC'), '2001-01-01 00:00:00')
00373         self.utils.editMetadata(self.doc, effective_date='None')
00374         self.assertEqual(self.doc.EffectiveDate(), 'None')
00375         self.assertEqual(self.doc.effective_date, None)
00376 
00377     def testSetExpirationDate(self):
00378         self.assertEqual(self.doc.ExpirationDate(), 'None')
00379         self.utils.editMetadata(self.doc, expiration_date='2001-01-01')
00380         self.assertEqual(self.doc.ExpirationDate(zone='UTC'), '2001-01-01 00:00:00')
00381 
00382     def testClearExpirationDate(self):
00383         self.utils.editMetadata(self.doc, expiration_date='2001-01-01')
00384         self.assertEqual(self.doc.ExpirationDate(zone='UTC'), '2001-01-01 00:00:00')
00385         self.utils.editMetadata(self.doc, expiration_date='None')
00386         self.assertEqual(self.doc.ExpirationDate(), 'None')
00387         self.assertEqual(self.doc.expiration_date, None)
00388 
00389     # Test special cases of tuplification
00390 
00391     def testTuplifySubject_1(self):
00392         self.utils.editMetadata(self.doc, subject=['Foo', 'Bar', 'Baz'])
00393         self.assertEqual(self.doc.Subject(), ('Foo', 'Bar', 'Baz'))
00394 
00395     def testTuplifySubject_2(self):
00396         self.utils.editMetadata(self.doc, subject=['Foo', '', 'Bar', 'Baz'])
00397         # Note that empty entries are filtered
00398         self.assertEqual(self.doc.Subject(), ('Foo', 'Bar', 'Baz'))
00399 
00400     def DISABLED_testTuplifySubject_3(self):
00401         self.utils.editMetadata(self.doc, subject='Foo, Bar, Baz')
00402         # TODO: Wishful thinking
00403         self.assertEqual(self.doc.Subject(), ('Foo', 'Bar', 'Baz'))
00404 
00405     def testTuplifyContributors_1(self):
00406         self.utils.editMetadata(self.doc, contributors=['Foo', 'Bar', 'Baz'])
00407         self.assertEqual(self.doc.Contributors(), ('Foo', 'Bar', 'Baz'))
00408 
00409     def testTuplifyContributors_2(self):
00410         self.utils.editMetadata(self.doc, contributors=['Foo', '', 'Bar', 'Baz'])
00411         # Note that empty entries are filtered
00412         self.assertEqual(self.doc.Contributors(), ('Foo', 'Bar', 'Baz'))
00413 
00414     def DISABLED_testTuplifyContributors_3(self):
00415         self.utils.editMetadata(self.doc, contributors='Foo; Bar; Baz')
00416         # TODO: Wishful thinking
00417         self.assertEqual(self.doc.Contributors(), ('Foo', 'Bar', 'Baz'))
00418 
00419 
00420 class TestEditMetadataIndependence(PloneTestCase.PloneTestCase):
00421 
00422     def afterSetUp(self):
00423         self.utils = self.portal.plone_utils
00424         self.folder.invokeFactory('Document', id='doc')
00425         self.doc = self.folder.doc
00426         self.utils.editMetadata(self.doc,
00427                                 title='Foo',
00428                                 subject=('Bar',),
00429                                 description='Baz',
00430                                 contributors=('Fred',),
00431                                 effective_date='2001-01-01',
00432                                 expiration_date='2003-01-01',
00433                                 format='text/html',
00434                                 language='de',
00435                                 rights='Copyleft',
00436                                )
00437 
00438     def testEditTitleOnly(self):
00439         self.utils.editMetadata(self.doc, title='Oh Happy Day')
00440         self.assertEqual(self.doc.Title(), 'Oh Happy Day')
00441         # Other elements must not change
00442         self.assertEqual(self.doc.Subject(), ('Bar',))
00443         self.assertEqual(self.doc.Description(), 'Baz')
00444         self.assertEqual(self.doc.Contributors(), ('Fred',))
00445         self.assertEqual(self.doc.EffectiveDate(zone='UTC'), '2001-01-01 00:00:00')
00446         self.assertEqual(self.doc.ExpirationDate(zone='UTC'), '2003-01-01 00:00:00')
00447         self.assertEqual(self.doc.Format(), 'text/html')
00448         self.assertEqual(self.doc.Language(), 'de')
00449         self.assertEqual(self.doc.Rights(), 'Copyleft')
00450 
00451     def testEditSubjectOnly(self):
00452         self.utils.editMetadata(self.doc, subject=('Oh', 'Happy', 'Day'))
00453         self.assertEqual(self.doc.Subject(), ('Oh', 'Happy', 'Day'))
00454         # Other elements must not change
00455         self.assertEqual(self.doc.Title(), 'Foo')
00456         self.assertEqual(self.doc.Description(), 'Baz')
00457         self.assertEqual(self.doc.Contributors(), ('Fred',))
00458         self.assertEqual(self.doc.EffectiveDate(zone='UTC'), '2001-01-01 00:00:00')
00459         self.assertEqual(self.doc.ExpirationDate(zone='UTC'), '2003-01-01 00:00:00')
00460         self.assertEqual(self.doc.Format(), 'text/html')
00461         self.assertEqual(self.doc.Language(), 'de')
00462         self.assertEqual(self.doc.Rights(), 'Copyleft')
00463 
00464     def testEditEffectiveDateOnly(self):
00465         self.utils.editMetadata(self.doc, effective_date='2001-12-31')
00466         self.assertEqual(self.doc.EffectiveDate(zone='UTC'), '2001-12-31 00:00:00')
00467         # Other elements must not change
00468         self.assertEqual(self.doc.Title(), 'Foo')
00469         self.assertEqual(self.doc.Subject(), ('Bar',))
00470         self.assertEqual(self.doc.Description(), 'Baz')
00471         self.assertEqual(self.doc.Contributors(), ('Fred',))
00472         self.assertEqual(self.doc.ExpirationDate(zone='UTC'), '2003-01-01 00:00:00')
00473         self.assertEqual(self.doc.Format(), 'text/html')
00474         self.assertEqual(self.doc.Language(), 'de')
00475         self.assertEqual(self.doc.Rights(), 'Copyleft')
00476 
00477     def testEditLanguageOnly(self):
00478         self.utils.editMetadata(self.doc, language='fr')
00479         self.assertEqual(self.doc.Language(), 'fr')
00480         # Other elements must not change
00481         self.assertEqual(self.doc.Title(), 'Foo')
00482         self.assertEqual(self.doc.Subject(), ('Bar',))
00483         self.assertEqual(self.doc.Description(), 'Baz')
00484         self.assertEqual(self.doc.Contributors(), ('Fred',))
00485         self.assertEqual(self.doc.EffectiveDate(zone='UTC'), '2001-01-01 00:00:00')
00486         self.assertEqual(self.doc.ExpirationDate(zone='UTC'), '2003-01-01 00:00:00')
00487         self.assertEqual(self.doc.Format(), 'text/html')
00488         self.assertEqual(self.doc.Rights(), 'Copyleft')
00489 
00490 
00491 class TestFormulatorFields(PloneTestCase.PloneTestCase):
00492     """This feature should probably go away entirely.
00493     """
00494 
00495     def afterSetUp(self):
00496         self.utils = self.portal.plone_utils
00497         self.folder.invokeFactory('Document', id='doc')
00498         self.doc = self.folder.doc
00499 
00500     def setField(self, name, value):
00501         form = self.app.REQUEST.form
00502         pfx = self.utils.field_prefix
00503         form[pfx+name] = value
00504 
00505     def testTitleField(self):
00506         self.setField('title', 'Foo')
00507         self.utils.editMetadata(self.doc)
00508         self.assertEqual(self.doc.Title(), 'Foo')
00509 
00510     def testSubjectField(self):
00511         self.setField('subject', ['Foo', 'Bar', 'Baz'])
00512         self.utils.editMetadata(self.doc)
00513         self.assertEqual(self.doc.Subject(), ('Foo', 'Bar', 'Baz'))
00514 
00515     def testEffectiveDateField(self):
00516         self.setField('effective_date', '2001-01-01')
00517         self.utils.editMetadata(self.doc)
00518         self.assertEqual(self.doc.EffectiveDate(zone='UTC'), '2001-01-01 00:00:00')
00519 
00520     def testLanguageField(self):
00521         self.setField('language', 'de')
00522         self.utils.editMetadata(self.doc)
00523         # TODO: Note that language, format, and rights do not 
00524         #      receive the Formulator treatment.
00525         self.assertEqual(self.doc.Language(), '')
00526 
00527 
00528 class TestBreadCrumbs(PloneTestCase.PloneTestCase):
00529     '''Tests for the portal tabs query'''
00530 
00531     def afterSetUp(self):
00532         self.utils = self.portal.plone_utils
00533         self.populateSite()
00534 
00535     def populateSite(self):
00536         self.setRoles(['Manager'])
00537         self.portal.invokeFactory('Folder', 'folder1')
00538         folder1 = getattr(self.portal, 'folder1')
00539         folder1.invokeFactory('Document', 'doc11')
00540         folder1.invokeFactory('File', 'file11')
00541         self.setRoles(['Member'])
00542 
00543     def testCreateBreadCrumbs(self):
00544         # See if we can create one at all
00545         doc = self.portal.folder1.doc11
00546         crumbs = self.utils.createBreadCrumbs(doc)
00547         self.failUnless(crumbs)
00548         self.assertEqual(len(crumbs), 2)
00549         self.assertEqual(crumbs[-1]['absolute_url'], doc.absolute_url())
00550         self.assertEqual(crumbs[-2]['absolute_url'], doc.aq_parent.absolute_url())
00551 
00552     def testBreadcrumbsRespectTypesWithViewAction(self):
00553         # With a type in typesUseViewActionInListings as current action it
00554         # should return a breadcrumb which has '/view' appended to the url
00555         file = self.portal.folder1.file11
00556         crumbs = self.utils.createBreadCrumbs(file)
00557         self.failUnless(crumbs)
00558         self.assertEqual(crumbs[-1]['absolute_url'][-5:],'/view')
00559 
00560 
00561 class TestIDGenerationMethods(PloneTestCase.PloneTestCase):
00562     """Tests the isIDAutoGenerated method and pretty_title_or_id
00563     """
00564 
00565     def afterSetUp(self):
00566         self.utils = self.portal.plone_utils
00567 
00568     def testAutoGeneratedId(self):
00569         r = self.utils.isIDAutoGenerated('document.2004-11-09.0123456789')
00570         self.assertEqual(r, True)
00571 
00572     def testValidPortalTypeNameButNotAutoGeneratedId(self):
00573         # This was raising an IndexError exception for
00574         # Zope < 2.7.3 (DateTime.py < 1.85.12.11) and a
00575         # SyntaxError for Zope >= 2.7.3 (DateTime.py >= 1.85.12.11)
00576         r = self.utils.isIDAutoGenerated('document.tar.gz')
00577         self.assertEqual(r, False)
00578         r = self.utils.isIDAutoGenerated('document.tar.12/32/2004')
00579         self.assertEqual(r, False)
00580         r = self.utils.isIDAutoGenerated('document.tar.12/31/2004 12:62')
00581         self.assertEqual(r, False)
00582 
00583     def test_pretty_title_or_id_returns_title(self):
00584         self.folder.setTitle('My pretty title')
00585         self.assertEqual(self.utils.pretty_title_or_id(self.folder),
00586                          'My pretty title')
00587 
00588     def test_pretty_title_or_id_returns_id(self):
00589         self.folder.setTitle('')
00590         self.assertEqual(self.utils.pretty_title_or_id(self.folder),
00591                          self.folder.getId())
00592 
00593     def test_pretty_title_or_id_when_autogenerated(self):
00594         self.setRoles(['Manager','Member'])
00595         self.folder.setTitle('')
00596         self.folder.setId('folder.2004-11-09.0123456789')
00597         self.assertEqual(self.utils.pretty_title_or_id(self.folder),
00598                          self.utils.getEmptyTitle())
00599         self.assertEqual(self.utils.pretty_title_or_id(self.folder, 'Marker'),
00600                                 'Marker')
00601 
00602     def test_pretty_title_or_id_works_with_method_that_needs_context(self):
00603         self.setRoles(['Manager','Member'])
00604         # Create a dummy class that looks at it's context to find the title
00605         new_obj = DummyTitle()
00606         new_obj = new_obj.__of__(self.folder)
00607         try:
00608             title = self.utils.pretty_title_or_id(new_obj)
00609         except AttributeError, e:
00610             self.fail('pretty_title_or_id failed to include context %s'%e)
00611         self.assertEqual(title, 'portal_catalog')
00612 
00613     def test_pretty_title_or_id_on_catalog_brain(self):
00614         cat = self.portal.portal_catalog
00615         self.setRoles(['Manager','Member'])
00616         self.folder.edit(title='My pretty title', subject='foobar')
00617         results = cat(Subject='foobar')
00618         self.assertEqual(len(results), 1)
00619         self.assertEqual(self.utils.pretty_title_or_id(results[0]),
00620                                                         'My pretty title')
00621 
00622     def test_pretty_title_or_id_on_catalog_brain_returns_id(self):
00623         cat = self.portal.portal_catalog
00624         self.setRoles(['Manager','Member'])
00625         self.folder.edit(title='', subject='foobar')
00626         results = cat(Subject='foobar')
00627         self.assertEqual(len(results), 1)
00628         self.assertEqual(self.utils.pretty_title_or_id(results[0]),
00629                                                         self.folder.getId())
00630 
00631     def test_pretty_title_or_id_on_catalog_brain_autogenerated(self):
00632         cat = self.portal.portal_catalog
00633         self.setRoles(['Manager','Member'])
00634         self.folder.edit(id='folder.2004-11-09.0123456789',
00635                          title='', subject='foobar')
00636         results = cat(Subject='foobar')
00637         self.assertEqual(len(results), 1)
00638         self.assertEqual(self.utils.pretty_title_or_id(results[0], 'Marker'),
00639                                                         'Marker')
00640 
00641     def test_pretty_title_or_id_on_catalog_brain_no_title(self):
00642         cat = self.portal.portal_catalog
00643         self.setRoles(['Manager','Member'])
00644         # Remove Title from catalog metadata to simulate a catalog with no
00645         # Title metadata and similar pathological cases.
00646         cat.delColumn('Title')
00647         self.folder.edit(title='',subject='foobar')
00648         results = cat(Subject='foobar')
00649         self.assertEqual(len(results), 1)
00650         # Give the portal a title because this is what will show up on
00651         # failure
00652         self.portal.title = 'This is not the title you are looking for'
00653         self.assertEqual(self.utils.pretty_title_or_id(results[0]),
00654                                                         self.folder.getId())
00655 
00656     def testGetMethodAliases(self):
00657         fti = self.folder.getTypeInfo()
00658         expectedAliases = fti.getMethodAliases()
00659         aliases = self.utils.getMethodAliases(fti)
00660         self.assertEqual(len(expectedAliases), len(aliases))
00661         for k, v in aliases.items():
00662             self.assertEqual(expectedAliases[k], v)
00663 
00664 class TestPortalStatusMessages(PloneTestCase.PloneTestCase):
00665     '''Tests the portal status messages methods'''
00666 
00667     def afterSetUp(self):
00668         self.utils = self.portal.plone_utils
00669 
00670     # XXX these don't work anymore as the cookie handling needs a real
00671     # request/response to take effect
00672     def DISABLED_testPortalMessages(self):
00673         # make sure there's no stored message
00674         self.assertEqual(len(self.utils.showPortalMessages()), 0)
00675         # add one message
00676         self.utils.addPortalMessage(u'message', type=u'info')
00677         msgs = self.utils.showPortalMessages()
00678         self.assertEqual(len(msgs), 1)
00679         self.assertEqual(msgs[0].message, u'message')
00680         self.assertEqual(msgs[0].type, u'info')
00681         # make sure messages are removed
00682         msgs = self.utils.showPortalMessages()
00683         self.assertEqual(len(msgs), 0)
00684         # add two messages
00685         self.utils.addPortalMessage(u'message1', type=u'info')
00686         self.utils.addPortalMessage(u'message2', type=u'warning')
00687         msgs = self.utils.showPortalMessages()
00688         self.assertEqual(len(msgs), 2)
00689         self.assertEqual(msgs[0].message, u'message1')
00690         self.assertEqual(msgs[0].type, u'info')
00691         self.assertEqual(msgs[1].message, u'message2')
00692         self.assertEqual(msgs[1].type, u'warning')
00693         # make sure there's no stored message
00694         self.assertEqual(len(self.utils.showPortalMessages()), 0)
00695 
00696 
00697 def test_suite():
00698     from unittest import TestSuite, makeSuite
00699     suite = TestSuite()
00700     suite.addTest(makeSuite(TestPloneTool))
00701     suite.addTest(makeSuite(TestOwnershipStuff))
00702     suite.addTest(makeSuite(TestEditMetadata))
00703     suite.addTest(makeSuite(TestEditMetadataIndependence))
00704     suite.addTest(makeSuite(TestFormulatorFields))
00705     suite.addTest(makeSuite(TestBreadCrumbs))
00706     suite.addTest(makeSuite(TestIDGenerationMethods))
00707     suite.addTest(makeSuite(TestPortalStatusMessages))
00708     return suite