testInteractionWorkflow.py 25 KB
Newer Older
1
# -*- coding: utf-8 -*-
Sebastien Robin's avatar
Sebastien Robin committed
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
##############################################################################
#
# 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.
#
##############################################################################

30
import unittest
Sebastien Robin's avatar
Sebastien Robin committed
31

32
import transaction
Sebastien Robin's avatar
Sebastien Robin committed
33 34
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.Base import _aq_reset
35
from AccessControl import ClassSecurityInfo
36
from AccessControl.SecurityManagement import newSecurityManager
37
import Products.ERP5Type
38
from Products.ERP5Type.Workflow import addWorkflowByType
Sebastien Robin's avatar
Sebastien Robin committed
39

40
class TestInteractionWorkflow(ERP5TypeTestCase):
Sebastien Robin's avatar
Sebastien Robin committed
41 42 43 44 45 46 47 48 49 50

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

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

Sebastien Robin's avatar
Sebastien Robin committed
51 52 53
  def getBusinessTemplateList(self):
    return ('erp5_base',)

54
  def afterSetUp(self):
Sebastien Robin's avatar
Sebastien Robin committed
55 56
    self.login()

57
  def beforeTearDown(self):
58
    Organisation = Products.ERP5.Document.Organisation.Organisation
59 60 61 62 63 64
    Organisation.security.names.pop('doSomethingStupid', None)
    if hasattr(Organisation, 'doSomethingStupid'):
      delattr(Organisation, 'doSomethingStupid')
    if hasattr(Organisation, 'doSomethingStupid__roles__'):
      delattr(Organisation, 'doSomethingStupid__roles__')

Sebastien Robin's avatar
Sebastien Robin committed
65 66
  def login(self, quiet=0):
    uf = self.getPortal().acl_users
67
    uf._doAddUser('seb', '', ['Manager', 'Assignor'], [])
Sebastien Robin's avatar
Sebastien Robin committed
68 69 70 71 72 73 74 75
    user = uf.getUserById('seb').__of__(uf)
    newSecurityManager(None, user)

  def createData(self):
    def doSomethingStupid(self,value,**kw):
      """
      """
      self.setDescription(value)
76
    Organisation = Products.ERP5.Document.Organisation.Organisation
Sebastien Robin's avatar
Sebastien Robin committed
77
    Organisation.doSomethingStupid = doSomethingStupid
Sebastien Robin's avatar
Sebastien Robin committed
78
    portal_type = self.getTypeTool()['Organisation']
79
    portal_type._setTypeBaseCategoryList(['size'])
80
    organisation_module = self.getOrganisationModule()
81 82
    self.organisation = organisation_module.newContent(
                          portal_type = self.portal_type)
83
    self.organisation.immediateReindexObject()
Sebastien Robin's avatar
Sebastien Robin committed
84

85 86
  def _createInteractionWorkflowWithId(self, wf_id):
    wf_tool = self.getWorkflowTool()
87
    return addWorkflowByType(wf_tool, "interaction_workflow", wf_id)
88

Sebastien Robin's avatar
Sebastien Robin committed
89 90
  def createInteractionWorkflow(self):
    id = 'test_workflow'
91
    wf_type = "interaction_workflow"
92
    if getattr(self.getWorkflowTool(), id, None) is None:
93
      self._createInteractionWorkflowWithId(id)
Sebastien Robin's avatar
Sebastien Robin committed
94 95
    wf = self.getWorkflowTool()[id]
    self.wf = wf
96 97 98
    if getattr(wf.scripts, 'afterEdit', None) is None:
      wf.scripts.manage_addProduct['PythonScripts']\
                    .manage_addPythonScript(id='afterEdit')
Sebastien Robin's avatar
Sebastien Robin committed
99
    self.script = wf.scripts['afterEdit']
100 101
    if getattr(wf.interactions, 'edit_interaction', None) is None:
      wf.interactions.addInteraction(id='edit_interaction')
102
    self.interaction = wf.interactions['edit_interaction']
