testERP5Category.py 13.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
##############################################################################
#
# Copyright (c) 2004 Nexedi SARL and Contributors. All Rights Reserved.
#          Sebastien Robin <seb@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

29
import unittest
30 31
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.Base import _aq_reset
32
from AccessControl.SecurityManagement import newSecurityManager
33

34
class TestERP5Category(ERP5TypeTestCase):
35 36 37

  # Different variables used for this test
  run_all_test = 1
38
  quiet = 1
39
  base_cat = 'abc'
40
  base_cat2 = 'efg'
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  cat_list = [base_cat + '/1',base_cat + '/2']
  new_cat_list = [base_cat + '/3',base_cat + '/2']
  deep_cat_list = [base_cat + '/1/1',base_cat + '/2/1']
  new_deep_cat_list = [base_cat + '/3/1',base_cat + '/2/1']

  def getTitle(self):
    """
    """
    return "ERP5 Categories"

  def getBusinessTemplateList(self):
    """
      Return the list of business templates.

    """
Sebastien Robin's avatar
Sebastien Robin committed
56
    return ('erp5_base',)
57 58 59 60 61 62 63 64 65 66 67 68 69 70

  def getPortalId(self):
    return self.getPortal().getId()

  def getOrderLine(self):
    return self.getSaleOrderModule()['1']['1']

  def getPredicate(self):
    return self.getSalePackingListModule()['1']

  def afterSetUp(self):
    self.login()
    # This add the base category size
    portal_categories = self.getCategoryTool()
71
    person_module = self.getPersonModule()
72
    bc = self.base_cat
73 74 75 76 77 78 79 80 81 82 83 84 85 86
    if bc not in portal_categories.objectIds():
      portal_categories.newContent(portal_type='Base Category', id=bc)
    if not portal_categories[bc].has_key('1'):
      portal_categories[bc].newContent(id='1', portal_type='Category')
    self.cat1 = portal_categories[bc]['1']
    if not self.cat1.has_key('1'):
      self.cat1.newContent(id='1', portal_type='Category')
    self.deep_cat1 = self.cat1['1']
    if not portal_categories[bc].has_key('2'):
      portal_categories[bc].newContent(id='2', portal_type='Category')
    self.cat2 = portal_categories[bc]['2']
    if not self.cat2.has_key('1'):
      self.cat2.newContent(id='1', portal_type='Category')
    self.deep_cat2 = self.cat2['1']
87

88
    # associate base categories on Organisation portal type
89 90
    type_info = self.getTypeTool().Organisation
    type_info._setTypeBaseCategoryList([self.base_cat, self.base_cat2])
91

92
    self.commit()
93

94
    organisation_module = self.getOrganisationModule()
95
    if not organisation_module.has_key('1'):
96
      organisation_module.newContent(id='1', portal_type='Organisation')
97 98 99 100 101
    self.organisation = organisation_module['1']
    if not self.organisation.has_key('1'):
      self.organisation.newContent(id='1', portal_type='Telephone')
    self.telephone = self.organisation['1']
    if not organisation_module.has_key('2'):
102
      organisation_module.newContent(id='2', portal_type='Organisation')
103 104 105 106 107 108 109
    self.organisation2 = organisation_module['2']
    if not self.organisation2.has_key('1'):
      self.organisation2.newContent(id='1', portal_type='Telephone')
    self.telephone2 = self.organisation2['1']
    if not person_module.has_key('1'):
      person_module.newContent(id='1', portal_type = 'Person')
    self.person = person_module['1']
110

111
    bc2 = self.base_cat2
112 113 114 115 116 117 118 119 120 121 122 123 124 125
    if bc2 not in portal_categories.objectIds():
      portal_categories.newContent(portal_type='Base Category', id=bc2)
    if not portal_categories[bc2].has_key('1'):
      portal_categories[bc2].newContent(id='1', portal_type='Category')
    self.efg_l1 = portal_categories[bc2]['1']
    if not self.efg_l1.has_key('11'):
      self.efg_l1.newContent(id='11', portal_type='Category')
    self.efg_l2 = self.efg_l1['11']
    if not self.efg_l2.has_key('111'):
      self.efg_l2.newContent(id='111', portal_type='Category')
    self.efg_l3 = self.efg_l2['111']
    if not self.efg_l3.has_key('1111'):
      self.efg_l3.newContent(id='1111',portal_type='Category')
    self.efg_l4 = self.efg_l3['1111']
126

127 128
    # We have no place to put a Predicate, we will put it in the
    # Organisation Module
129 130 131 132 133 134 135
    module_type = organisation_module.getTypeInfo()
    content_type_set = set(module_type.getTypeAllowedContentTypeList())
    content_type_set.add('Mapped Value')
    module_type._setTypeAllowedContentTypeList(tuple(content_type_set))
    if not organisation_module.has_key('predicate'):
      organisation_module.newContent(id='predicate', portal_type='Mapped Value')
    predicate = organisation_module['predicate']
136
    predicate.setCriterion('quantity', identity=None, min=None, max=None)
137 138
    self.predicate = predicate

