Back to index

plone3  3.1.7
testWorkflowTool.py
Go to the documentation of this file.
00001 #
00002 # Tests the workflow tool
00003 #
00004 
00005 from zope.interface import directlyProvides
00006 from zope.component import provideAdapter, getGlobalSiteManager
00007 
00008 from Products.CMFPlone.tests import PloneTestCase
00009 from Products.CMFPlone.tests.dummy import Dummy, DummyWorkflowChainAdapter
00010 from Products.CMFDefault.interfaces import IDocument
00011 from Products.CMFCore.interfaces import IWorkflowTool
00012 
00013 default_user = PloneTestCase.default_user
00014 
00015 # XXX - Ugh...Rather than use and update ambiguous numbers,
00016 # we maintain a mapping of the various workflows to stats
00017 # though there are some obvious downsides to this, it's better than just
00018 # asserting that there are X published states in all workflows, etc.
00019 workflow_dict = {
00020       'folder_workflow':('private','published','visible',)
00021     , 'intranet_folder_workflow':('internal','private',)
00022     , 'intranet_workflow':('internal','internally_published','pending',
00023                            'private','external',)
00024     , 'one_state_workflow':('published',)
00025     , 'plone_workflow':('pending','private','published','visible',)
00026     , 'simple_publication_workflow':('private','published','pending',)
00027 }
00028 # then we join all states into one master list
00029 all_states = []
00030 for states in workflow_dict.values():
00031     all_states += list(states)
00032 
00033 class TestWorkflowTool(PloneTestCase.PloneTestCase):
00034 
00035     def afterSetUp(self):
00036         self.workflow = self.portal.portal_workflow
00037 
00038         self.portal.acl_users._doAddUser('member', 'secret', ['Member'], [])
00039         self.portal.acl_users._doAddUser('reviewer', 'secret', ['Reviewer'], [])
00040         self.portal.acl_users._doAddUser('manager', 'secret', ['Manager'], [])
00041 
00042         self.folder.invokeFactory('Document', id='doc')
00043         self.doc = self.folder.doc
00044 
00045         self.folder.invokeFactory('Event', id='ev')
00046         self.ev = self.folder.ev
00047 
00048     def testGetTransitionsForProvidesURL(self):
00049         trans = self.workflow.getTransitionsFor(self.doc)
00050         self.assertEqual(len(trans), 2)
00051         self.failUnless(trans[0].has_key('url'))
00052         # Test that url has filled in string substitutions for content url
00053         self.failUnless('http://' in trans[0]['url'])
00054 
00055     def testGetTransitionsForProvidesDescription(self):
00056         trans = self.workflow.getTransitionsFor(self.doc)
00057         self.assertEqual(len(trans), 2)
00058         self.failUnless(trans[0].has_key('description'))
00059 
00060     def testGetTitleForStateOnType(self):
00061         state_id = self.workflow.getInfoFor(self.doc, 'review_state', '')
00062         state_title = self.workflow.getTitleForStateOnType(state_id, self.doc.portal_type)
00063         self.assertEqual(state_id, 'visible')
00064         self.assertEqual(state_title.lower(), 'public draft')
00065 
00066     def testGetTitleForStateOnTypeFallsBackOnStateId(self):
00067         state_id = 'nonsense'
00068         state_title = self.workflow.getTitleForStateOnType(state_id, self.doc.portal_type)
00069         self.assertEqual(state_title, 'nonsense')
00070 
00071     def testGetTitleForStateOnTypeSucceedsWithNonString(self):
00072         # Related to http://dev.plone.org/plone/ticket/4638
00073         # Non content objects can pass None or MissingValue.
00074         state_id = None
00075         state_title = self.workflow.getTitleForStateOnType(state_id, self.doc.portal_type)
00076         self.assertEqual(state_title, state_id)
00077 
00078     def testGetTitleForTransitionOnType(self):
00079         state_id = 'hide'
00080         state_title = self.workflow.getTitleForTransitionOnType(state_id, self.doc.portal_type)
00081         self.assertEqual(state_title, 'Make private')
00082 
00083     def testGetTitleForTransitionOnTypeFallsBackOnTransitionId(self):
00084         state_id = 'nonsense'
00085         state_title = self.workflow.getTitleForTransitionOnType(state_id, self.doc.portal_type)
00086         self.assertEqual(state_title, 'nonsense')
00087 
00088     def testGetTitleForTransitionOnTypeSucceedsWithNonString(self):
00089         # Related to http://dev.plone.org/plone/ticket/4638
00090         # Non content objects can pass None or MissingValue.
00091         state_id = None
00092         state_title = self.workflow.getTitleForTransitionOnType(state_id, self.doc.portal_type)
00093         self.assertEqual(state_title, state_id)
00094 
00095     def testListWFStatesByTitle(self):
00096         states = self.workflow.listWFStatesByTitle()
00097         self.assertEqual(len(states), len(all_states))
00098         pub_states = [s for s in states if s[1]=='published']
00099         priv_states = [s for s in states if s[1]=='private']
00100         pend_states = [s for s in states if s[1]=='pending']
00101         vis_states = [s for s in states if s[1]=='visible']
00102         external_states = [s for s in states if s[1]=='external']
00103         internal_states = [s for s in states if s[1]=='internal']
00104         internal_pub_states = [s for s in states
00105                                  if s[1]=='internally_published']
00106 
00107         self.assertEqual(len(pub_states), all_states.count('published'))
00108         self.assertEqual(len(priv_states), all_states.count('private'))
00109         self.assertEqual(len(pend_states), all_states.count('pending'))
00110         self.assertEqual(len(vis_states), all_states.count('visible'))
00111         self.assertEqual(len(external_states), all_states.count('external'))
00112         self.assertEqual(len(internal_states), all_states.count('internal'))
00113         self.assertEqual(len(internal_pub_states),
00114                          all_states.count('internally_published'))
00115 
00116     def testAdaptationBasedWorkflowOverride(self):
00117         # We take a piece of dummy content and register a dummy
00118         # workflow chain adapter for it.
00119         content = Dummy()
00120         directlyProvides(content, IDocument)
00121         provideAdapter(DummyWorkflowChainAdapter,
00122                         adapts=(IDocument, IWorkflowTool))
00123         self.assertEqual(self.workflow.getChainFor(content),
00124                          ('Static Workflow',))
00125         # undo our registration so we don't break tests
00126         components = getGlobalSiteManager()
00127         components.unregisterAdapter(DummyWorkflowChainAdapter,
00128                                      required=(IDocument, IWorkflowTool))
00129 
00130 def test_suite():
00131     from unittest import TestSuite, makeSuite
00132     suite = TestSuite()
00133     suite.addTest(makeSuite(TestWorkflowTool))
00134     return suite