103
    self.getWorkflowTool().setChainForPortalTypes(
104
                  [self.portal_type],'test_workflow, validation_workflow')
Sebastien Robin's avatar
Sebastien Robin committed
105
    _aq_reset() # XXX Fails XXX _setLastId not found when doing newContent
106

107 108 109
  def createInteractionWorkflowWithTwoInteractions(self):
    id = 'test_workflow'
    wf_type = "interaction_workflow (Web-configurable interaction workflow)"
110
    wf = self._createInteractionWorkflowWithId(id)
111 112 113 114 115 116 117 118 119 120 121 122
    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(
123
                  [self.portal_type],'test_workflow, validation_workflow')
124
    _aq_reset() # XXX Fails XXX _setLastId not found when doing newContent
Sebastien Robin's avatar
Sebastien Robin committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

  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()
140 141 142 143
    self.interaction.setProperties(
            'afterEdit',
            method_id='edit',
            after_script_name=('afterEdit',))
Sebastien Robin's avatar
Sebastien Robin committed
144 145 146 147 148 149 150
    #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
151
    organisation.setDescription('bad')
Sebastien Robin's avatar
Sebastien Robin committed
152 153 154 155 156 157
    organisation.edit()
    self.assertEquals(organisation.getDescription(),'toto')

  def test_03(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
158 159
      self.logMessage(
        'Interactions, Edit Set Description and also After Script')
Sebastien Robin's avatar
Sebastien Robin committed
160
    self.createInteractionWorkflow()
161 162 163 164
    self.interaction.setProperties(
            'afterEdit',
            method_id='edit',
            after_script_name=('afterEdit',))
Sebastien Robin's avatar
Sebastien Robin committed
165 166 167 168 169 170
    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
171
    organisation.setDescription('bad')
Sebastien Robin's avatar
Sebastien Robin committed
172 173 174 175 176 177 178 179
    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()
180 181 182 183
    self.interaction.setProperties(
            'afterEdit',
            method_id='doSomethingStupid',
            after_script_name=('afterEdit',))
Sebastien Robin's avatar
Sebastien Robin committed
184 185 186
    body = "context = sci.object\n" +\
           "context.setDescription('toto')"
    params = 'sci,**kw'
187
    self.script.ZPythonScript_edit(params, body)
Sebastien Robin's avatar
Sebastien Robin committed
188 189
    self.createData()
    organisation = self.organisation
Sebastien Robin's avatar
Sebastien Robin committed
190
    organisation.setDescription('bad')
Sebastien Robin's avatar
Sebastien Robin committed
191 192 193
    organisation.doSomethingStupid('tutu')
    self.assertEquals(organisation.getDescription(),'toto')

Sebastien Robin's avatar
Sebastien Robin committed
194 195 196
  def test_05(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
197 198
      self.logMessage(
        'Interactions, Automatic Workflow Method With Extra Base Category')
Sebastien Robin's avatar
Sebastien Robin committed
199
    self.createInteractionWorkflow()
200 201 202 203
    self.interaction.setProperties(
            'afterEdit',
            method_id='setSizeList _setSizeList',
            after_script_name=('afterEdit',))
Sebastien Robin's avatar
Sebastien Robin committed
204 205 206 207 208 209 210 211 212 213
    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')

214 215 216 217 218
  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()
219 220 221 222
    self.interaction.setProperties(
            'afterEdit',
            method_id='edit',
            after_script_name=('afterEdit',))
223 224 225 226 227 228 229 230 231 232 233
    params = 'sci,**kw'
    body = "context = sci.object\n" +\
           "description = context.getDescription()\n" +\
           "context.setDescription(description + 'a')"
    self.script.ZPythonScript_edit(params,body)
    self.createData()
    organisation = self.organisation
    organisation.edit()
    self.assertEquals(organisation.getDescription(),'a')
    organisation.edit()
    self.assertEquals(organisation.getDescription(),'aa')
Sebastien Robin's avatar
Sebastien Robin committed
234

235
  def test_07(self, quiet=0, run=run_all_test):
236 237 238 239 240
    if not run: return
    if not quiet:
      self.logMessage('Interactions, Check If The Return Value Is Not Altered')
    self.createInteractionWorkflow()
    self.interaction.setProperties(
241 242 243
            'afterEdit',
            method_id='newContent',
            after_script_name=('afterEdit',))
244 245 246 247 248 249 250 251 252 253 254 255
    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
256

257 258 259 260 261 262 263
  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',
264
            method_id='setCorporateName setActivityCode',
265 266 267 268 269 270 271 272
            after_script_name=('afterEdit',))
    params = 'sci,**kw'
    body = "context = sci.object\n" +\
           "description = context.getDescription()\n" +\
           "context.setDescription(description + 'a')"
    self.script.ZPythonScript_edit(params,body)
    self.createData()
    organisation = self.organisation
273
    organisation.setCorporateName('corp')
274
    self.assertEquals(organisation.getDescription(),'a')
275
    organisation.setActivityCode('acode')
276
    self.assertEquals(organisation.getDescription(),'aa')
277

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  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" +\
           "context.setDescription(description + 'a')"
    self.scriptA.ZPythonScript_edit(params, body)
    self.scriptB.ZPythonScript_edit(params, body.replace("'a'", "'b'"))
299

300 301 302 303 304 305 306 307 308 309
    self.createData()
    organisation = self.organisation
    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())
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  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" +\
           "context.setDescription(description + 'a')"
    self.scriptA.ZPythonScript_edit(params, body)
    self.scriptB.ZPythonScript_edit(params, body.replace("'a'", "'b'"))
