testERP5Category.py 14 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
import transaction
31 32
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.Base import _aq_reset
33
from AccessControl.SecurityManagement import newSecurityManager
34

35
class TestERP5Category(ERP5TypeTestCase):
36 37 38

  # Different variables used for this test
  run_all_test = 1
39
  quiet = 1
40
  base_cat = 'abc'
41
  base_cat2 = 'efg'
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  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
57
    return ('erp5_base',)
58 59 60 61 62 63 64 65 66 67 68 69 70 71

  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()
72
    person_module = self.getPersonModule()
73
    bc = self.base_cat
74 75 76 77 78 79 80 81 82 83 84 85 86 87
    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']
88

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

Sebastien Robin's avatar
Sebastien Robin committed
93 94
    # Reset aq dynamic
    _aq_reset()
95

96
    organisation_module = self.getOrganisationModule()
97
    if not organisation_module.has_key('1'):
98
      organisation_module.newContent(id='1', portal_type='Organisation')
99 100 101 102 103
    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'):
104
      organisation_module.newContent(id='2', portal_type='Organisation')
105 106 107 108 109 110 111
    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']
112

113
    bc2 = self.base_cat2
114 115 116 117 118 119 120 121 122 123 124 125 126 127
    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']
128

129 130
    # We have no place to put a Predicate, we will put it in the
    # Organisation Module
131 132 133 134 135 136 137
    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']
138
    predicate.setCriterion('quantity', identity=None, min=None, max=None)
139 140
    self.predicate = predicate

141
    transaction.commit() # If we don't commit, then we can't rename
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    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()
160

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

167 168 169
  def commitAndTic(self):
    """Just to save one line.
    """
170
    transaction.commit()
171 172
    self.tic()

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

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

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

214 215
  def test_04_RenameObjectWithRelatedSubObject(
                            self, quiet=quiet, run=run_all_test):
216 217 218 219 220 221 222
    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])
223
    self.commitAndTic()
224
    self.assertEquals(organisation.getAbcValueList(),[telephone2])
225
    self.assertEquals(organisation.getAbcList(),[telephone2.getRelativeUrl()])
226
    organisation2.edit(id='new_id')
227
    self.commitAndTic()
228
    self.assertEquals(organisation.getAbcValueList(),[telephone2])
229
    self.assertEquals(organisation.getAbcList(),[telephone2.getRelativeUrl()])
230

231 232
  def test_05_RenameMembershipCriterionCategory(
                            self, quiet=quiet, run=run_all_test):
233 234 235 236 237 238
    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)
239
    self.commitAndTic()
240 241 242
    self.failIfDifferentSet(predicate.getMembershipCriterionCategoryList(),self.cat_list)
    portal_categories = self.getCategoryTool()
    portal_categories[self.base_cat]['1'].edit(id='3')
243
    self.commitAndTic()
244 245
    self.failIfDifferentSet(predicate.getMembershipCriterionCategoryList(),self.new_cat_list)

246 247
  def test_06_RenameModuleWithObjectOuterRelated(
                                self, quiet=quiet, run=run_all_test):
248 249 250 251 252 253 254
    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)
255
    self.commitAndTic()
256
    self.assertEquals(person.getSubordinationValue(),organisation)
257
    organisation_module.edit(id='new_id')
258
    self.commitAndTic()
259
    self.assertEquals(person.getSubordinationValue(),organisation)
260

261 262
  def test_07_RenameBaseCategoryWithPersonRelatedToSubSubSubCategory(
                                  self, quiet=quiet, run=run_all_test):
263 264 265 266 267
    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])
268
    self.commitAndTic()
Alexandre Boeglin's avatar
Alexandre Boeglin committed
269
    self.failIfDifferentSet(o.getEfgList(base=1),[self.base_cat2+'/1/11/111/1111'])
270
    self.efg_l1.edit(id='new_id')
271
    self.commitAndTic()
Alexandre Boeglin's avatar
Alexandre Boeglin committed
272
    self.failIfDifferentSet(o.getEfgList(base=1),[self.base_cat2+'/new_id/11/111/1111'])
273

274 275
  def test_08_RenameModuleWithObjectsInnerRelated(
                        self, quiet=quiet, run=run_all_test):
276 277 278 279 280
    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'])
281
    self.commitAndTic()
282 283 284 285 286
    self.assertEquals(len(om['2'].getRelatedValueList('abc')), 1)
    self.assertEquals(len(om['2'].Base_zSearchRelatedObjectsByCategory(category_uid = om['2'].getUid())),1)    
    self.assertEquals(om['1'].getAbc(),om['2'].getRelativeUrl())
    original_uid = om['2'].getUid()
    om.edit(id='new_id')
287
    self.commitAndTic()
288 289
    om = self.getPortal()['new_id']
    self.assertEquals(original_uid, om['2'].getUid())
290
    self.assertEquals(om['1'].getAbc(),om['2'].getRelativeUrl())
291 292
    self.assertEquals(len(om['2'].getRelatedValueList('abc')), 1)
    self.assertEquals(len(om['2'].Base_zSearchRelatedObjectsByCategory(category_uid = om['2'].getUid())),1)
293

294 295
  def test_09_Base_viewDictWithCategoryWithSubCategory(
                        self, quiet=quiet, run=run_all_test):
296 297 298 299 300 301 302 303 304
    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)

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

  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
    self.assertEquals(['parent'], test_aq_category.getAcquisitionBaseCategoryList())
    # only if portal type of the current document and his parent are in
    # acquisition portal type
    self.assertEquals(['Organisation', 'Telephone'],
                      test_aq_category.getAcquisitionPortalTypeList())
    
    # associate the base category with our portal types
    ttool = self.getTypesTool()
323 324
    ttool['Organisation']._setTypeBaseCategoryList(['test_aq_category'])
    ttool['Telephone']._setTypeBaseCategoryList(['test_aq_category'])
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

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

    doc.setCategoryList(['test_aq_category/1'])
    self.assertEquals(['test_aq_category/1'], ctool.getAcquiredCategoryList(doc))
    self.assertEquals(['test_aq_category/1'], doc.getAcquiredCategoryList())
    
    # Telephone subdocument acquire categories, because 'test_aq_category' has
    # 'parent' in its acquisition_base_category_list
    self.assertEquals([], subdoc.getCategoryList())
    self.assertEquals(['test_aq_category/1'], subdoc.getAcquiredCategoryList())
    
    doc.setCategoryList([])
    self.assertEquals([], ctool.getAcquiredCategoryList(doc))

    # XXX this test's beforeTearDown commits transaction
342
    transaction.abort()
343 344


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