Back to index

plone3  3.1.7
test_configuration.py
Go to the documentation of this file.
00001 from transaction import commit
00002 import time
00003 
00004 from zope.component import getUtility
00005 from zope.component import getMultiAdapter
00006 
00007 from plone.contentrules.engine.interfaces import IRuleAssignmentManager
00008 from plone.contentrules.engine.interfaces import IRuleStorage
00009 
00010 from plone.app.contentrules.tests.base import ContentRulesTestCase
00011 
00012 from Products.Five import zcml
00013 from Products.Five import fiveconfigure
00014 
00015 from Products.GenericSetup.interfaces import IBody
00016 from Products.GenericSetup.context import TarballExportContext
00017 
00018 from Products.PloneTestCase.layer import PloneSite
00019 from Testing import ZopeTestCase
00020 
00021 from zope.lifecycleevent.interfaces import IObjectModifiedEvent
00022 
00023 zcml_string = """\
00024 <configure xmlns="http://namespaces.zope.org/zope"
00025            xmlns:gs="http://namespaces.zope.org/genericsetup"
00026            package="plone.app.contentrules"
00027            i18n_domain="plone">
00028 
00029     <gs:registerProfile
00030         name="testing"
00031         title="plone.app.contentrules testing"
00032         description="Used for testing only" 
00033         directory="tests/profiles/testing"
00034         for="Products.CMFCore.interfaces.ISiteRoot"
00035         provides="Products.GenericSetup.interfaces.EXTENSION"
00036         />
00037         
00038 </configure>
00039 """
00040 
00041 class TestContentrulesGSLayer(PloneSite):
00042     
00043     @classmethod
00044     def setUp(cls):
00045         
00046         fiveconfigure.debug_mode = True
00047         zcml.load_string(zcml_string)
00048         fiveconfigure.debug_mode = False
00049         
00050         app = ZopeTestCase.app()
00051         portal = app.plone
00052         
00053         portal_setup = portal.portal_setup
00054         portal_setup.runAllImportStepsFromProfile('profile-plone.app.contentrules:testing')
00055         
00056         commit()
00057         ZopeTestCase.close(app)
00058 
00059     @classmethod
00060     def tearDown(cls):
00061         app = ZopeTestCase.app()
00062         portal = app.plone
00063         
00064         storage = getUtility(IRuleStorage, context=portal)
00065         for key in list(storage.keys()):
00066             del storage[key]
00067         
00068         commit()
00069         ZopeTestCase.close(app)
00070 
00071 class TestGenericSetup(ContentRulesTestCase):
00072 
00073     layer = TestContentrulesGSLayer
00074     
00075     def afterSetUp(self):
00076         self.storage = getUtility(IRuleStorage)
00077     
00078     def testRuleInstalled(self):
00079         self.failUnless('test1' in self.storage)
00080         self.failUnless('test2' in self.storage)
00081         
00082     def testRulesConfigured(self):
00083         rule1 = self.storage['test1']
00084         self.assertEquals("Test rule 1", rule1.title)
00085         self.assertEquals("A test rule", rule1.description)
00086         self.assertEquals(IObjectModifiedEvent, rule1.event)
00087         self.assertEquals(True, rule1.enabled)
00088         self.assertEquals(False, rule1.stop)
00089         
00090         self.assertEquals(2, len(rule1.conditions))
00091         self.assertEquals("plone.conditions.PortalType", rule1.conditions[0].element)
00092         self.assertEquals(["Document", "News Item"], list(rule1.conditions[0].check_types))
00093         self.assertEquals("plone.conditions.Role", rule1.conditions[1].element)
00094         self.assertEquals(["Manager"], list(rule1.conditions[1].role_names))
00095         
00096         self.assertEquals(1, len(rule1.actions))
00097         self.assertEquals("plone.actions.Notify", rule1.actions[0].element)
00098         self.assertEquals(u"A message: Hej d\xe5", rule1.actions[0].message)
00099         self.assertEquals("info", rule1.actions[0].message_type)
00100         
00101         rule2 = self.storage['test2']
00102         self.assertEquals("Test rule 2", rule2.title)
00103         self.assertEquals("Another test rule", rule2.description)
00104         self.assertEquals(IObjectModifiedEvent, rule2.event)
00105         self.assertEquals(False, rule2.enabled)
00106         self.assertEquals(True, rule2.stop)
00107         
00108         self.assertEquals(1, len(rule2.conditions))
00109         self.assertEquals("plone.conditions.PortalType", rule2.conditions[0].element)
00110         self.assertEquals(["Event"], list(rule2.conditions[0].check_types))
00111         
00112         self.assertEquals(1, len(rule2.actions))
00113         self.assertEquals("plone.actions.Workflow", rule2.actions[0].element)
00114         self.assertEquals("publish", rule2.actions[0].transition)
00115         
00116     def testRuleAssigned(self):
00117         assignable = IRuleAssignmentManager(self.portal.news)
00118         self.assertEquals(3, len(assignable))
00119         
00120         self.assertEquals(True, assignable['test1'].enabled)
00121         self.assertEquals(False, assignable['test1'].bubbles)
00122         
00123         self.assertEquals(False, assignable['test2'].enabled)
00124         self.assertEquals(True, assignable['test2'].bubbles)
00125         
00126         self.assertEquals(False, assignable['test3'].enabled)
00127         self.assertEquals(False, assignable['test3'].bubbles)
00128         
00129     def testAssignmentOrdering(self):
00130         assignable = IRuleAssignmentManager(self.portal.news)
00131         self.assertEquals([u'test3', u'test2', u'test1'], assignable.keys())
00132         
00133     def testImportTwice(self):
00134         # Ensure rules, actions/conditions and assignments are not duplicated
00135         # if the profile is re-imported; see bug #8027.
00136         portal_setup = self.portal.portal_setup
00137         time.sleep(1) # avoid timestamp colission
00138         portal_setup.runAllImportStepsFromProfile('profile-plone.app.contentrules:testing')
00139         
00140         # We should get the same results as before
00141         self.testRuleInstalled()
00142         self.testRulesConfigured()
00143         self.testRuleAssigned()
00144         
00145     def testExport(self):
00146         site = self.portal
00147         context = TarballExportContext(self.portal.portal_setup)
00148         exporter = getMultiAdapter((site, context), IBody, name=u'plone.contentrules')
00149         
00150         expected = """\
00151 <?xml version="1.0"?>
00152 <contentrules>
00153  <rule name="test1" title="Test rule 1" description="A test rule"
00154     enabled="True" event="zope.lifecycleevent.interfaces.IObjectModifiedEvent"
00155     stop-after="False">
00156   <conditions>
00157    <condition type="plone.conditions.PortalType">
00158     <property name="check_types">
00159      <element>Document</element>
00160      <element>News Item</element>
00161     </property>
00162    </condition>
00163    <condition type="plone.conditions.Role">
00164     <property name="role_names">
00165      <element>Manager</element>
00166     </property>
00167    </condition>
00168   </conditions>
00169   <actions>
00170    <action type="plone.actions.Notify">
00171     <property name="message">A message: Hej då</property>
00172     <property name="message_type">info</property>
00173    </action>
00174   </actions>
00175  </rule>
00176  <rule name="test2" title="Test rule 2" description="Another test rule"
00177     enabled="False"
00178     event="zope.lifecycleevent.interfaces.IObjectModifiedEvent"
00179     stop-after="True">
00180   <conditions>
00181    <condition type="plone.conditions.PortalType">
00182     <property name="check_types">
00183      <element>Event</element>
00184     </property>
00185    </condition>
00186   </conditions>
00187   <actions>
00188    <action type="plone.actions.Workflow">
00189     <property name="transition">publish</property>
00190    </action>
00191   </actions>
00192  </rule>
00193  <rule name="test3" title="Test rule 3" description="Third test rule"
00194     enabled="True" event="zope.app.container.interfaces.IObjectMovedEvent"
00195     stop-after="False">
00196   <conditions/>
00197   <actions/>
00198  </rule>
00199  <assignment name="test3" bubbles="False" enabled="False" location="/news"/>
00200  <assignment name="test2" bubbles="True" enabled="False" location="/news"/>
00201  <assignment name="test1" bubbles="False" enabled="True" location="/news"/>
00202 </contentrules>
00203 """
00204 
00205         body = exporter.body
00206         self.assertEquals(expected.strip(), body.strip(), body)
00207         
00208 def test_suite():
00209     from unittest import TestSuite, makeSuite
00210     suite = TestSuite()
00211     suite.addTest(makeSuite(TestGenericSetup))
00212     return suite