testBPMCore.py 55.9 KB
Newer Older
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
1
# -*- coding: utf-8 -*-
2 3 4
##############################################################################
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#          Łukasz Nowak <luke@nexedi.com>
5
#          Yusuke Muraoka <yusuke@nexedi.com>
6
#          Fabien Morin <fabien@nexedi.com>
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
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility 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
# guarantees 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.
#
##############################################################################

import unittest
import transaction

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from DateTime import DateTime

from Products.CMFCore.utils import getToolByName
38
from Products.ERP5Type.tests.utils import reindex
39 40

class TestBPMMixin(ERP5TypeTestCase):
41
  """Skeletons for tests which depend on BPM"""
42 43 44

  def getBusinessTemplateList(self):
    return ('erp5_base', 'erp5_pdm', 'erp5_trade', 'erp5_accounting',
45
      'erp5_invoicing', 'erp5_simplified_invoicing')
46 47

  business_process_portal_type = 'Business Process'
48
  business_link_portal_type = 'Business Link'
49
  trade_model_path_portal_type = 'Trade Model Path'
50

51 52 53
  normal_resource_use_category_list = ['normal']
  invoicing_resource_use_category_list = ['discount', 'tax']

54 55
  def createCategoriesInCategory(self, category, category_id_list):
    for category_id in category_id_list:
56
      if not category.hasObject(category_id):
57 58 59 60 61
        category.newContent(portal_type='Category', id = category_id,
            title = category_id)

  @reindex
  def createCategories(self):
62 63
    category_tool = getToolByName(self.portal, 'portal_categories')
    self.createCategoriesInCategory(category_tool.base_amount, ['discount',
64
      'tax', 'total_tax', 'total_discount', 'total'])
65 66 67 68
    self.createCategoriesInCategory(category_tool.use,
        self.normal_resource_use_category_list + \
            self.invoicing_resource_use_category_list)
    self.createCategoriesInCategory(category_tool.trade_phase, ['default',])
69 70
    self.createCategoriesInCategory(category_tool.trade_phase.default,
        ['accounting', 'delivery', 'invoicing', 'discount', 'tax', 'payment'])
71
    self.createCategoriesInCategory(category_tool.trade_state,
72
        ['ordered', 'invoiced', 'delivered', 'taxed',
73
         'state_a', 'state_b', 'state_c', 'state_d', 'state_e'])
74 75

  @reindex
76
  def createBusinessProcess(self, **kw):
77 78
    module = self.portal.getDefaultModule(
        portal_type=self.business_process_portal_type)
79 80
    return module.newContent(portal_type=self.business_process_portal_type,
        **kw)
81 82

  @reindex
83
  def createBusinessLink(self, business_process=None, **kw):
84
    if business_process is None:
85
      business_process = self.createBusinessProcess()
86
    kw['destination_method_id'] = kw.pop('destination_method_id',
87
        'BusinessLink_getDefaultDestinationList')
88
    kw['source_method_id'] = kw.pop('source_method_id',
89 90 91 92
        'BusinessLink_getDefaultSourceList')
    business_link = business_process.newContent(
      portal_type=self.business_link_portal_type, **kw)
    return business_link
93

94 95 96 97 98 99
  def createTradeModelPath(self, business_process=None, **kw):
    if business_process is None:
      business_process = self.createBusinessProcess()
    return business_process.newContent(
      portal_type=self.trade_model_path_portal_type, **kw)

100 101 102 103 104 105
  def createMovement(self):
    # returns a movement for testing
    applied_rule = self.portal.portal_simulation.newContent(
        portal_type='Applied Rule')
    return applied_rule.newContent(portal_type='Simulation Movement')

106 107 108 109 110 111 112 113
  @reindex
  def createAndValidateAccount(self, account_id, account_type):
    account_module = self.portal.account_module
    account = account_module.newContent(portal_type='Account',
          title=account_id,
          account_type=account_type)
    self.assertNotEqual(None, account.getAccountTypeValue())
    account.validate()
114
    return account
115

Yusuke Muraoka's avatar
Yusuke Muraoka committed
116
  def createInvoiceTransactionRule(self):
117 118 119 120 121 122 123 124 125 126 127 128 129
    self.receivable_account = self.createAndValidateAccount('receivable',
        'asset/receivable')
    self.payable_account = self.createAndValidateAccount('payable',
        'liability/payable')
    self.income_account = self.createAndValidateAccount('income', 'income')
    self.expense_account = self.createAndValidateAccount('expense', 'expense')
    self.collected_tax_account = self.createAndValidateAccount(
        'collected_tax', 'liability/payable/collected_vat')
    self.refundable_tax_account = self.createAndValidateAccount(
        'refundable_tax',
        'asset/receivable/refundable_vat')

    itr = self.portal.portal_rules.newContent(
130
                        portal_type='Invoice Transaction Simulation Rule',
131
                        reference='default_invoice_transaction_rule',
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
132 133
                        id='test_invoice_transaction_simulation_rule',
                        title='Transaction Simulation Rule',
134
                        test_method_id=
135
                        'SimulationMovement_testInvoiceTransactionSimulationRule',
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
                        version=100)
    predicate = itr.newContent(portal_type='Predicate',)
    predicate.edit(
            string_index='use',
            title='tax',
            int_index=1,
            membership_criterion_base_category='resource_use',
            membership_criterion_category='resource_use/use/tax')
    predicate = itr.newContent(portal_type='Predicate',)
    predicate.edit(
            string_index='use',
            title='discount',
            int_index=2,
            membership_criterion_base_category='resource_use',
            membership_criterion_category='resource_use/use/discount')
    predicate = itr.newContent(portal_type='Predicate',)
    predicate.edit(
            string_index='use',
            title='normal',
            int_index=3,
            membership_criterion_base_category='resource_use',
            membership_criterion_category='resource_use/use/normal')
158
    transaction.commit()
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    self.tic()
    accounting_rule_cell_list = itr.contentValues(
                            portal_type='Accounting Rule Cell')
    self.assertEquals(3, len(accounting_rule_cell_list))
    tax_rule_cell = itr._getOb("movement_0")
    self.assertEquals(tax_rule_cell.getTitle(), 'tax')
    tax_rule_cell.newContent(
                         portal_type='Accounting Transaction Line',
                         source_value=self.receivable_account,
                         destination_value=self.payable_account,
                         quantity=-1)
    tax_rule_cell.newContent(
                         portal_type='Accounting Transaction Line',
                         source_value=self.collected_tax_account,
                         destination_value=self.refundable_tax_account,
                         quantity=1)

    discount_rule_cell = itr._getOb("movement_1")
    self.assertEquals(discount_rule_cell.getTitle(), 'discount')
    discount_rule_cell.newContent(
                         portal_type='Accounting Transaction Line',
                         source_value=self.receivable_account,
                         destination_value=self.payable_account,
                         quantity=-1)
    discount_rule_cell.newContent(
                         portal_type='Accounting Transaction Line',
                         source_value=self.income_account,
                         destination_value=self.expense_account,
                         quantity=1)

    normal_rule_cell = itr._getOb("movement_2")
    self.assertEquals(normal_rule_cell.getTitle(), 'normal')
    normal_rule_cell.newContent(
                         portal_type='Accounting Transaction Line',
                         source_value=self.receivable_account,
                         destination_value=self.payable_account,
                         quantity=-1)
    normal_rule_cell.newContent(
                         portal_type='Accounting Transaction Line',
                         source_value=self.income_account,
                         destination_value=self.expense_account,
                         quantity=1)
