From e13a75d252984a0d1a4b07b4f220323311009148 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Mon, 31 Mar 2008 10:14:03 +0000
Subject: [PATCH] r20200 changed behaviour of relation to non existant objects.
 Restore the previous behaviour with a test to clarify the behaviour.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@20221 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Type/Base.py               |  4 ++-
 product/ERP5Type/tests/testERP5Type.py | 41 ++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/product/ERP5Type/Base.py b/product/ERP5Type/Base.py
index 5804a4a189..776698d656 100644
--- a/product/ERP5Type/Base.py
+++ b/product/ERP5Type/Base.py
@@ -2007,7 +2007,9 @@ class Base( CopyContainer,
     ref_list = []
     for path in self._getAcquiredCategoryMembershipList(id, base=1,
                                                 spec=spec,  filter=filter, **kw):
-      ref_list.append(self._getCategoryTool().resolveCategory(path))
+      category = self._getCategoryTool().resolveCategory(path)
+      if category is not None:
+        ref_list.append(category)
     return ref_list
 
   security.declareProtected(Permissions.AccessContentsInformation, 
diff --git a/product/ERP5Type/tests/testERP5Type.py b/product/ERP5Type/tests/testERP5Type.py
index f0dc3184e3..c9f73b0aa2 100644
--- a/product/ERP5Type/tests/testERP5Type.py
+++ b/product/ERP5Type/tests/testERP5Type.py
@@ -28,6 +28,7 @@
 
 import md5
 import unittest
+import sys
 
 from random import randint
 from Testing import ZopeTestCase
@@ -1823,6 +1824,46 @@ class TestPropertySheet:
       self.assertRaises(Unauthorized, foo.getRegionValue)
       self.assertRaises(Unauthorized, foo.getRegionTitle)
 
+
+    def test_category_accessor_to_non_existing_documents(self):
+      # tests behaviour of category accessors with relations to non existing
+      # documents.
+      region_category = self.getPortal().portal_categories.region
+      beta_id = "beta"
+      beta_title = "Beta System"
+      beta = region_category.newContent(
+              portal_type = "Category",
+              id =          beta_id,
+              title =       beta_title, )
+      beta_path = beta.getCategoryRelativeUrl()
+
+      # gamma does not exist
+
+      # Make sure categories are reindexed
+      get_transaction().commit()
+      self.tic()
+
+      # Create a new person, and associate it to beta and gamma.
+      module = self.getPersonModule()
+      foo = module.newContent(portal_type='Person', title='Foo')
+      foo.setRegionList(('beta', 'gamma'))
+
+      self.assertEquals([beta_path, 'gamma'],
+                        foo.getRegionList())
+      # using relations to non existant objects will issue a warning in
+      # event.log
+      self._catch_log_errors(ignored_level=sys.maxint)
+      self.assertEquals([beta],
+                        foo.getRegionValueList())
+      self.assertEquals([beta_title],
+                        foo.getRegionTitleList())
+      self._ignore_log_errors()
+      logged_errors = [ logrecord for logrecord in self.logged
+                        if logrecord[0] == 'CMFCategory' ]
+      self.assertEquals('Could not access object region/gamma',
+                        logged_errors[0][2])
+      
+
     def test_list_accessors(self):
       self._addProperty('Person', '''{'id': 'dummy',
                                       'type': 'lines',
-- 
2.30.9