328

329 330 331 332 333 334 335 336 337 338
    self.createData()
    organisation = self.organisation
    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())
339

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
  def test_11(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage(
        'Interactions, Test that the private accessor is called')

    self.createInteractionWorkflowWithTwoInteractions()
    self.interactionA.setProperties(
            'afterEditA',
            method_id='_setVatCode',
            after_script_name=('afterEditA',))
    self.interactionB.setProperties(
            'afterEditB',
            method_id='setVatCode',
            after_script_name=('afterEditB',))
    params = 'sci,**kw'
    body = "context = sci.object\n" +\
357
           "context.log('InteractionWF.test_11 in script', 'a')\n" +\
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
           "description = context.getDescription()\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._baseSetVatCode('x')
    organisation.setDescription('x')
    self.assertEquals(organisation.getVatCode(),'x')
    self.assertEquals(organisation.getDescription(),'x')
    organisation.edit(description='bar')
    organisation.edit(vat_code='foo')
    self.assertEquals(organisation.getVatCode(),'foo')
    self.assertEquals(organisation.getDescription(),'bara')

  def test_12(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage(
        'Interactions, Test that the private accessor is called, '
        'when using an Acquired Property')

    self.createInteractionWorkflowWithTwoInteractions()
    self.interactionA.setProperties(
            'afterEditA',
            method_id='_setDefaultEmailText',
            after_script_name=('afterEditA',))
    self.interactionB.setProperties(
            'afterEditB',
            method_id='setDefaultEmailText',
            after_script_name=('afterEditB',))
    params = 'sci,**kw'
    body = "context = sci.object\n" +\
392
           "context.log('InteractionWF.test_12 in script', 'a')\n" +\
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
           "vat_code = context.getVatCode()\n" +\
           "if vat_code is None:\n" +\
           "  vat_code = ''\n" +\
           "context.setVatCode(vat_code + 'a')"
    self.scriptA.ZPythonScript_edit(params, body)
    self.scriptB.ZPythonScript_edit(params, body.replace("'a'", "'b'"))

    self.createData()
    organisation = self.organisation
    organisation._baseSetDefaultEmailText('x')
    organisation.setVatCode('x')
    self.assertEquals(organisation.getDefaultEmailText(),'x')
    self.assertEquals(organisation.getVatCode(),'x')
    organisation.edit(vat_code='foo')
    organisation.edit(default_email_text='bar')
    self.assertEquals(organisation.getVatCode(),'fooa')
    self.assertEquals(organisation.getDefaultEmailText(),'bar')

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  def test_13(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Interactions, Check that edit does not detect the '
          'property modified in interaction script as modified by user')
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'afterEdit',
            method_id='setTitle',
            after_script_name=('afterEdit',))
    params = 'sci,**kw'
    body = "context = sci.object\n" +\
           "vat_code = context.getVatCode()\n" +\
           "if vat_code is None:\n" +\
           "  vat_code = ''\n" +\
           "context.setVatCode(vat_code + 'a')"
    self.script.ZPythonScript_edit(params,body)
    self.createData()
    organisation = self.organisation
    organisation.setTitle('foo')
    organisation.setVatCode('bar')
    self.assertEquals(organisation.getTitle(), 'foo')
    self.assertEquals(organisation.getVatCode(), 'bar')

435 436
    organisation.edit(title='baz', vat_code='bar', edit_order=['vat_code',
      'title'])
437
    self.assertEquals(organisation.getTitle(),'baz')
438
    # here, the wrong behaviour is:
439 440 441 442 443 444 445 446
    # - edit:setTitle(baz)
    # - interaction:setVatCode(bara)
    # - edit:setVatCode(bar)
    # whereas, the correct order is:
    # - edit:setTitle(baz)
    # - edit:setVatCode(bar)
    # - interaction:setVatCode(bara)
    self.assertEquals(organisation.getVatCode(),'bara')
447 448 449 450 451 452 453 454 455 456
    # now, test the other way around
    organisation.edit(title='baz', vat_code='bara', edit_order=['title',
      'vat_code'])
    self.assertEquals(organisation.getTitle(),'baz')
    # here, we assert the failure:
    # - edit:setTitle(baz)
    # - interaction:setVatCode(baraa)
    # - edit:setVatCode(bara)
    self.assertEquals(organisation.getVatCode(),'bara')

457

458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
  def test_14_BeforeScriptParameters(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Before Script Parameters')
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'afterEdit',
            method_id='getProperty',
            script_name=('afterEdit',))
    params = 'sci,**kw'
    body = """\
context = sci['object']
kwargs = sci['kwargs'] or {}
d = kwargs.get('d', None)
args = kwargs.get('workflow_method_args', ())
result = kwargs.get('workflow_method_result', None)
context.setDescription('%s,%s,%s' % (d, args, result))
"""
    self.script.ZPythonScript_edit(params,body)
    self.createData()
    organisation = self.organisation
    organisation.setDescription('bad')
    value = organisation.getProperty('description', d='toto')
    self.assertEquals(value, "toto,('description',),None")

  def test_15_AfterScriptParameters(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('After Script Parameters')
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'afterEdit',
            method_id='getProperty',
            after_script_name=('afterEdit',))
    params = 'sci,**kw'
    body = """\
context = sci['object']
kwargs = sci['kwargs'] or {}
d = kwargs.get('d', None)
args = kwargs.get('workflow_method_args', ())
result = kwargs.get('workflow_method_result', None)
context.setDescription('%s,%s,%s' % (d, args, result))
"""
    self.script.ZPythonScript_edit(params,body)
    self.createData()
    organisation = self.organisation
    organisation.setDescription('bad')
    organisation.getProperty('description', d='toto')
    value = organisation.getDescription()
    self.assertEquals(value, "toto,('description',),bad")
508

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
  def test_16_BeforeCommitParameters(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Before Commit Script Parameters')
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'beforeCommit',
            method_id='getProperty',
            before_commit_script_name=('afterEdit',))
    params = 'sci, **kw'
    body = """\
context = sci['object']
kwargs = sci['kwargs'] or {}
d = kwargs.get('d', None)
args = kwargs.get('workflow_method_args', ())
result = kwargs.get('workflow_method_result', None)
context.setDescription('%s,%s,%s' % (d, args, result))
"""
    self.script.ZPythonScript_edit(params, body)
    self.createData()
    organisation = self.organisation
    organisation.setDescription('bad')
    self.assertEquals(organisation.getDescription(), 'bad')
    organisation.getProperty('description', d='toto')
    self.assertEquals(organisation.getDescription(), 'bad')
534
    transaction.commit()
535 536
    self.assertEquals(organisation.getDescription(), "toto,('description',),bad")

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
  def test_17_activity_interaction(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Later Script (In activity)')
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'editObject',
            once_per_transaction=1,
            method_id='_setGroup.*',
            activate_script_name=('afterEdit',))
    params = 'sci, **kw'
    body = """\
context = sci['object']
context.setTitle('Bar')
"""
    self.script.ZPythonScript_edit(params, body)
    self.createData()
    organisation = self.organisation
    organisation.setTitle('Foo')
    organisation.setGroupValue(organisation)
    self.assertEquals(organisation.getTitle(), 'Foo')
558
    transaction.commit()
559 560 561 562 563
    self.assertEquals(organisation.getTitle(), 'Foo')
    self.tic()
    self.assertEquals(organisation.getTitle(), 'Bar')


564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
  def test_regular_expression(self):
    # test that we can add an interaction by defining methods using regular
    # expression
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'regexp',
            method_id='set.*',
            after_script_name=('afterEdit',))

    call_list = self.portal.REQUEST['call_list'] = []
    self.script.ZPythonScript_edit('sci',
        'container.REQUEST["call_list"].append(1)')
    self.createData()
    organisation = self.organisation
    # all methods matching set.* regular expression are matched
    organisation.setDescription('')
    self.assertEquals(len(call_list), 1)
    organisation.setTitle('')
    self.assertEquals(len(call_list), 2)
    organisation.getDescription()
    self.assertEquals(len(call_list), 2)
    organisation.edit(description='desc')
    self.assertEquals(len(call_list), 3)