Łukasz Nowak's avatar
Łukasz Nowak committed
201

202 203
    itr.validate()

204
  def afterSetUp(self):
205 206 207 208 209 210
    rule_tool = self.getRuleTool()
    for rule in rule_tool.contentValues(
        portal_type=rule_tool.getPortalRuleTypeList()):
      if rule.getId().startswith('new_') and \
         rule.getValidationState() != 'validated':
        rule.validate()
211
    self.createCategories()
Yusuke Muraoka's avatar
Yusuke Muraoka committed
212
    self.createInvoiceTransactionRule()
213
    self.stepTic()
214

215
  def beforeTearDown(self):
216 217 218 219 220 221
    # abort any transaction
    transaction.abort()
    # put non finished activities into ignored state
    activity_connection = self.portal.cmf_activity_sql_connection
    for table in 'message', 'message_queue':
      activity_connection.manage_test(
222
          'delete from %s where processing_node=-2' % table)
223
    # remove not needed rules
224
    self.portal.portal_rules.manage_delObjects(
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
225
        ids=['test_invoice_transaction_simulation_rule'])
226
    self.stepTic()
227

228 229 230 231 232
class TestBPMImplementation(TestBPMMixin):
  """Business Process implementation tests"""
  def test_BusinessProcess_getPathValueList(self):
    business_process = self.createBusinessProcess()

233 234
    accounting_business_link = business_process.newContent(
        portal_type=self.business_link_portal_type,
235 236
        trade_phase='default/accounting')

237 238
    delivery_business_link = business_process.newContent(
        portal_type=self.business_link_portal_type,
239 240
        trade_phase='default/delivery')

241 242
    accounting_delivery_business_link = business_process.newContent(
        portal_type=self.business_link_portal_type,
243 244 245 246 247
        trade_phase=('default/accounting', 'default/delivery'))

    self.stepTic()

    self.assertSameSet(
248
      (accounting_business_link, accounting_delivery_business_link),
249 250 251 252
      business_process.getPathValueList(trade_phase='default/accounting')
    )

    self.assertSameSet(
253
      (delivery_business_link, accounting_delivery_business_link),
254 255 256 257
      business_process.getPathValueList(trade_phase='default/delivery')
    )

    self.assertSameSet(
258 259
      (accounting_delivery_business_link, delivery_business_link,
        accounting_business_link),
260 261 262 263
      business_process.getPathValueList(trade_phase=('default/delivery',
        'default/accounting'))
    )

264
  def test_BusinessLinkStandardCategoryAccessProvider(self):
265 266 267
    source_node = self.portal.organisation_module.newContent(
                    portal_type='Organisation')
    source_section_node = self.portal.organisation_module.newContent(
268
                    portal_type='Organisation')
269 270 271 272 273
    business_link = self.createBusinessLink()
    business_link.setSourceValue(source_node)
    business_link.setSourceSectionValue(source_section_node)
    self.assertEquals([source_node], business_link.getSourceValueList())
    self.assertEquals([source_node.getRelativeUrl()], business_link.getSourceList())
274
    self.assertEquals(source_node.getRelativeUrl(),
275
        business_link.getSource(default='something'))
276

277 278 279 280
  def test_EmptyBusinessLinkStandardCategoryAccessProvider(self):
    business_link = self.createBusinessLink()
    self.assertEquals(None, business_link.getSourceValue())
    self.assertEquals(None, business_link.getSource())
281
    self.assertEquals('something',
282
        business_link.getSource(default='something'))
283

284
  def test_BuinessPathDynamicCategoryAccessProvider(self):
285 286 287
    source_node = self.portal.organisation_module.newContent(
                    portal_type='Organisation')
    source_section_node = self.portal.organisation_module.newContent(
288
                    portal_type='Organisation')
289 290
    business_link = self.createBusinessLink()
    business_link.setSourceMethodId('BusinessLink_getDefaultSourceList')
291 292

    context_movement = self.createMovement()
293 294
    context_movement.setSourceValue(source_node)
    context_movement.setSourceSectionValue(source_section_node)
295
    self.assertEquals(None, business_link.getSourceValue())
296
    self.assertEquals([source_node],
297
                      business_link.getSourceValueList(context=context_movement))
298
    self.assertEquals([source_node.getRelativeUrl()],
299
                      business_link.getSourceList(context=context_movement))
Łukasz Nowak's avatar
Łukasz Nowak committed
300
    self.assertEquals(source_node.getRelativeUrl(),
301
      business_link.getSource(context=context_movement, default='something'))
302

303
  def test_BuinessPathDynamicCategoryAccessProviderBusinessLinkPrecedence(self):
304 305 306 307
    movement_node = self.portal.organisation_module.newContent(
                    portal_type='Organisation')
    path_node = self.portal.organisation_module.newContent(
                    portal_type='Organisation')
308 309 310
    business_link = self.createBusinessLink()
    business_link.setSourceMethodId('BusinessLink_getDefaultSourceList')
    business_link.setSourceValue(path_node)
311 312 313

    context_movement = self.createMovement()
    context_movement.setSourceValue(movement_node)
314
    self.assertEquals(path_node, business_link.getSourceValue())
315
    self.assertEquals(path_node,
316
                      business_link.getSourceValue(context=context_movement))
317
    self.assertEquals([path_node],
318
                      business_link.getSourceValueList(context=context_movement))
319

320
  def test_BuinessPathDynamicCategoryAccessProviderEmptyMovement(self):
321 322
    business_link = self.createBusinessLink()
    business_link.setSourceMethodId('BusinessLink_getDefaultSourceList')
323 324

    context_movement = self.createMovement()
325
    self.assertEquals(None, business_link.getSourceValue())
326
    self.assertEquals(None,
327
                      business_link.getSourceValue(context=context_movement))
328
    self.assertEquals(None,
329
                      business_link.getSource(context=context_movement))
330
    self.assertEquals('something',
331
      business_link.getSource(context=context_movement, default='something'))
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
  def test_BusinessState_getRemainingTradePhaseList(self):
    """
    This test case is described for what trade_phase is remaining after the state.
    In this case, root explanation is path of between "b" and "d", and
    path of between "a" and "b" has a condition which simulation state of
    explanation must be "ordered" to pass the path. (*1)
    But this test case will be passed the condition.

                            (root explanation)
       default/discount     default/invoicing     default/accounting
    a ------------------ b ------------------- d -------------------- e
       (cond="ordered")   \                   /
                           \                 /
          default/delivery  \               / default/payment
                             \             /
                              \           /
                               \         /
                                \       /
                                 \     /
                                  \   /
                                   \ /
                                    c
    """
    # define business process