139
    self.commit()# If we don't commit, then we can't rename
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    self.tic()

  def beforeTearDown(self):
    portal_categories = self.getCategoryTool()
    if portal_categories[self.base_cat].has_key('3'):
      portal_categories[self.base_cat].manage_delObjects('3')
      self.commitAndTic()

    portal = self.getPortal()
    if 'new_id' in portal.objectIds():
      portal['new_id'].edit(id='organisation_module')
      self.commitAndTic()

    organisation_module = self.getOrganisationModule()
    if organisation_module.has_key('new_id'):
      organisation_module.manage_delObjects('new_id')

    self.commitAndTic()
158

159
  def login(self):
160 161 162 163 164
    uf = self.getPortal().acl_users
    uf._doAddUser('seb', '', ['Manager'], [])
    user = uf.getUserById('seb').__of__(uf)
    newSecurityManager(None, user)

165 166 167 168 169
  def commitAndTic(self):
    """Just to save one line.
    """
    self.tic()

170
  def test_01_RenameCategory(self, quiet=quiet, run=run_all_test):
171 172 173 174 175
    if not run: return
    if not quiet:
      self.logMessage('Rename Category')
    organisation = self.organisation
    organisation.setCategoryList(self.cat_list)
176
    self.commitAndTic()
177 178 179
    self.failIfDifferentSet(organisation.getCategoryList(),self.cat_list)
    portal_categories = self.getCategoryTool()
    portal_categories[self.base_cat]['1'].edit(id='3')
180
    self.commitAndTic()
181 182
    self.failIfDifferentSet(organisation.getCategoryList(),self.new_cat_list)

183
  def test_02_RenameCategoryTree(self, quiet=quiet, run=run_all_test):
184 185 186 187 188
    if not run: return
    if not quiet:
      self.logMessage('Rename Category Tree')
    organisation = self.organisation
    organisation.setCategoryList(self.deep_cat_list)
189
    self.commitAndTic()
190 191 192
    self.failIfDifferentSet(organisation.getCategoryList(),self.deep_cat_list)
    portal_categories = self.getCategoryTool()
    portal_categories[self.base_cat]['1'].edit(id='3')
193
    self.commitAndTic()
194 195
    self.failIfDifferentSet(organisation.getCategoryList(),self.new_deep_cat_list)

196
  def test_03_RenameRelatedObject(self, quiet=quiet, run=run_all_test):
197 198 199 200 201 202
    if not run: return
    if not quiet:
      self.logMessage('Rename Related Object')
    organisation = self.organisation
    organisation2 = self.organisation2
    organisation.setAbcValueList([organisation2])
203
    self.commitAndTic()
204 205
    self.assertEqual(organisation.getAbcValueList(),[organisation2])
    self.assertEqual(organisation.getAbcIdList(),['2'])
206
    organisation2.edit(id='new_id')
207
    self.commitAndTic()
208 209
    self.assertEqual(organisation.getAbcValueList(),[organisation2])
    self.assertEqual(organisation.getAbcIdList(),['new_id'])
210

211 212
  def test_04_RenameObjectWithRelatedSubObject(
                            self, quiet=quiet, run=run_all_test):
213 214 215 216 217 218 219
    if not run: return
    if not quiet:
      self.logMessage('Rename Object With a related Sub Object')
    telephone2 = self.telephone2
    organisation = self.organisation
    organisation2 = self.organisation2
    organisation.setAbcValueList([telephone2])
220
    self.commitAndTic()
221 222
    self.assertEqual(organisation.getAbcValueList(),[telephone2])
    self.assertEqual(organisation.getAbcList(),[telephone2.getRelativeUrl()])
223
    organisation2.edit(id='new_id')
224
    self.commitAndTic()
225 226
    self.assertEqual(organisation.getAbcValueList(),[telephone2])
    self.assertEqual(organisation.getAbcList(),[telephone2.getRelativeUrl()])
227

228 229
  def test_05_RenameMembershipCriterionCategory(
                            self, quiet=quiet, run=run_all_test):
230 231 232 233 234 235
    if not run: return
    if not quiet:
      self.logMessage('Rename Membership Criterion Category')
    predicate = self.predicate
    predicate.setMembershipCriterionBaseCategoryList(self.base_cat)
    predicate.setMembershipCriterionCategoryList(self.cat_list)
236
    self.commitAndTic()
237 238 239
    self.failIfDifferentSet(predicate.getMembershipCriterionCategoryList(),self.cat_list)
    portal_categories = self.getCategoryTool()
    portal_categories[self.base_cat]['1'].edit(id='3')
240
    self.commitAndTic()
241 242
    self.failIfDifferentSet(predicate.getMembershipCriterionCategoryList(),self.new_cat_list)

243 244
  def test_06_RenameModuleWithObjectOuterRelated(
                                self, quiet=quiet, run=run_all_test):
245 246 247 248 249 250 251
    if not run: return
    if not quiet:
      self.logMessage('Rename Module With an Object Related to an Object it Contains')
    organisation_module = self.getOrganisationModule()
    organisation = self.organisation
    person = self.person
    person.setSubordinationValue(organisation)
