From fd7504f760a31de78949f873ef56238f14f45502 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Sat, 18 Nov 2006 18:25:55 +0000
Subject: [PATCH] PropertyTypeValidity constraint is now able to repair local
 properties when the property is later defined by a property sheet.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@11368 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../Constraint/PropertyTypeValidity.py        |  4 ++
 product/ERP5Type/tests/testConstraint.py      | 65 ++++++++++++++++++-
 2 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/product/ERP5Type/Constraint/PropertyTypeValidity.py b/product/ERP5Type/Constraint/PropertyTypeValidity.py
index f85307ded6..e63790b6af 100644
--- a/product/ERP5Type/Constraint/PropertyTypeValidity.py
+++ b/product/ERP5Type/Constraint/PropertyTypeValidity.py
@@ -107,4 +107,8 @@ class PropertyTypeValidity(Constraint):
               obj.setProperty(property_id, value)
               error_message += " (Fixed)"
         errors.append(self._generateError(obj, error_message))
+      elif fixit:
+        oldvalue = getattr(obj, property_id, value)
+        if oldvalue != value:
+          obj.setProperty(property_id, oldvalue)
     return errors
diff --git a/product/ERP5Type/tests/testConstraint.py b/product/ERP5Type/tests/testConstraint.py
index 12129cf98c..2044099491 100644
--- a/product/ERP5Type/tests/testConstraint.py
+++ b/product/ERP5Type/tests/testConstraint.py
@@ -33,12 +33,12 @@ if __name__ == '__main__':
 os.environ['EVENT_LOG_FILE'] = os.path.join(os.getcwd(), 'zLOG.log')
 os.environ['EVENT_LOG_SEVERITY'] = '-300'
 
-from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
+from Products.ERP5Type.tests.testERP5Type import PropertySheetTestCase
 from AccessControl.SecurityManagement import newSecurityManager, \
                                              noSecurityManager
 from Products.ERP5Type.tests.Sequence import Sequence, SequenceList
 
-class TestConstraint(ERP5TypeTestCase):
+class TestConstraint(PropertySheetTestCase):
 
   run_all_test = 1
   quiet = 1
@@ -1112,6 +1112,67 @@ class TestConstraint(ERP5TypeTestCase):
                    id='tales_constraint',
                    expression='error: " ')
     self.assertRaises(CompilerError, constraint.checkConsistency, obj)
+  
+  def test_PropertyTypeValidityFixLocalProperties(self):
+    """Tests PropertyTypeValidity can repairs local property when this property
+    is added on the class later.
+    """
+    constraint = self._createGenericConstraint(Sequence(),
+                   klass_name='PropertyTypeValidity',
+                   id='type_validity_constraint', )
+    obj = self._makeOne()
+    obj.edit(local_property='1')
+    self.assertEquals([], constraint.checkConsistency(obj))
+    # now add a 'local_property' property defined on a property sheet
+    self._addProperty(obj.getPortalType(),
+                  '''{'id': 'local_property', 'type': 'int'}''')
+    constraint.fixConsistency(obj)
+    self.assertEquals(1, obj.getLocalProperty())
+    obj.edit(local_property=3)
+    self.assertEquals(3, obj.getLocalProperty())
+  
+  def test_PropertyTypeValidityFixLocalPropertiesContent(self):
+    """Tests PropertyTypeValidity can repairs local property of type content
+    when this property is added on the class later.
+    """
+    constraint = self._createGenericConstraint(Sequence(),
+                   klass_name='PropertyTypeValidity',
+                   id='type_validity_constraint', )
+    obj = self._makeOne()
+    obj.edit(default_organisation_title='foo')
+    self.assertEquals([], constraint.checkConsistency(obj))
+    # now add a 'local_property' property defined on a property sheet
+    self._addProperty(obj.getPortalType(),
+                 ''' { 'id':         'organisation',
+                        'storage_id': 'default_organisation',
+                        'type':       'content',
+                        'portal_type': ('Organisation', ),
+                        'acquired_property_id': ('title', ),
+                        'mode':       'w', }''')
+    constraint.fixConsistency(obj)
+    self.assertEquals('foo', obj.getDefaultOrganisationTitle())
+    self.assertEquals('foo', obj.default_organisation.getTitle())
+  
+  def test_PropertyTypeValidityFixLocalPropertiesForCategories(self):
+    """Tests PropertyTypeValidity can repairs categories when this property
+    is added on the class later.
+    """
+    bc = self.getPortal().portal_categories.newContent(
+                              portal_type='Base Category',
+                              id='testing_category')
+    constraint = self._createGenericConstraint(Sequence(),
+                   klass_name='PropertyTypeValidity',
+                   id='type_validity_constraint', )
+    obj = self._makeOne()
+    obj.edit(testing_category=obj.getRelativeUrl())
+    self.assertEquals([], constraint.checkConsistency(obj))
+    # now add a 'local_property' property defined on a property sheet
+    self._addPropertySheet(obj.getPortalType(),
+      '''class TestPropertySheet: _categories=('testing_category',)''')
+    # fix consistency
+    constraint.fixConsistency(obj)
+    # now we can use testing_category as any category accessor
+    self.assertEquals(obj, obj.getTestingCategoryValue())
 
 
 if __name__ == '__main__':
-- 
2.30.9