357
    category_tool = self.getCategoryTool()
358
    business_process = self.createBusinessProcess()
359 360 361 362 363
    business_link_a_b = self.createBusinessLink(business_process)
    business_link_b_c = self.createBusinessLink(business_process)
    business_link_b_d = self.createBusinessLink(business_process)
    business_link_c_d = self.createBusinessLink(business_process)
    business_link_d_e = self.createBusinessLink(business_process)
364 365 366 367 368
    business_state_a = category_tool.trade_state.state_a
    business_state_b = category_tool.trade_state.state_b
    business_state_c = category_tool.trade_state.state_c
    business_state_d = category_tool.trade_state.state_d
    business_state_e = category_tool.trade_state.state_e
369 370 371 372 373 374 375 376 377 378
    business_link_a_b.setPredecessorValue(business_state_a)
    business_link_b_c.setPredecessorValue(business_state_b)
    business_link_b_d.setPredecessorValue(business_state_b)
    business_link_c_d.setPredecessorValue(business_state_c)
    business_link_d_e.setPredecessorValue(business_state_d)
    business_link_a_b.setSuccessorValue(business_state_b)
    business_link_b_c.setSuccessorValue(business_state_c)
    business_link_b_d.setSuccessorValue(business_state_d)
    business_link_c_d.setSuccessorValue(business_state_d)
    business_link_d_e.setSuccessorValue(business_state_e)
379 380

    # set title for debug
381 382 383 384 385
    business_link_a_b.edit(title="a_b")
    business_link_b_c.edit(title="b_c")
    business_link_b_d.edit(title="b_d")
    business_link_c_d.edit(title="c_d")
    business_link_d_e.edit(title="d_e")
386 387
    
    # set trade_phase
388
    business_link_a_b.edit(trade_phase=['default/discount'],
389
                           completed_state=['ordered']) # (*1)
390 391 392 393
    business_link_b_c.edit(trade_phase=['default/delivery'])
    business_link_b_d.edit(trade_phase=['default/invoicing'])
    business_link_c_d.edit(trade_phase=['default/payment'])
    business_link_d_e.edit(trade_phase=['default/accounting'])
394 395 396 397 398 399 400 401 402 403 404 405

    # mock order
    order = self.portal.sale_order_module.newContent(portal_type="Sale Order")
    order_line = order.newContent(portal_type="Sale Order Line")

    # make simulation
    order.order()

    self.stepTic()

    applied_rule = order.getCausalityRelatedValue()
    sm = applied_rule.contentValues(portal_type="Simulation Movement")[0]
406
    sm.edit(causality_value=business_link_a_b)
407 408 409

    # make other movements for each business path
    applied_rule.newContent(portal_type="Simulation Movement",
410
                            causality_value=business_link_b_c,
411 412
                            order_value=order_line)
    applied_rule.newContent(portal_type="Simulation Movement",
413
                            causality_value=business_link_b_d,
414 415
                            order_value=order_line)
    applied_rule.newContent(portal_type="Simulation Movement",
416
                            causality_value=business_link_c_d,
417 418
                            order_value=order_line)
    applied_rule.newContent(portal_type="Simulation Movement",
419
                            causality_value=business_link_d_e,
420 421 422 423 424 425 426
                            order_value=order_line)

    self.stepTic()

    trade_phase = self.portal.portal_categories.trade_phase.default

    # assertion which getRemainingTradePhaseList must return category which will be passed
427
    # discount is passed, business_link_a_b is already completed, because simulation state is "ordered"
428 429 430 431
    self.assertEquals(set([trade_phase.delivery,
                           trade_phase.invoicing,
                           trade_phase.payment,
                           trade_phase.accounting]),
432 433
                      set(business_process.getRemainingTradePhaseList(order,
                          business_state_a)))
434 435 436 437
    self.assertEquals(set([trade_phase.delivery,
                           trade_phase.invoicing,
                           trade_phase.payment,
                           trade_phase.accounting]),
438 439
                      set(business_process.getRemainingTradePhaseList(order,
                          business_state_b)))
440 441
    self.assertEquals(set([trade_phase.payment,
                           trade_phase.accounting]),
442 443
                      set(business_process.getRemainingTradePhaseList(order,
                          business_state_c)))
444
    self.assertEquals(set([trade_phase.accounting]),
445 446
                      set(business_process.getRemainingTradePhaseList(order,
                          business_state_d)))
447 448 449 450

    # when trade_phase_list is defined in arguments, the result is filtered by base category.
    self.assertEquals(set([trade_phase.delivery,
                           trade_phase.accounting]),
451 452
                      set(business_process\
                          .getRemainingTradePhaseList(order, business_state_a,
453 454 455
                                                      trade_phase_list=['default/delivery',
                                                                        'default/accounting'])))

456
  def test_BusinessLink_calculateExpectedDate(self):
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
    """
    This test case is described for what start/stop date is expected on
    each path by explanation.
    In this case, root explanation is path of between "b" and "d", and
    lead time and wait time is set on each path.
    ("l" is lead time, "w" is wait_time)

    Each path must calculate most early day from getting most longest
    path in the simulation.

    "referential_date" represents for which date have to get of explanation from reality.

                  (root_explanation)
        l:2, w:1         l:3, w:1       l:4, w:2
    a ------------ b -------------- d -------------- e
                    \             /
                     \           /
             l:2, w:1 \         / l:3, w:0
                       \       /
                        \     /
                         \   /
                          \ /
                           c
    """
    # define business process
482
    category_tool = self.getCategoryTool()
483
    business_process = self.createBusinessProcess()
484 485 486 487 488
    business_link_a_b = self.createBusinessLink(business_process)
    business_link_b_c = self.createBusinessLink(business_process)
    business_link_b_d = self.createBusinessLink(business_process)
    business_link_c_d = self.createBusinessLink(business_process)
    business_link_d_e = self.createBusinessLink(business_process)
489 490 491 492 493
    business_state_a = category_tool.trade_state.state_a
    business_state_b = category_tool.trade_state.state_b
    business_state_c = category_tool.trade_state.state_c
    business_state_d = category_tool.trade_state.state_d
    business_state_e = category_tool.trade_state.state_e
494 495 496 497 498 499 500 501 502 503
    business_link_a_b.setPredecessorValue(business_state_a)
    business_link_b_c.setPredecessorValue(business_state_b)
    business_link_b_d.setPredecessorValue(business_state_b)
    business_link_c_d.setPredecessorValue(business_state_c)
    business_link_d_e.setPredecessorValue(business_state_d)
    business_link_a_b.setSuccessorValue(business_state_b)
    business_link_b_c.setSuccessorValue(business_state_c)
    business_link_b_d.setSuccessorValue(business_state_d)
    business_link_c_d.setSuccessorValue(business_state_d)
    business_link_d_e.setSuccessorValue(business_state_e)
504 505

    business_process.edit(referential_date='stop_date')
