Back to index

plone3  3.1.7
testContentTypes.py
Go to the documentation of this file.
00001 #
00002 # Tests the content types
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 from Products.CMFPlone.tests import dummy
00007 
00008 from zope.i18nmessageid.message import Message
00009 
00010 from Acquisition import aq_base
00011 from Products.ATContentTypes.interfaces import IATContentType
00012 
00013 AddPortalTopics = 'Add portal topics'
00014 
00015 atct_types = ('Document', 'Event', 'Favorite', 'File', 'Folder',
00016               'Large Plone Folder', 'Image', 'Link', 'News Item',
00017              )
00018 
00019 
00020 class TestATContentTypes(PloneTestCase.PloneTestCase):
00021 
00022     def afterSetUp(self):
00023         perms = self.getPermissionsOfRole('Member')
00024         self.setPermissions(perms + [AddPortalTopics], 'Member')
00025         self.types = self.portal.portal_types
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 construct(self, portal_type, id, folder):
00032         fti = self.types.getTypeInfo(portal_type)
00033         fti.constructInstance(folder, id=id)
00034         return getattr(folder, id)
00035 
00036     def createWithoutConstruction(self, portal_type, id, folder):
00037         fti = self.types.getTypeInfo(portal_type)
00038         # manual creation
00039         p = folder.manage_addProduct[fti.product]
00040         m = getattr(p, fti.factory)
00041         m(id) # create it
00042         return folder._getOb(id)
00043 
00044     def testPortalTypeName(self):
00045         for pt in atct_types:
00046             ob = self.construct(pt, pt, self.folder)
00047             self.failUnlessEqual(ob._getPortalTypeName(), pt)
00048             self.failUnlessEqual(ob.portal_type, pt)
00049             self.failUnless(IATContentType.isImplementedBy(ob))
00050 
00051 
00052 class TestContentTypes(PloneTestCase.PloneTestCase):
00053     # This test mirrors TestContentTypeScripts but calls the API and
00054     # not the skin scripts.
00055 
00056     def afterSetUp(self):
00057         perms = self.getPermissionsOfRole('Member')
00058         self.setPermissions(perms + [AddPortalTopics], 'Member')
00059 
00060     def getPermissionsOfRole(self, role):
00061         perms = self.portal.permissionsOfRole(role)
00062         return [p['name'] for p in perms if p['selected']]
00063 
00064     def testDocumentEdit(self):
00065         self.folder.invokeFactory('Document', id='doc')
00066         self.folder.doc.edit(title='Foo', text='data', text_format='html')
00067         self.assertEqual(self.folder.doc.EditableBody(), 'data')
00068         self.assertEqual(self.folder.doc.Format(), 'text/html')
00069         self.assertEqual(self.folder.doc.Title(), 'Foo')
00070 
00071     def testEventEdit(self):
00072         self.folder.invokeFactory('Event', id='event')
00073         self.folder.event.edit(title='Foo',
00074                                start_date='2003-09-18',
00075                                end_date='2003-09-19')
00076         self.assertEqual(self.folder.event.Title(), 'Foo')
00077         self.assertEqual(self.folder.event.start().ISO(), '2003-09-18 00:00:00')
00078         self.assertEqual(self.folder.event.end().ISO(), '2003-09-19 00:00:00')
00079 
00080     def testFavoriteEdit(self):
00081         # Note: link_edit does not traverse to remote_url
00082         self.folder.invokeFactory('Favorite', id='favorite')
00083         self.folder.favorite.edit(remote_url='bar/baz.html', title='Foo')
00084         self.assertEqual(self.folder.favorite.getRemoteUrl(),
00085                          '%s/bar/baz.html' % self.portal.portal_url())
00086         self.assertEqual(self.folder.favorite.Title(), 'Foo')
00087 
00088     def testFileEdit(self):
00089         self.folder.invokeFactory('File', id='file')
00090         self.folder.file.edit(file=dummy.File())
00091         self.assertEqual(str(self.folder.file), dummy.TEXT)
00092 
00093     def testImageEdit(self):
00094         self.folder.invokeFactory('Image', id='image')
00095         self.folder.image.edit(file=dummy.Image())
00096         self.assertEqual(str(self.folder.image.data), dummy.GIF)
00097 
00098     def testFolderEdit(self):
00099         self.folder.invokeFactory('Folder', id='folder')
00100         self.folder.folder.edit(title='Foo', description='Bar')
00101         self.assertEqual(self.folder.folder.Title(), 'Foo')
00102         self.assertEqual(self.folder.folder.Description(), 'Bar')
00103         # Edit a second time
00104         self.folder.folder.edit(title='Fred', description='BamBam')
00105         self.assertEqual(self.folder.folder.Title(), 'Fred')
00106         self.assertEqual(self.folder.folder.Description(), 'BamBam')
00107 
00108     def testLinkEdit(self):
00109         self.folder.invokeFactory('Link', id='link')
00110         self.folder.link.edit(remote_url='http://foo.com', title='Foo')
00111         self.assertEqual(self.folder.link.Title(), 'Foo')
00112         self.assertEqual(self.folder.link.getRemoteUrl(), 'http://foo.com')
00113 
00114     def testNewsItemEdit(self):
00115         self.folder.invokeFactory('News Item', id='newsitem')
00116         self.folder.newsitem.edit(text='data', text_format='html', title='Foo')
00117         self.assertEqual(self.folder.newsitem.EditableBody(), 'data')
00118         self.assertEqual(self.folder.newsitem.Format(), 'text/html')
00119         self.assertEqual(self.folder.newsitem.Title(), 'Foo')
00120 
00121     def testTopicEdit(self):
00122         self.folder.invokeFactory('Topic', id='topic')
00123         self.folder.topic.edit(title='Foo')
00124         self.assertEqual(self.folder.topic.Title(), 'Foo')
00125 
00126 
00127 class TestContentTypeInformation(PloneTestCase.PloneTestCase):
00128 
00129     def afterSetUp(self):
00130         self.types = self.portal.portal_types
00131 
00132     def testTypeTitlesAreMessages(self):
00133         types = self.types.objectValues()
00134         for t in types:
00135             # If the title is empty we get back the id
00136             if t.title:
00137                 self.failUnless(isinstance(t.Title(), Message))
00138             self.failUnless(isinstance(t.Description(), Message))
00139 
00140 
00141 def test_suite():
00142     from unittest import TestSuite, makeSuite
00143     suite = TestSuite()
00144     suite.addTest(makeSuite(TestATContentTypes))
00145     suite.addTest(makeSuite(TestContentTypes))
00146     suite.addTest(makeSuite(TestContentTypeInformation))
00147     return suite