Back to index

plone3  3.1.7
test_part_reloading.py
Go to the documentation of this file.
00001 import unittest
00002 from Products.PloneTestCase import PloneTestCase
00003 
00004 from plone.app.kss.plonekssview import PloneKSSView
00005 from plone.app.kss.interfaces import IPortalObject
00006 from plone.app.kss.portlets import attributesTriggerNavigationPortletReload
00007 from plone.app.kss.portlets import attributesTriggerRecentPortletReload
00008 from plone.app.kss.tests.kss_and_plone_layer import KSSAndPloneTestCase
00009 
00010 from zope.lifecycleevent import ObjectModifiedEvent
00011 from zope import lifecycleevent
00012 
00013 from Products.Archetypes.event import ObjectEditedEvent
00014 
00015 from plone.app.portlets.portlets.recent import Assignment as RecentAssignment
00016 from plone.portlets.interfaces import IPortletManager, IPortletAssignmentMapping
00017 from zope.component import getUtility, getMultiAdapter
00018 
00019 PloneTestCase.setupPloneSite()
00020 
00021 class SampleView(PloneKSSView):
00022 
00023     def change_title(self, title):
00024         # normally you would change the zope database here
00025         self.handle(ObjectModifiedEvent(self.context))
00026         return self.render()
00027 
00028 class TestPortletReloading(KSSAndPloneTestCase):
00029             
00030     def afterSetUp(self):
00031         KSSAndPloneTestCase.afterSetUp(self)
00032         self.setDebugRequest()
00033         self.loginAsPortalOwner()
00034         self.setRoles(['Manager',])
00035         # register the sample view
00036         self.view = self.portal.restrictedTraverse('@@change_title')
00037 
00038     def test_no_update_of_nav_portlet_when_unhooked(self):
00039         # nothing should happen
00040         result = self.view.render()
00041         self.assertEqual(result, [])
00042 
00043     def test_no_update_of_nav_portlet_when_hooked_with_wrong_event(self):
00044         # nothing should happen still because we must change the title or the
00045         # description
00046         modified_event = ObjectEditedEvent(self.folder)
00047         attributesTriggerNavigationPortletReload(self.folder, self.view, modified_event)
00048         result = self.view.render()
00049         self.assertEqual(result, [])
00050 
00051     def test_update_of_nav_portlet(self):
00052         # create the view on the user folder instead of the portal root,
00053         # because there is no more a nav portlet on the portal root.
00054         self.view = self.folder.restrictedTraverse('@@change_title')
00055         descriptor = lifecycleevent.Attributes(IPortalObject, 'title')
00056         modified_event = ObjectEditedEvent(self.folder, descriptor)
00057         attributesTriggerNavigationPortletReload(self.folder, self.view, modified_event)
00058         result = self.view.render()
00059         command = result[0]
00060         self.failUnless(command.has_key('selector'))
00061         self.failUnless(command['selector'].startswith('portletwrapper'))
00062         self.failUnless(command.has_key('name'))
00063         self.assertEqual(command['name'], 'replaceInnerHTML')
00064         self.failUnless(command.has_key('params'))
00065         params = result[0]['params']
00066         self.failUnless(params.has_key('html'))
00067         html = params['html']
00068         self.failUnless('portletNavigationTree' in html)
00069         self.failUnless(command.has_key('selectorType'))
00070         self.assertEqual(command['selectorType'], 'htmlid')
00071 
00072     def create_portlet(self, name, portlet):
00073         leftColumn = getUtility(IPortletManager, name=u'plone.leftcolumn',
00074                             context=self.portal)
00075         left = getMultiAdapter((self.portal, leftColumn,), IPortletAssignmentMapping,
00076                             context=self.portal)
00077         assert name not in left, 'Portlet is already there, no need to create it - fix me'
00078         left[name] = portlet
00079 
00080     def test_update_of_recent_portlet(self):
00081         # there is no recent portlet now at all, so we need to create one
00082         # in the front page so we can test it.
00083         self.create_portlet(u'recent', RecentAssignment())
00084         #
00085         descriptor = lifecycleevent.Attributes(IPortalObject, 'title')
00086         modified_event = ObjectEditedEvent(self.folder, descriptor)
00087         attributesTriggerRecentPortletReload(self.folder, self.view, modified_event)
00088         result = self.view.render()
00089         command = result[0]
00090         self.failUnless(command.has_key('selector'))
00091         self.failUnless(command['selector'].startswith('portletwrapper'))
00092         self.failUnless(command.has_key('name'))
00093         self.assertEqual(command['name'], 'replaceInnerHTML')
00094         self.failUnless(command.has_key('params'))
00095         params = result[0]['params']
00096         self.failUnless(params.has_key('html'))
00097         html = params['html']
00098         self.failUnless('portletRecent' in html)
00099         self.failUnless(command.has_key('selectorType'))
00100         self.assertEqual(command['selectorType'], 'htmlid')
00101 
00102 def test_suite():
00103     suites = []
00104     suites.append(unittest.makeSuite(TestPortletReloading))
00105     return unittest.TestSuite(suites)