252
    self.commitAndTic()
253
    self.assertEqual(person.getSubordinationValue(),organisation)
254
    organisation_module.edit(id='new_id')
255
    self.commitAndTic()
256
    self.assertEqual(person.getSubordinationValue(),organisation)
257

258 259
  def test_07_RenameBaseCategoryWithPersonRelatedToSubSubSubCategory(
                                  self, quiet=quiet, run=run_all_test):
260 261 262 263 264
    if not run: return
    if not quiet:
      self.logMessage('Rename a Base Category with a Person Related to a Sub-Sub-Sub-Category')
    o = self.organisation
    o.setEfgValueList([self.efg_l4])
265
    self.commitAndTic()
Alexandre Boeglin's avatar
Alexandre Boeglin committed
266
    self.failIfDifferentSet(o.getEfgList(base=1),[self.base_cat2+'/1/11/111/1111'])
267
    self.efg_l1.edit(id='new_id')
268
    self.commitAndTic()
Alexandre Boeglin's avatar
Alexandre Boeglin committed
269
    self.failIfDifferentSet(o.getEfgList(base=1),[self.base_cat2+'/new_id/11/111/1111'])
270

271 272
  def test_08_RenameModuleWithObjectsInnerRelated(
                        self, quiet=quiet, run=run_all_test):
273 274 275 276 277
    if not run: return
    if not quiet:
      self.logMessage('Rename a Module with Contained Objects Refering to Other Objects inside the Same Module')
    om = self.getOrganisationModule()
    om['1'].setAbcValue(om['2'])
278
    self.commitAndTic()
279 280 281
    self.assertEqual(len(om['2'].getRelatedValueList('abc')), 1)
    self.assertEqual(len(om['2'].Base_zSearchRelatedObjectsByCategory(category_uid = om['2'].getUid())),1)    
    self.assertEqual(om['1'].getAbc(),om['2'].getRelativeUrl())
282 283
    original_uid = om['2'].getUid()
    om.edit(id='new_id')
284
    self.commitAndTic()
285
    om = self.getPortal()['new_id']
286 287 288 289
    self.assertEqual(original_uid, om['2'].getUid())
    self.assertEqual(om['1'].getAbc(),om['2'].getRelativeUrl())
    self.assertEqual(len(om['2'].getRelatedValueList('abc')), 1)
    self.assertEqual(len(om['2'].Base_zSearchRelatedObjectsByCategory(category_uid = om['2'].getUid())),1)
290

291 292
  def test_09_Base_viewDictWithCategoryWithSubCategory(
                        self, quiet=quiet, run=run_all_test):
293 294 295 296 297 298 299 300 301
    if not run: return
    if not quiet:
      self.logMessage('Make sure Base_viewDict is working for categories with sub categories')
    portal_categories = self.getCategoryTool()
    base_category = portal_categories.activity
    self.assertTrue(len(base_category.Base_viewDict())>0)
    base_category.newContent(id='toto',title='Toto')
    self.assertTrue(len(base_category.Base_viewDict())>0)

302 303 304 305 306 307 308 309 310 311

  def test_getAcquiredCategoryList(self):
    # create a base category
    ctool = self.getCategoryTool()
    test_aq_category = ctool.newContent(id='test_aq_category',
        portal_type='Base Category',
        acquisition_base_category_list=['parent'],
        acquisition_portal_type="python:['Organisation', 'Telephone']",)
    test_aq_category.newContent(portal_type='Category', id='1')
    # this category will acquire from parent category
312
    self.assertEqual(['parent'], test_aq_category.getAcquisitionBaseCategoryList())
313 314
    # only if portal type of the current document and his parent are in
    # acquisition portal type
315
    self.assertEqual(['Organisation', 'Telephone'],
316 317 318 319
                      test_aq_category.getAcquisitionPortalTypeList())
    
    # associate the base category with our portal types
    ttool = self.getTypesTool()
320 321
    ttool['Organisation']._setTypeBaseCategoryList(['test_aq_category'])
    ttool['Telephone']._setTypeBaseCategoryList(['test_aq_category'])
322
    self.commit()
323 324 325 326 327

    doc = self.organisation
    subdoc = doc['1']

    doc.setCategoryList(['test_aq_category/1'])
328 329
    self.assertEqual(['test_aq_category/1'], ctool.getAcquiredCategoryList(doc))
    self.assertEqual(['test_aq_category/1'], doc.getAcquiredCategoryList())
330 331 332
    
    # Telephone subdocument acquire categories, because 'test_aq_category' has
    # 'parent' in its acquisition_base_category_list
333 334
    self.assertEqual([], subdoc.getCategoryList())
    self.assertEqual(['test_aq_category/1'], subdoc.getAcquiredCategoryList())
335 336
    
    doc.setCategoryList([])
337
    self.assertEqual([], ctool.getAcquiredCategoryList(doc))
338 339

    # XXX this test's beforeTearDown commits transaction
340
    self.abort()
341 342


343 344 345 346
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestERP5Category))
  return suite
347