Back to index

plone3  3.1.7
testPloneView.py
Go to the documentation of this file.
00001 #
00002 # Test methods used to make ...
00003 #
00004 
00005 from zope.interface import directlyProvides, noLongerProvides
00006 
00007 from Products.CMFPlone.interfaces import INonStructuralFolder
00008 from Products.CMFPlone.interfaces.NonStructuralFolder import \
00009      INonStructuralFolder as z2INonStructuralFolder
00010 from Products.CMFPlone.tests import PloneTestCase
00011 from Products.CMFPlone.tests import dummy
00012 
00013 from Products.CMFPlone.browser.ploneview import Plone
00014 
00015 from Products.CMFPlone.ActionsTool import ActionsTool
00016 from Products.CMFPlone.InterfaceTool import InterfaceTool
00017 from Products.CMFPlone.MembershipTool import MembershipTool
00018 from Products.CMFPlone.SyndicationTool import SyndicationTool
00019 from Products.CMFPlone.URLTool import URLTool
00020 from Products.CMFCore.WorkflowTool import WorkflowTool
00021 
00022 from zope.publisher.browser import setDefaultSkin
00023 
00024 class TestPloneView(PloneTestCase.PloneTestCase):
00025     """Tests the global plone view.  All the old global_defines should be
00026        in the _data mapping of the view, which is globablized into
00027        calling templates."""
00028 
00029     def afterSetUp(self):
00030         # We need to fiddle the request for zope 2.9+
00031         setDefaultSkin(self.app.REQUEST)
00032         self.folder.invokeFactory('Document', 'test',
00033                                   title='Test default page')
00034         self.view = Plone(self.portal, self.app.REQUEST)
00035         self.view._initializeData()
00036 
00037     def testUTool(self):
00038         assert isinstance(self.view._data['utool'], URLTool)
00039 
00040     def testPortal(self):
00041         assert self.view._data['portal'] == self.portal
00042 
00043     def testPortalURL(self):
00044         assert isinstance(self.view._data['portal_url'], type(''))
00045 
00046     def testMTool(self):
00047         assert isinstance(self.view._data['mtool'], MembershipTool)
00048 
00049     def testATool(self):
00050         assert isinstance(self.view._data['atool'], ActionsTool)
00051 
00052     def testPUtils(self):
00053         pass
00054 
00055     def testWTool(self):
00056         assert isinstance(self.view._data['wtool'], WorkflowTool)
00057 
00058     def testIFaceTool(self):
00059         assert isinstance(self.view._data['ifacetool'], InterfaceTool)
00060 
00061     def testSynTool(self):
00062         assert isinstance(self.view._data['syntool'], SyndicationTool)
00063 
00064     def testPortalTitle(self):
00065         pass
00066 
00067     def testToLocalizedTime(self):
00068         localdate = self.view.toLocalizedTime
00069         value = localdate('Mar 9, 1997 1:45pm', long_format=True)
00070         self.assertEquals(value, 'Mar 09, 1997 01:45 PM')
00071 
00072     def testIsStructuralFolderWithNonFolder(self):
00073         i = dummy.Item()
00074         self.failIf(Plone(i, self.app.REQUEST).isStructuralFolder())
00075 
00076     def testIsStructuralFolderWithFolder(self):
00077         f = dummy.Folder('struct_folder')
00078         self.failUnless(Plone(f, self.app.REQUEST).isStructuralFolder())
00079 
00080     def testIsStructuralFolderWithNonStructuralFolder(self):
00081         f = dummy.NonStructuralFolder('ns_folder')
00082         self.failIf(Plone(f, self.app.REQUEST).isStructuralFolder())
00083 
00084     def testIsStructuralFolderWithZ2NonStructuralFolder(self):
00085         f = dummy.Folder('z2_nsFolder')
00086         f.__implements__ = f.__implements__ + (z2INonStructuralFolder,)
00087         view = Plone(f, self.app.REQUEST)
00088         value = view.isStructuralFolder()
00089         self.failIf(Plone(f, self.app.REQUEST).isStructuralFolder())
00090 
00091     def testIsDefaultPageInFolder(self):
00092         view = Plone(self.folder.test, self.app.REQUEST)
00093         self.failIf(view.isDefaultPageInFolder())
00094         self.failUnless(self.folder.canSelectDefaultPage())
00095         self.folder.saveDefaultPage('test')
00096         # re-create the view, because the old value is cached
00097         del self.app.REQUEST.__annotations__
00098         view = Plone(self.folder.test, self.app.REQUEST)
00099         self.failUnless(view.isDefaultPageInFolder())
00100 
00101     def testNavigationRootPath(self):
00102         view = Plone(self.folder, self.app.REQUEST)
00103         self.assertEqual(view.navigationRootPath(), self.portal.portal_url.getPortalPath())
00104 
00105     def testNavigationRootUrl(self):
00106         view = Plone(self.folder, self.app.REQUEST)
00107         self.assertEqual(view.navigationRootUrl(), self.portal.absolute_url())
00108 
00109     def testGetParentObject(self):
00110         view = Plone(self.folder.test, self.app.REQUEST)
00111         self.assertEqual(view.getParentObject(), self.folder)
00112         # Make sure this looks only at containment
00113         del self.app.REQUEST.__annotations__
00114         view = Plone(self.folder.test.__of__(self.portal), self.app.REQUEST)
00115         self.assertEqual(view.getParentObject(), self.folder)
00116 
00117     def testIsFolderOrFolderDefaultPage(self):
00118         # an actual folder whould return true
00119         view = Plone(self.folder, self.app.REQUEST)
00120         self.failUnless(view.isFolderOrFolderDefaultPage())
00121         # But not a document
00122         del self.app.REQUEST.__annotations__
00123         view = Plone(self.folder.test, self.app.REQUEST)
00124         self.failIf(view.isFolderOrFolderDefaultPage())
00125         # Unless we make it the default view
00126         self.folder.saveDefaultPage('test')
00127         del self.app.REQUEST.__annotations__
00128         view = Plone(self.folder.test, self.app.REQUEST)
00129         self.failUnless(view.isFolderOrFolderDefaultPage())
00130         # And if we have a non-structural folder it should not be true
00131         f = dummy.NonStructuralFolder('ns_folder')
00132         self.folder._setObject('ns_folder', f)
00133         del self.app.REQUEST.__annotations__
00134         view = Plone(self.folder.ns_folder, self.app.REQUEST)
00135         self.failIf(view.isFolderOrFolderDefaultPage())
00136 
00137     def testIsPortalOrPortalDefaultPage(self):
00138         # an actual folder whould return true
00139         view = Plone(self.portal, self.app.REQUEST)
00140         self.failUnless(view.isPortalOrPortalDefaultPage())
00141         # But not a document
00142         self.setRoles(['Manager'])
00143         self.portal.invokeFactory('Document', 'portal_test',
00144                                   title='Test default page')
00145         del self.app.REQUEST.__annotations__
00146         view = Plone(self.portal.portal_test, self.app.REQUEST)
00147         self.failIf(view.isPortalOrPortalDefaultPage())
00148         # Unless we make it the default view
00149         self.portal.saveDefaultPage('portal_test')
00150         del self.app.REQUEST.__annotations__
00151         view = Plone(self.portal.portal_test, self.app.REQUEST)
00152         self.failUnless(view.isPortalOrPortalDefaultPage())
00153 
00154     def testGetCurrentFolder(self):
00155         # If context is a folder, then the folder is returned
00156         view = Plone(self.folder, self.app.REQUEST)
00157         self.assertEqual(view.getCurrentFolder(), self.folder)
00158         
00159         # If context is not a folder, then the parent is returned
00160         # A bit crude ... we need to make sure our memos don't stick in the tests
00161         del self.app.REQUEST.__annotations__
00162         view = Plone(self.folder.test, self.app.REQUEST)
00163         self.assertEqual(view.getCurrentFolder(), self.folder)
00164         
00165         # The real container is returned regardless of context
00166         del self.app.REQUEST.__annotations__
00167         view = Plone(self.folder.test.__of__(self.portal), self.app.REQUEST)
00168         self.assertEqual(view.getCurrentFolder(), self.folder)
00169         
00170         # A non-structural folder does not count as a folder`
00171         f = dummy.NonStructuralFolder('ns_folder')
00172         self.folder._setObject('ns_folder', f)
00173         del self.app.REQUEST.__annotations__
00174         view = Plone(self.folder.ns_folder, self.app.REQUEST)
00175         self.assertEqual(view.getCurrentFolder(), self.folder)
00176         
00177         # And even a structural folder that is used as a default page
00178         # returns its parent
00179         self.setRoles(['Manager'])
00180         self.folder.invokeFactory('Topic', 'topic')
00181         
00182         del self.app.REQUEST.__annotations__
00183         view = Plone(self.folder.topic, self.app.REQUEST)
00184         self.assertEqual(view.getCurrentFolder(), self.folder.topic)
00185         self.folder.saveDefaultPage('topic')
00186         
00187         del self.app.REQUEST.__annotations__
00188         view = Plone(self.folder.topic, self.app.REQUEST)
00189         self.assertEqual(view.getCurrentFolder(), self.folder)
00190 
00191     def testHavePortlets(self):
00192         view = Plone(self.portal, self.app.REQUEST)
00193         self.assertEqual(False, view.have_portlets('plone.leftcolumn'))
00194         self.assertEqual(True, view.have_portlets('plone.rightcolumn'))
00195 
00196     def testDisablePortlets(self):
00197         view = Plone(self.portal, self.app.REQUEST)
00198         view._initializeData()
00199         data = view._data
00200         self.assertEqual(True, data['sr'])
00201         self.assertEqual('visualColumnHideOne', data['hidecolumns'])
00202         view._initializeData(options={'no_portlets': True})
00203         self.assertEqual(False, data['sr'])
00204         self.assertEqual('visualColumnHideOneTwo', data['hidecolumns'])
00205 
00206     def testCropText(self):
00207         view = Plone(self.portal, self.app.REQUEST)
00208         self.assertEqual(view.cropText('Hello world', 7), 'Hello ...')
00209         self.assertEqual(view.cropText('Hello world', 99), 'Hello world')
00210         self.assertEqual(view.cropText('Hello world', 10), 'Hello worl...')
00211         self.assertEqual(view.cropText(u'Hello world', 10), u'Hello worl...')
00212         self.assertEqual(view.cropText(u'Koko\u0159\xedn', 5), u'Koko\u0159...')
00213         # Test utf encoded string Kokorin with 'r' and 'i' accented 
00214         # Must return 6 characters, because 5th character is two byte
00215         text = u'Koko\u0159\xedn'.encode('utf8')
00216         self.assertEqual(view.cropText(text, 5), 'Koko\xc5\x99...')
00217     
00218     def testUniqueIndexIterator(self):
00219         iterator = self.view._data['uniqueItemIndex']
00220         self.assertEquals(0, iterator.next())
00221         self.assertEquals(1, iterator.next())
00222         self.assertEquals(2, iterator.next())
00223         
00224     def testPrepareObjectTabsOnPortalRoot(self):
00225         del self.app.REQUEST.__annotations__
00226         self.loginAsPortalOwner()
00227         self.app.REQUEST['ACTUAL_URL'] = self.portal.absolute_url()
00228         view = self.portal.restrictedTraverse('@@plone')
00229         tabs = view.prepareObjectTabs()
00230         self.assertEquals(tabs[0]['id'], 'folderContents')
00231         self.assertEquals(['view'], [t['id'] for t in tabs if t['selected']])
00232         
00233     def testPrepareObjectTabsNonFolder(self):
00234         del self.app.REQUEST.__annotations__
00235         self.loginAsPortalOwner()
00236         self.app.REQUEST['ACTUAL_URL'] = self.folder.test.absolute_url()
00237         view = self.folder.test.restrictedTraverse('@@plone')
00238         tabs = view.prepareObjectTabs()
00239         self.assertEquals(0, len([t for t in tabs if t['id'] == 'folderContents']))
00240         self.assertEquals(['view'], [t['id'] for t in tabs if t['selected']])
00241         
00242     def testPrepareObjectTabsNonStructuralFolder(self):
00243         del self.app.REQUEST.__annotations__
00244         self.loginAsPortalOwner()
00245         self.app.REQUEST['ACTUAL_URL'] = self.folder.absolute_url()
00246         directlyProvides(self.folder, INonStructuralFolder)
00247         view = self.folder.restrictedTraverse('@@plone')
00248         tabs = view.prepareObjectTabs()
00249         noLongerProvides(self.folder, INonStructuralFolder)
00250         self.assertEquals(0, len([t for t in tabs if t['id'] == 'folderContents']))
00251         self.assertEquals(['view'], [t['id'] for t in tabs if t['selected']])
00252         
00253     def testPrepareObjectTabsDefaultView(self):
00254         del self.app.REQUEST.__annotations__
00255         self.loginAsPortalOwner()
00256         self.app.REQUEST['ACTUAL_URL'] = self.folder.test.absolute_url() + '/edit'
00257         view = self.folder.test.restrictedTraverse('@@plone')
00258         tabs = view.prepareObjectTabs()
00259         self.assertEquals(0, len([t for t in tabs if t['id'] == 'folderContents']))
00260         self.assertEquals(['edit'], [t['id'] for t in tabs if t['selected']])
00261 
00262     def testActionOverrideFromTemplate(self):
00263         # We should be able to pass actions in from the template
00264         # and have them override the calculated actions
00265         view = Plone(self.portal, self.app.REQUEST)
00266         view._initializeData()
00267         data = view._data
00268         self.failUnless(data['actions'])
00269         self.failUnless(data['keyed_actions'])
00270         self.failUnless(data['user_actions'])
00271         no_actions = {'folder':[], 'user':[], 'global':[], 'workflow':[]}
00272         view._initializeData(options={'actions':no_actions})
00273         self.assertEqual(data['actions'], no_actions)
00274         self.assertEqual(data['keyed_actions'], no_actions)
00275         self.failIf(data['user_actions'])
00276 
00277 
00278 class TestVisibleIdsEnabled(PloneTestCase.PloneTestCase):
00279     '''Tests the visibleIdsEnabled method'''
00280 
00281     def afterSetUp(self):
00282         self.view = Plone(self.portal, self.app.REQUEST)
00283         self.member = self.portal.portal_membership.getAuthenticatedMember()
00284         self.props = self.portal.portal_properties.site_properties
00285 
00286     def testFailsWithSitePropertyDisabled(self):
00287         # Set baseline
00288         self.member.setProperties(visible_ids=False)
00289         self.props.manage_changeProperties(visible_ids=False)
00290         # Should fail when site property is set false
00291         self.failIf(self.view.visibleIdsEnabled())
00292         self.member.setProperties(visible_ids=True)
00293         self.failIf(self.view.visibleIdsEnabled())
00294 
00295     def testFailsWithMemberPropertyDisabled(self):
00296         # Should fail when member property is false
00297         self.member.setProperties(visible_ids=False)
00298         self.props.manage_changeProperties(visible_ids=True)
00299         self.failIf(self.view.visibleIdsEnabled())
00300 
00301     def testSucceedsWithMemberAndSitePropertyEnabled(self):
00302         # Should succeed only when site property and member property are true
00303         self.props.manage_changeProperties(visible_ids=True)
00304         self.member.setProperties(visible_ids=True)
00305         self.failUnless(self.view.visibleIdsEnabled())
00306 
00307 def test_suite():
00308     from unittest import TestSuite, makeSuite
00309     suite = TestSuite()
00310     suite.addTest(makeSuite(TestPloneView))
00311     suite.addTest(makeSuite(TestVisibleIdsEnabled))
00312     return suite