506 507 508 509 510
    business_link_a_b.edit(title='a_b', lead_time=2, wait_time=1)
    business_link_b_c.edit(title='b_c', lead_time=2, wait_time=1)
    business_link_b_d.edit(title='b_d', lead_time=3, wait_time=1)
    business_link_c_d.edit(title='c_d', lead_time=3, wait_time=0)
    business_link_d_e.edit(title='d_e', lead_time=4, wait_time=2)
511 512

    # root explanation
513
    business_link_b_d.edit(deliverable=True)
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    self.stepTic()

    """
    Basic test, lead time of reality and simulation are consistent.
    """
    class Mock:
      def __init__(self, date):
        self.date = date
      def getStartDate(self):
        return self.date
      def getStopDate(self):
        return self.date + 3 # lead time of reality

    base_date = DateTime('2009/04/01 GMT+9')
    mock = Mock(base_date)

    # root explanation.
531 532
    self.assertEquals(business_link_b_d.getExpectedStartDate(mock), DateTime('2009/04/01 GMT+9'))
    self.assertEquals(business_link_b_d.getExpectedStopDate(mock), DateTime('2009/04/04 GMT+9'))
533 534

    # assertion for each path without root explanation.
535 536 537 538 539 540 541 542
    self.assertEquals(business_link_a_b.getExpectedStartDate(mock), DateTime('2009/03/27 GMT+9'))
    self.assertEquals(business_link_a_b.getExpectedStopDate(mock), DateTime('2009/03/29 GMT+9'))
    self.assertEquals(business_link_b_c.getExpectedStartDate(mock), DateTime('2009/03/30 GMT+9'))
    self.assertEquals(business_link_b_c.getExpectedStopDate(mock), DateTime('2009/04/01 GMT+9'))
    self.assertEquals(business_link_c_d.getExpectedStartDate(mock), DateTime('2009/04/01 GMT+9'))
    self.assertEquals(business_link_c_d.getExpectedStopDate(mock), DateTime('2009/04/04 GMT+9'))
    self.assertEquals(business_link_d_e.getExpectedStartDate(mock), DateTime('2009/04/06 GMT+9'))
    self.assertEquals(business_link_d_e.getExpectedStopDate(mock), DateTime('2009/04/10 GMT+9'))
543 544 545 546 547 548 549 550

    """
    Test of illegal case, lead time of reality and simulation are inconsistent,
    always reality is taken, but it depends on which date(e.g. start_date and stop_date) is referential.

    How we know which is referential, currently implementation of it can be known by
    BusinessProcess.isStartDateReferential and BusinessProcess.isStopDateReferential.

551
    In this test case, stop_date on business_link_b_d is referential, because business_link_b_d is
552 553 554
    root explanation and business_process refer to stop_date as referential.

    calculation example(when referential date is 2009/04/06 GMT+9):
555
    start_date of business_link_b_d = referential_date - 3(lead_time of business_link_b_d)
556 557 558 559 560 561 562 563 564 565 566 567 568 569
                                    = 2009/04/06 GMT+9 - 3
                                    = 2009/04/03 GMT+9
    """
    class Mock:
      def __init__(self, date):
        self.date = date
      def getStartDate(self):
        return self.date
      def getStopDate(self):
        return self.date + 5 # changed

    base_date = DateTime('2009/04/01 GMT+9')
    mock = Mock(base_date)

570
    self.assertEquals(business_link_b_d.getExpectedStartDate(mock), DateTime('2009/04/03 GMT+9'))
571
    # This is base in this context, because referential_date is 'stop_date'
572
    self.assertEquals(business_link_b_d.getExpectedStopDate(mock), DateTime('2009/04/06 GMT+9'))
573 574

    # assertion for each path without root explanation.
575 576 577 578 579 580 581 582
    self.assertEquals(business_link_a_b.getExpectedStartDate(mock), DateTime('2009/03/29 GMT+9'))
    self.assertEquals(business_link_a_b.getExpectedStopDate(mock), DateTime('2009/03/31 GMT+9'))
    self.assertEquals(business_link_b_c.getExpectedStartDate(mock), DateTime('2009/04/01 GMT+9'))
    self.assertEquals(business_link_b_c.getExpectedStopDate(mock), DateTime('2009/04/03 GMT+9'))
    self.assertEquals(business_link_c_d.getExpectedStartDate(mock), DateTime('2009/04/03 GMT+9'))
    self.assertEquals(business_link_c_d.getExpectedStopDate(mock), DateTime('2009/04/06 GMT+9'))
    self.assertEquals(business_link_d_e.getExpectedStartDate(mock), DateTime('2009/04/08 GMT+9'))
    self.assertEquals(business_link_d_e.getExpectedStopDate(mock), DateTime('2009/04/12 GMT+9'))
583

584
class TestBPMDummyDeliveryMovementMixin(TestBPMMixin):
585 586 587 588 589 590 591
  def _createDelivery(self, **kw):
    return self.folder.newContent(portal_type='Dummy Delivery', **kw)

  def _createMovement(self, delivery, **kw):
    return delivery.newContent(portal_type='Dummy Movement', **kw)

  def getBusinessTemplateList(self):
Łukasz Nowak's avatar
Łukasz Nowak committed
592 593
    return TestBPMMixin.getBusinessTemplateList(self) \
        + ('erp5_dummy_movement', )
594 595 596 597 598 599 600 601 602 603 604 605 606

  def afterSetUp(self):
    TestBPMMixin.afterSetUp(self)
    if not hasattr(self.portal, 'testing_folder'):
      self.portal.newContent(portal_type='Folder',
                            id='testing_folder')
    self.folder = self.portal.testing_folder
    self.stepTic()

  def beforeTearDown(self):
    TestBPMMixin.beforeTearDown(self)
    self.portal.deleteContent(id='testing_folder')
    self.stepTic()
Łukasz Nowak's avatar
Łukasz Nowak committed
607

608 609
  completed_state = 'delivered'
  frozen_state = 'confirmed'
610 611 612

  completed_state_list = [completed_state, frozen_state]
  frozen_state_list = [frozen_state]
613

614
  def _createOrderedDeliveredInvoicedBusinessProcess(self):
615 616
    # simple business process preparation
    business_process = self.createBusinessProcess()
617 618 619 620
    category_tool = self.getCategoryTool()
    ordered = category_tool.trade_state.ordered
    delivered = category_tool.trade_state.delivered
    invoiced = category_tool.trade_state.invoiced
621 622 623

    # path which is completed, as soon as related simulation movements are in
    # proper state
624
    self.order_path = self.createBusinessLink(business_process,
625 626 627 628 629
        successor_value = ordered,
        trade_phase='default/order',
        completed_state_list = self.completed_state_list,
        frozen_state_list = self.frozen_state_list)

630
    self.delivery_path = self.createBusinessLink(business_process,
631
        predecessor_value = ordered, successor_value = delivered,
632 633 634
        trade_phase='default/delivery',
        completed_state_list = self.completed_state_list,
        frozen_state_list = self.frozen_state_list)
635

