Back to index

plone3  3.1.7
test_action_notify.py
Go to the documentation of this file.
00001 from zope.interface import implements, Interface
00002 from zope.component import getUtility, getMultiAdapter
00003 
00004 from plone.contentrules.engine.interfaces import IRuleStorage
00005 from plone.contentrules.rule.interfaces import IRuleAction
00006 from plone.contentrules.rule.interfaces import IExecutable
00007 
00008 from plone.app.contentrules.actions.notify import NotifyAction
00009 from plone.app.contentrules.actions.notify import NotifyEditForm
00010 
00011 from plone.app.contentrules.rule import Rule
00012 
00013 from plone.app.contentrules.tests.base import ContentRulesTestCase
00014 
00015 from Products.statusmessages import STATUSMESSAGEKEY
00016 from Products.statusmessages.interfaces import IStatusMessage
00017 from Products.statusmessages.adapter import _decodeCookieValue
00018 
00019 class DummyEvent(object):
00020     implements(Interface)
00021 
00022 class TestNotifyAction(ContentRulesTestCase):
00023 
00024     def afterSetUp(self):
00025         self.setRoles(('Manager',))
00026 
00027     def testRegistered(self): 
00028         element = getUtility(IRuleAction, name='plone.actions.Notify')
00029         self.assertEquals('plone.actions.Notify', element.addview)
00030         self.assertEquals('edit', element.editview)
00031         self.assertEquals(None, element.for_)
00032         self.assertEquals(None, element.event)
00033     
00034     def testInvokeAddView(self): 
00035         element = getUtility(IRuleAction, name='plone.actions.Notify')
00036         storage = getUtility(IRuleStorage)
00037         storage[u'foo'] = Rule()
00038         rule = self.portal.restrictedTraverse('++rule++foo')
00039         
00040         adding = getMultiAdapter((rule, self.portal.REQUEST), name='+action')
00041         addview = getMultiAdapter((adding, self.portal.REQUEST), name=element.addview)
00042         
00043         addview.createAndAdd(data={'message' : 'Hello world', 'message_type' : 'info'})
00044         
00045         e = rule.actions[0]
00046         self.failUnless(isinstance(e, NotifyAction))
00047         self.assertEquals('Hello world', e.message)
00048         self.assertEquals('info', e.message_type)
00049     
00050     def testInvokeEditView(self): 
00051         element = getUtility(IRuleAction, name='plone.actions.Notify')
00052         e = NotifyAction()
00053         editview = getMultiAdapter((e, self.folder.REQUEST), name=element.editview)
00054         self.failUnless(isinstance(editview, NotifyEditForm))
00055 
00056     def testExecute(self): 
00057         e = NotifyAction()
00058         e.message = 'Hello world'
00059         e.message_type = 'info'
00060         
00061         ex = getMultiAdapter((self.folder, e, DummyEvent()), IExecutable)
00062         self.assertEquals(True, ex())
00063         
00064         status = IStatusMessage(self.app.REQUEST)
00065         new_cookies = self.app.REQUEST.RESPONSE.cookies[STATUSMESSAGEKEY]
00066         messages = _decodeCookieValue(new_cookies['value'])
00067         self.assertEquals(1, len(messages))
00068         self.assertEquals('Hello world', messages[0].message)
00069         self.assertEquals('info', messages[0].type)
00070         
00071         
00072 def test_suite():
00073     from unittest import TestSuite, makeSuite
00074     suite = TestSuite()
00075     suite.addTest(makeSuite(TestNotifyAction))
00076     return suite