From 85ca76e8f63f3fb4a525bd076526be9a07bb9ad2 Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Thu, 28 Oct 2010 20:58:49 +0000
Subject: [PATCH] Fix test_167_InstanceAndRelatedClassDefinedInSameBT

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@39639 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/BusinessTemplate.py     | 18 ++++--
 product/ERP5/tests/testBusinessTemplate.py    | 15 +++--
 .../PathTemplateItem/old_file.xml             | 19 ++++++
 .../PathTemplateItem/some_file.xml            |  5 +-
 .../PathTemplateItem/some_foo.xml             | 16 +++++
 .../portal_types/Foo.xml                      | 60 +++++++++++++++++++
 .../bt/template_path_list                     |  4 +-
 .../bt/template_portal_type_id_list           |  1 +
 ...template_update_business_template_workflow |  1 -
 .../bt/template_update_tool                   |  1 -
 10 files changed, 123 insertions(+), 17 deletions(-)
 create mode 100644 product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PathTemplateItem/old_file.xml
 create mode 100644 product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PathTemplateItem/some_foo.xml
 create mode 100644 product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PortalTypeTemplateItem/portal_types/Foo.xml
 create mode 100644 product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_portal_type_id_list
 delete mode 100644 product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_update_business_template_workflow
 delete mode 100644 product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_update_tool

diff --git a/product/ERP5/Document/BusinessTemplate.py b/product/ERP5/Document/BusinessTemplate.py
index 15413cf7b7..7719260cac 100644
--- a/product/ERP5/Document/BusinessTemplate.py
+++ b/product/ERP5/Document/BusinessTemplate.py
@@ -59,6 +59,7 @@ from Products.ERP5Type.Utils import readLocalTest, \
 from Products.ERP5Type.Utils import convertToUpperCase
 from Products.ERP5Type import Permissions, PropertySheet, interfaces
 from Products.ERP5Type.XMLObject import XMLObject
+from Products.ERP5Type.dynamic.portal_type_class import synchronizeDynamicModules
 from OFS.Traversable import NotFound
 from OFS import SimpleItem, XMLExportImport
 from cStringIO import StringIO
@@ -3354,6 +3355,7 @@ class DocumentTemplateItem(BaseTemplateItem):
     update_dict = kw.get('object_to_update')
     force = kw.get('force')
     if context.getTemplateFormatVersion() == 1:
+      need_reset = isinstance(self, DocumentTemplateItem)
       for id in self._objects.keys():
         if update_dict.has_key(id) or force:
           if not force:
@@ -3366,11 +3368,14 @@ class DocumentTemplateItem(BaseTemplateItem):
           try:
             self.local_file_writer_name(name, text, create=0)
           except IOError, error:
-            LOG("BusinessTemplate.py", WARNING, "Cannot install class %s on file system" %(name,))
+            LOG(self.__class__.__name__, WARNING,
+                "Cannot install class %r on file system" % name)
             if error.errno:
               raise
             continue
-          if self.local_file_importer_name is not None:
+          if self.local_file_importer_name is None:
+            continue
+          if need_reset:
             # before any import, flush all ZODB caches to force a DB reload
             # otherwise we could have objects trying to get commited while
             # holding reference to a class that is no longer the same one as
@@ -3380,9 +3385,12 @@ class DocumentTemplateItem(BaseTemplateItem):
             transaction.savepoint(optimistic=True)
             # Then we need to flush from all caches, not only the one from this
             # connection
-            self.getPortalObject()._p_jar.db().cacheMinimize()
+            portal = self.getPortalObject()
+            portal._p_jar.db().cacheMinimize()
+            synchronizeDynamicModules(portal, force=True)
             gc.collect()
-            self.local_file_importer_name(name)
+            need_reset = False
+          self.local_file_importer_name(name)
     else:
       BaseTemplateItem.install(self, context, trashbin, **kw)
       for id in self._archive.keys():
@@ -4861,6 +4869,8 @@ Business Template is a set of definitions, such as skins, portal types and categ
 
       # Create temporary modules/classes for classes defined by this BT.
       # This is required if the BT contains instances of one of these classes.
+      # XXX This is not required with portal types as classes.
+      #     It is still there for compatibility with non-migrated objects.
       module_id_list = []
       for template_id in self.getTemplateDocumentIdList():
         module_id = 'Products.ERP5Type.Document.' + template_id
diff --git a/product/ERP5/tests/testBusinessTemplate.py b/product/ERP5/tests/testBusinessTemplate.py
index 9ca3334dd3..a42111a62f 100644
--- a/product/ERP5/tests/testBusinessTemplate.py
+++ b/product/ERP5/tests/testBusinessTemplate.py
@@ -5556,7 +5556,7 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     self.assertTrue(compareVersionStrings('1.0rc1', '>= 1.0rc1'))
 
   def test_checkDependencies(self):