636
    self.invoice_path = self.createBusinessLink(business_process,
637
        predecessor_value = delivered, successor_value = invoiced,
638
        trade_phase='default/invoicing')
639 640 641 642
    self.stepTic()

  def _createOrderedInvoicedDeliveredBusinessProcess(self):
    business_process = self.createBusinessProcess()
643 644 645 646
    category_tool = self.getCategoryTool()
    ordered = category_tool.trade_state.ordered
    delivered = category_tool.trade_state.delivered
    invoiced = category_tool.trade_state.invoiced
647

648
    self.order_path = self.createBusinessLink(business_process,
649 650 651 652 653
        successor_value = ordered,
        trade_phase='default/order',
        completed_state_list = self.completed_state_list,
        frozen_state_list = self.frozen_state_list)

654
    self.invoice_path = self.createBusinessLink(business_process,
655
        predecessor_value = ordered, successor_value = invoiced,
656 657 658
        trade_phase='default/invoicing',
        completed_state_list = self.completed_state_list,
        frozen_state_list = self.frozen_state_list)
659

660
    self.delivery_path = self.createBusinessLink(business_process,
661 662 663
        predecessor_value = invoiced, successor_value = delivered,
        trade_phase='default/delivery')
    self.stepTic()
664

665 666 667 668 669 670 671 672 673 674
class TestBPMisBuildableImplementation(TestBPMDummyDeliveryMovementMixin):
  def test_isBuildable_OrderedDeliveredInvoiced(self):
    """Test isBuildable for ordered, delivered and invoiced sequence

    Here Business Process sequence corresponds simulation tree.

    delivery_path is related to root applied rule, and invoice_path is related
    to rule below, and invoice_path is after delivery_path
    """
    self._createOrderedDeliveredInvoicedBusinessProcess()
675
    # create order and order line to have starting point for business process
676 677
    order = self._createDelivery()
    order_line = self._createMovement(order)
678 679 680 681 682 683 684

    # first level rule with simulation movement
    applied_rule = self.portal.portal_simulation.newContent(
        portal_type='Applied Rule', causality_value=order)

    simulation_movement = applied_rule.newContent(
      portal_type = 'Simulation Movement',
685 686
      delivery_value = order_line,
      causality_value = self.order_path
687 688 689
    )

    # second level rule with simulation movement
690 691 692 693 694 695 696 697
    delivery_rule = simulation_movement.newContent(
        portal_type='Applied Rule')
    delivery_simulation_movement = delivery_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.delivery_path)

    # third level rule with simulation movement
    invoicing_rule = delivery_simulation_movement.newContent(
698 699
        portal_type='Applied Rule')
    invoicing_simulation_movement = invoicing_rule.newContent(
700 701
        portal_type='Simulation Movement',
        causality_value = self.invoice_path)
702 703 704

    # split simulation movement for first level applied rule
    split_simulation_movement = applied_rule.newContent(
705 706
      portal_type = 'Simulation Movement', delivery_value = order_line,
      causality_value = self.order_path)
707 708

    # second level rule with simulation movement for split parent movement
709 710 711 712 713 714 715 716
    split_delivery_rule = split_simulation_movement.newContent(
        portal_type='Applied Rule')
    split_delivery_simulation_movement = split_delivery_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.delivery_path)

    # third level rule with simulation movement for split parent movement
    split_invoicing_rule = split_delivery_simulation_movement.newContent(
717 718
        portal_type='Applied Rule')
    split_invoicing_simulation_movement = split_invoicing_rule.newContent(
719 720
        portal_type='Simulation Movement',
        causality_value = self.invoice_path)
721

722
    order.setSimulationState(self.completed_state)
723 724 725
    self.stepTic()

    # in the beginning only order related movements shall be buildable
726
    self.assertEquals(self.delivery_path.isBuildable(order), True)
727 728
    self.assertEquals(delivery_simulation_movement.isBuildable(), True)
    self.assertEquals(split_delivery_simulation_movement.isBuildable(), True)
729

730
    self.assertEquals(self.invoice_path.isBuildable(order), False)
731 732 733 734 735
    self.assertEquals(invoicing_simulation_movement.isBuildable(), False)
    self.assertEquals(split_invoicing_simulation_movement.isBuildable(),
        False)

    # add delivery
736 737
    delivery = self._createDelivery(causality_value = order)
    delivery_line = self._createMovement(delivery)
738 739

    # relate not split movement with delivery (deliver it)
740
    delivery_simulation_movement.edit(delivery_value = delivery_line)
741 742 743 744 745 746

    self.stepTic()

    # delivery_path (for order) is still buildable, as split movement is not
    # delivered yet
    #
Łukasz Nowak's avatar
Łukasz Nowak committed
747 748
    # invoice_path is not yet buildable, delivery is in inproper simulation
    # state
749 750 751
    #
    # delivery_path (for delivery) is not buildable - delivery is already
    # built for those movements
752
    self.assertEquals(self.delivery_path.isBuildable(order), True)
753
    self.assertEquals(split_delivery_simulation_movement.isBuildable(), True)
754

755 756
    self.assertEquals(self.delivery_path.isBuildable(delivery), False)
    self.assertEquals(self.invoice_path.isBuildable(delivery), False)
757
    self.assertEquals(delivery_simulation_movement.isBuildable(), False)
758
    self.assertEquals(invoicing_simulation_movement.isBuildable(), False)
759
    self.assertEquals(self.invoice_path.isBuildable(order), False)
760 761 762 763 764
    self.assertEquals(split_invoicing_simulation_movement.isBuildable(),
        False)

    # put delivery in simulation state configured on path (and this state is
    # available directly on movements)
765

766
    delivery.setSimulationState(self.completed_state)
767

768
    self.assertEqual(self.completed_state, delivery.getSimulationState())
769 770 771 772 773 774

    self.stepTic()

    # delivery_path (for order) is still buildable, as split movement is not
    # delivered yet
    #
775
    # invoicing_path (for delivery and order) is buildable - in case of order,
776 777 778 779
    # because part of tree is buildable
    #
    # split movement for invoicing is not buildable - no proper delivery
    # related for previous path
780
    self.assertEquals(self.delivery_path.isBuildable(order), True)
781
    self.assertEquals(invoicing_simulation_movement.isBuildable(), True)
782
    self.assertEquals(self.invoice_path.isBuildable(delivery), True)
783

784
    # XXX look at comments in BusinessLink.isBuildable
785
    self.assertEquals(self.invoice_path.isBuildable(order), True)
786

787
    self.assertEquals(self.delivery_path.isBuildable(delivery), False)
788
    self.assertEquals(delivery_simulation_movement.isBuildable(), False)
789 790 791
    self.assertEquals(split_invoicing_simulation_movement.isBuildable(),
        False)

792 793 794 795 796 797 798 799
  def test_isBuildable_OrderedInvoicedDelivered(self):
    """Test isBuildable for ordered, invoiced and delivered sequence

    Here Business Process sequence do not corresponds simulation tree.

    delivery_path is related to root applied rule, and invoice_path is related
    to rule below, but invoice_path is before delivery_path in seuqence.
    """
