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,
return update_list
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:
test_before and test_after have to be a method with one parameter.
......@@ -1116,7 +1117,7 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn,
if hasattr(obase,'upgradeObjectClass'):
test_list += o.upgradeObjectClass(test_before=test_before, \
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
if test_before(o) is not None:
......@@ -1124,7 +1125,7 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn,
klass = obase.__class__
LOG("upgradeObjectClass: klass ", 0 ,str(klass))
LOG("upgradeObjectClass: from_class ", 0 ,str(from_class))
if klass == from_class:
if klass == from_class and not test_only:
try:
newob = to_class(obase.id)
newob.id = obase.id # This line activates obase.
......@@ -1144,6 +1145,9 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn,
object_to_test = self._getOb(id)
test_list += test_after(object_to_test)
if klass == from_class and test_only:
test_list += test_after(o)
return test_list
......
......@@ -163,6 +163,14 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor):
# Reset to original permissions
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):
""" Test if it changes Object Class """
type_list = ['Folder', 'Category' ]
......@@ -170,10 +178,7 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor):
obj = self.folder.newContent(portal_type="Category")
from_class = obj.__class__
to_class = self.folder.__class__
createZODBPythonScript(self.getPortal().portal_skins.custom,
"test_upgradeObject", 'x',
'return [1]')
test_script = self.getPortal().portal_skins.custom.test_upgradeObject
test_script = self._createUpgradeObjectClassPythonScript()
result = self.folder.upgradeObjectClass(test_script, from_class,
to_class, test_script)
transaction.commit()
......@@ -181,6 +186,21 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor):
self.assertNotEquals(self.folder[obj.getId()].__class__, from_class)
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):
""" Test if migrate sub objects Hierarchicaly """
type_list = ['Folder', 'Category', 'Base Category']
......@@ -189,10 +209,7 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor):
obj = subfolder.newContent(portal_type="Category")
from_class = obj.__class__
to_class = self.folder.__class__
createZODBPythonScript(self.getPortal().portal_skins.custom,
"test_upgradeObjectZ", 'x',
'return [1]')
test_script = self.getPortal().portal_skins.custom.test_upgradeObjectZ
test_script = self._createUpgradeObjectClassPythonScript()
result = self.folder.upgradeObjectClass(test_script, from_class,
to_class, test_script)
transaction.commit()
......@@ -208,10 +225,7 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor):
obj = subobject.newContent(portal_type="Category")
from_class = obj.__class__
to_class = self.folder.__class__
createZODBPythonScript(self.getPortal().portal_skins.custom,
"test_upgradeObjectZ", 'x',
'return [1]')
test_script = self.getPortal().portal_skins.custom.test_upgradeObjectZ
test_script = self._createUpgradeObjectClassPythonScript()
result = self.folder.upgradeObjectClass(test_script, from_class,
to_class, test_script)
transaction.commit()
......@@ -230,10 +244,7 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor):
to_class_as_string = 'Products.ERP5Type.Document.Folder.Folder'
from_class = obj.__class__
to_class = self.folder.__class__
createZODBPythonScript(self.getPortal().portal_skins.custom,
"test_upgradeObject", 'x',
'return [1]')
test_script = self.getPortal().portal_skins.custom.test_upgradeObject
test_script = self._createUpgradeObjectClassPythonScript()
result = self.folder.upgradeObjectClass(test_script, from_class_as_string,
to_class_as_string, test_script)
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