Commit fb9d4643 authored by Rafael Monnerat's avatar Rafael Monnerat

Added test_only parameter to Folder.upgradeObjectClass. This allows developer...

Added test_only parameter to Folder.upgradeObjectClass. This allows developer only test if there is any object to upgrade or not. (Added unit test also.)

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@36319 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 81dccbf9
...@@ -1082,7 +1082,8 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn, ...@@ -1082,7 +1082,8 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn,
return update_list return update_list
security.declareProtected( Permissions.ModifyPortalContent, 'upgradeObjectClass' ) security.declareProtected( Permissions.ModifyPortalContent, 'upgradeObjectClass' )
def upgradeObjectClass(self, test_before, from_class, to_class, test_after): def upgradeObjectClass(self, test_before, from_class, to_class, test_after,
test_only=0):
""" """
Upgrade the class of all objects inside this particular folder: Upgrade the class of all objects inside this particular folder:
test_before and test_after have to be a method with one parameter. test_before and test_after have to be a method with one parameter.
...@@ -1116,7 +1117,7 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn, ...@@ -1116,7 +1117,7 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn,
if hasattr(obase,'upgradeObjectClass'): if hasattr(obase,'upgradeObjectClass'):
test_list += o.upgradeObjectClass(test_before=test_before, \ test_list += o.upgradeObjectClass(test_before=test_before, \
from_class=from_class, to_class=to_class, from_class=from_class, to_class=to_class,
test_after=test_after) test_after=test_after, test_only=test_only)
# Test if we must apply the upgrade # Test if we must apply the upgrade
if test_before(o) is not None: if test_before(o) is not None:
...@@ -1124,7 +1125,7 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn, ...@@ -1124,7 +1125,7 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn,
klass = obase.__class__ klass = obase.__class__
LOG("upgradeObjectClass: klass ", 0 ,str(klass)) LOG("upgradeObjectClass: klass ", 0 ,str(klass))
LOG("upgradeObjectClass: from_class ", 0 ,str(from_class)) LOG("upgradeObjectClass: from_class ", 0 ,str(from_class))
if klass == from_class: if klass == from_class and not test_only:
try: try:
newob = to_class(obase.id) newob = to_class(obase.id)
newob.id = obase.id # This line activates obase. newob.id = obase.id # This line activates obase.
...@@ -1144,6 +1145,9 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn, ...@@ -1144,6 +1145,9 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn,
object_to_test = self._getOb(id) object_to_test = self._getOb(id)
test_list += test_after(object_to_test) test_list += test_after(object_to_test)
if klass == from_class and test_only:
test_list += test_after(o)
return test_list return test_list
......
...@@ -163,6 +163,14 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor): ...@@ -163,6 +163,14 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor):
# Reset to original permissions # Reset to original permissions
self.folder.manage_permission('Modify portal content', original_permission_list[0]['roles'], original_permission_list[0]['acquire']) self.folder.manage_permission('Modify portal content', original_permission_list[0]['roles'], original_permission_list[0]['acquire'])
def _createUpgradeObjectClassPythonScript(self):
"""Create a simple python script """
createZODBPythonScript(self.getPortal().portal_skins.custom,
"test_upgradeObject", 'x',
'return [1]')
return self.getPortal().portal_skins.custom.test_upgradeObject
def test_upgradeObjectClass(self): def test_upgradeObjectClass(self):
""" Test if it changes Object Class """ """ Test if it changes Object Class """
type_list = ['Folder', 'Category' ] type_list = ['Folder', 'Category' ]
...@@ -170,10 +178,7 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor): ...@@ -170,10 +178,7 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor):
obj = self.folder.newContent(portal_type="Category") obj = self.folder.newContent(portal_type="Category")
from_class = obj.__class__ from_class = obj.__class__
to_class = self.folder.__class__ to_class = self.folder.__class__
createZODBPythonScript(self.getPortal().portal_skins.custom, test_script = self._createUpgradeObjectClassPythonScript()
"test_upgradeObject", 'x',
'return [1]')
test_script = self.getPortal().portal_skins.custom.test_upgradeObject
result = self.folder.upgradeObjectClass(test_script, from_class, result = self.folder.upgradeObjectClass(test_script, from_class,
to_class, test_script) to_class, test_script)
transaction.commit() transaction.commit()
...@@ -181,6 +186,21 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor): ...@@ -181,6 +186,21 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor):
self.assertNotEquals(self.folder[obj.getId()].__class__, from_class) self.assertNotEquals(self.folder[obj.getId()].__class__, from_class)
self.assertEquals([1], result) self.assertEquals([1], result)
def test_upgradeObjectClassOnlyTest(self):
""" Test if it DOES NOT change Object Class, only test it. """
type_list = ['Folder', 'Category' ]
self._setAllowedContentTypesForFolderType(type_list)
obj = self.folder.newContent(portal_type="Category")
from_class = obj.__class__
to_class = self.folder.__class__
test_script = self._createUpgradeObjectClassPythonScript()
result = self.folder.upgradeObjectClass(test_script, from_class,
to_class, test_script, test_only=1)
transaction.commit()
self.assertNotEquals(self.folder[obj.getId()].__class__, to_class)
self.assertEquals(self.folder[obj.getId()].__class__, from_class)
self.assertEquals([1], result)
def test_upgradeObjectClassHierarchicaly(self): def test_upgradeObjectClassHierarchicaly(self):
""" Test if migrate sub objects Hierarchicaly """ """ Test if migrate sub objects Hierarchicaly """
type_list = ['Folder', 'Category', 'Base Category'] type_list = ['Folder', 'Category', 'Base Category']
...@@ -189,10 +209,7 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor): ...@@ -189,10 +209,7 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor):
obj = subfolder.newContent(portal_type="Category") obj = subfolder.newContent(portal_type="Category")
from_class = obj.__class__ from_class = obj.__class__
to_class = self.folder.__class__ to_class = self.folder.__class__
createZODBPythonScript(self.getPortal().portal_skins.custom, test_script = self._createUpgradeObjectClassPythonScript()
"test_upgradeObjectZ", 'x',
'return [1]')
test_script = self.getPortal().portal_skins.custom.test_upgradeObjectZ
result = self.folder.upgradeObjectClass(test_script, from_class, result = self.folder.upgradeObjectClass(test_script, from_class,
to_class, test_script) to_class, test_script)
transaction.commit() transaction.commit()
...@@ -208,10 +225,7 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor): ...@@ -208,10 +225,7 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor):
obj = subobject.newContent(portal_type="Category") obj = subobject.newContent(portal_type="Category")
from_class = obj.__class__ from_class = obj.__class__
to_class = self.folder.__class__ to_class = self.folder.__class__
createZODBPythonScript(self.getPortal().portal_skins.custom, test_script = self._createUpgradeObjectClassPythonScript()
"test_upgradeObjectZ", 'x',
'return [1]')
test_script = self.getPortal().portal_skins.custom.test_upgradeObjectZ
result = self.folder.upgradeObjectClass(test_script, from_class, result = self.folder.upgradeObjectClass(test_script, from_class,
to_class, test_script) to_class, test_script)
transaction.commit() transaction.commit()
...@@ -230,10 +244,7 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor): ...@@ -230,10 +244,7 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor):
to_class_as_string = 'Products.ERP5Type.Document.Folder.Folder' to_class_as_string = 'Products.ERP5Type.Document.Folder.Folder'
from_class = obj.__class__ from_class = obj.__class__
to_class = self.folder.__class__ to_class = self.folder.__class__
createZODBPythonScript(self.getPortal().portal_skins.custom, test_script = self._createUpgradeObjectClassPythonScript()
"test_upgradeObject", 'x',
'return [1]')
test_script = self.getPortal().portal_skins.custom.test_upgradeObject
result = self.folder.upgradeObjectClass(test_script, from_class_as_string, result = self.folder.upgradeObjectClass(test_script, from_class_as_string,
to_class_as_string, test_script) to_class_as_string, test_script)
transaction.commit() transaction.commit()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment