testInteractionWorkflow.py 29.6 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 33

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

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

  # 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
50 51 52
  def getBusinessTemplateList(self):
    return ('erp5_base',)

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

56
  def beforeTearDown(self):
57
    Organisation = Products.ERP5.Document.Organisation.Organisation
58 59 60 61 62 63
    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
64 65
  def login(self, quiet=0):
    uf = self.getPortal().acl_users
66
    uf._doAddUser('seb', '', ['Manager', 'Assignor'], [])
Sebastien Robin's avatar
Sebastien Robin committed
67 68 69 70 71 72 73 74
    user = uf.getUserById('seb').__of__(uf)
    newSecurityManager(None, user)

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

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

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

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

  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()
132
    self.assertEqual(organisation.getDescription(),'')
Sebastien Robin's avatar
Sebastien Robin committed
133 134 135 136 137 138

  def test_02(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Interactions On Edit')
    self.createInteractionWorkflow()
139 140 141 142
    self.interaction.setProperties(
            'afterEdit',
            method_id='edit',
            after_script_name=('afterEdit',))
Sebastien Robin's avatar
Sebastien Robin committed
143 144 145 146 147 148 149
    #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
150
    organisation.setDescription('bad')
Sebastien Robin's avatar
Sebastien Robin committed
151
    organisation.edit()
152
    self.assertEqual(organisation.getDescription(),'toto')
Sebastien Robin's avatar
Sebastien Robin committed
153 154 155 156

  def test_03(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
157 158
      self.logMessage(
        'Interactions, Edit Set Description and also After Script')
Sebastien Robin's avatar
Sebastien Robin committed
159
    self.createInteractionWorkflow()
160 161 162 163
    self.interaction.setProperties(
            'afterEdit',
            method_id='edit',
            after_script_name=('afterEdit',))
Sebastien Robin's avatar
Sebastien Robin committed
164 165 166 167 168 169
    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
170
    organisation.setDescription('bad')
Sebastien Robin's avatar
Sebastien Robin committed
171
    organisation.edit(description='tutu')
172
    self.assertEqual(organisation.getDescription(),'toto')
Sebastien Robin's avatar
Sebastien Robin committed
173 174 175 176 177 178

  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()
179 180 181 182
    self.interaction.setProperties(
            'afterEdit',
            method_id='doSomethingStupid',
            after_script_name=('afterEdit',))
Sebastien Robin's avatar
Sebastien Robin committed
183 184 185
    body = "context = sci.object\n" +\
           "context.setDescription('toto')"
    params = 'sci,**kw'
186
    self.script.ZPythonScript_edit(params, body)
Sebastien Robin's avatar
Sebastien Robin committed
187 188
    self.createData()
    organisation = self.organisation
Sebastien Robin's avatar
Sebastien Robin committed
189
    organisation.setDescription('bad')
Sebastien Robin's avatar
Sebastien Robin committed
190
    organisation.doSomethingStupid('tutu')
191
    self.assertEqual(organisation.getDescription(),'toto')
Sebastien Robin's avatar
Sebastien Robin committed
192

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

213 214 215 216 217
  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()
218 219 220 221
    self.interaction.setProperties(
            'afterEdit',
            method_id='edit',
            after_script_name=('afterEdit',))
222 223 224 225 226 227 228 229
    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()
230
    self.assertEqual(organisation.getDescription(),'a')
231
    organisation.edit()
232
    self.assertEqual(organisation.getDescription(),'aa')
Sebastien Robin's avatar
Sebastien Robin committed
233

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

256 257 258 259 260 261 262
  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',
263
            method_id='setCorporateName setActivityCode',
264 265 266 267 268 269 270 271
            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
272
    organisation.setCorporateName('corp')
273
    self.assertEqual(organisation.getDescription(),'a')
274
    organisation.setActivityCode('acode')
275
    self.assertEqual(organisation.getDescription(),'aa')
276

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  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'"))
298

299 300 301 302 303 304 305 306 307 308
    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())
309

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  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'"))
327

328 329 330 331 332 333 334 335 336 337
    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())
338

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
  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" +\
356
           "context.log('InteractionWF.test_11 in script', 'a')\n" +\
357 358 359 360 361 362 363 364 365
           "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')
366 367
    self.assertEqual(organisation.getVatCode(),'x')
    self.assertEqual(organisation.getDescription(),'x')
368 369
    organisation.edit(description='bar')
    organisation.edit(vat_code='foo')
370 371
    self.assertEqual(organisation.getVatCode(),'foo')
    self.assertEqual(organisation.getDescription(),'bara')
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

  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" +\
391
           "context.log('InteractionWF.test_12 in script', 'a')\n" +\
392 393 394 395 396 397 398 399 400 401 402
           "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')
403 404
    self.assertEqual(organisation.getDefaultEmailText(),'x')
    self.assertEqual(organisation.getVatCode(),'x')
405 406
    organisation.edit(vat_code='foo')
    organisation.edit(default_email_text='bar')
407 408
    self.assertEqual(organisation.getVatCode(),'fooa')
    self.assertEqual(organisation.getDefaultEmailText(),'bar')
409

410 411 412 413 414 415 416 417
  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',
418
            method_id='_setTitle',
419 420 421 422 423 424 425 426 427 428 429 430
            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')
431 432
    self.assertEqual(organisation.getTitle(), 'foo')
    self.assertEqual(organisation.getVatCode(), 'bar')
433

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

456

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
  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')
480
    self.assertEqual(value, "toto,('description',),None")
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

  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()
506
    self.assertEqual(value, "toto,('description',),bad")
507

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
  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')
530
    self.assertEqual(organisation.getDescription(), 'bad')
531
    organisation.getProperty('description', d='toto')
