From 8d4ab96ffc837eea28e984ef662c644dab382753 Mon Sep 17 00:00:00 2001
From: Romain Courteaud <romain@nexedi.com>
Date: Thu, 27 Aug 2009 13:13:18 +0000
Subject: [PATCH] Do not delete skin selection registered by other skin folders

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@28660 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/BusinessTemplate.py  |  20 +++-
 product/ERP5/tests/testBusinessTemplate.py | 101 +++++++++++++++++++++
 2 files changed, 118 insertions(+), 3 deletions(-)

diff --git a/product/ERP5/Document/BusinessTemplate.py b/product/ERP5/Document/BusinessTemplate.py
index cf93c7bda3..d9513a6dec 100644
--- a/product/ERP5/Document/BusinessTemplate.py
+++ b/product/ERP5/Document/BusinessTemplate.py
@@ -966,11 +966,25 @@ class ObjectTemplateItem(BaseTemplateItem):
         if trash and trashbin is not None:
           self.portal_trash.backupObject(trashbin, container_path, object_id, save=1, keep_subobjects=1)
         if container.meta_type == 'CMF Skins Tool':
-          # we are removing a skin folder, check and remove if registered skin selection
+          # we are removing a skin folder, check and 
+          # remove if registered skin selection
           skin_folder = container[object_id]
-          registered_skin_selections = getattr(skin_folder, 'business_template_registered_skin_selections', None)
+          registered_skin_selections = getattr(skin_folder, 
+              'business_template_registered_skin_selections', None)
           if registered_skin_selections is not None:
-            container.manage_skinLayers(chosen=registered_skin_selections, del_skin=1)
+
+            for other_skin_folder in container.objectValues():
+              if other_skin_folder.getId() != object_id:
+                for other_registered_skin_selection in \
+                      other_skin_folder.getProperty(
+                       'business_template_registered_skin_selections', ()):
+                  if other_registered_skin_selection in \
+                                        registered_skin_selections:
+                    registered_skin_selections.remove(
+                        other_registered_skin_selection)
+            if registered_skin_selections:
+              container.manage_skinLayers(chosen=registered_skin_selections, 
+                                          del_skin=1)
         container.manage_delObjects([object_id])
         if container.aq_parent.meta_type == 'ERP5 Catalog' and len(container.objectIds()) == 0:
           # We are removing a ZSQLMethod, remove the SQLCatalog if empty
diff --git a/product/ERP5/tests/testBusinessTemplate.py b/product/ERP5/tests/testBusinessTemplate.py
index 9ef93b70b1..3c3937a785 100644
--- a/product/ERP5/tests/testBusinessTemplate.py
+++ b/product/ERP5/tests/testBusinessTemplate.py
@@ -112,6 +112,21 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
             new_selection.append(skin_id)
         ps.manage_skinLayers(skinpath=tuple(new_selection),
                              skinname=skin_name, add_skin=1)
+    if 'erp5_static' in self.getSkinsTool().objectIds():
+      self.getSkinsTool().manage_delObjects(['erp5_static'])
+      ps = self.getSkinsTool()
+      for skin_name, selection in ps.getSkinPaths():
+        new_selection = []
+        selection = selection.split(',')
+        for skin_id in selection:
+          if skin_id != 'erp5_static':
+            new_selection.append(skin_id)
+        ps.manage_skinLayers(skinpath=tuple(new_selection),
+                             skinname=skin_name, add_skin=1)
+
+    if 'Foo' in self.getSkinsTool().getSkinSelections():
+      self.getSkinsTool().manage_skinLayers(chosen=('Foo',), del_skin=1)
+
     if 'Geek Object' in self.getTypeTool().objectIds():
       self.getTypeTool().manage_delObjects(['Geek Object', 'Geek Module'])
     if 'geek_module' in self.getPortal().objectIds():
@@ -613,6 +628,23 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
         selection.append('erp5_geek')
       ps.manage_skinLayers(skinpath = tuple(selection), skinname = skin_name, add_skin = 1)
 
+  def stepCreateStaticSkinFolder(self, sequence=None, sequence_list=None, **kw):
+    """
+    Create a skin folder not managed by the bt5
+    """
+    ps = self.getSkinsTool()
+    ps.manage_addProduct['OFSP'].manage_addFolder('erp5_static')
+    skin_folder = ps._getOb('erp5_static', None)
+    self.failUnless(skin_folder is not None)
+    sequence.edit(static_skin_folder_id=skin_folder.getId())
+    # add skin in layers
+    for skin_name, selection in ps.getSkinPaths():
+      selection = selection.split(',')
+      if 'erp5_static' not in selection:
+        selection.append('erp5_static')
+      ps.manage_skinLayers(skinpath=tuple(selection), skinname=skin_name, 
+                           add_skin=1)
+
   def stepCreateSkinSubFolder(self, sequence=None, sequence_list=None, **kw):
     ps = self.getSkinsTool()
     skin_folder = ps._getOb('erp5_geek', None)
