Back to index

plone3  3.1.7
test_configuration.py
Go to the documentation of this file.
00001 import time
00002 import transaction
00003 
00004 from zope.i18nmessageid import Message
00005 from zope.interface import Interface
00006 from zope.component import getUtility, queryUtility, getMultiAdapter
00007 from zope.component.interfaces import IFactory
00008 
00009 from zope.component import getSiteManager
00010 
00011 from plone.app.portlets.tests.base import PortletsTestCase
00012 
00013 from Products.Five import zcml
00014 from Products.Five import fiveconfigure
00015 from Products.Five.browser import BrowserView
00016 
00017 from Products.GenericSetup.interfaces import IBody
00018 from Products.GenericSetup.context import TarballExportContext
00019 from Products.GenericSetup.tests.common import DummyImportContext
00020 
00021 from Products.PloneTestCase.layer import PloneSite
00022 from Testing import ZopeTestCase
00023 
00024 from plone.portlets.interfaces import ILocalPortletAssignmentManager
00025 from plone.portlets.interfaces import IPortletType
00026 from plone.portlets.interfaces import IPortletRenderer
00027 from plone.portlets.interfaces import IPortletManager
00028 
00029 from plone.portlets.constants import CONTEXT_CATEGORY
00030 from plone.portlets.constants import GROUP_CATEGORY
00031 from plone.portlets.constants import CONTENT_TYPE_CATEGORY
00032 
00033 from plone.portlets.manager import PortletManager
00034 from plone.portlets.utils import registerPortletType
00035 
00036 from plone.app.portlets.interfaces import IPortletTypeInterface
00037 from plone.app.portlets.interfaces import IColumn
00038 
00039 from plone.app.portlets.browser.adding import PortletAdding
00040 from plone.app.portlets.utils import assignment_mapping_from_key
00041 
00042 from plone.app.portlets.exportimport.portlets import importPortlets
00043 
00044 
00045 class DummyView(BrowserView):
00046     pass
00047 
00048 # A sample portlet
00049 
00050 from zope.interface import implements
00051 from zope import schema
00052 from zope.formlib import form
00053 
00054 from plone.portlets.interfaces import IPortletDataProvider
00055 from plone.app.portlets.portlets import base
00056 
00057 class ITestPortlet(IPortletDataProvider):
00058     test_text = schema.TextLine(title=u"Test")
00059     test_bool = schema.Bool(title=u"Test")
00060     test_tuple = schema.Tuple(title=u"Test",
00061                               value_type=schema.Choice(vocabulary="plone.app.vocabularies.WorkflowStates"))
00062 
00063 class TestAssignment(base.Assignment):
00064     implements(ITestPortlet)
00065     
00066     test_text = None
00067     test_bool = None
00068     test_tuple = None
00069     
00070     title = "Sample portlet"
00071 
00072 class TestRenderer(base.Renderer):
00073     def render(self):
00074         return "Portlet for testing"
00075 
00076 class TestAddForm(base.AddForm):
00077     form_fields = form.Fields(ITestPortlet)
00078     label = u"Test portlet"
00079 
00080     def create(self, data):
00081         a = TestAssignment()
00082         a.title = data.get('title', u"")
00083         return a
00084 
00085 class TestEditForm(base.EditForm):
00086     form_fields = form.Fields(ITestPortlet)
00087     label = u"Test portlet"
00088 
00089 # A test portlet manager
00090 
00091 class ITestColumn(IColumn):
00092     pass
00093 
00094 zcml_string = """\
00095 <configure xmlns="http://namespaces.zope.org/zope"
00096            xmlns:browser="http://namespaces.zope.org/browser"
00097            xmlns:plone="http://namespaces.plone.org/plone"
00098            xmlns:gs="http://namespaces.zope.org/genericsetup"
00099            package="plone.app.portlets">
00100 
00101     <plone:portlet
00102         name="portlets.test.Test"
00103         interface="plone.app.portlets.tests.test_configuration.ITestPortlet"
00104         assignment="plone.app.portlets.tests.test_configuration.TestAssignment"
00105         renderer="plone.app.portlets.tests.test_configuration.TestRenderer"
00106         addview="plone.app.portlets.tests.test_configuration.TestAddForm"
00107         editview="plone.app.portlets.tests.test_configuration.TestEditForm"
00108         />
00109         
00110     <gs:registerProfile
00111         name="testing"
00112         title="plone.app.portlets testing"
00113         description="Used for testing only" 
00114         directory="tests/profiles/testing"
00115         for="Products.CMFCore.interfaces.ISiteRoot"
00116         provides="Products.GenericSetup.interfaces.EXTENSION"
00117         />
00118         
00119 </configure>
00120 """
00121 
00122 class TestPortletZCMLLayer(PloneSite):
00123     
00124     @classmethod
00125     def setUp(cls):
00126         fiveconfigure.debug_mode = True
00127         zcml.load_string(zcml_string)
00128         fiveconfigure.debug_mode = False
00129 
00130     @classmethod
00131     def tearDown(cls):
00132         pass
00133         
00134 class TestPortletGSLayer(TestPortletZCMLLayer):
00135     
00136     @classmethod
00137     def setUp(cls):
00138         app = ZopeTestCase.app()
00139         portal = app.plone
00140         
00141         portal_setup = portal.portal_setup
00142         # wait a bit or we get duplicate ids on import
00143         time.sleep(1)
00144         portal_setup.runAllImportStepsFromProfile('profile-plone.app.portlets:testing')
00145         
00146         transaction.commit()
00147         ZopeTestCase.close(app)
00148 
00149     @classmethod
00150     def tearDown(cls):
00151         pass
00152 
00153 class TestZCML(PortletsTestCase):
00154 
00155     layer = TestPortletZCMLLayer
00156     
00157     def testPortletTypeInterfaceRegistered(self):
00158         iface = getUtility(IPortletTypeInterface, name=u"portlets.test.Test")
00159         self.assertEquals(ITestPortlet, iface)
00160         
00161     def testFactoryRegistered(self):
00162         factory = getUtility(IFactory, name=u"portlets.test.Test")
00163         self.assertEquals(TestAssignment, factory._callable)
00164     
00165     def testRendererRegistered(self):
00166         context = self.portal
00167         request = self.portal.REQUEST
00168         view = DummyView(context, request)
00169         manager = PortletManager()
00170         assignment = TestAssignment()
00171         
00172         renderer = getMultiAdapter((context, request, view, manager, assignment), IPortletRenderer)
00173         self.failUnless(isinstance(renderer, TestRenderer))
00174         
00175     def testAddViewRegistered(self):
00176         request = self.portal.REQUEST
00177         adding = PortletAdding(self.portal, request)
00178         
00179         addview = getMultiAdapter((adding, request), name=u"portlets.test.Test")
00180         self.failUnless(isinstance(addview, TestAddForm))
00181         
00182     def testEditViewRegistered(self):
00183         assignment = TestAssignment()
00184         request = self.portal.REQUEST
00185         
00186         editview = getMultiAdapter((assignment, request), name=u"edit")
00187         self.failUnless(isinstance(editview, TestEditForm))
00188 
00189 class TestGenericSetup(PortletsTestCase):
00190     
00191     layer = TestPortletGSLayer
00192     
00193     def testPortletManagerInstalled(self):
00194         manager = getUtility(IPortletManager, name=u"test.testcolumn")
00195         self.failUnless(ITestColumn.providedBy(manager))
00196     
00197     def disabled_testPortletTypeRegistered(self):
00198         portlet_type = getUtility(IPortletType, name=u"portlets.test.Test")
00199         self.assertEquals('portlets.test.Test', portlet_type.addview)
00200         self.assertEquals([Interface], portlet_type.for_)
00201         # XXX Missing i18n support in the exportimport code
00202         self.failUnless(isinstance(portlet_type.title, Message),
00203                         "Portlet title should be a Message instance")
00204         self.failUnless(isinstance(portlet_type.description, Message),
00205                         "Portlet description should be a Message instance")
00206         self.assertEquals(u"title_test_portlet", portlet_type.title)
00207         self.assertEquals(u"description_test_portlet", portlet_type.description)
00208         self.assertEquals(u"Test portlet", portlet_type.title.default)
00209         self.assertEquals(u"A test portlet", portlet_type.description.default)
00210         self.assertEquals(u"plone", portlet_type.title.domain)
00211         self.assertEquals(u"plone", portlet_type.description.domain)
00212 
00213     def testAssignmentCreatedAndOrdered(self):
00214         mapping = assignment_mapping_from_key(self.portal,
00215             manager_name=u"test.testcolumn", category=CONTEXT_CATEGORY, key="/")
00216         self.assertEquals(3, len(mapping))
00217         self.assertEquals(['test.portlet3', 'test.portlet2', 'test.portlet1'], list(mapping.keys()))
00218         
00219     def testAssignmentPropertiesSet(self):
00220         mapping = assignment_mapping_from_key(self.portal,
00221             manager_name=u"test.testcolumn", category=CONTEXT_CATEGORY, key="/")
00222         
00223         assignment = mapping['test.portlet1']
00224         self.assertEquals(u'Test pr\xf6p 1', assignment.test_text)
00225         self.assertEquals(False, assignment.test_bool)
00226         self.assertEquals((u'published', u'private'), assignment.test_tuple)
00227         
00228         assignment = mapping['test.portlet2']
00229         self.assertEquals('Test prop 2', assignment.test_text)
00230         self.assertEquals(True, assignment.test_bool)
00231         self.assertEquals((), assignment.test_tuple)
00232         
00233         assignment = mapping['test.portlet3']
00234         self.assertEquals(None, assignment.test_text)
00235         self.assertEquals(None, assignment.test_bool)
00236         self.assertEquals(None, assignment.test_tuple)
00237         
00238     def testAssignmentRemoval(self):
00239         portal_setup = self.portal.portal_setup
00240         
00241         # wait a bit or we get duplicate ids on import
00242         time.sleep(1)
00243         portal_setup.runAllImportStepsFromProfile('profile-plone.app.portlets:testing')
00244 
00245         mapping = assignment_mapping_from_key(self.portal,
00246             manager_name=u"test.testcolumn", category=CONTEXT_CATEGORY, key="/")
00247 
00248         # initally there should be no portlet7
00249         self.assertEqual(mapping.get('test.portlet7', None), None)
00250 
00251         # now we add one
00252         portlet_factory = getUtility(IFactory, name='portlets.test.Test')
00253         assignment = portlet_factory()
00254         mapping['test.portlet7'] = assignment
00255 
00256         # make sure it's there
00257         self.assertNotEqual(mapping.get('test.portlet7', None), None)
00258 
00259         # wait a bit or we get duplicate ids on import
00260         time.sleep(1)
00261         # run the profile
00262         portal_setup.runAllImportStepsFromProfile('profile-plone.app.portlets:testing')
00263 
00264         # and should have got rid of it again
00265         self.assertEqual(mapping.get('test.portlet7', None), None)
00266 
00267     def testAssignmentPurging(self):
00268         # initially there should be 3 assignments on the root
00269         mapping = assignment_mapping_from_key(self.portal,
00270             manager_name=u"test.testcolumn", category=CONTEXT_CATEGORY, key="/")
00271         self.assertEquals(3, len(mapping))
00272 
00273         context = DummyImportContext(self.portal, purge=False)
00274         context._files['portlets.xml'] = """<?xml version="1.0"?>
00275             <portlets>
00276                 <assignment
00277                     manager="test.testcolumn" 
00278                     category="context"
00279                     key="/"
00280                     purge="True"
00281                     />
00282             </portlets>
00283         """
00284         importPortlets(context)
00285 
00286         # now they should be gone
00287         mapping = assignment_mapping_from_key(self.portal,
00288             manager_name=u"test.testcolumn", category=CONTEXT_CATEGORY, key="/")
00289         self.assertEquals(0, len(mapping))
00290 
00291         # group assignments should still be there
00292         mapping = assignment_mapping_from_key(self.portal,
00293             manager_name=u"test.testcolumn", category=GROUP_CATEGORY, key="Reviewers")
00294         self.assertEquals(1, len(mapping))
00295 
00296         # and be purgable
00297         context = DummyImportContext(self.portal, purge=False)
00298         context._files['portlets.xml'] = """<?xml version="1.0"?>
00299             <portlets>
00300                 <assignment
00301                     manager="test.testcolumn" 
00302                     category="group"
00303                     key="Reviewers"
00304                     purge="True"
00305                     />
00306             </portlets>
00307         """
00308         importPortlets(context)
00309 
00310         # now they should be gone
00311         mapping = assignment_mapping_from_key(self.portal,
00312             manager_name=u"test.testcolumn", category=GROUP_CATEGORY, key="Reviewers")
00313         self.assertEquals(0, len(mapping))
00314 
00315         # also content type assignments should still be there
00316         mapping = assignment_mapping_from_key(self.portal,
00317             manager_name=u"test.testcolumn", category=CONTENT_TYPE_CATEGORY, key="Folder")
00318         self.assertEquals(2, len(mapping))
00319 
00320         # and be purgable
00321         context = DummyImportContext(self.portal, purge=False)
00322         context._files['portlets.xml'] = """<?xml version="1.0"?>
00323             <portlets>
00324                 <assignment
00325                     manager="test.testcolumn" 
00326                     category="content_type"
00327                     key="Folder"
00328                     purge="True"
00329                     />
00330             </portlets>
00331         """
00332         importPortlets(context)
00333 
00334         # now they should be gone
00335         mapping = assignment_mapping_from_key(self.portal,
00336             manager_name=u"test.testcolumn", category=CONTENT_TYPE_CATEGORY, key="Folder")
00337         self.assertEquals(0, len(mapping))
00338 
00339     def testBlacklisting(self):
00340         news = self.portal.news
00341         manager = getUtility(IPortletManager, name=u"test.testcolumn")
00342         
00343         assignable = getMultiAdapter((news, manager), ILocalPortletAssignmentManager)
00344         self.assertEquals(True, assignable.getBlacklistStatus(CONTEXT_CATEGORY))
00345         self.assertEquals(False, assignable.getBlacklistStatus(GROUP_CATEGORY))
00346         self.assertEquals(None, assignable.getBlacklistStatus(CONTENT_TYPE_CATEGORY))
00347 
00348     def testPurgeMethod(self):
00349         sm = getSiteManager()
00350         context = TarballExportContext(self.portal.portal_setup)
00351         handler = getMultiAdapter((sm, context), IBody, name=u'plone.portlets')
00352         handler._purgePortlets()
00353         
00354         manager = queryUtility(IPortletManager, name=u"test.testcolumn")
00355         self.assertEquals(None, manager)
00356 
00357     def testPurge(self):
00358         manager = queryUtility(IPortletManager, name=u"test.testcolumn")
00359         self.assertNotEquals(None, manager)
00360 
00361         context = DummyImportContext(self.portal, purge=False)
00362         context._files['portlets.xml'] = """<?xml version="1.0"?>
00363             <portlets purge="True">
00364             </portlets>
00365         """
00366         importPortlets(context)
00367 
00368         manager = queryUtility(IPortletManager, name=u"test.testcolumn")
00369         self.assertEquals(None, manager)
00370 
00371     def testManagerRemove(self):
00372         manager = queryUtility(IPortletManager, name=u"test.testcolumn")
00373         self.assertNotEquals(None, manager)
00374 
00375         context = DummyImportContext(self.portal, purge=False)
00376         context._files['portlets.xml'] = """<?xml version="1.0"?>
00377             <portlets>
00378                 <portletmanager 
00379                     name="test.testcolumn" 
00380                     remove="True"
00381                     />
00382             </portlets>
00383         """
00384         importPortlets(context)
00385 
00386         manager = queryUtility(IPortletManager, name=u"test.testcolumn")
00387         self.assertEquals(None, manager)
00388 
00389     def testManagerPurge(self):
00390         context = DummyImportContext(self.portal, purge=False)
00391         context._files['portlets.xml'] = """<?xml version="1.0"?>
00392             <portlets>
00393                 <portletmanager 
00394                     name="test.testcolumn" 
00395                     purge="True"
00396                     />
00397             </portlets>
00398         """
00399         importPortlets(context)
00400 
00401         self.assertRaises(KeyError,
00402                           assignment_mapping_from_key,
00403                           self.portal, manager_name=u"test.testcolumn",
00404                           category=GROUP_CATEGORY, key="Reviewers")
00405 
00406         self.assertRaises(KeyError,
00407                           assignment_mapping_from_key,
00408                           self.portal, manager_name=u"test.testcolumn",
00409                           category=CONTENT_TYPE_CATEGORY, key="Folder")
00410 
00411         # context assignment at the root are purged as well
00412         mapping = assignment_mapping_from_key(self.portal,
00413             manager_name=u"test.testcolumn", category=CONTEXT_CATEGORY, key="/")
00414         self.assertEquals(0, len(mapping))
00415 
00416     def testExport(self):
00417         sm = getSiteManager()
00418         context = TarballExportContext(self.portal.portal_setup)
00419         handler = getMultiAdapter((sm, context), IBody, name=u'plone.portlets')
00420         handler._purgePortlets()
00421         
00422         time.sleep(1)
00423         
00424         portal_setup = self.portal.portal_setup
00425         portal_setup.runAllImportStepsFromProfile('profile-plone.app.portlets:testing')
00426 
00427         expected = """\
00428 <?xml version="1.0"?>
00429 <portlets>
00430  <portletmanager name="test.testcolumn"
00431     type="plone.app.portlets.tests.test_configuration.ITestColumn"/>
00432  <portlet title="Test portlet" addview="portlets.test.Test"
00433     description="A test portlet"/>
00434  <assignment name="test.portlet6" category="group" key="Reviewers"
00435     manager="test.testcolumn" type="portlets.test.Test">
00436   <property name="test_bool"/>
00437   <property name="test_tuple"/>
00438   <property name="test_text"/>
00439  </assignment>
00440  <assignment name="test.portlet4" category="content_type" key="Folder"
00441     manager="test.testcolumn" type="portlets.test.Test">
00442   <property name="test_bool"/>
00443   <property name="test_tuple"/>
00444   <property name="test_text"/>
00445  </assignment>
00446  <assignment name="test.portlet5" category="content_type" key="Folder"
00447     manager="test.testcolumn" type="portlets.test.Test">
00448   <property name="test_bool"/>
00449   <property name="test_tuple"/>
00450   <property name="test_text"/>
00451  </assignment>
00452  <assignment name="test.portlet3" category="context" key="/"
00453     manager="test.testcolumn" type="portlets.test.Test">
00454   <property name="test_bool"/>
00455   <property name="test_tuple"/>
00456   <property name="test_text"/>
00457  </assignment>
00458  <assignment name="test.portlet2" category="context" key="/"
00459     manager="test.testcolumn" type="portlets.test.Test">
00460   <property name="test_bool">True</property>
00461   <property name="test_tuple"/>
00462   <property name="test_text">Test prop 2</property>
00463  </assignment>
00464  <assignment name="test.portlet1" category="context" key="/"
00465     manager="test.testcolumn" type="portlets.test.Test">
00466   <property name="test_bool">False</property>
00467   <property name="test_tuple"/>
00468   <property name="test_text">Test pröp 1</property>
00469  </assignment>
00470  <blacklist category="user" location="/" manager="test.testcolumn"
00471     status="acquire"/>
00472  <blacklist category="group" location="/" manager="test.testcolumn"
00473     status="show"/>
00474  <blacklist category="content_type" location="/" manager="test.testcolumn"
00475     status="block"/>
00476  <blacklist category="context" location="/" manager="test.testcolumn"
00477     status="acquire"/>
00478 </portlets>
00479 """
00480 
00481         body = handler.body
00482         self.assertEquals(expected.strip(), body.strip(), body)
00483         
00484 
00485 def test_suite():
00486     from unittest import TestSuite, makeSuite
00487     suite = TestSuite()
00488     suite.addTest(makeSuite(TestZCML))
00489     suite.addTest(makeSuite(TestGenericSetup))
00490     return suite