800
    self._createOrderedInvoicedDeliveredBusinessProcess()
801 802 803 804 805 806 807 808 809

    order = self._createDelivery()
    order_line = self._createMovement(order)

    applied_rule = self.portal.portal_simulation.newContent(
        portal_type='Applied Rule', causality_value=order)

    simulation_movement = applied_rule.newContent(
      portal_type = 'Simulation Movement',
810 811
      delivery_value = order_line,
      causality_value = self.order_path
812 813
    )

814 815 816 817 818 819 820
    delivery_rule = simulation_movement.newContent(
        portal_type='Applied Rule')
    delivery_simulation_movement = delivery_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.delivery_path)

    invoicing_rule = delivery_simulation_movement.newContent(
821 822
        portal_type='Applied Rule')
    invoicing_simulation_movement = invoicing_rule.newContent(
823 824
        portal_type='Simulation Movement',
        causality_value = self.invoice_path)
825

826
    order.setSimulationState(self.completed_state)
827 828
    self.stepTic()

829
    self.assertEquals(self.delivery_path.isBuildable(order), False)
830
    self.assertEquals(delivery_simulation_movement.isBuildable(), False)
831

832
    self.assertEquals(self.invoice_path.isBuildable(order), True)
833 834 835 836 837 838 839 840 841
    self.assertEquals(invoicing_simulation_movement.isBuildable(), True)

    delivery = self._createDelivery(causality_value = order)
    delivery_line = self._createMovement(delivery)

    invoicing_simulation_movement.edit(delivery_value = delivery_line)

    self.stepTic()

842
    self.assertEquals(self.delivery_path.isBuildable(order), False)
843

844 845
    self.assertEquals(self.delivery_path.isBuildable(delivery), False)
    self.assertEquals(self.invoice_path.isBuildable(delivery), False)
846
    self.assertEquals(delivery_simulation_movement.isBuildable(), False)
847
    self.assertEquals(invoicing_simulation_movement.isBuildable(), False)
848
    self.assertEquals(self.invoice_path.isBuildable(order), False)
849 850 851 852

    # put delivery in simulation state configured on path (and this state is
    # available directly on movements)

853
    delivery.setSimulationState(self.completed_state)
854

855
    self.assertEqual(self.completed_state, delivery.getSimulationState())
856 857 858

    self.stepTic()

859 860
    self.assertEquals(self.delivery_path.isBuildable(order), True)
    self.assertEquals(self.delivery_path.isBuildable(delivery), True)
861
    self.assertEquals(invoicing_simulation_movement.isBuildable(), False)
862 863
    self.assertEquals(self.invoice_path.isBuildable(delivery), False)
    self.assertEquals(self.invoice_path.isBuildable(order), False)
864
    self.assertEquals(delivery_simulation_movement.isBuildable(), True)
865 866 867

    # now simulate compensation

868
    compensated_simulation_movement = delivery_rule.newContent(
869
      portal_type = 'Simulation Movement',
870
      delivery_value = order_line,
871
      causality_value = self.delivery_path
872 873 874 875 876
    )

    compensated_invoicing_rule = compensated_simulation_movement.newContent(
        portal_type='Applied Rule')

877 878 879
    compensated_invoicing_simulation_movement = compensated_invoicing_rule \
      .newContent(portal_type='Simulation Movement',
          causality_value = self.invoice_path)
880 881 882 883 884 885

    # and delivery some part of tree

    another_delivery = self._createDelivery(causality_value = delivery)
    another_delivery_line = self._createMovement(another_delivery)

886
    delivery_simulation_movement.edit(delivery_value=another_delivery_line)
887 888 889

    self.stepTic()

890
    self.assertEquals(self.delivery_path.isBuildable(order), False)
891

892
    self.assertEquals(delivery_simulation_movement.isBuildable(), False)
893 894
    self.assertEquals(invoicing_simulation_movement.isBuildable(), False)

895 896 897
    self.assertEquals(self.invoice_path.isBuildable(order), True)
    self.assertEquals(compensated_invoicing_simulation_movement.isBuildable(),
        True)
898 899 900

    self.assertEquals(compensated_simulation_movement.isBuildable(), False)

901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
class TestBPMisCompletedImplementation(TestBPMDummyDeliveryMovementMixin):
  def test_isCompleted_OrderedDeliveredInvoiced(self):
    """Test isCompleted for ordered, delivered and invoiced sequence"""
    self._createOrderedDeliveredInvoicedBusinessProcess()

    # create order and order line to have starting point for business process
    order = self._createDelivery()
    order_line = self._createMovement(order)

    # first level rule with simulation movement
    applied_rule = self.portal.portal_simulation.newContent(
        portal_type='Applied Rule', causality_value=order)

    simulation_movement = applied_rule.newContent(
      portal_type = 'Simulation Movement',
916 917
      delivery_value = order_line,
      causality_value = self.order_path
918 919 920
    )

    # second level rule with simulation movement
921 922 923 924 925 926 927 928
    delivery_rule = simulation_movement.newContent(
        portal_type='Applied Rule')
    delivery_simulation_movement = delivery_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.delivery_path)

    # third level rule with simulation movement
    invoicing_rule = delivery_simulation_movement.newContent(
929 930 931 932 933 934 935
        portal_type='Applied Rule')
    invoicing_simulation_movement = invoicing_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.invoice_path)

    # split simulation movement for first level applied rule
    split_simulation_movement = applied_rule.newContent(
936 937
      portal_type = 'Simulation Movement', delivery_value = order_line,
      causality_value = self.order_path)
938 939

    # second level rule with simulation movement for split parent movement
940 941 942 943 944 945 946 947
    split_delivery_rule = split_simulation_movement.newContent(
        portal_type='Applied Rule')
    split_delivery_simulation_movement = split_delivery_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.delivery_path)

    # third level rule with simulation movement for split parent movement
    split_invoicing_rule = split_delivery_simulation_movement.newContent(
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
        portal_type='Applied Rule')
    split_invoicing_simulation_movement = split_invoicing_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.invoice_path)

    self.stepTic()

    self.assertEqual(self.delivery_path.isCompleted(order), False)
    self.assertEqual(self.delivery_path.isPartiallyCompleted(order), False)

    self.assertEqual(self.invoice_path.isCompleted(order), False)
    self.assertEqual(self.invoice_path.isPartiallyCompleted(order), False)

    # add delivery
    delivery = self._createDelivery(causality_value = order)
    delivery_line = self._createMovement(delivery)

    # relate not split movement with delivery (deliver it)