@@ -4532,6 +4564,24 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
           'business_template_registered_skin_selections', ('Foo',),
           type='tokens')
 
+  def stepCreateSkinSelection(self, sequence=None, **kw):
+    ps = self.getSkinsTool()
+    ps.manage_skinLayers(skinpath=('erp5_core',), skinname='Foo', add_skin=1)
+
+  def stepSetStaticSkinFolderRegistredSelections(self, sequence=None, **kw):
+    ps = self.getSkinsTool()
+    skin_id = sequence.get('static_skin_folder_id')
+    skin_folder = ps._getOb(skin_id, None)
+    skin_folder._setProperty(
+          'business_template_registered_skin_selections', ('Foo',),
+          type='tokens')
+    selection = ps.getSkinPath('Foo')
+    selection = selection.split(',')
+    if skin_id not in selection:
+      selection.append(skin_id)
+      ps.manage_skinLayers(skinpath=tuple(selection), 
+                           skinname='Foo', add_skin=1)
+
   def stepCheckSkinSelectionAdded(self, sequence=None, **kw):
     ps = self.getSkinsTool()
     skin_id = sequence.get('skin_folder_id')
@@ -4544,6 +4594,20 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     self.assertTrue('erp5_core' in layers, layers)
     self.assertFalse('erp5_xhtml_style' in layers, layers)
 
+  def stepCheckStaticSkinSelection(self, sequence=None, **kw):
+    ps = self.getSkinsTool()
+    skin_id = sequence.get('skin_folder_id')
+    static_skin_id = sequence.get('static_skin_folder_id')
+    skin_paths = ps.getSkinPaths()
+    # a new skin selection is added
+    self.assertTrue('Foo' in ps.getSkinSelections())
+    # and it contains good layers
+    layers = ps.getSkinPath('Foo').split(',')
+    self.assertTrue(skin_id in layers, layers)
+    self.assertTrue('erp5_core' in layers, layers)
+    self.assertFalse('erp5_xhtml_style' in layers, layers)
+    self.assertTrue(static_skin_id in layers, layers)
+
   def test_33_BusinessTemplateWithNewSkinSelection(self, quiet=quiet,
                                                         run=run_all_test):
     if not run: return
@@ -5217,6 +5281,13 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     """
     self.assertTrue('Foo' not in self.portal.portal_skins.getSkinSelections())
 
+  def stepCheckSkinSelectionNotRemoved(self, sequence=None, 
+                                       sequence_list=None, **kw):
+    """
+    Check that a skin selection has not been removed.
+    """
+    self.assertTrue('Foo' in self.portal.portal_skins.getSkinSelections())
+
   def stepUserDisableSkinSelectionRegistration(self, sequence=None, sequence_list=None, **kw):
     """
     Simulate User disabling skin registration from UI.
@@ -5357,6 +5428,36 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     sequence_list.addSequenceString(sequence_string)
     sequence_list.play(self, quiet=quiet)
 
+  def test_158_BusinessTemplateSkinSelectionRemoveOnlyIfUnused(self, quiet=quiet,
+                                                               run=run_all_test):
+    if not run: return
+    if not quiet:
+      message = 'Test Business Template Uninstall With an used Skin Selection'
+      ZopeTestCase._print('\n%s ' % message)
+      LOG('Testing... ', 0, message)
+    sequence_list = SequenceList()
+    sequence_string = 'CreateSkinFolder \
+                       CreateStaticSkinFolder \
+                       CreateSkinSelection \
+                       SetSkinFolderRegistredSelections \
+                       SetStaticSkinFolderRegistredSelections \
+                       CreateNewBusinessTemplate \
+                       UseExportBusinessTemplate \
+                       AddSkinFolderToBusinessTemplate \
+                       BuildBusinessTemplate \
+                       SaveBusinessTemplate \
+                       RemoveSkinFolder \
+                       ImportBusinessTemplate \
+                       UseImportBusinessTemplate \
+                       InstallBusinessTemplate \
+                       Tic \
+                       CheckStaticSkinSelection \
+                       UninstallBusinessTemplate \
+                       CheckSkinSelectionNotRemoved \
+                       '
+    sequence_list.addSequenceString(sequence_string)
+    sequence_list.play(self, quiet=quiet)
+
   def test_159_BusinessTemplateNotRegisterSkin(self, quiet=quiet,
                                                         run=run_all_test):
     if not run: return
-- 
2.30.9