testRule.py 26.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
##############################################################################
#
# Copyright (c) 2007 Nexedi SARL and Contributors. All Rights Reserved.
#
# 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.
#
##############################################################################

import unittest

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import createZODBPythonScript
from zLOG import LOG
from Products.ERP5Type.tests.Sequence import SequenceList
from testOrder import TestOrderMixin

class TestRuleMixin(TestOrderMixin):
  """
  Test basic rule behaviours
  """

  def afterSetUp(self):
    self.test_data = []
    # delete rules
    self.getRuleTool().manage_delObjects(
        ids=list(list(self.getRuleTool().objectIds())))
    # recreate rules
    self.getRuleTool().newContent(portal_type="Order Rule",
        id='default_order_rule',
        reference='default_order_rule', version='1')
    self.getRuleTool().newContent(portal_type="Delivery Rule",
        id='default_delivery_rule',
        reference='default_delivery_rule', version='1')
    # create packing list if necessary
    pl_module = self.getPortal().getDefaultModule(
        self.packing_list_portal_type)
    if pl_module.objectCount() == 0:
      self.pl = self.createPackingList()
    else:
      self.pl = self.getPortal().getDefaultModule(
          self.packing_list_portal_type).objectValues()[0]
    #delete applied_rule
    self.getSimulationTool().manage_delObjects(
        ids=list(self.getSimulationTool().objectIds()))
    # commit
    get_transaction().commit()
    self.tic()


  def beforeTearDown(self):
    for container, id in self.test_data:
      container.manage_delObjects(ids=[id])
    self.getSimulationTool().manage_delObjects(
        ids=list(self.getSimulationTool().objectIds()))
    get_transaction().commit()
    self.tic()

  def getTitle(self):
    return "Rule"

  def getBusinessTemplateList(self):
    """
    Add erp5_dms_mysql_innodb_catalog, as for now, it's the one in charge of
    cataloging the version property.
    """
    return TestOrderMixin.getBusinessTemplateList(self) + ('erp5_accounting',
86
                'erp5_invoicing', 'erp5_dms_mysql_innodb_catalog',)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 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 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 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 621 622 623 624 625 626 627 628 629 630 631 632 633 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 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721

  def createPackingList(self):
    """
    create a packing list, to allow testing
    """
    self.getCategoryTool()['group'].newContent(portal_type='Category', id='a')
    self.getCategoryTool()['group'].newContent(portal_type='Category', id='b')
    pl_module = self.getPortal().getDefaultModule(
        self.packing_list_portal_type)
    pl = pl_module.newContent(portal_type=self.packing_list_portal_type,
        source_section='group/a', destination_section='group/b')
    pl.newContent(portal_type=self.packing_list_line_portal_type, id='line')
    pl.setStartDate("2007-07-01")
    get_transaction().commit()
    self.tic()
    pl.confirm()
    get_transaction().commit()
    self.tic()
    return pl