966
    delivery_simulation_movement.edit(delivery_value = delivery_line)
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016

    self.stepTic()

    # nothing changes
    self.assertEqual(self.delivery_path.isCompleted(order), False)
    self.assertEqual(self.delivery_path.isPartiallyCompleted(order), False)

    self.assertEqual(self.invoice_path.isCompleted(order), False)
    self.assertEqual(self.invoice_path.isPartiallyCompleted(order), False)

    # from delivery point of view everything is same
    self.assertEqual(self.delivery_path.isCompleted(delivery), False)
    self.assertEqual(self.delivery_path.isPartiallyCompleted(delivery), False)

    self.assertEqual(self.invoice_path.isCompleted(delivery), False)
    self.assertEqual(self.invoice_path.isPartiallyCompleted(delivery), False)

    # put delivery in simulation state configured on path (and this state is
    # available directly on movements)

    delivery.setSimulationState(self.completed_state)

    self.assertEqual(self.completed_state, delivery.getSimulationState())

    self.stepTic()

    self.assertEqual(self.delivery_path.isCompleted(order), False)
    self.assertEqual(self.delivery_path.isPartiallyCompleted(order), True)

    self.assertEqual(self.invoice_path.isCompleted(order), False)
    self.assertEqual(self.invoice_path.isPartiallyCompleted(order), False)

    self.assertEqual(self.delivery_path.isCompleted(delivery), True)
    self.assertEqual(self.delivery_path.isPartiallyCompleted(delivery), True)

    self.assertEqual(self.invoice_path.isCompleted(delivery), False)
    self.assertEqual(self.invoice_path.isPartiallyCompleted(delivery), False)

  def test_isCompleted_OrderedInvoicedDelivered(self):
    """Test isCompleted for ordered, invoiced and invoiced sequence"""
    self._createOrderedInvoicedDeliveredBusinessProcess()

    order = self._createDelivery()
    order_line = self._createMovement(order)

    applied_rule = self.portal.portal_simulation.newContent(
        portal_type='Applied Rule', causality_value=order)

    simulation_movement = applied_rule.newContent(
      portal_type = 'Simulation Movement',
1017
      delivery_value = order_line,
1018 1019 1020
      causality_value = self.delivery_path
    )

1021 1022 1023 1024 1025 1026 1027
    delivery_rule = simulation_movement.newContent(
        portal_type='Applied Rule')
    delivery_simulation_movement = delivery_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.delivery_path)

    invoicing_rule = delivery_simulation_movement.newContent(
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
        portal_type='Applied Rule')
    invoicing_simulation_movement = invoicing_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.invoice_path)

    self.stepTic()

    self.assertEqual(self.delivery_path.isCompleted(order), False)
    self.assertEqual(self.delivery_path.isPartiallyCompleted(order), False)

    self.assertEqual(self.invoice_path.isCompleted(order), False)
    self.assertEqual(self.invoice_path.isPartiallyCompleted(order), False)

    delivery = self._createDelivery(causality_value = order)
    delivery_line = self._createMovement(delivery)

    invoicing_simulation_movement.edit(delivery_value = delivery_line)

    self.stepTic()

    self.assertEqual(self.delivery_path.isCompleted(order), False)
    self.assertEqual(self.delivery_path.isPartiallyCompleted(order), False)

    self.assertEqual(self.invoice_path.isCompleted(order), False)
    self.assertEqual(self.invoice_path.isPartiallyCompleted(order), False)

    self.assertEqual(self.delivery_path.isCompleted(delivery), False)
    self.assertEqual(self.delivery_path.isPartiallyCompleted(delivery), False)

    self.assertEqual(self.invoice_path.isCompleted(delivery), False)
    self.assertEqual(self.invoice_path.isPartiallyCompleted(delivery), False)

    # put delivery in simulation state configured on path (and this state is
    # available directly on movements)

    delivery.setSimulationState(self.completed_state)

    self.assertEqual(self.completed_state, delivery.getSimulationState())

    self.stepTic()

    self.assertEqual(self.delivery_path.isCompleted(order), False)
    self.assertEqual(self.delivery_path.isPartiallyCompleted(order), False)

    self.assertEqual(self.invoice_path.isCompleted(order), True)
    self.assertEqual(self.invoice_path.isPartiallyCompleted(order), True)

    self.assertEqual(self.delivery_path.isCompleted(delivery), False)
    self.assertEqual(self.delivery_path.isPartiallyCompleted(delivery), False)

    self.assertEqual(self.invoice_path.isCompleted(delivery), True)
    self.assertEqual(self.invoice_path.isPartiallyCompleted(delivery), True)

    # now simulate compensation

1083
    compensated_simulation_movement = delivery_rule.newContent(
1084
      portal_type = 'Simulation Movement',
1085
      delivery_value = order_line,
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
      causality_value = self.delivery_path
    )

    compensated_invoicing_rule = compensated_simulation_movement.newContent(
        portal_type='Applied Rule')

    compensated_invoicing_simulation_movement = compensated_invoicing_rule \
        .newContent(portal_type='Simulation Movement',
            causality_value = self.invoice_path)

    # and delivery some part of tree

    another_delivery = self._createDelivery(causality_value = delivery)
    another_delivery_line = self._createMovement(another_delivery)

1101
    delivery_simulation_movement.edit(delivery_value=another_delivery_line)
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116

    self.stepTic()

    self.assertEqual(self.delivery_path.isCompleted(order), False)
    self.assertEqual(self.delivery_path.isPartiallyCompleted(order), False)

    self.assertEqual(self.invoice_path.isCompleted(order), False)
    self.assertEqual(self.invoice_path.isPartiallyCompleted(order), True)

    self.assertEqual(self.delivery_path.isCompleted(delivery), False)
    self.assertEqual(self.delivery_path.isPartiallyCompleted(delivery), False)

    self.assertEqual(self.invoice_path.isCompleted(delivery), True)
    self.assertEqual(self.invoice_path.isPartiallyCompleted(delivery), True)

1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
class TestBPMisFrozenImplementation(TestBPMDummyDeliveryMovementMixin):
  def test_isFrozen_OrderedDeliveredInvoiced(self):
    """Test isFrozen for ordered, delivered and invoiced sequence"""
    self._createOrderedDeliveredInvoicedBusinessProcess()

    # create order and order line to have starting point for business process
    order = self._createDelivery()
    order_line = self._createMovement(order)

    # first level rule with simulation movement
    applied_rule = self.portal.portal_simulation.newContent(
        portal_type='Applied Rule', causality_value=order)

    simulation_movement = applied_rule.newContent(
      portal_type = 'Simulation Movement',
1132
      delivery_value = order_line,
1133 1134 1135 1136
      causality_value = self.delivery_path
    )

    # second level rule with simulation movement
1137 1138 1139 1140 1141 1142 1143 1144
    delivery_rule = simulation_movement.newContent(
        portal_type='Applied Rule')
    delivery_simulation_movement = delivery_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.delivery_path)

    # third level rule with simulation movement
    invoicing_rule = delivery_simulation_movement.newContent(
1145 1146 1147 1148 1149 1150 1151
        portal_type='Applied Rule')
    invoicing_simulation_movement = invoicing_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.invoice_path)

    # split simulation movement for first level applied rule
    split_simulation_movement = applied_rule.newContent(
1152 1153
      portal_type = 'Simulation Movement', delivery_value = order_line,
      causality_value = self.order_path)
