Back to index

plone3  3.1.7
test_action_copy.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.copy import CopyAction
00009 from plone.app.contentrules.actions.copy import CopyEditForm
00010 
00011 from plone.app.contentrules.rule import Rule
00012 
00013 from plone.app.contentrules.tests.base import ContentRulesTestCase
00014 
00015 from zope.component.interfaces import IObjectEvent
00016 
00017 from Products.PloneTestCase.setup import default_user
00018 
00019 class DummyEvent(object):
00020     implements(IObjectEvent)
00021     
00022     def __init__(self, object):
00023         self.object = object
00024 
00025 class TestCopyAction(ContentRulesTestCase):
00026 
00027     def afterSetUp(self):
00028         self.loginAsPortalOwner()
00029         self.portal.invokeFactory('Folder', 'target')
00030         self.login(default_user)
00031         self.folder.invokeFactory('Document', 'd1')
00032 
00033     def testRegistered(self): 
00034         element = getUtility(IRuleAction, name='plone.actions.Copy')
00035         self.assertEquals('plone.actions.Copy', element.addview)
00036         self.assertEquals('edit', element.editview)
00037         self.assertEquals(None, element.for_)
00038         self.assertEquals(IObjectEvent, element.event)
00039     
00040     def testInvokeAddView(self): 
00041         element = getUtility(IRuleAction, name='plone.actions.Copy')
00042         storage = getUtility(IRuleStorage)
00043         storage[u'foo'] = Rule()
00044         rule = self.portal.restrictedTraverse('++rule++foo')
00045         
00046         adding = getMultiAdapter((rule, self.portal.REQUEST), name='+action')
00047         addview = getMultiAdapter((adding, self.portal.REQUEST), name=element.addview)
00048         
00049         addview.createAndAdd(data={'target_folder' : '/target',})
00050         
00051         e = rule.actions[0]
00052         self.failUnless(isinstance(e, CopyAction))
00053         self.assertEquals('/target', e.target_folder)
00054     
00055     def testInvokeEditView(self): 
00056         element = getUtility(IRuleAction, name='plone.actions.Copy')
00057         e = CopyAction()
00058         editview = getMultiAdapter((e, self.folder.REQUEST), name=element.editview)
00059         self.failUnless(isinstance(editview, CopyEditForm))
00060 
00061     def testExecute(self): 
00062         e = CopyAction()
00063         e.target_folder = '/target'
00064         
00065         ex = getMultiAdapter((self.folder, e, DummyEvent(self.folder.d1)), IExecutable)
00066         self.assertEquals(True, ex())
00067         
00068         self.failUnless('d1' in self.folder.objectIds())
00069         self.failUnless('d1' in self.portal.target.objectIds())
00070         
00071     def testExecuteWithError(self): 
00072         e = CopyAction()
00073         e.target_folder = '/dummy'
00074         
00075         ex = getMultiAdapter((self.folder, e, DummyEvent(self.folder.d1)), IExecutable)
00076         self.assertEquals(False, ex())
00077         
00078         self.failUnless('d1' in self.folder.objectIds())
00079         self.failIf('d1' in self.portal.target.objectIds())
00080         
00081     def testExecuteWithoutPermissionsOnTarget(self):
00082         self.setRoles(('Member',))
00083         
00084         e = CopyAction()
00085         e.target_folder = '/target'
00086         
00087         ex = getMultiAdapter((self.folder, e, DummyEvent(self.folder.d1)), IExecutable)
00088         self.assertEquals(True, ex())
00089         
00090         self.failUnless('d1' in self.folder.objectIds())
00091         self.failUnless('d1' in self.portal.target.objectIds())
00092         
00093     def testExecuteWithNamingConflict(self):
00094         self.setRoles(('Manager',))
00095         self.portal.target.invokeFactory('Document', 'd1')
00096         self.setRoles(('Member',))
00097         
00098         e = CopyAction()
00099         e.target_folder = '/target'
00100         
00101         ex = getMultiAdapter((self.folder, e, DummyEvent(self.folder.d1)), IExecutable)
00102         self.assertEquals(True, ex())
00103         
00104         self.failUnless('d1' in self.folder.objectIds())
00105         self.failUnless('d1' in self.portal.target.objectIds())
00106         self.failUnless('d1.1' in self.portal.target.objectIds())
00107         
00108 def test_suite():
00109     from unittest import TestSuite, makeSuite
00110     suite = TestSuite()
00111     suite.addTest(makeSuite(TestCopyAction))
00112     return suite