testInteractionWorkflow.py 13.8 KB
Newer Older
Sebastien Robin's avatar
Sebastien Robin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
##############################################################################
#
# Copyright (c) 2004 Nexedi SARL and Contributors. All Rights Reserved.
#          Sebastien Robin <seb@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################



#
# Skeleton ZopeTestCase
#

from random import randint

import os, sys
if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))

# Needed in order to have a log file inside the current folder
os.environ['EVENT_LOG_FILE'] = os.path.join(os.getcwd(), 'zLOG.log')
os.environ['EVENT_LOG_SEVERITY'] = '-300'

from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.Base import _aq_reset
from Products.ERP5.Document.Organisation import Organisation
from Products.ERP5Type.Tool.ClassTool import _aq_reset
from DateTime import DateTime
from Products.ERP5.Document.Person import Person
from AccessControl.SecurityManagement import newSecurityManager, noSecurityManager
from zLOG import LOG
import time

56
class TestInteractionWorkflow(ERP5TypeTestCase):
Sebastien Robin's avatar
Sebastien Robin committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70

  # Different variables used for this test
  run_all_test = 1
  portal_type = 'Organisation'

  def getTitle(self):
    """
    """
    return "Interaction Workflow"

  def logMessage(self,message):
    ZopeTestCase._print('\n%s ' % message)
    LOG('Testing... ',0,message)

Sebastien Robin's avatar
Sebastien Robin committed
71 72 73
  def getBusinessTemplateList(self):
    return ('erp5_base',)

74
  def afterSetUp(self):
Sebastien Robin's avatar
Sebastien Robin committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    self.login()
    #self.createData()

  def login(self, quiet=0):
    uf = self.getPortal().acl_users
    uf._doAddUser('seb', '', ['Manager'], [])
    user = uf.getUserById('seb').__of__(uf)
    newSecurityManager(None, user)

  def createData(self):
    def doSomethingStupid(self,value,**kw):
      """
      """
      self.setDescription(value)
    Organisation.doSomethingStupid = doSomethingStupid
Sebastien Robin's avatar
Sebastien Robin committed
90 91
    portal_type = self.getTypeTool()['Organisation']
    portal_type.base_category_list = ['size']
92
    organisation_module = self.getOrganisationModule()
93 94
    self.organisation = organisation_module.newContent(
                          portal_type = self.portal_type)
95
    self.organisation.immediateReindexObject()
Sebastien Robin's avatar
Sebastien Robin committed
96 97 98 99 100 101 102

  def createInteractionWorkflow(self):
    id = 'test_workflow'
    wf_type = "interaction_workflow (Web-configurable interaction workflow)"
    self.getWorkflowTool().manage_addWorkflow(workflow_type=wf_type,id=id)
    wf = self.getWorkflowTool()[id]
    self.wf = wf
103 104
    wf.scripts.manage_addProduct['PythonScripts']\
                  .manage_addPythonScript(id='afterEdit')
Sebastien Robin's avatar
Sebastien Robin committed
105 106 107
    self.script = wf.scripts['afterEdit']
    wf.interactions.addInteraction(id='edit')
    self.interaction = wf.interactions['edit']
108 109
    self.getWorkflowTool().setChainForPortalTypes(
                  [self.portal_type],'test_workflow')
Sebastien Robin's avatar
Sebastien Robin committed
110
    _aq_reset() # XXX Fails XXX _setLastId not found when doing newContent
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  
  def createInteractionWorkflowWithTwoInteractions(self):
    id = 'test_workflow'
    wf_type = "interaction_workflow (Web-configurable interaction workflow)"
    self.getWorkflowTool().manage_addWorkflow(workflow_type=wf_type,id=id)
    wf = self.getWorkflowTool()[id]
    self.wf = wf
    wf.scripts.manage_addProduct['PythonScripts']\
                  .manage_addPythonScript(id='afterEditA')
    self.scriptA = wf.scripts['afterEditA']
    wf.interactions.addInteraction(id='editA')
    self.interactionA = wf.interactions['editA']
    wf.scripts.manage_addProduct['PythonScripts']\
                  .manage_addPythonScript(id='afterEditB')
    self.scriptB = wf.scripts['afterEditB']
    wf.interactions.addInteraction(id='editB')
    self.interactionB = wf.interactions['editB']
    self.getWorkflowTool().setChainForPortalTypes(
                  [self.portal_type],'test_workflow')
    _aq_reset() # XXX Fails XXX _setLastId not found when doing newContent