1154 1155

    # second level rule with simulation movement for split parent movement
1156 1157 1158 1159 1160 1161 1162 1163
    split_delivery_rule = split_simulation_movement.newContent(
        portal_type='Applied Rule')
    split_delivery_simulation_movement = split_delivery_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.delivery_path)

    # third level rule with simulation movement for split parent movement
    split_invoicing_rule = split_delivery_simulation_movement.newContent(
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
        portal_type='Applied Rule')
    split_invoicing_simulation_movement = split_invoicing_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.invoice_path)

    self.stepTic()

    self.assertEqual(self.delivery_path.isFrozen(order), False)
    self.assertEqual(self.invoice_path.isFrozen(order), False)

    self.assertEqual(simulation_movement.isFrozen(), False)
    self.assertEqual(invoicing_simulation_movement.isFrozen(), False)
    self.assertEqual(split_simulation_movement.isFrozen(), False)
    self.assertEqual(split_invoicing_simulation_movement.isFrozen(), False)

    # add delivery
    delivery = self._createDelivery(causality_value = order)
    delivery_line = self._createMovement(delivery)

    # relate not split movement with delivery (deliver it)
1184
    delivery_simulation_movement.edit(delivery_value = delivery_line)
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211

    self.stepTic()

    # nothing changes
    self.assertEqual(self.delivery_path.isFrozen(order), False)
    self.assertEqual(self.invoice_path.isFrozen(order), False)

    # from delivery point of view everything is same
    self.assertEqual(self.delivery_path.isFrozen(delivery), False)
    self.assertEqual(self.invoice_path.isFrozen(delivery), False)

    self.assertEqual(simulation_movement.isFrozen(), False)
    self.assertEqual(invoicing_simulation_movement.isFrozen(), False)
    self.assertEqual(split_simulation_movement.isFrozen(), False)
    self.assertEqual(split_invoicing_simulation_movement.isFrozen(), False)

    # put delivery in simulation state configured on path (and this state is
    # available directly on movements)

    delivery.setSimulationState(self.frozen_state)

    self.assertEqual(self.frozen_state, delivery.getSimulationState())

    self.stepTic()

    self.assertEqual(self.delivery_path.isFrozen(order), False)
    self.assertEqual(self.invoice_path.isFrozen(order), False)
1212
    self.assertEqual(self.delivery_path.isFrozen(delivery), False)
1213 1214
    self.assertEqual(self.invoice_path.isFrozen(delivery), False)

1215
    self.assertEqual(delivery_simulation_movement.isFrozen(), True)
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
    self.assertEqual(invoicing_simulation_movement.isFrozen(), False)
    self.assertEqual(split_simulation_movement.isFrozen(), False)
    self.assertEqual(split_invoicing_simulation_movement.isFrozen(), False)

  def test_isFrozen_OrderedInvoicedDelivered(self):
    """Test isFrozen for ordered, invoiced and invoiced sequence"""
    self._createOrderedInvoicedDeliveredBusinessProcess()

    order = self._createDelivery()
    order_line = self._createMovement(order)

    applied_rule = self.portal.portal_simulation.newContent(
        portal_type='Applied Rule', causality_value=order)

    simulation_movement = applied_rule.newContent(
      portal_type = 'Simulation Movement',
1232
      delivery_value = order_line,
1233 1234 1235
      causality_value = self.delivery_path
    )

1236 1237 1238 1239 1240 1241 1242
    delivery_rule = simulation_movement.newContent(
        portal_type='Applied Rule')
    delivery_simulation_movement = delivery_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.delivery_path)

    invoicing_rule = delivery_simulation_movement.newContent(
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
        portal_type='Applied Rule')
    invoicing_simulation_movement = invoicing_rule.newContent(
        portal_type='Simulation Movement',
        causality_value = self.invoice_path)

    self.stepTic()

    self.assertEqual(self.delivery_path.isFrozen(order), False)
    self.assertEqual(self.invoice_path.isFrozen(order), False)

    self.assertEqual(simulation_movement.isFrozen(), False)
    self.assertEqual(invoicing_simulation_movement.isFrozen(), False)

    delivery = self._createDelivery(causality_value = order)
    delivery_line = self._createMovement(delivery)

    invoicing_simulation_movement.edit(delivery_value = delivery_line)

    self.stepTic()

    self.assertEqual(self.delivery_path.isFrozen(order), False)
    self.assertEqual(self.invoice_path.isFrozen(order), False)
    self.assertEqual(self.delivery_path.isFrozen(delivery), False)
    self.assertEqual(self.invoice_path.isFrozen(delivery), False)

    self.assertEqual(simulation_movement.isFrozen(), False)
    self.assertEqual(invoicing_simulation_movement.isFrozen(), False)

    # put delivery in simulation state configured on path (and this state is
    # available directly on movements)

    delivery.setSimulationState(self.frozen_state)

    self.assertEqual(self.frozen_state, delivery.getSimulationState())

    self.stepTic()

    self.assertEqual(self.delivery_path.isFrozen(order), False)
    self.assertEqual(self.invoice_path.isFrozen(order), True)
    self.assertEqual(self.delivery_path.isFrozen(delivery), False)
    self.assertEqual(self.invoice_path.isFrozen(delivery), True)

    self.assertEqual(simulation_movement.isFrozen(), False)
    self.assertEqual(invoicing_simulation_movement.isFrozen(), True)

    # now simulate compensation

1290
    compensated_simulation_movement = delivery_rule.newContent(
1291
      portal_type = 'Simulation Movement',
1292
      delivery_value = order_line,
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
      causality_value = self.delivery_path
    )

    compensated_invoicing_rule = compensated_simulation_movement.newContent(
        portal_type='Applied Rule')

    compensated_invoicing_simulation_movement = compensated_invoicing_rule \
        .newContent(portal_type='Simulation Movement',
            causality_value = self.invoice_path)

    # and delivery some part of tree

    another_delivery = self._createDelivery(causality_value = delivery)
    another_delivery_line = self._createMovement(another_delivery)

1308
    delivery_simulation_movement.edit(delivery_value=another_delivery_line)
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326

    self.stepTic()

    self.assertEqual(self.delivery_path.isFrozen(order), False)

    self.assertEqual(self.invoice_path.isFrozen(order), False)

    self.assertEqual(self.delivery_path.isFrozen(delivery), False)

    self.assertEqual(self.invoice_path.isFrozen(delivery), True)

    self.assertEqual(simulation_movement.isFrozen(), False)
    self.assertEqual(invoicing_simulation_movement.isFrozen(), True)

    self.assertEqual(compensated_simulation_movement.isFrozen(), False)
    self.assertEqual(compensated_invoicing_simulation_movement.isFrozen(),
        False)

1327 1328 1329
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestBPMImplementation))
1330
  suite.addTest(unittest.makeSuite(TestBPMisBuildableImplementation))
1331
  suite.addTest(unittest.makeSuite(TestBPMisCompletedImplementation))
1332
  suite.addTest(unittest.makeSuite(TestBPMisFrozenImplementation))
1333
  return suite