Back to index

plone3  3.1.7
testContentTypeScripts.py
Go to the documentation of this file.
00001 #
00002 # Tests the content type scripts
00003 #
00004 
00005 from AccessControl import Unauthorized
00006 from Products.CMFPlone.tests import PloneTestCase
00007 from Products.CMFPlone.tests import dummy
00008 
00009 AddPortalTopics = 'Add portal topics'
00010 import transaction
00011 from OFS.CopySupport import CopyError
00012 
00013 #    NOTE
00014 #    document, link, and newsitem edit's are now validated
00015 #    so we must pass in fields that the validators need
00016 #    such as title on a favorite's link_edit
00017 
00018 
00019 class TestContentTypeScripts(PloneTestCase.PloneTestCase):
00020 
00021     def afterSetUp(self):
00022         perms = self.getPermissionsOfRole('Member')
00023         self.setPermissions(perms + [AddPortalTopics], 'Member')
00024         self.discussion = self.portal.portal_discussion
00025         self.request = self.app.REQUEST
00026 
00027     def getPermissionsOfRole(self, role):
00028         perms = self.portal.permissionsOfRole(role)
00029         return [p['name'] for p in perms if p['selected']]
00030 
00031     def testDiscussionReply(self):
00032         self.folder.invokeFactory('Document', id='doc')
00033         # Create the talkback object
00034         self.discussion.overrideDiscussionFor(self.folder.doc, 1)
00035         self.discussion.getDiscussionFor(self.folder.doc)
00036         # Now test it
00037         self.folder.doc.discussion_reply('Foo', 'blah')
00038         talkback = self.discussion.getDiscussionFor(self.folder.doc)
00039         reply = talkback.objectValues()[0]
00040         self.assertEqual(reply.Title(), 'Foo')
00041         self.assertEqual(reply.EditableBody(), 'blah')
00042 
00043     def testDocumentCreate(self):
00044         self.folder.invokeFactory('Document', id='doc', text='data')
00045         self.assertEqual(self.folder.doc.EditableBody(), 'data')
00046         self.assertEqual(self.folder.doc.Format(), 'text/plain')
00047 
00048     def testDocumentEdit(self):
00049         self.folder.invokeFactory('Document', id='doc')
00050         self.folder.doc.document_edit('html', 'data', title='Foo')
00051         self.assertEqual(self.folder.doc.EditableBody(), 'data')
00052         self.assertEqual(self.folder.doc.Format(), 'text/html')
00053         self.assertEqual(self.folder.doc.Title(), 'Foo')
00054 
00055     def testEventCreate(self):
00056         self.folder.invokeFactory('Event', id='event',
00057                                   title = 'Foo',
00058                                   start_date='2003-09-18',
00059                                   end_date='2003-09-19')
00060         self.assertEqual(self.folder.event.Title(), 'Foo')
00061         self.assertEqual(self.folder.event.start().ISO(), '2003-09-18 00:00:00')
00062         self.assertEqual(self.folder.event.end().ISO(), '2003-09-19 00:00:00')
00063 
00064     def testEventEdit(self):
00065         self.folder.invokeFactory('Event', id='event')
00066         self.folder.event.event_edit(title='Foo',
00067                                      start_date='2003-09-18',
00068                                      end_date='2003-09-19')
00069         self.assertEqual(self.folder.event.Title(), 'Foo')
00070         self.assertEqual(self.folder.event.start().ISO(), '2003-09-18 00:00:00')
00071         self.assertEqual(self.folder.event.end().ISO(), '2003-09-19 00:00:00')
00072 
00073     def testFavoriteCreate(self):
00074         # Ugh, addFavorite traverses to remote_url, so make sure it can.
00075         self.setRoles(['Manager'])
00076         self.portal.invokeFactory('Folder', id='bar')
00077         self.portal.bar.invokeFactory('Document', id='baz.html')
00078         self.setRoles(['Member'])
00079         # back to normal
00080         self.folder.invokeFactory('Favorite', id='favorite',
00081                                   remote_url='bar/baz.html',
00082                                   title='Foo')
00083         self.assertEqual(self.folder.favorite.getRemoteUrl(),
00084                          '%s/bar/baz.html' % self.portal.portal_url())
00085         self.assertEqual(self.folder.favorite.Title(), 'Foo')
00086 
00087     def testFavoriteEdit(self):
00088         # Note: link_edit does not traverse to remote_url
00089         self.folder.invokeFactory('Favorite', id='favorite')
00090         self.folder.favorite.link_edit('bar/baz.html', title='Foo')
00091         self.assertEqual(self.folder.favorite.getRemoteUrl(),
00092                          '%s/bar/baz.html' % self.portal.portal_url())
00093         self.assertEqual(self.folder.favorite.Title(), 'Foo')
00094 
00095     def testFileCreate(self):
00096         self.folder.invokeFactory('File', id='file', file=dummy.File())
00097         self.assertEqual(str(self.folder.file), dummy.TEXT)
00098 
00099     def testFileEdit(self):
00100         self.folder.invokeFactory('File', id='file')
00101         self.folder.file.file_edit(file=dummy.File())
00102         self.assertEqual(str(self.folder.file), dummy.TEXT)
00103 
00104     def testImageCreate(self):
00105         self.folder.invokeFactory('Image', id='image', file=dummy.Image())
00106         self.assertEqual(str(self.folder.image.data), dummy.GIF)
00107 
00108     def testImageEdit(self):
00109         self.folder.invokeFactory('Image', id='image')
00110         self.folder.image.image_edit(file=dummy.Image())
00111         self.assertEqual(str(self.folder.image.data), dummy.GIF)
00112 
00113     def testFolderCreate(self):
00114         self.folder.invokeFactory('Folder', id='folder', title='Foo', description='Bar')
00115         self.assertEqual(self.folder.folder.Title(), 'Foo')
00116         self.assertEqual(self.folder.folder.Description(), 'Bar')
00117 
00118     def testFolderEdit(self):
00119         self.folder.invokeFactory('Folder', id='folder')
00120         self.folder.folder.folder_edit('Foo', 'Bar')
00121         self.assertEqual(self.folder.folder.Title(), 'Foo')
00122         self.assertEqual(self.folder.folder.Description(), 'Bar')
00123 
00124     def testLinkCreate(self):
00125         self.folder.invokeFactory('Link', id='link', remote_url='http://foo.com', title='Foo')
00126         self.assertEqual(self.folder.link.Title(), 'Foo')
00127         self.assertEqual(self.folder.link.getRemoteUrl(), 'http://foo.com')
00128 
00129     def testLinkEdit(self):
00130         self.folder.invokeFactory('Link', id='link')
00131         self.folder.link.link_edit('http://foo.com', title='Foo')
00132         self.assertEqual(self.folder.link.Title(), 'Foo')
00133         self.assertEqual(self.folder.link.getRemoteUrl(), 'http://foo.com')
00134 
00135     def testNewsItemCreate(self):
00136         self.folder.invokeFactory('News Item', id='newsitem', text='data', title='Foo')
00137         self.assertEqual(self.folder.newsitem.EditableBody(), 'data')
00138         self.assertEqual(self.folder.newsitem.Title(), 'Foo')
00139 
00140     def testNewsItemEdit(self):
00141         self.folder.invokeFactory('News Item', id='newsitem')
00142         self.folder.newsitem.newsitem_edit('data', 'plain', title='Foo')
00143         self.assertEqual(self.folder.newsitem.EditableBody(), 'data')
00144         self.assertEqual(self.folder.newsitem.Title(), 'Foo')
00145 
00146     # Bug tests
00147 
00148     def testClearImageTitle(self):
00149         # Test for http://dev.plone.org/plone/ticket/3303
00150         # Should be able to clear Image title
00151         self.folder.invokeFactory('Image', id='image', title='Foo', file=dummy.Image())
00152         self.assertEqual(self.folder.image.Title(), 'Foo')
00153         self.folder.image.image_edit(title='')
00154         self.assertEqual(self.folder.image.Title(), '')
00155 
00156     def testAddToFavorites(self):
00157         # Ugh, addFavorite traverses to remote_url, so make sure it can.
00158         self.setRoles(['Manager'])
00159         self.portal.invokeFactory('Folder', id='bar')
00160         self.portal.bar.invokeFactory('Document', id='baz.html')
00161         self.setRoles(['Member'])
00162         # back to normal
00163         self.failIf('Favorites' in self.folder.objectIds())
00164         self.portal.bar['baz.html'].addtoFavorites()
00165         self.failUnless('Favorites' in self.folder.objectIds())
00166         favorite = self.folder.Favorites.objectValues()[0]
00167         self.assertEqual(favorite.getRemoteUrl(),
00168                          '%s/bar/baz.html' % self.portal.portal_url())
00169         # Make sure the script created AT types
00170         self.assertEqual(self.folder.Favorites.meta_type, 'ATFolder')
00171         self.assertEqual(favorite.meta_type, 'ATFavorite')
00172         # Make sure that the script gave a Title to the folder
00173         self.assertEqual(self.folder.Favorites.Title(), 'Favorites')
00174 
00175     def test_listMetaTypes(self):
00176         self.folder.invokeFactory('Document', id='doc')
00177         tool = self.portal.plone_utils
00178         doc = self.folder.doc
00179         doc.setTitle('title')
00180         metatypes = tool.listMetaTags(doc)
00181         # TODO: atm it checks only of the script can be called w/o an error
00182 
00183     def testObjectDeleteFailsOnGET(self):
00184         self.assertRaises(Unauthorized, self.folder.object_delete,)
00185 
00186     def testObjectDelete(self):
00187         self.folder.invokeFactory('Document', id='doc')
00188         self.setupAuthenticator()
00189         self.setRequestMethod('POST')
00190         self.folder.doc.object_delete()
00191         self.failIf('doc' in self.folder.objectIds())
00192 
00193 
00194 class TestEditShortName(PloneTestCase.PloneTestCase):
00195     # Test fix for http://dev.plone.org/plone/ticket/2246
00196     # Short name should be editable without specifying a file.
00197 
00198     def afterSetUp(self):
00199         self.folder.invokeFactory('File', id='file', file=dummy.File())
00200         self.folder.invokeFactory('Image', id='image', file=dummy.Image())
00201 
00202     def testFileEditNone(self):
00203         self.assertEqual(str(self.folder.file), dummy.TEXT)
00204         self.folder.file.file_edit(file=None, title='Foo')
00205         self.assertEqual(self.folder.file.Title(), 'Foo')
00206         # Data is not changed
00207         self.assertEqual(str(self.folder.file), dummy.TEXT)
00208 
00209     def testImageEditNone(self):
00210         self.assertEqual(str(self.folder.image.data), dummy.GIF)
00211         self.folder.image.image_edit(file=None, title='Foo')
00212         self.assertEqual(self.folder.image.Title(), 'Foo')
00213         # Data is not changed
00214         self.assertEqual(str(self.folder.image.data), dummy.GIF)
00215 
00216     def testFileEditEmptyString(self):
00217         self.assertEqual(str(self.folder.file), dummy.TEXT)
00218         self.folder.file.file_edit(file='', title='Foo')
00219         self.assertEqual(self.folder.file.Title(), 'Foo')
00220         # Data is not changed
00221         self.assertEqual(str(self.folder.file), dummy.TEXT)
00222 
00223     def testImageEditEmptyString(self):
00224         self.assertEqual(str(self.folder.image.data), dummy.GIF)
00225         self.folder.image.image_edit(file='', title='Foo')
00226         self.assertEqual(self.folder.image.Title(), 'Foo')
00227         # Data is not changed
00228         self.assertEqual(str(self.folder.image.data), dummy.GIF)
00229 
00230     def testFileEditString(self):
00231         self.folder.file.file_edit(file='foo')
00232         self.assertEqual(str(self.folder.file.getFile()), 'foo')
00233 
00234     def testImageEditString(self):
00235         self.folder.image.image_edit(file=dummy.GIF)
00236         self.assertEqual(str(self.folder.image.data), dummy.GIF)
00237 
00238     def testFileEditShortName(self):
00239         transaction.savepoint(optimistic=True) # make rename work
00240         self.folder.file.file_edit(id='fred')
00241         self.failUnless('fred' in self.folder.objectIds())
00242 
00243     def testImageEditShortName(self):
00244         transaction.savepoint(optimistic=True) # make rename work
00245         self.folder.image.image_edit(id='fred')
00246         self.failUnless('fred' in self.folder.objectIds())
00247 
00248 
00249 class TestEditFileKeepsMimeType(PloneTestCase.PloneTestCase):
00250     # Tests covering http://dev.plone.org/plone/ticket/2792
00251     # Editing a file should not change MIME type
00252 
00253     def afterSetUp(self):
00254         self.folder.invokeFactory('File', id='file')
00255         self.folder.file.file_edit(file=dummy.File('foo.pdf'))
00256         self.folder.invokeFactory('Image', id='image')
00257         self.folder.image.image_edit(file=dummy.Image('foo.gif'))
00258 
00259     def testFileMimeType(self):
00260         self.assertEqual(self.folder.file.Format(), 'application/pdf')
00261         self.assertEqual(self.folder.file.getFile().content_type, 'application/pdf')
00262 
00263     def testImageMimeType(self):
00264         self.assertEqual(self.folder.image.Format(), 'image/gif')
00265         self.assertEqual(self.folder.image.content_type, 'image/gif')
00266 
00267     def testFileEditKeepsMimeType(self):
00268         self.assertEqual(self.folder.file.Format(), 'application/pdf')
00269         self.assertEqual(self.folder.file.getFile().content_type, 'application/pdf')
00270         self.folder.file.file_edit(title='Foo')
00271         self.assertEqual(self.folder.file.Title(), 'Foo')
00272         self.assertEqual(self.folder.file.Format(), 'application/pdf')
00273         self.assertEqual(self.folder.file.getFile().content_type, 'application/pdf')
00274 
00275     def testImageEditKeepsMimeType(self):
00276         self.assertEqual(self.folder.image.Format(), 'image/gif')
00277         self.assertEqual(self.folder.image.content_type, 'image/gif')
00278         self.folder.image.image_edit(title='Foo')
00279         self.assertEqual(self.folder.image.Title(), 'Foo')
00280         self.assertEqual(self.folder.image.Format(), 'image/gif')
00281         self.assertEqual(self.folder.image.content_type, 'image/gif')
00282 
00283     def testFileRenameKeepsMimeType(self):
00284         self.assertEqual(self.folder.file.Format(), 'application/pdf')
00285         self.assertEqual(self.folder.file.getFile().content_type, 'application/pdf')
00286         transaction.savepoint(optimistic=True) # make rename work
00287         self.folder.file.file_edit(id='foo')
00288         self.assertEqual(self.folder.foo.Format(), 'application/pdf')
00289         self.assertEqual(self.folder.foo.getFile().content_type, 'application/pdf')
00290 
00291     def testImageRenameKeepsMimeType(self):
00292         self.assertEqual(self.folder.image.Format(), 'image/gif')
00293         self.assertEqual(self.folder.image.content_type, 'image/gif')
00294         transaction.savepoint(optimistic=True) # make rename work
00295         self.folder.image.image_edit(id='foo')
00296         self.assertEqual(self.folder.foo.Format(), 'image/gif')
00297         self.assertEqual(self.folder.foo.content_type, 'image/gif')
00298 
00299 
00300 class TestFileURL(PloneTestCase.PloneTestCase):
00301     # Tests covering http://dev.plone.org/plone/ticket/3296
00302     # file:// URLs should contain correct number of slashes
00303     # NOTABUG: This is how urlparse.urlparse() works.
00304 
00305     def testFileURLWithHost(self):
00306         self.folder.invokeFactory('Link', id='link', remote_url='file://foo.com/baz.txt')
00307         self.assertEqual(self.folder.link.getRemoteUrl(), 'file://foo.com/baz.txt')
00308 
00309     def testFileURLNoHost(self):
00310         self.folder.invokeFactory('Link', id='link', remote_url='file:///foo.txt')
00311         self.assertEqual(self.folder.link.getRemoteUrl(), 'file:///foo.txt')
00312 
00313     def testFileURLFourSlash(self):
00314         self.folder.invokeFactory('Link', id='link', remote_url='file:////foo.com/baz.txt')
00315         # See urlparse.urlparse()
00316         self.assertEqual(self.folder.link.getRemoteUrl(), 'file://foo.com/baz.txt')
00317 
00318     def testFileURLFiveSlash(self):
00319         self.folder.invokeFactory('Link', id='link', remote_url='file://///foo.com/baz.txt')
00320         # See urlparse.urlparse()
00321         self.assertEqual(self.folder.link.getRemoteUrl(), 'file:///foo.com/baz.txt')
00322 
00323     def testFileURLSixSlash(self):
00324         self.folder.invokeFactory('Link', id='link', remote_url='file://////foo.com/baz.txt')
00325         # See urlparse.urlparse()
00326         self.assertEqual(self.folder.link.getRemoteUrl(), 'file:////foo.com/baz.txt')
00327 
00328 
00329 class TestFileExtensions(PloneTestCase.PloneTestCase):
00330 
00331     file_id = 'File.2001-01-01.12345'
00332     image_id = 'Image.2001-01-01.12345'
00333 
00334     def afterSetUp(self):
00335         self.folder.invokeFactory('File', id=self.file_id)
00336         self.folder.invokeFactory('Image', id=self.image_id)
00337         transaction.savepoint(optimistic=True) # make rename work
00338 
00339     def testUploadFile(self):
00340         self.folder[self.file_id].file_edit(file=dummy.File('fred.txt'))
00341         self.failUnless('fred.txt' in self.folder.objectIds())
00342 
00343     def testUploadImage(self):
00344         self.folder[self.image_id].image_edit(file=dummy.Image('fred.gif'))
00345         self.failUnless('fred.gif' in self.folder.objectIds())
00346 
00347     def DISABLED_testFileRenameKeepsExtension(self):
00348         # XXX Wishful thinking
00349         self.folder[self.file_id].file_edit(id='barney')
00350         self.failUnless('barney.txt' in self.folder.objectIds())
00351 
00352     def DISABLED_testImageRenameKeepsExtension(self):
00353         # XXX Wishful thinking
00354         self.folder[self.image_id].image_edit(id='barney')
00355         self.failUnless('barney.gif' in self.folder.objectIds())
00356 
00357 
00358 class TestBadFileIds(PloneTestCase.PloneTestCase):
00359 
00360     file_id = 'File.2001-01-01.12345'
00361     image_id = 'Image.2001-01-01.12345'
00362 
00363     def afterSetUp(self):
00364         self.folder.invokeFactory('File', id=self.file_id)
00365         self.folder.invokeFactory('Image', id=self.image_id)
00366         transaction.savepoint(optimistic=True) # make rename work
00367 
00368     def testUploadBadFile(self):
00369         # http://dev.plone.org/plone/ticket/3416
00370         try:
00371             self.folder[self.file_id].file_edit(file=dummy.File('fred%.txt'))
00372         except CopyError:
00373             # Somehow we'd get one of these *sometimes* (not consistently)
00374             # when running tests... since all we're testing is that the
00375             # object doesn't get renamed, this shouldn't matter
00376             pass
00377         self.failIf('fred%.txt' in self.folder.objectIds())
00378 
00379     def testUploadBadImage(self):
00380         # http://dev.plone.org/plone/ticket/3518
00381         try:
00382             self.folder[self.image_id].image_edit(file=dummy.File('fred%.gif'))
00383         except CopyError:
00384             # (ditto - see above)
00385             pass
00386         self.failIf('fred%.gif' in self.folder.objectIds())
00387 
00388     # TODO: Dang! No easy way to get at the validator state...
00389 
00390 
00391 class TestImageProps(PloneTestCase.PloneTestCase):
00392 
00393     def testImageComputedProps(self):
00394         from OFS.Image import Image
00395         tag = Image.tag.im_func
00396         kw = {'_title':'some title',
00397               '_alt':'alt tag',
00398               'height':100,
00399               'width':100}
00400         # Wrap object so that ComputedAttribute gets executed.
00401         self.ob = dummy.ImageComputedProps(**kw).__of__(self.folder)
00402 
00403         endswith = ('alt="alt tag" title="some title" '
00404                     'height="100" width="100" />')
00405         self.assertEqual(tag(self.ob)[-len(endswith):], endswith)
00406 
00407 
00408 class TestWorkflowHistory(PloneTestCase.PloneTestCase):
00409 
00410     def afterSetUp(self):
00411         self.folder.invokeFactory('Document',id='doc')
00412         self.doc = self.folder.doc
00413         self.wf = self.portal.portal_workflow
00414 
00415     def test_WorkflowHistory(self):
00416         # see if doc returns any history at this point
00417         history = self.doc.getWorkflowHistory()
00418         self.failUnlessEqual(history, [])
00419 
00420         # do a transition
00421         self.setRoles(['Reviewer']) # has no modify portal content permission
00422         self.wf.doActionFor(self.doc, 'publish')
00423         self.wf.doActionFor(self.doc, 'retract')
00424         self.wf.doActionFor(self.doc, 'publish')
00425         history = self.doc.getWorkflowHistory()
00426 
00427         # should return no history: no edit permission
00428         self.failUnlessEqual([],[])
00429 
00430         # give edit permissions.
00431         self.doc.manage_permission('Modify portal content', ['Reviewer'], acquire=0)
00432         # should return the history list
00433         history = self.doc.getWorkflowHistory()
00434         self.failUnlessEqual(history[0]['action'], 'publish')
00435         self.failUnlessEqual(history[1]['action'], 'retract')
00436         self.failUnlessEqual(history[2]['action'], 'publish')
00437 
00438 
00439 def test_suite():
00440     from unittest import TestSuite, makeSuite
00441     suite = TestSuite()
00442     suite.addTest(makeSuite(TestContentTypeScripts))
00443     suite.addTest(makeSuite(TestEditShortName))
00444     suite.addTest(makeSuite(TestEditFileKeepsMimeType))
00445     suite.addTest(makeSuite(TestFileURL))
00446     suite.addTest(makeSuite(TestFileExtensions))
00447     suite.addTest(makeSuite(TestBadFileIds))
00448     suite.addTest(makeSuite(TestImageProps))
00449     suite.addTest(makeSuite(TestWorkflowHistory))
00450     return suite