Sebastien Robin's avatar
Sebastien Robin committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

  def test_01(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('No Interactions')
    self.createData()
    organisation = self.organisation
    organisation.edit()
    self.assertEquals(organisation.getDescription(),'')

  def test_02(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Interactions On Edit')
    self.createInteractionWorkflow()
146 147 148 149
    self.interaction.setProperties(
            'afterEdit',
            method_id='edit',
            after_script_name=('afterEdit',))
Sebastien Robin's avatar
Sebastien Robin committed
150 151 152 153 154 155 156
    #body = "sci.object.setDescription('toto')"
    params = 'sci,**kw'
    body = "context = sci.object\n" +\
           "context.setDescription('toto')"
    self.script.ZPythonScript_edit(params,body)
    self.createData()
    organisation = self.organisation
Sebastien Robin's avatar
Sebastien Robin committed
157
    organisation.setDescription('bad')
Sebastien Robin's avatar
Sebastien Robin committed
158 159 160 161 162 163
    organisation.edit()
    self.assertEquals(organisation.getDescription(),'toto')

  def test_03(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
164 165
      self.logMessage(
        'Interactions, Edit Set Description and also After Script')
Sebastien Robin's avatar
Sebastien Robin committed
166
    self.createInteractionWorkflow()
167 168 169 170
    self.interaction.setProperties(
            'afterEdit',
            method_id='edit',
            after_script_name=('afterEdit',))
Sebastien Robin's avatar
Sebastien Robin committed
171 172 173 174 175 176
    body = "context = sci.object\n" +\
           "context.setDescription('toto')"
    params = 'sci,**kw'
    self.script.ZPythonScript_edit(params,body)
    self.createData()
    organisation = self.organisation
Sebastien Robin's avatar
Sebastien Robin committed
177
    organisation.setDescription('bad')
Sebastien Robin's avatar
Sebastien Robin committed
178 179 180 181 182 183 184 185
    organisation.edit(description='tutu')
    self.assertEquals(organisation.getDescription(),'toto')

  def test_04(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Interactions, Automatic Workflow Method')
    self.createInteractionWorkflow()
186 187 188 189
    self.interaction.setProperties(
            'afterEdit',
            method_id='doSomethingStupid',
            after_script_name=('afterEdit',))
Sebastien Robin's avatar
Sebastien Robin committed
190 191 192 193 194 195
    body = "context = sci.object\n" +\
           "context.setDescription('toto')"
    params = 'sci,**kw'
    self.script.ZPythonScript_edit(params,body)
    self.createData()
    organisation = self.organisation
Sebastien Robin's avatar
Sebastien Robin committed
196
    organisation.setDescription('bad')
Sebastien Robin's avatar
Sebastien Robin committed
197 198 199
    organisation.doSomethingStupid('tutu')
    self.assertEquals(organisation.getDescription(),'toto')

Sebastien Robin's avatar
Sebastien Robin committed
200 201 202
  def test_05(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
203 204
      self.logMessage(
        'Interactions, Automatic Workflow Method With Extra Base Category')
Sebastien Robin's avatar
Sebastien Robin committed
205
    self.createInteractionWorkflow()
206 207 208 209
    self.interaction.setProperties(
            'afterEdit',
            method_id='setSizeList _setSizeList',
            after_script_name=('afterEdit',))
Sebastien Robin's avatar
Sebastien Robin committed
210 211 212 213 214 215 216 217 218 219
    body = "context = sci.object\n" +\
           "context.setDescription('toto')"
    params = 'sci,**kw'
    self.script.ZPythonScript_edit(params,body)
    self.createData()
    organisation = self.organisation
    organisation.setDescription('bad')
    organisation.setSizeList(['size/1','size/2'])
    self.assertEquals(organisation.getDescription(),'toto')

220 221 222 223 224
  def test_06(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Interactions, Check If There Is Only One Call')
    self.createInteractionWorkflow()
225 226 227 228
    self.interaction.setProperties(
            'afterEdit',
            method_id='edit',
            after_script_name=('afterEdit',))
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    params = 'sci,**kw'
    body = "context = sci.object\n" +\
           "description = context.getDescription()\n" +\
           "if description is None:\n" +\
           "  description = ''\n" +\
           "context.setDescription(description + 'a')"
    self.script.ZPythonScript_edit(params,body)
    self.createData()
    organisation = self.organisation
    organisation.setDescription(None)
    self.assertEquals(organisation.getDescription(),None)
    organisation.edit()
    self.assertEquals(organisation.getDescription(),'a')
    organisation.edit()
    self.assertEquals(organisation.getDescription(),'aa')
Sebastien Robin's avatar
Sebastien Robin committed
244

245
  def test_07(self, quiet=0, run=run_all_test):
246 247 248 249 250
    if not run: return
    if not quiet:
      self.logMessage('Interactions, Check If The Return Value Is Not Altered')
    self.createInteractionWorkflow()
    self.interaction.setProperties(
251 252 253
            'afterEdit',
            method_id='newContent',
            after_script_name=('afterEdit',))
254 255 256 257 258 259 260 261 262 263 264 265
    params = 'sci,**kw'
    body = "context = sci.object\n" +\
           "return 3\n"
    self.script.ZPythonScript_edit(params,body)
    self.createData()
    organisation = self.organisation
    dummy_bank_account = organisation.newContent(
          portal_type='Bank Account',
          id='dummy_bank_account')
    self.assertNotEquals(dummy_bank_account, None)
    self.assertNotEquals(dummy_bank_account, 3)
    self.assertEquals(dummy_bank_account.getPortalType(), 'Bank Account')
Sebastien Robin's avatar
Sebastien Robin committed
266

267 268 269 270 271 272 273
  def test_08(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Interactions, Check If Multiple method_id Can Be Hooked')
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'afterEdit',
274
            method_id='setCorporateName setActivityCode',
275 276 277 278 279 280 281 282 283 284 285
            after_script_name=('afterEdit',))
    params = 'sci,**kw'
    body = "context = sci.object\n" +\
           "description = context.getDescription()\n" +\
           "if description is None:\n" +\
           "  description = ''\n" +\
           "context.setDescription(description + 'a')"
    self.script.ZPythonScript_edit(params,body)
    self.createData()
    organisation = self.organisation
    organisation.setDescription(None)
286 287
    self.assertEquals(organisation.getDescription(), None)
    organisation.setCorporateName('corp')
288
    self.assertEquals(organisation.getDescription(),'a')
289
    organisation.setActivityCode('acode')
290 291
    self.assertEquals(organisation.getDescription(),'aa')
    
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
  def test_09(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Interactions, Check if the same method_id '\
                      'can be hooked by two Interactions')
    self.createInteractionWorkflowWithTwoInteractions()
    self.interactionA.setProperties(
            'afterEditA',
            method_id='edit',
            after_script_name=('afterEditA',))
    self.interactionB.setProperties(
            'afterEditB',
            method_id='edit',
            after_script_name=('afterEditB',))
    params = 'sci,**kw'
    body = "context = sci.object\n" +\
           "context.log('InteractionWF.test_09 in script', 'a')\n" +\
           "description = context.getDescription()\n" +\
           "if description is None:\n" +\
           "  description = ''\n" +\
           "context.setDescription(description + 'a')"
    self.scriptA.ZPythonScript_edit(params, body)
    self.scriptB.ZPythonScript_edit(params, body.replace("'a'", "'b'"))
    
    self.createData()
    organisation = self.organisation
    organisation.setDescription(None)
    self.assertEquals(organisation.getDescription(), None)
    organisation.edit()
    self.assert_(organisation.getDescription() in ('ab', 'ba'),
        "description should be 'ab' or 'ba', it is %s" %
        organisation.getDescription())
    organisation.setCorporateName("this should not change anything")
    self.assert_(organisation.getDescription() in ('ab', 'ba'),
        "description should be 'ab' or 'ba', it is %s" %
        organisation.getDescription())
    
  def test_10(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Interactions, check if multiple scripts can be '
                      'called')
    self.createInteractionWorkflowWithTwoInteractions()
    self.interactionA.setProperties(
            'afterEdit',
            method_id='edit',
            after_script_name=('afterEditA', 'afterEditB'))
    params = 'sci,**kw'
    body = "context = sci.object\n" +\
           "context.log('InteractionWF.test_10 in script', 'a')\n" +\
           "description = context.getDescription()\n" +\
           "if description is None:\n" +\
           "  description = ''\n" +\
           "context.setDescription(description + 'a')"
    self.scriptA.ZPythonScript_edit(params, body)
    self.scriptB.ZPythonScript_edit(params, body.replace("'a'", "'b'"))
    
    self.createData()
    organisation = self.organisation
    organisation.setDescription(None)
    self.assertEquals(organisation.getDescription(), None)
    organisation.edit()
    self.assert_(organisation.getDescription() in ('ab', 'ba'),
        "description should be 'ab' or 'ba', it is %s" %
        organisation.getDescription())
    organisation.setCorporateName("this should not change anything")
    self.assert_(organisation.getDescription() in ('ab', 'ba'),
        "description should be 'ab' or 'ba', it is %s" %
        organisation.getDescription())
    
Sebastien Robin's avatar
Sebastien Robin committed
362 363 364 365 366 367 368 369 370
if __name__ == '__main__':
    framework()
else:
    import unittest
    def test_suite():
        suite = unittest.TestSuite()
        suite.addTest(unittest.makeSuite(Test))
        return suite