532
    self.assertEqual(organisation.getDescription(), 'bad')
533 534
    # before-commit interactions should be immune to security changes
    self.logout()
535
    self.commit()
536
    self.login()
537
    self.assertEqual(organisation.getDescription(), "toto,('description',),bad")
538

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
  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)
559
    self.assertEqual(organisation.getTitle(), 'Foo')
560
    self.commit()
561
    self.assertEqual(organisation.getTitle(), 'Foo')
562
    self.tic()
563
    self.assertEqual(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 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
  def test_18_no_temp_object(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Skips Temp Objects')
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'editObject',
            temporary_document_disallowed=False,
            method_id='_setGroup.*',
            after_script_name=('afterEdit',))
    params = 'sci, **kw'
    body = """\
context = sci['object']
context.setTitle('Bar')
"""
    self.script.ZPythonScript_edit(params, body)
    self.createData()
    organisation = self.organisation
    temp = organisation.asContext()
    temp.setTitle('Foo')
    # interaction workflows can affect temp objects
    temp.setGroupValue(organisation)
    self.assertEqual(temp.getTitle(), 'Bar')
    # but not if it has been forbidden
    temp.setTitle('Foo')
    self.interaction.setProperties(
            'editObject',
            temporary_document_disallowed=True,
            method_id='_setGroup.*',
            after_script_name=('afterEdit',))
    temp.setGroupValue(None)
    self.assertEqual(temp.getTitle(), 'Foo')

  def test_19_temp_object_doesnt_skip_normal(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Skips Temp Objects, but run in normal objects in the same transaction')
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'editObject',
            once_per_transaction=True,
            temporary_document_disallowed=True,
            method_id='_setGroup.*',
            after_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')
    temp = organisation.asContext()
    # temp and organisation have the same path
    self.assertEqual(temp.getPath(), organisation.getPath())
621 622 623 624
    # which means that a transactional variable key based on path
    # would match both the organisation and the temp object, but
    # triggering the workflow on the temp object should not change it
    # because it's prevented by configuration:
625
    temp.setGroupValue(organisation)
626
    self.assertEqual(temp.getTitle(), 'Foo')
627
    # nor should it change the normal object
628
    self.assertEqual(organisation.getTitle(), 'Foo')
629 630 631
    # however, it should allow triggering the normal object later on the same
    # transaction
    organisation.setGroupValue(organisation)
632
    self.assertEqual(organisation.getTitle(), 'Bar')
633
    # while still not changing the temp object
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    self.assertEqual(temp.getTitle(), 'Foo')

  def test_20_temp_object_does_skip_normal(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Runs on temp Objects and skip normal objects')
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'editObject',
            once_per_transaction=True,
            temporary_document_disallowed=False,
            method_id='_setGroup.*',
            after_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')
    temp = organisation.asContext()
    # temp and organisation have the same path
    self.assertEqual(temp.getPath(), organisation.getPath())
    # which means that a transactional variable key based on path
    # would match both the organisation and the temp
    # object. Triggering the workflow on the temp object Will change
    # it, since this is not prevented by configuration:
    temp.setGroupValue(organisation)
    self.assertEqual(temp.getTitle(), 'Bar')
    # This should not change the normal object
    self.assertEqual(organisation.getTitle(), 'Foo')
    # However, since the interaction can only run once per transaction
    # (and per object path), it cannot run again on the normal object:
    organisation.setGroupValue(organisation)
    self.assertEqual(organisation.getTitle(), 'Foo')
    # This can be considered an undesired side-effect, so if this test
    # starts failing in the assertion above for a good reason, just
    # fix the test.
674
    self.commit()
675 676 677 678
    # committing the transaction allows the interaction workflow to
    # run on the normal object again:
    organisation.setGroupValue(None)
    self.assertEqual(organisation.getTitle(), 'Bar')
679

680 681 682 683 684 685
  def test_regular_expression(self):
    # test that we can add an interaction by defining methods using regular
    # expression
    self.createInteractionWorkflow()
    self.interaction.setProperties(
            'regexp',
686
            method_id='_set.* set.*',
687 688 689 690 691 692 693 694 695
            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('')
696
    # two calls: setDescription, _setDescription
697
    self.assertEqual(len(call_list), 2)
698 699
    organisation.setTitle('')
    # two calls: setTitle, _setTitle
700
    self.assertEqual(len(call_list), 4)
701
    organisation.getDescription()
702
    # no calls
703
    self.assertEqual(len(call_list), 4)
704
    organisation.edit(description='desc')
705
    # two calls: one to _setProperty, and one to _setDescription
706
    self.assertEqual(len(call_list), 6)
707 708


709 710 711 712 713 714 715 716 717 718 719 720 721
  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)
722
    self.assertEqual(self.organisation.nonExistantMethod__roles__,
723
                      ('Role1',))
724

725 726 727 728 729 730 731 732 733 734 735 736 737 738
  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)
739
    self.assertEqual(self.organisation.setDescription__roles__,
740 741 742 743 744 745
                      ('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)
746
    Organisation = Products.ERP5.Document.Organisation.Organisation
747 748 749
    security = ClassSecurityInfo()
    security.declarePrivate('doSomethingStupid')
    security.apply(Organisation)
750 751 752 753 754 755 756 757 758

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

759
    self.assertEqual(self.organisation.doSomethingStupid__roles__, ())
760

761 762 763 764 765 766 767 768 769 770 771 772
  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()
773
    self.assertEqual('', self.organisation.getDescription())
774
    self.portal.portal_workflow.doActionFor(self.organisation, 'validate_action')
775 776
    self.assertEqual('validated', self.organisation.getValidationState())
    self.assertEqual('titi', self.organisation.getDescription())
777

778 779 780 781
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestInteractionWorkflow))
  return suite