-    from Products.ERP5Type.Document.BusinessTemplate import \
+    from Products.ERP5.Document.BusinessTemplate import \
           BusinessTemplateMissingDependency
     template_tool = self.getPortal().portal_templates
     erp5_core_version = template_tool.getInstalledBusinessTemplate(
@@ -6664,13 +6664,15 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     # This test does too much since we don't modify objects anymore during
     # download. Objects are cleaned up during installation, which does not
     # require any specific action about garbage collection or pickle cache.
-    from Products.ERP5Type.Document.BusinessTemplate import BaseTemplateItem
+    from Products.ERP5.Document.BusinessTemplate import BaseTemplateItem
     portal = self.portal
     BaseTemplateItem_removeProperties = BaseTemplateItem.removeProperties
+    object_id_list = 'old_file', 'some_file', 'some_foo'
     marker_list = []
     def removeProperties(self, obj, export):
       # Check it works if the object is modified during install.
-      obj.int_index = marker_list.pop()
+      if obj.id in object_id_list:
+        obj.int_index = marker_list.pop()
       return obj
     SimpleItem_getCopy = SimpleItem._getCopy
     try:
@@ -6680,7 +6682,7 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
       bt_path = os.path.join(os.path.dirname(__file__), 'test_data',
                              'test_167_InstanceAndRelatedClassDefinedInSameBT')
       # create a previously existing instance of the overriden document type
-      from Products.ERP5Type.Document.File import File
+      File = portal.portal_types.getPortalTypeClass('File')
       portal._setObject('another_file', File('another_file'))
       transaction.commit()
       self.tic()
@@ -6690,7 +6692,7 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
       # check its class has not yet been overriden
       self.assertFalse(getattr(portal.another_file, 'isClassOverriden', False))
       for i in (0, 1):
-        marker_list.append(i)
+        marker_list += [i] * len(object_id_list)
         gc.disable()
         bt = template_tool.download(bt_path)
         assert marker_list
@@ -6700,7 +6702,8 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
         bt.install(force=1)
         assert not marker_list
         gc.enable()
-        self.assertEqual(portal.some_file.int_index, i)
+        for id in object_id_list:
+          self.assertEqual(getattr(portal, id).int_index, i)
         transaction.commit()
         self.tic()
     finally:
diff --git a/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PathTemplateItem/old_file.xml b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PathTemplateItem/old_file.xml
new file mode 100644
index 0000000000..fffccb4e74
--- /dev/null
+++ b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PathTemplateItem/old_file.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <tuple>
+        <global name="File" module="Products.ERP5Type.Document.File"/>
+        <tuple/>
+      </tuple>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>old_file</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PathTemplateItem/some_file.xml b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PathTemplateItem/some_file.xml
index f0c079191e..eec98a0224 100644
--- a/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PathTemplateItem/some_file.xml
+++ b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PathTemplateItem/some_file.xml
@@ -2,10 +2,7 @@
 <ZopeData>
   <record id="1" aka="AAAAAAAAAAE=">
     <pickle>
-      <tuple>
-        <global name="File" module="Products.ERP5Type.Document.File"/>
-        <tuple/>
-      </tuple>
+      <global name="File" module="erp5.portal_type"/>
     </pickle>
     <pickle>
       <dictionary>
diff --git a/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PathTemplateItem/some_foo.xml b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PathTemplateItem/some_foo.xml
new file mode 100644
index 0000000000..b8600916b2
--- /dev/null
+++ b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PathTemplateItem/some_foo.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="Foo" module="erp5.portal_type"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>some_foo</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PortalTypeTemplateItem/portal_types/Foo.xml b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PortalTypeTemplateItem/portal_types/Foo.xml
new file mode 100644
index 0000000000..9d18343068
--- /dev/null
+++ b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/PortalTypeTemplateItem/portal_types/Foo.xml
@@ -0,0 +1,60 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="Base Type" module="erp5.portal_type"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>content_icon</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>description</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>factory</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>Foo</string> </value>
+        </item>
+        <item>
+            <key> <string>init_script</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>permission</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>portal_type</string> </key>
+            <value> <string>Base Type</string> </value>
+        </item>
+        <item>
+            <key> <string>type_class</string> </key>
+            <value> <string>XMLObject</string> </value>
+        </item>
+        <item>
+            <key> <string>type_mixin</string> </key>
+            <value>
+              <tuple/>
+            </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_path_list b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_path_list
index 434ecfe189..49e699114d 100644
--- a/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_path_list
+++ b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_path_list
@@ -1 +1,3 @@
-some_file
\ No newline at end of file
+old_file
+some_file
+some_foo
\ No newline at end of file
diff --git a/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_portal_type_id_list b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_portal_type_id_list
new file mode 100644
index 0000000000..9f26b637f0
--- /dev/null
+++ b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_portal_type_id_list
@@ -0,0 +1 @@
+Foo
\ No newline at end of file
diff --git a/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_update_business_template_workflow b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_update_business_template_workflow
deleted file mode 100644
index c227083464..0000000000
--- a/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_update_business_template_workflow
+++ /dev/null
@@ -1 +0,0 @@
-0
\ No newline at end of file
diff --git a/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_update_tool b/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_update_tool
deleted file mode 100644
index c227083464..0000000000
--- a/product/ERP5/tests/test_data/test_167_InstanceAndRelatedClassDefinedInSameBT/bt/template_update_tool
+++ /dev/null
@@ -1 +0,0 @@
-0
\ No newline at end of file
-- 
2.30.9