589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
  def test_security(self):
    # wrapping a method in an interaction workflow adds a default security to
    # this method if the method does not exists.
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'default',
            method_id='nonExistantMethod',
            after_script_name=('afterEdit',))
    self.script.ZPythonScript_edit('sci', '')
    self.createData()
    # the default security is "Access contents information"
    self.organisation.manage_permission(
                      'Access contents information', ['Role1'], 0)
    self.assertEquals(self.organisation.nonExistantMethod__roles__,
                      ('Role1',))
604

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
  def test_security_defined(self):
    # wrapping a method in an interaction workflow adds a default security to
    # this method, but does not override existing security definition
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'default',
            method_id='setDescription',
            after_script_name=('afterEdit',))
    self.script.ZPythonScript_edit('sci', '')
    self.createData()
    # This rely on the fact that 'setDescription' is protected with 'Modify
    # portal content'
    self.organisation.manage_permission(
                     'Modify portal content', ['Role2'], 0)
    self.assertEquals(self.organisation.setDescription__roles__,
                      ('Role2',))

  def test_security_defined_on_class(self):
    # wrapping a method in an interaction workflow adds a default security to
    # this method, but does not override existing security definition (defined
    # on the class)
626
    Organisation = Products.ERP5.Document.Organisation.Organisation
627 628 629
    security = ClassSecurityInfo()
    security.declarePrivate('doSomethingStupid')
    security.apply(Organisation)
630 631 632 633 634 635 636 637 638 639 640

    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'default',
            method_id='doSomethingStupid',
            after_script_name=('afterEdit',))
    self.script.ZPythonScript_edit('sci', '')
    self.createData()

    self.assertEquals(self.organisation.doSomethingStupid__roles__, ())

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
  def test_wrap_workflow_transition(self):
    self.logMessage('Wrap workflow transition')
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'default',
            method_id='validate',
            after_script_name=('afterEdit',))
    params = 'sci, **kw'
    body = "context = sci[\'object\']\n" +\
           "context.setDescription('titi')"
    self.script.ZPythonScript_edit(params, body)
    self.createData()
    self.assertEquals('', self.organisation.getDescription())
    self.portal.portal_workflow.doActionFor(self.organisation, 'validate_action')
    self.assertEquals('validated', self.organisation.getValidationState())
    self.assertEquals('titi', self.organisation.getDescription())
657

658 659 660 661
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestInteractionWorkflow))
  return suite