class TestRule(TestRuleMixin, ERP5TypeTestCase) :

  run_all_test = 1
  quiet = 0

  def test_01_ValidatedRuleWithNoScript(self, quiet=quiet, run=run_all_test):
    """
    test that when a rule is validated, but has no script it will not apply
    """
    if not run: return

    delivery_rule = self.getRuleTool().searchFolder(
        reference='default_delivery_rule')[0]
    delivery_rule.validate()
    get_transaction().commit()
    self.tic()

    self.assertEquals(self.getRuleTool().countFolder(
      validation_state="validated")[0][0], 1)
    self.assertEquals(len(self.getRuleTool().searchRuleList(self.pl)), 0)

  def test_02_WrongTestMethod(self, quiet=quiet, run=run_all_test):
    """
    test that when a rule's test method returns False, it will not apply
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'wrong_script', 'rule',
        'return False')
    delivery_rule = self.getRuleTool().searchFolder(
        reference='default_delivery_rule')[0]
    delivery_rule.setTestMethodId('wrong_script')
    delivery_rule.validate()
    get_transaction().commit()
    self.tic()

    self.assertEquals(self.getRuleTool().countFolder(
      validation_state="validated")[0][0], 1)
    self.assertEquals(len(self.getRuleTool().searchRuleList(self.pl)), 0)

  def test_03_GoodTestMethod(self, quiet=quiet, run=run_all_test):
    """
    test that when a rule's test method returns True, it will apply
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'good_script', 'rule',
        'return True')
    delivery_rule = self.getRuleTool().searchFolder(
        reference='default_delivery_rule')[0]
    delivery_rule.setTestMethodId('good_script')
    delivery_rule.validate()
    get_transaction().commit()
    self.tic()

    self.assertEquals(self.getRuleTool().countFolder(
      validation_state="validated")[0][0], 1)
    self.assertEquals(len(self.getRuleTool().searchRuleList(self.pl)), 1)

  def test_04_NotValidatedRule(self, quiet=quiet, run=run_all_test):
    """
    test that when a rule is not validated, it will not apply, even if it has
    a good script
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'good_script', 'rule',
        'return True')
    delivery_rule = self.getRuleTool().searchFolder(
        reference='default_delivery_rule')[0]
    delivery_rule.setTestMethodId('good_script')
    delivery_rule.validate()
    delivery_rule.invalidate()
    get_transaction().commit()
    self.tic()

    self.assertEquals(self.getRuleTool().countFolder(
      validation_state="validated")[0][0], 0)
    self.assertEquals(len(self.getRuleTool().searchRuleList(self.pl)), 0)

  def test_05_ValidatedRule(self, quiet=quiet, run=run_all_test):
    """
    test that when a rule is validated, it will apply
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'good_script', 'rule',
        'return True')
    delivery_rule = self.getRuleTool().searchFolder(
        reference='default_delivery_rule')[0]
    delivery_rule.setTestMethodId('good_script')
    delivery_rule.validate()
    get_transaction().commit()
    self.tic()

    self.assertEquals(self.getRuleTool().countFolder(
      validation_state="validated")[0][0], 1)
    self.assertEquals(len(self.getRuleTool().searchRuleList(self.pl)), 1)

  def test_06_WrongDateRange(self, quiet=quiet, run=run_all_test):
    """
    test that when a rule is validated but does not have correct date range,
    it will not apply
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'good_script', 'rule',
        'return True')
    delivery_rule = self.getRuleTool().searchFolder(
        reference='default_delivery_rule')[0]
    delivery_rule.setTestMethodId('good_script')
    delivery_rule.setStartDateRangeMin('2007-06-01')
    delivery_rule.setStartDateRangeMax('2007-06-04')
    delivery_rule.validate()
    get_transaction().commit()
    self.tic()

    self.assertEquals(self.getRuleTool().countFolder(
      validation_state="validated")[0][0], 1)
    self.assertEquals(len(self.getRuleTool().searchRuleList(self.pl)), 0)

  def test_07_GoodDateRange(self, quiet=quiet, run=run_all_test):
    """
    test that when a rule is validated and has a correct date range, it will
    apply
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'good_script', 'rule',
        'return True')
    delivery_rule = self.getRuleTool().searchFolder(
        reference='default_delivery_rule')[0]
    delivery_rule.setTestMethodId('good_script')
    delivery_rule.setStartDateRangeMin('2007-06-01')
    delivery_rule.setStartDateRangeMax('2007-08-01')
    delivery_rule.validate()
    get_transaction().commit()
    self.tic()

    self.assertEquals(self.getRuleTool().countFolder(
      validation_state="validated")[0][0], 1)
    self.assertEquals(len(self.getRuleTool().searchRuleList(self.pl)), 1)

  def test_08_updateAppliedRule(self, quiet=quiet, run=run_all_test):
    """
    test that when updateAppliedRule is called, the rule with the correct
    reference and higher version is used

    XXX as expand is triggered here, make sure rules won't be created forever
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'rule_script', 'rule',
        "return False")

    # wrong reference
    order_rule = self.getRuleTool().searchFolder(
        reference='default_order_rule')[0]
    order_rule.setTestMethodId('rule_script')
    order_rule.validate()
    
    delivery_rule_1 = self.getRuleTool().searchFolder(
        reference='default_delivery_rule')[0]
    delivery_rule_1.setTestMethodId('rule_script')
    delivery_rule_1.validate()
    
    delivery_rule_2 = self.getRuleTool().newContent(
        portal_type="Delivery Rule", reference='default_delivery_rule',
        version='2')
    delivery_rule_2.setTestMethodId('rule_script')
    delivery_rule_2.validate()
    get_transaction().commit()
    self.tic()

    # delivery_rule_2 should be applied
    self.pl.updateAppliedRule('default_delivery_rule')
    get_transaction().commit()
    self.tic()
    self.assertEquals(self.pl.getCausalityRelatedValue().getSpecialise(),
        delivery_rule_2.getRelativeUrl())

    self.getSimulationTool().manage_delObjects(
        ids=[self.pl.getCausalityRelatedId()])

    # increase version of delivery_rule_1
    delivery_rule_1.setVersion("3")
    get_transaction().commit()
    self.tic()

    # delivery_rule_1 should be applied
    self.pl.updateAppliedRule('default_delivery_rule')
    get_transaction().commit()
    self.tic()
    self.assertEquals(self.pl.getCausalityRelatedValue().getSpecialise(),
        delivery_rule_1.getRelativeUrl())

  def test_09_expandTwoRules(self, quiet=quiet, run=run_all_test):
    """
    test that when expand is called on a simulation movement, if two rules
    with the same reference are found, only the one with the higher version
    will be applied.

    XXX as expand is triggered here, make sure rules won't be created forever
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'delivery_rule_script', 'rule',
        "return False")

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'invoice_rule_script', 'rule',
        "return context.getParentValue().getSpecialiseReference() == 'default_delivery_rule'")

    delivery_rule = self.getRuleTool().searchFolder(
        reference='default_delivery_rule')[0]
    delivery_rule.validate()
    
    invoicing_rule_1 = self.getRuleTool().newContent(
        portal_type="Invoicing Rule", reference='default_invoicing_rule',
        version='1')
    invoicing_rule_1.setTestMethodId('invoice_rule_script')
    invoicing_rule_1.validate()

    invoicing_rule_2 = self.getRuleTool().newContent(
        portal_type="Invoicing Rule", reference='default_invoicing_rule',
        version='2')
    invoicing_rule_2.setTestMethodId('invoice_rule_script')
    invoicing_rule_2.validate()

    # clear simulation
    self.getSimulationTool().manage_delObjects(
        ids=list(self.getSimulationTool().objectIds()))
    get_transaction().commit()
    self.tic()

    self.pl.updateAppliedRule('default_delivery_rule')
    get_transaction().commit()
    self.tic()

    # check that only one invoicing rule (higher version) was applied
    root_applied_rule = self.pl.getCausalityRelatedValue()
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())

    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_2.getRelativeUrl())

    # increase version of other rule, clean simulation and check again
    self.getSimulationTool().manage_delObjects(
        ids=[self.pl.getCausalityRelatedId()])
    invoicing_rule_1.setVersion('3')
    get_transaction().commit()
    self.tic()

    self.pl.updateAppliedRule('default_delivery_rule')
    get_transaction().commit()
    self.tic()

    # check that only one invoicing rule (higher version) was applied
    root_applied_rule = self.pl.getCausalityRelatedValue()
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())

    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())

  def test_10_expandAddsRule(self, quiet=quiet, run=run_all_test):
    """
    test that if a rule didn't match previously, and does now, it should apply
    if no rule with the same type is already applied.
    - test that it happens if no rule is already applied
    - test that nothing changes if a rule of same type is already applied (no
      matter what the reference or version is)
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'delivery_rule_script', 'rule',
        "return False")

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'invoice_rule_script', 'rule',
        "return context.getParentValue().getSpecialiseReference() == 'default_delivery_rule'")

    delivery_rule = self.getRuleTool().searchFolder(
        reference='default_delivery_rule')[0]
    delivery_rule.validate()
    
    # create rule with a wrong script
    invoicing_rule_1 = self.getRuleTool().newContent(
        portal_type="Invoicing Rule", reference='default_invoicing_rule',
        version='1')
    invoicing_rule_1.setTestMethodId('delivery_rule_script')
    invoicing_rule_1.validate()

    # clear simulation
    self.getSimulationTool().manage_delObjects(
        ids=list(self.getSimulationTool().objectIds()))
    get_transaction().commit()
    self.tic()

    self.pl.updateAppliedRule('default_delivery_rule')
    get_transaction().commit()
    self.tic()
    root_applied_rule = self.pl.getCausalityRelatedValue()

    # check that no invoicing rule was applied
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 0)

    # change rule script so that it matches and test again
    invoicing_rule_1.setTestMethodId('invoice_rule_script')
    root_applied_rule.expand()
    get_transaction().commit()
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())

    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())

    # add more invoicing_rule and test that nothing is changed
    ## same reference, higher version
    invoicing_rule_n = self.getRuleTool().newContent(
        portal_type="Invoicing Rule", reference='default_invoicing_rule',
        version='2', test_method_id='invoice_rule_script')
    invoicing_rule_n.validate()
    ## different reference, higher version (but version shouldn't matter here)
    invoicing_rule_n = self.getRuleTool().newContent(
        portal_type="Invoicing Rule", reference='default_invoicing_rule_2',
        version='2', test_method_id='invoice_rule_script')
    invoicing_rule_n.validate()
    get_transaction().commit()
    self.tic()
    root_applied_rule.expand()
    get_transaction().commit()
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())

    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())


  def test_11_expandRemovesRule(self, quiet=quiet, run=run_all_test):
    """
    test that if a rule matched previously and does not anymore, it should be
    removed, if no child movement of this rule is delivered
    - test that it happens if no child is delivered
    - test that nothing is changed if at least one child is delivered
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'delivery_rule_script', 'rule',
        "return False")

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'invoice_rule_script', 'rule',
        "return context.getParentValue().getSpecialiseReference() == 'default_delivery_rule'")

    delivery_rule = self.getRuleTool().searchFolder(
        reference='default_delivery_rule')[0]
    delivery_rule.validate()
    
    invoicing_rule_1 = self.getRuleTool().newContent(
        portal_type="Invoicing Rule", reference='default_invoicing_rule',
        version='1')
    invoicing_rule_1.setTestMethodId('invoice_rule_script')
    invoicing_rule_1.validate()

    # clear simulation
    self.getSimulationTool().manage_delObjects(
        ids=list(self.getSimulationTool().objectIds()))

    get_transaction().commit()
    self.tic()

    self.pl.updateAppliedRule('default_delivery_rule')
    get_transaction().commit()
    self.tic()
    root_applied_rule = self.pl.getCausalityRelatedValue()

    # check that the invoicing rule was applied
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())

    # invalidate the rule and test that it is still there
    invoicing_rule_1.invalidate()
    get_transaction().commit()
    self.tic()
    self.assertEquals(invoicing_rule_1.getValidationState(), 'invalidated')
    root_applied_rule.expand()
    get_transaction().commit()
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())

    # change the test method to one that fails, and test that the rule is
    # removed
    invoicing_rule_1.setTestMethodId('delivery_rule_script')
    root_applied_rule.expand()
    get_transaction().commit()
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 0)

    # change the test to one that succeeds, revalidate, expand, add a delivery
    # relation, change the test method to one that fails, expand, and test
    # that the rule is still there
    invoicing_rule_1.setTestMethodId('invoice_rule_script')
    invoicing_rule_1.validate()
    get_transaction().commit()
    self.tic()
    self.assertEquals(invoicing_rule_1.getValidationState(), 'validated')
    root_applied_rule.expand()
    get_transaction().commit()
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())
    self.assertEquals(applied_rule.objectCount(), 1)
    sub_movement = applied_rule.objectValues()[0]

    sub_movement.setDeliveryValue(self.pl.line)

    invoicing_rule_1.setTestMethodId('delivery_rule_script')
    root_applied_rule.expand()
    get_transaction().commit()
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())
    self.assertEquals(applied_rule.objectCount(), 1)
    sub_movement = applied_rule.objectValues()[0]
    self.assertEquals(sub_movement.getDelivery(), self.pl.line.getRelativeUrl())

  def test_12_expandReplacesRule(self, quiet=quiet, run=run_all_test):
    """
    test that if a rule matched previously and does not anymore, and another
    rule matches now, the old rule should be replaced by the new one, if no
    child movement of this rule is delivered
    - test that it happens if no child is delivered
    - test that nothing is changed if at least one child is delivered
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'delivery_rule_script', 'rule',
        "return False")

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'invoice_rule_script', 'rule',
        "return context.getParentValue().getSpecialiseReference() == 'default_delivery_rule'")

    delivery_rule = self.getRuleTool().searchFolder(
        reference='default_delivery_rule')[0]
    delivery_rule.validate()
    
    invoicing_rule_1 = self.getRuleTool().newContent(
        portal_type="Invoicing Rule", reference='default_invoicing_rule',
        version='1')
    invoicing_rule_1.setTestMethodId('invoice_rule_script')
    invoicing_rule_1.validate()

    invoicing_rule_2 = self.getRuleTool().newContent(
        portal_type="Invoicing Rule", reference='default_invoicing_rule',
        version='2')
    invoicing_rule_2.setTestMethodId('invoice_rule_script')
    invoicing_rule_2.validate()

    # clear simulation
    self.getSimulationTool().manage_delObjects(
        ids=list(self.getSimulationTool().objectIds()))

    get_transaction().commit()
    self.tic()

    self.pl.updateAppliedRule('default_delivery_rule')
    get_transaction().commit()
    self.tic()
    root_applied_rule = self.pl.getCausalityRelatedValue()

    # check that the invoicing rule 2 was applied
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_2.getRelativeUrl())

    # change the test method to one that fails, and test that the rule is
    # replaced by invoicing rule 1
    invoicing_rule_2.setTestMethodId('delivery_rule_script')
    root_applied_rule.expand()
    get_transaction().commit()
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())

    # change the test of invoicing rule 2 to one that succeeds, add a delivery
    # relation, expand, and test that the invoicing rule 1 is still there
    invoicing_rule_2.setTestMethodId('invoice_rule_script')
    sub_movement = applied_rule.objectValues()[0]
    sub_movement.setDeliveryValue(self.pl.line)
    root_applied_rule.expand()
    get_transaction().commit()
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())
    self.assertEquals(applied_rule.objectCount(), 1)
    sub_movement = applied_rule.objectValues()[0]
    self.assertEquals(sub_movement.getDelivery(), self.pl.line.getRelativeUrl())


def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestRule))
  return suite