testBase.py 43 KB
Newer Older
Romain Courteaud's avatar
Romain Courteaud committed
1 2
##############################################################################
#
3 4
# Copyright (c) 2004, 2005, 2006 Nexedi SARL and Contributors. 
# All Rights Reserved.
Romain Courteaud's avatar
Romain Courteaud committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#          Romain Courteaud <romain@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.
#
##############################################################################

30
import unittest
31
import os
Romain Courteaud's avatar
Romain Courteaud committed
32

33
from Testing import ZopeTestCase
Romain Courteaud's avatar
Romain Courteaud committed
34
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
Jérome Perrin's avatar
Jérome Perrin committed
35 36
from AccessControl.SecurityManagement import newSecurityManager
from Products.ERP5Type.tests.Sequence import SequenceList
37
from zExceptions import BadRequest
38
from Products.ERP5Type.Tool.ClassTool import _aq_reset
Romain Courteaud's avatar
Romain Courteaud committed
39

40
class TestBase(ERP5TypeTestCase, ZopeTestCase.Functional):
Romain Courteaud's avatar
Romain Courteaud committed
41

Romain Courteaud's avatar
Romain Courteaud committed
42
  run_all_test = 1
Jérome Perrin's avatar
Jérome Perrin committed
43 44
  quiet = 1

Romain Courteaud's avatar
Romain Courteaud committed
45
  object_portal_type = "Organisation"
46 47
  not_defined_property_id = "azerty_qwerty"
  not_defined_property_value = "qwerty_azerty"
Romain Courteaud's avatar
Romain Courteaud committed
48

49 50 51
  temp_class = "Amount"
  defined_property_id = "title"
  defined_property_value = "a_wonderful_title"
52
  not_related_to_temp_object_property_id = "string_index"
53
  not_related_to_temp_object_property_value = "a_great_index"
54 55
  
  username = 'rc'
56

Romain Courteaud's avatar
Romain Courteaud committed
57 58 59 60 61 62
  def getTitle(self):
    return "Base"

  def getBusinessTemplateList(self):
    """
    """
Sebastien Robin's avatar
Sebastien Robin committed
63
    return ('erp5_base',)
Romain Courteaud's avatar
Romain Courteaud committed
64

65
  def login(self):
Romain Courteaud's avatar
Romain Courteaud committed
66
    uf = self.getPortal().acl_users
67 68
    uf._doAddUser(self.username, '', ['Manager'], [])
    user = uf.getUserById(self.username).__of__(uf)
Romain Courteaud's avatar
Romain Courteaud committed
69 70
    newSecurityManager(None, user)

71
  def afterSetUp(self):
Romain Courteaud's avatar
Romain Courteaud committed
72 73 74 75 76 77 78
    self.login()
    portal = self.getPortal()
    self.category_tool = self.getCategoryTool()
    portal_catalog = self.getCatalogTool()
    #portal_catalog.manage_catalogClear()
    self.createCategories()

79 80 81 82 83 84 85 86 87 88 89 90 91
    #Overwrite immediateReindexObject() with a crashing method
    def crashingMethod(self):
      self.ImmediateReindexObjectIsCalled()
    from Products.ERP5Type.Document.Organisation import Organisation
    Organisation.immediateReindexObject = crashingMethod
    Organisation.recursiveImmediateReindexObject = crashingMethod

  def beforeTearDown(self):
    # Remove crashing method
    from Products.ERP5Type.Document.Organisation import Organisation
    del Organisation.immediateReindexObject
    del Organisation.recursiveImmediateReindexObject

Romain Courteaud's avatar
Romain Courteaud committed
92 93 94 95 96
  def createCategories(self):
    """ 
      Light install create only base categories, so we create 
      some categories for testing them
    """
Romain Courteaud's avatar
Romain Courteaud committed
97
    category_list = ['testGroup1', 'testGroup2']
98
    if 'testGroup1' not in self.category_tool.group.contentIds():
Romain Courteaud's avatar
Romain Courteaud committed
99 100 101
      for category_id in category_list:
        o = self.category_tool.group.newContent(portal_type='Category',
                                                id=category_id)
Romain Courteaud's avatar
Romain Courteaud committed
102 103 104 105

  def stepTic(self,**kw):
    self.tic()

Romain Courteaud's avatar
Romain Courteaud committed
106 107
  def stepRemoveWorkflowsRelated(self, sequence=None, sequence_list=None, 
                                 **kw):
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    """
      Remove workflow related to the portal type
    """
    self.getWorkflowTool().setChainForPortalTypes(
        ['Organisation'], ())
    _aq_reset()

  def stepAssociateWorkflows(self, sequence=None, sequence_list=None, **kw):
    """
      Associate workflow to the portal type
    """
    self.getWorkflowTool().setChainForPortalTypes(
        ['Organisation'], ('validation_workflow', 'edit_workflow'))
    _aq_reset()

Romain Courteaud's avatar
Romain Courteaud committed
123 124
  def stepAssociateWorkflowsExcludingEdit(self, sequence=None, 
                                          sequence_list=None, **kw):
125 126 127 128 129 130 131
    """
      Associate workflow to the portal type
    """
    self.getWorkflowTool().setChainForPortalTypes(
        ['Organisation'], ('validation_workflow',))
    _aq_reset()

Romain Courteaud's avatar
Romain Courteaud committed
132 133
  def stepCreateObject(self, sequence=None, sequence_list=None, **kw):
    """
Romain Courteaud's avatar
Romain Courteaud committed
134
      Create a object_instance which will be tested.
Romain Courteaud's avatar
Romain Courteaud committed
135 136 137
    """
    portal = self.getPortal()
    module = portal.getDefaultModule(self.object_portal_type)
Romain Courteaud's avatar
Romain Courteaud committed
138
    object_instance = module.newContent(portal_type=self.object_portal_type)
Romain Courteaud's avatar
Romain Courteaud committed
139
    sequence.edit(
Romain Courteaud's avatar
Romain Courteaud committed
140
        object_instance=object_instance,
141
        current_title=object_instance.getId(), # title defaults to id
Romain Courteaud's avatar
Romain Courteaud committed
142
        current_group_value=None
Romain Courteaud's avatar
Romain Courteaud committed
143 144 145 146 147 148
    )

  def stepCheckTitleValue(self, sequence=None, sequence_list=None, **kw):
    """
      Check if getTitle return a correect value
    """
Romain Courteaud's avatar
Romain Courteaud committed
149
    object_instance = sequence.get('object_instance')
Romain Courteaud's avatar
Romain Courteaud committed
150
    current_title = sequence.get('current_title')
Romain Courteaud's avatar
Romain Courteaud committed
151
    self.assertEquals(object_instance.getTitle(), current_title)
Romain Courteaud's avatar
Romain Courteaud committed
152

Romain Courteaud's avatar
Romain Courteaud committed
153 154
  def stepSetDifferentTitleValueWithEdit(self, sequence=None, 
                                         sequence_list=None, **kw):
Romain Courteaud's avatar
Romain Courteaud committed
155 156 157
    """
      Set a different title value
    """
Romain Courteaud's avatar
Romain Courteaud committed
158
    object_instance = sequence.get('object_instance')
Romain Courteaud's avatar
Romain Courteaud committed
159 160
    current_title = sequence.get('current_title')
    new_title_value = '%s_a' % current_title
Romain Courteaud's avatar
Romain Courteaud committed
161
    object_instance.edit(title=new_title_value)
Romain Courteaud's avatar
Romain Courteaud committed
162 163 164 165 166 167 168 169 170 171 172 173
    sequence.edit(
        current_title=new_title_value
    )

  def stepCheckIfActivitiesAreCreated(self, sequence=None, sequence_list=None,
                                      **kw):
    """
      Check if there is a activity in activity queue.
    """
    portal = self.getPortal()
    get_transaction().commit()
    message_list = portal.portal_activities.getMessageList()
174 175 176 177 178 179 180 181
    method_id_list = [x.method_id for x in message_list]
    # XXX FIXME: how many activities should be created normally ?
    # Sometimes it's one, sometimes 2...
    self.failUnless(len(message_list) > 0)
    self.failUnless(len(message_list) < 3)
    for method_id in method_id_list:
      self.failUnless(method_id in ["immediateReindexObject", 
                                    "recursiveImmediateReindexObject"])
Romain Courteaud's avatar
Romain Courteaud committed
182

Romain Courteaud's avatar
Romain Courteaud committed
183 184
  def stepSetSameTitleValueWithEdit(self, sequence=None, sequence_list=None, 
                                    **kw):
Romain Courteaud's avatar
Romain Courteaud committed
185 186 187
    """
      Set a different title value
    """
Romain Courteaud's avatar
Romain Courteaud committed
188 189
    object_instance = sequence.get('object_instance')
    object_instance.edit(title=object_instance.getTitle())
Romain Courteaud's avatar
Romain Courteaud committed
190 191 192 193 194 195 196 197 198 199

  def stepCheckIfMessageQueueIsEmpty(self, sequence=None, 
                                     sequence_list=None, **kw):
    """
      Check if there is no activity in activity queue.
    """
    portal = self.getPortal()
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list), 0)

Jérome Perrin's avatar
Jérome Perrin committed
200
  def test_01_areActivitiesWellLaunchedByPropertyEdit(self, quiet=quiet,
Romain Courteaud's avatar
Romain Courteaud committed
201
                                                      run=run_all_test):
Romain Courteaud's avatar
Romain Courteaud committed
202
    """
203 204
      Test if setter does not call a activity if the attribute 
      value is not changed.
Romain Courteaud's avatar
Romain Courteaud committed
205 206 207
    """
    if not run: return
    sequence_list = SequenceList()
208 209 210 211
    # Test without workflows associated to the portal type
    sequence_string = '\
              RemoveWorkflowsRelated \
              CreateObject \
Romain Courteaud's avatar
Romain Courteaud committed
212
              Tic \
213
              CheckTitleValue \
Romain Courteaud's avatar
Romain Courteaud committed
214
              SetDifferentTitleValueWithEdit \
215 216 217 218
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
Romain Courteaud's avatar
Romain Courteaud committed
219
              SetSameTitleValueWithEdit \
220
              CheckTitleValue \
221
              CheckIfMessageQueueIsEmpty \
Romain Courteaud's avatar
Romain Courteaud committed
222
              SetDifferentTitleValueWithEdit \
223 224 225 226 227 228 229 230 231 232
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              '
    sequence_list.addSequenceString(sequence_string)
    # Test with workflows associated to the portal type
    sequence_string = '\
              AssociateWorkflows \
              CreateObject \
Romain Courteaud's avatar
Romain Courteaud committed
233
              Tic \
Romain Courteaud's avatar
Romain Courteaud committed
234
              CheckTitleValue \
Romain Courteaud's avatar
Romain Courteaud committed
235
              SetDifferentTitleValueWithEdit \
Romain Courteaud's avatar
Romain Courteaud committed
236 237 238 239
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
Romain Courteaud's avatar
Romain Courteaud committed
240
              SetSameTitleValueWithEdit \
241 242 243
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
Romain Courteaud's avatar
Romain Courteaud committed
244
              CheckIfMessageQueueIsEmpty \
Romain Courteaud's avatar
Romain Courteaud committed
245
              SetDifferentTitleValueWithEdit \
Romain Courteaud's avatar
Romain Courteaud committed
246 247 248 249 250 251
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              '
    sequence_list.addSequenceString(sequence_string)
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    # Test with workflows associated to the portal type, excluding edit_workflow
    sequence_string = '\
              AssociateWorkflowsExcludingEdit \
              CreateObject \
              Tic \
              CheckTitleValue \
              SetDifferentTitleValueWithEdit \
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              SetSameTitleValueWithEdit \
              CheckIfMessageQueueIsEmpty \
              CheckTitleValue \
              SetDifferentTitleValueWithEdit \
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              '
    sequence_list.addSequenceString(sequence_string)
Jérome Perrin's avatar
Jérome Perrin committed
273
    sequence_list.play(self, quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
274

Romain Courteaud's avatar
Romain Courteaud committed
275 276 277 278
  def stepCheckGroupValue(self, sequence=None, sequence_list=None, **kw):
    """
      Check if getTitle return a correect value
    """
Romain Courteaud's avatar
Romain Courteaud committed
279
    object_instance = sequence.get('object_instance')
Romain Courteaud's avatar
Romain Courteaud committed
280
    current_group_value = sequence.get('current_group_value')
Romain Courteaud's avatar
Romain Courteaud committed
281
    self.assertEquals(object_instance.getGroupValue(), current_group_value)
Romain Courteaud's avatar
Romain Courteaud committed
282 283 284 285 286 287

  def stepSetDifferentGroupValueWithEdit(self, sequence=None, 
                                         sequence_list=None, **kw):
    """
      Set a different title value
    """
Romain Courteaud's avatar
Romain Courteaud committed
288
    object_instance = sequence.get('object_instance')
Romain Courteaud's avatar
Romain Courteaud committed
289
    current_group_value = sequence.get('current_group_value')
Romain Courteaud's avatar
Romain Courteaud committed
290 291 292 293
    group1 = object_instance.portal_categories.\
                       restrictedTraverse('group/testGroup1')
    group2 = object_instance.portal_categories.\
                       restrictedTraverse('group/testGroup2')
Romain Courteaud's avatar
Romain Courteaud committed
294 295 296 297 298 299
    if (current_group_value is None) or \
       (current_group_value == group2) :
      new_group_value = group1
    else:
      new_group_value = group2
#     new_group_value = '%s_a' % current_title
Romain Courteaud's avatar
Romain Courteaud committed
300
    object_instance.edit(group_value=new_group_value)
Romain Courteaud's avatar
Romain Courteaud committed
301 302 303 304 305 306 307 308 309
    sequence.edit(
        current_group_value=new_group_value
    )

  def stepSetSameGroupValueWithEdit(self, sequence=None, sequence_list=None, 
                                    **kw):
    """
      Set a different title value
    """
Romain Courteaud's avatar
Romain Courteaud committed
310 311
    object_instance = sequence.get('object_instance')
    object_instance.edit(group_value=object_instance.getGroupValue())
Romain Courteaud's avatar
Romain Courteaud committed
312 313


Jérome Perrin's avatar
Jérome Perrin committed
314
  def test_02_areActivitiesWellLaunchedByCategoryEdit(self, quiet=quiet,
Romain Courteaud's avatar
Romain Courteaud committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
                                                      run=run_all_test):
    """
      Test if setter does not call a activity if the attribute 
      value is not changed.
    """
    if not run: return
    sequence_list = SequenceList()
    # Test without workflows associated to the portal type
    sequence_string = '\
              RemoveWorkflowsRelated \
              CreateObject \
              Tic \
              CheckGroupValue \
              SetDifferentGroupValueWithEdit \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              SetSameGroupValueWithEdit \
              CheckIfMessageQueueIsEmpty \
              SetDifferentGroupValueWithEdit \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              '
    sequence_list.addSequenceString(sequence_string)
    # Test with workflows associated to the portal type
    sequence_string = '\
              AssociateWorkflows \
              CreateObject \
              Tic \
              CheckGroupValue \
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
              SetDifferentGroupValueWithEdit \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              SetSameGroupValueWithEdit \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              SetDifferentGroupValueWithEdit \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              '
    sequence_list.addSequenceString(sequence_string)
    # Test with workflows associated to the portal type, excluding edit_workflow
    sequence_string = '\
              AssociateWorkflowsExcludingEdit \
              CreateObject \
              Tic \
              CheckGroupValue \
Romain Courteaud's avatar
Romain Courteaud committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384
              SetDifferentGroupValueWithEdit \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              SetSameGroupValueWithEdit \
              CheckIfMessageQueueIsEmpty \
              SetDifferentGroupValueWithEdit \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              '
    sequence_list.addSequenceString(sequence_string)
Jérome Perrin's avatar
Jérome Perrin committed
385
    sequence_list.play(self, quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
386 387 388 389 390 391

  def stepSetDifferentTitleValueWithSetter(self, sequence=None, 
                                           sequence_list=None, **kw):
    """
      Set a different title value
    """
Romain Courteaud's avatar
Romain Courteaud committed
392
    object_instance = sequence.get('object_instance')
Romain Courteaud's avatar
Romain Courteaud committed
393 394
    current_title = sequence.get('current_title')
    new_title_value = '%s_a' % current_title
Romain Courteaud's avatar
Romain Courteaud committed
395
    object_instance.setTitle(new_title_value)
Romain Courteaud's avatar
Romain Courteaud committed
396 397 398 399 400 401 402 403 404
    sequence.edit(
        current_title=new_title_value
    )

  def stepSetSameTitleValueWithSetter(self, sequence=None, 
                                      sequence_list=None, **kw):
    """
      Set a different title value
    """
Romain Courteaud's avatar
Romain Courteaud committed
405 406
    object_instance = sequence.get('object_instance')
    object_instance.setTitle(object_instance.getTitle())
Romain Courteaud's avatar
Romain Courteaud committed
407

Jérome Perrin's avatar
Jérome Perrin committed
408
  def test_03_areActivitiesWellLaunchedByPropertySetter(self, quiet=quiet,
Romain Courteaud's avatar
Romain Courteaud committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
                                                        run=run_all_test):
    """
      Test if setter does not call a activity if the attribute 
      value is not changed.
    """
    if not run: return
    sequence_list = SequenceList()
    # Test without workflows associated to the portal type
    sequence_string = '\
              RemoveWorkflowsRelated \
              CreateObject \
              Tic \
              CheckTitleValue \
              SetDifferentTitleValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              SetSameTitleValueWithSetter \
428 429 430
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
Romain Courteaud's avatar
Romain Courteaud committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
              CheckIfMessageQueueIsEmpty \
              SetDifferentTitleValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              '
    sequence_list.addSequenceString(sequence_string)
    # Test with workflows associated to the portal type
    sequence_string = '\
              AssociateWorkflows \
              CreateObject \
              Tic \
              CheckTitleValue \
              SetDifferentTitleValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              SetSameTitleValueWithSetter \
451 452 453
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
Romain Courteaud's avatar
Romain Courteaud committed
454 455 456 457 458 459 460 461
              CheckIfMessageQueueIsEmpty \
              SetDifferentTitleValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              '
    sequence_list.addSequenceString(sequence_string)
Jérome Perrin's avatar
Jérome Perrin committed
462
    sequence_list.play(self, quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
463 464 465 466 467 468

  def stepSetDifferentGroupValueWithSetter(self, sequence=None, 
                                           sequence_list=None, **kw):
    """
      Set a different title value
    """
Romain Courteaud's avatar
Romain Courteaud committed
469
    object_instance = sequence.get('object_instance')
Romain Courteaud's avatar
Romain Courteaud committed
470
    current_group_value = sequence.get('current_group_value')
Romain Courteaud's avatar
Romain Courteaud committed
471 472 473 474
    group1 = object_instance.portal_categories.\
                                   restrictedTraverse('group/testGroup1')
    group2 = object_instance.portal_categories.\
                                   restrictedTraverse('group/testGroup2')
Romain Courteaud's avatar
Romain Courteaud committed
475 476 477 478 479 480
    if (current_group_value is None) or \
       (current_group_value == group2) :
      new_group_value = group1
    else:
      new_group_value = group2
#     new_group_value = '%s_a' % current_title
Romain Courteaud's avatar
Romain Courteaud committed
481
    object_instance.setGroupValue(new_group_value)
Romain Courteaud's avatar
Romain Courteaud committed
482 483 484 485 486 487 488 489 490
    sequence.edit(
        current_group_value=new_group_value
    )

  def stepSetSameGroupValueWithSetter(self, sequence=None, 
                                      sequence_list=None, **kw):
    """
      Set a different title value
    """
Romain Courteaud's avatar
Romain Courteaud committed
491 492
    object_instance = sequence.get('object_instance')
    object_instance.setGroupValue(object_instance.getGroupValue())
Romain Courteaud's avatar
Romain Courteaud committed
493

Jérome Perrin's avatar
Jérome Perrin committed
494
  def test_04_areActivitiesWellLaunchedByCategorySetter(self, quiet=quiet,
Romain Courteaud's avatar
Romain Courteaud committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
                                                        run=run_all_test):
    """
      Test if setter does not call a activity if the attribute 
      value is not changed.
    """
    if not run: return
    sequence_list = SequenceList()
    # Test without workflows associated to the portal type
    sequence_string = '\
              RemoveWorkflowsRelated \
              CreateObject \
              Tic \
              CheckGroupValue \
              SetDifferentGroupValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              SetSameGroupValueWithSetter \
514 515 516
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
Romain Courteaud's avatar
Romain Courteaud committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
              CheckIfMessageQueueIsEmpty \
              SetDifferentGroupValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              '
    sequence_list.addSequenceString(sequence_string)
    # Test with workflows associated to the portal type
    sequence_string = '\
              AssociateWorkflows \
              CreateObject \
              Tic \
              CheckGroupValue \
              SetDifferentGroupValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              SetSameGroupValueWithSetter \
537 538 539
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
Romain Courteaud's avatar
Romain Courteaud committed
540 541 542 543 544 545 546 547
              CheckIfMessageQueueIsEmpty \
              SetDifferentGroupValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              '
    sequence_list.addSequenceString(sequence_string)
Jérome Perrin's avatar
Jérome Perrin committed
548
    sequence_list.play(self, quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
549

550 551 552
  def stepSetObjectNotDefinedProperty(self, sequence=None, 
                                      sequence_list=None, **kw):
    """
Romain Courteaud's avatar
Romain Courteaud committed
553
    Set a not defined property on the object_instance.
554
    """
Romain Courteaud's avatar
Romain Courteaud committed
555 556
    object_instance = sequence.get('object_instance')
    object_instance.setProperty(self.not_defined_property_id,
557 558 559 560 561
                       self.not_defined_property_value)

  def stepCheckNotDefinedPropertySaved(self, sequence=None, 
                                       sequence_list=None, **kw):
    """
Romain Courteaud's avatar
Romain Courteaud committed
562
    Check if a not defined property is stored on the object_instance.
563
    """
Romain Courteaud's avatar
Romain Courteaud committed
564
    object_instance = sequence.get('object_instance')
565
    self.assertEquals(self.not_defined_property_value,
Romain Courteaud's avatar
Romain Courteaud committed
566
                      getattr(object_instance, self.not_defined_property_id))
567 568 569 570 571 572

  def stepCheckGetNotDefinedProperty(self, sequence=None, 
                                     sequence_list=None, **kw):
    """
    Check getProperty with a not defined property.
    """
Romain Courteaud's avatar
Romain Courteaud committed
573
    object_instance = sequence.get('object_instance')
574
    self.assertEquals(self.not_defined_property_value,
Romain Courteaud's avatar
Romain Courteaud committed
575
                    object_instance.getProperty(self.not_defined_property_id))
576 577 578 579

  def stepCheckObjectPortalType(self, sequence=None, 
                                sequence_list=None, **kw):
    """
Romain Courteaud's avatar
Romain Courteaud committed
580
    Check the portal type of the object_instance.
581
    """
Romain Courteaud's avatar
Romain Courteaud committed
582 583
    object_instance = sequence.get('object_instance')
    object_instance.getPortalType()
584
    self.assertEquals(self.object_portal_type,
Romain Courteaud's avatar
Romain Courteaud committed
585
                      object_instance.getPortalType())
586 587 588

  def stepCreateTempObject(self, sequence=None, sequence_list=None, **kw):
    """
Romain Courteaud's avatar
Romain Courteaud committed
589
      Create a temp object_instance which will be tested.
590 591 592 593 594
    """
    portal = self.getPortal()
    from Products.ERP5Type.Document import newTempOrganisation
    tmp_object = newTempOrganisation(portal, "a_wonderful_id")
    sequence.edit(
Romain Courteaud's avatar
Romain Courteaud committed
595
        object_instance=tmp_object,
596 597 598 599
        current_title='',
        current_group_value=None
    )

Jérome Perrin's avatar
Jérome Perrin committed
600
  def test_05_getPropertyWithoutPropertySheet(self, quiet=quiet, run=run_all_test):
601 602 603 604 605
    """
    Test if set/getProperty work without any property sheet.
    """
    if not run: return
    sequence_list = SequenceList()
Romain Courteaud's avatar
Romain Courteaud committed
606
    # Test on object_instance.
607 608 609 610 611 612 613
    sequence_string = '\
              CreateObject \
              SetObjectNotDefinedProperty \
              CheckNotDefinedPropertySaved \
              CheckGetNotDefinedProperty \
              '
    sequence_list.addSequenceString(sequence_string)
Romain Courteaud's avatar
Romain Courteaud committed
614
    # Test on temp object_instance.
615 616 617 618 619 620 621 622
    sequence_string = '\
              CreateTempObject \
              CheckObjectPortalType \
              SetObjectNotDefinedProperty \
              CheckNotDefinedPropertySaved \
              CheckGetNotDefinedProperty \
              '
    sequence_list.addSequenceString(sequence_string)
Jérome Perrin's avatar
Jérome Perrin committed
623
    sequence_list.play(self, quiet=quiet)
624

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
  def stepCreateTempClass(self, sequence=None, sequence_list=None, **kw):
    """
    Create a temp object_instance which will be tested.
    """
    portal = self.getPortal()
    from Products.ERP5Type.Document import newTempAmount
    tmp_object = newTempAmount(portal, "another_wonderful_id")
    sequence.edit(
        object_instance=tmp_object,
        current_title='',
        current_group_value=None
    )

  def stepCheckTempClassPortalType(self, sequence=None, 
                                   sequence_list=None, **kw):
    """
    Check the portal type of the object_instance.
    Check that the portal type does not exist.
    """
    object_instance = sequence.get('object_instance')
    object_instance.getPortalType()
    self.assertEquals(self.temp_class,
                      object_instance.getPortalType())
    self.assertFalse(self.temp_class in \
                       object_instance.portal_types.listContentTypes())

  def stepSetObjectDefinedProperty(self, sequence=None, 
                                      sequence_list=None, **kw):
    """
    Set a defined property on the object_instance.
    """
    object_instance = sequence.get('object_instance')
    object_instance.setProperty(self.defined_property_id,
                       self.defined_property_value)

  def stepCheckDefinedPropertySaved(self, sequence=None, 
                                       sequence_list=None, **kw):
    """
    Check if a defined property is stored on the object_instance.
    """
    object_instance = sequence.get('object_instance')
    self.assertEquals(self.defined_property_value,
                      getattr(object_instance, self.defined_property_id))

  def stepCheckGetDefinedProperty(self, sequence=None, 
                                     sequence_list=None, **kw):
    """
    Check getProperty with a defined property.
    """
    object_instance = sequence.get('object_instance')
    self.assertEquals(self.defined_property_value,
                    object_instance.getProperty(self.defined_property_id))

  def stepSetObjectNotRelatedProperty(self, sequence=None, 
                                      sequence_list=None, **kw):
    """
    Set a defined property on the object_instance.
    """
    object_instance = sequence.get('object_instance')
    object_instance.setProperty(
                       self.not_related_to_temp_object_property_id,
                       self.not_related_to_temp_object_property_value)

  def stepCheckNotRelatedPropertySaved(self, sequence=None, 
                                       sequence_list=None, **kw):
    """
    Check if a defined property is stored on the object_instance.
    """
    object_instance = sequence.get('object_instance')
    self.assertEquals(self.not_related_to_temp_object_property_value,
                      getattr(object_instance, 
                              self.not_related_to_temp_object_property_id))

  def stepCheckGetNotRelatedProperty(self, sequence=None, 
                                  sequence_list=None, **kw):
    """
    Check getProperty with a defined property.
    """
    object_instance = sequence.get('object_instance')
    self.assertEquals(self.not_related_to_temp_object_property_value,
                    object_instance.getProperty(
                         self.not_related_to_temp_object_property_id))

708
  def test_06_getPropertyOnTempClass(self, quiet=quiet, run=1):
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
    """
    Test if set/getProperty work in temp object without 
    a portal type with the same name.
    """
    if not run: return
    sequence_list = SequenceList()
    # Test on temp tempAmount.
    sequence_string = '\
              CreateTempClass \
              CheckTempClassPortalType \
              SetObjectDefinedProperty \
              CheckDefinedPropertySaved \
              CheckGetDefinedProperty \
              SetObjectNotDefinedProperty \
              CheckNotDefinedPropertySaved \
              CheckGetNotDefinedProperty \
              SetObjectNotRelatedProperty \
              CheckNotRelatedPropertySaved \
              CheckGetNotRelatedProperty \
              '
    sequence_list.addSequenceString(sequence_string)
Jérome Perrin's avatar
Jérome Perrin committed
730
    sequence_list.play(self, quiet=quiet)
731

732 733 734 735 736 737
  def stepCheckEditMethod(self, sequence=None, 
                          sequence_list=None, **kw):
    """
    Check if edit method works.
    """
    object_instance = sequence.get('object_instance')
738 739 740 741
    object_instance.edit(title='toto')
    self.assertEquals(object_instance.getTitle(),'toto')
    object_instance.edit(title='tutu')
    self.assertEquals(object_instance.getTitle(),'tutu')
742 743 744 745 746 747 748

  def stepSetEditProperty(self, sequence=None, 
                          sequence_list=None, **kw):
    """
    Check if edit method works.
    """
    object_instance = sequence.get('object_instance')
749 750
    # can't override a method:
    self.assertRaises(BadRequest, object_instance.setProperty, 'edit',
751
                      "now this object is 'read only !!!'")
752 753 754 755 756 757 758 759
    # can't change the portal type and other internal instance attributes
    self.assertRaises(BadRequest, object_instance.setProperty,
                      'portal_type', "Other")
    self.assertRaises(BadRequest, object_instance.setProperty,
                      'workflow_history', {})
    self.assertRaises(BadRequest, object_instance.setProperty,
                      '__dict__', {})

760

Jérome Perrin's avatar
Jérome Perrin committed
761
  def test_07_setEditProperty(self, quiet=quiet, run=run_all_test):
762 763 764 765 766 767 768 769 770 771 772 773
    """
    Test if setProperty erase existing accessors/methods.
    """
    if not run: return
    sequence_list = SequenceList()
    sequence_string = '\
              CreateObject \
              CheckEditMethod \
              SetEditProperty \
              CheckEditMethod \
              '
    sequence_list.addSequenceString(sequence_string)
Jérome Perrin's avatar
Jérome Perrin committed
774
    sequence_list.play(self, quiet=quiet)
775

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
  def stepCreateBaseCategory(self, sequence=None, sequence_list=None, **kw):
    """
    Create a base category.
    """
    portal = self.getPortal()
    module = portal.portal_categories
    object_instance = module.newContent(portal_type="Base Category")
    sequence.edit(
        object_instance=object_instance,
    )

  def stepSetBadTalesExpression(self, sequence=None, sequence_list=None, **kw):
    """
    Set a wrong tales expression
    """
    object_instance = sequence.get('object_instance')
    tales_expression = "python: 1 + 'a'"
    object_instance.edit(acquisition_portal_type_list=tales_expression)
    sequence.edit(
        tales_expression=tales_expression,
    )

  def stepCheckTalesExpression(self, sequence=None, sequence_list=None, **kw):
    """
    Set a wrong tales expression
    """
    object_instance = sequence.get('object_instance')
    tales_expression = sequence.get('tales_expression')
    self.assertEquals(object_instance.getAcquisitionPortalTypeList(evaluate=0),
                      tales_expression)

  def stepSetGoodTalesExpression(self, sequence=None, 
                                 sequence_list=None, **kw):
    """
    Set a wrong tales expression
    """
    object_instance = sequence.get('object_instance')
    tales_expression = "python: 1 + 1"
    object_instance.edit(acquisition_portal_type_list=tales_expression)
    sequence.edit(
        tales_expression=tales_expression,
    )

Jérome Perrin's avatar
Jérome Perrin committed
819
  def test_07_setEditTalesExpression(self, quiet=quiet, run=run_all_test):
820 821 822 823 824 825 826 827 828 829 830 831 832
    """
    Test if edit update a tales expression.
    """
    if not run: return
    sequence_list = SequenceList()
    sequence_string = '\
              CreateBaseCategory \
              SetBadTalesExpression \
              CheckTalesExpression \
              SetGoodTalesExpression \
              CheckTalesExpression \
              '
    sequence_list.addSequenceString(sequence_string)
Jérome Perrin's avatar
Jérome Perrin committed
833
    sequence_list.play(self, quiet=quiet)
834
  
835 836 837 838
  def test_08_emptyObjectAcquiresTitle(self, quiet=quiet, run=run_all_test):
    """
    Test that an empty object has no title, and that getTitle on it acquires a
    value form the object's id.
839 840 841 842 843
    """
    if not run: return
    portal = self.getPortal()
    portal_type = "Organisation"
    module = portal.getDefaultModule(portal_type=portal_type)
844 845 846
    obj = module.newContent(portal_type=portal_type)
    # XXX title is an empty string by default, but it's still unsure wether it
    # should be None or ''
847 848 849 850
    self.assertEquals(obj.title, '')
    self.assertEquals(obj.getProperty("title"), obj.getId())
    self.assertEquals(obj._baseGetTitle(), obj.getId())
    self.assertEquals(obj.getTitle(), obj.getId())
851

852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
  def test_09_setPropertyDefinedProperty(self, quiet=quiet, run=run_all_test):
    """Test for setProperty on Base, when the property is defined.
    """
    if not run: return
    portal = self.getPortal()
    portal_type = "Organisation"
    module = portal.getDefaultModule(portal_type=portal_type)
    obj = module.newContent(portal_type=portal_type)
    title = 'Object title'
    obj.setProperty('title', title)
    self.assertEquals(obj.getProperty('title'), title)
    obj.setProperty('title', title)
    self.assertEquals(obj.getProperty('title'), title)
    obj.edit(title=title)
    self.assertEquals(obj.getProperty('title'), title)

  def test_10_setPropertyNotDefinedProperty(self, quiet=quiet,
                                            run=run_all_test):
    """Test for setProperty on Base, when the property is not defined.
    """
    if not run: return
    portal = self.getPortal()
    portal_type = "Organisation"
    module = portal.getDefaultModule(portal_type=portal_type)
    obj = module.newContent(portal_type=portal_type)
    property_value = 'Object title'
    property_name = 'a_dummy_not_exising_property'
    obj.setProperty(property_name, property_value)
    self.assertEquals(obj.getProperty(property_name), property_value)
    obj.setProperty(property_name, property_value)
    self.assertEquals(obj.getProperty(property_name), property_value)
    obj.edit(**{property_name: property_value})
    self.assertEquals(obj.getProperty(property_name), property_value)
  
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
  def test_11_setPropertyPropertyDefinedOnInstance(self,
                                        quiet=quiet, run=run_all_test):
    """Test for setProperty on Base, when the property is defined on the
    instance, the typical example is 'workflow_history' property.
    """
    if not run: return
    portal = self.getPortal()
    portal_type = "Organisation"
    module = portal.getDefaultModule(portal_type=portal_type)
    obj = module.newContent(portal_type=portal_type)
    
    property_value = 'Property value'
    property_name = 'a_dummy_object_property'
    setattr(obj, property_name, property_value)
    self.assertRaises(BadRequest, obj.setProperty,
                     property_name, property_value)

    self.assertRaises(BadRequest, obj.setProperty,
                     'workflow_history', property_value)
  
906
  def test_12_editTempObject(self, quiet=quiet, run=run_all_test):
907 908
    """Simple t
    est to edit a temp object.
909 910 911 912 913 914 915
    """
    portal = self.getPortal()
    from Products.ERP5Type.Document import newTempOrganisation
    tmp_object = newTempOrganisation(portal, "a_wonderful_id")
    tmp_object.edit(title='new title')
    self.assertEquals('new title', tmp_object.getTitle())

916 917 918 919 920 921 922 923 924 925 926 927 928
  def test_13_aqDynamicWithNonExistentWorkflow(self, quiet=quiet, run=run_all_test):
    """Test if _aq_dynamic still works even if an associated workflow
    is not present in the portal. This may cause an infinite recursion."""
    if not run: return

    portal = self.getPortal()
    portal_type = "Organisation"
    module = portal.getDefaultModule(portal_type = portal_type)
    obj = module.newContent(portal_type = portal_type)

    # Add a non-existent workflow.
    pw = self.getWorkflowTool()
    dummy_worlflow_id = 'never_existent_workflow'
929
    pw.manage_addWorkflow('dc_workflow (Web-configurable workflow)',
930 931 932 933 934 935 936 937 938 939 940 941 942 943
                          dummy_worlflow_id)
    cbt = pw._chains_by_type
    props = {}
    for id, wf_ids in cbt.iteritems():
      if id == portal_type:
        wf_ids = list(wf_ids) + [dummy_worlflow_id]
      props['chain_%s' % id] = ','.join(wf_ids)
    pw.manage_changeWorkflows('', props = props)
    pw.manage_delObjects([dummy_worlflow_id])

    # Make sure that _aq_dynamic will be called again.
    _aq_reset()

    try:
944 945
      self.assertRaises(AttributeError, getattr, obj,
                        'thisMethodShouldNotBePresent')
946 947 948 949 950 951 952 953 954 955 956 957
    finally:
      # Make sure that the artificial workflow is not referred to any longer.
      cbt = pw._chains_by_type
      props = {}
      for id, wf_ids in cbt.iteritems():
        if id == portal_type:
          # Remove the non-existent workflow.
          wf_ids = [wf_id for wf_id in wf_ids \
                    if wf_id != dummy_worlflow_id]
        props['chain_%s' % id] = ','.join(wf_ids)
      pw.manage_changeWorkflows('', props = props)

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
  def test_getViewPermissionOwnerDefault(self):
    """Test getViewPermissionOwner method behaviour"""
    portal = self.getPortal()
    obj = portal.organisation_module.newContent(portal_type='Organisation')
    self.assertEquals(self.username, obj.getViewPermissionOwner())

  def test_getViewPermissionOwnerNoOwnerLocalRole(self):
    # the actual owner doesn't have Owner local role
    portal = self.getPortal()
    obj = portal.organisation_module.newContent(portal_type='Organisation')
    obj.manage_delLocalRoles(self.username)
    self.assertEquals(self.username, obj.getViewPermissionOwner())

  def test_getViewPermissionOwnerNoViewPermission(self):
    # the owner cannot view the object
    portal = self.getPortal()
    obj = portal.organisation_module.newContent(portal_type='Organisation')
    obj.manage_permission('View', [], 0)
    self.assertEquals(None, obj.getViewPermissionOwner())

978 979
  def test_Member_Base_download(self):
    # tests that members can download files
980 981 982 983
    class DummyFile(file):
      def __init__(self, filename):
        self.filename = os.path.basename(filename)
        file.__init__(self, filename)
984
    f = self.portal.newContent(portal_type='File', id='f')
985
    f._edit(content_type='text/plain', file=DummyFile(__file__))
986 987 988 989 990 991 992
    # login as a member
    uf = self.portal.acl_users
    uf._doAddUser('member_user', 'secret', ['Member'], [])
    user = uf.getUserById('member_user').__of__(uf)
    newSecurityManager(None, user)

    # if it didn't raise Unauthorized, Ok
993 994 995
    response = self.publish('%s/Base_download' % f.getPath())
    self.assertEquals(file(__file__).read(), response.body)
    self.assertEquals('text/plain', response.headers['content-type'])
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
996
    self.assertEquals('attachment; filename="%s"' % os.path.basename(__file__),
997
                      response.headers['content-disposition'])
998

Nicolas Delaby's avatar
Nicolas Delaby committed
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
  def test_getTypeBasedMethod(self):
    """
    Test that getTypeBasedMethod look up at ancestor classes
    and stop after Base and Folder Classes
    """
    from Products.ERP5Type.tests.utils import createZODBPythonScript
    portal = self.getPortal()

    base_script = createZODBPythonScript(portal.portal_skins.custom,
                        'Base_fooMethod',
                        'scripts_params=None',
                        '# Script body\n'
                        'return "something"' )
    xml_object_script = createZODBPythonScript(portal.portal_skins.custom,
                        'XMLObject_dummyMethod',
                        'scripts_params=None',
                        '# Script body\n'
                        'return "something"' )
    person_script = createZODBPythonScript(portal.portal_skins.custom,
                        'Person_dummyMethod',
                        'scripts_params=None',
                        '# Script body\n'
                        'return "something"' )
    copy_container_script = createZODBPythonScript(portal.portal_skins.custom,
                        'CopyContainer_dummyFooMethod',
                        'scripts_params=None',
                        '# Script body\n'
                        'return "something"' )
    cmfbtree_folder_script = createZODBPythonScript(portal.portal_skins.custom,
Nicolas Delaby's avatar
Nicolas Delaby committed
1028
                        'CMFBTreeFolder_dummyFoo2Method',
Nicolas Delaby's avatar
Nicolas Delaby committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
                        'scripts_params=None',
                        '# Script body\n'
                        'return "something"' )
    org = portal.organisation_module.newContent(portal_type='Organisation')
    pers = portal.person_module.newContent(portal_type='Person')

    self.assertEqual(org._getTypeBasedMethod('dummyMethod'), xml_object_script)
    self.assertEqual(pers._getTypeBasedMethod('dummyMethod'), person_script)
    self.assertEqual(org._getTypeBasedMethod('fooMethod'), base_script)
    self.assertEqual(pers._getTypeBasedMethod('fooMethod'), base_script)
    self.assertEqual(org._getTypeBasedMethod('dummyFooMethod'), None)
Nicolas Delaby's avatar
Nicolas Delaby committed
1040
    self.assertEqual(org._getTypeBasedMethod('dummyFoo2Method'), None)
1041

1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
class TestERP5PropertyManager(unittest.TestCase):
  """Tests for ERP5PropertyManager.
  """
  def _makeOne(self, *args, **kw):
    from Products.ERP5Type.patches.PropertyManager import ERP5PropertyManager
    ob = ERP5PropertyManager(*args, **kw)
    # add missing methods for createExpressionContext
    ob.getPortalObject = lambda : None
    ob.absolute_url = lambda: ''
    return ob

  def test_setProperty(self):
    """_setProperty adds a new property if not present."""
    ob = self._makeOne('ob')
    dummy_property_value = 'test string value'
    ob._setProperty('a_dummy_property', dummy_property_value)

    # the property appears in property map
    self.failUnless('a_dummy_property' in [x['id'] for x in ob.propertyMap()])
    # the value and can be retrieved using getProperty
    self.assertEquals(ob.getProperty('a_dummy_property'), dummy_property_value)
    # the value is also stored as a class attribute
    self.assertEquals(ob.a_dummy_property, dummy_property_value)

  def test_setPropertyExistingProperty(self):
    """_setProperty raises an error if the property already exists."""
    ob = self._makeOne('ob')
    # make sure that title property exists
    self.failUnless('title' in [x['id'] for x in ob.propertyMap()])
    # trying to call _setProperty will with an existing property raises:
    #         BadRequest: Invalid or duplicate property id: title
    self.assertRaises(BadRequest, ob._setProperty, 'title', 'property value')

  def test_updatePropertyExistingProperty(self):
    """_updateProperty should be used if the existing property already exists.
    """
    ob = self._makeOne('ob')
    # make sure that title property exists
    self.failUnless('title' in [x['id'] for x in ob.propertyMap()])
    prop_value = 'title value'
    ob._updateProperty('title', prop_value)
    self.assertEquals(ob.getProperty('title'), prop_value)
    self.assertEquals(ob.title, prop_value)

  def test_setPropertyTypeInt(self):
    """You can specify the type of the property in _setProperty"""
    ob = self._makeOne('ob')
    dummy_property_value = 3
    ob._setProperty('a_dummy_property', dummy_property_value, type='int')
    self.assertEquals(['int'], [x['type'] for x in ob.propertyMap()
                                        if x['id'] == 'a_dummy_property'])
    self.assertEquals(type(ob.getProperty('a_dummy_property')), type(1))

  def test_setPropertyTALESType(self):
    """ERP5PropertyManager can use TALES Type for properties, TALES will then
    be evaluated in getProperty.
    """
    ob = self._makeOne('ob')
    dummy_property_value = 'python: 1+2'
    ob._setProperty('a_dummy_property', dummy_property_value, type='tales')
    self.assertEquals(ob.getProperty('a_dummy_property'), 1+2)

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
  def test_setPropertyTypeDate(self):
    """You can specify the type of the property in _setProperty"""
    ob = self._makeOne('ob')
    from DateTime import DateTime
    dummy_property_value = DateTime()
    ob._setProperty('a_dummy_property', dummy_property_value, type='date')
    self.assertEquals(['date'], [x['type'] for x in ob.propertyMap()
                                        if x['id'] == 'a_dummy_property'])
    self.assertEquals(type(ob.getProperty('a_dummy_property')), type(DateTime()))
    #Set Property without type argument
    ob._setProperty('a_second_dummy_property', dummy_property_value)
    self.assertEquals(['date'], [x['type'] for x in ob.propertyMap()
                                        if x['id'] == 'a_second_dummy_property'])
    self.assertEquals(type(ob.getProperty('a_second_dummy_property')),
                      type(DateTime()))

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
  def test_getPropertyNonExistantProps(self):
    """getProperty return None if the value is not found.
    """
    ob = self._makeOne('ob')
    self.assertEquals(ob.getProperty('a_dummy_property'), None)

  def test_getPropertyDefaultValue(self):
    """getProperty accepts a default value, if the property is not defined.
    """
    ob = self._makeOne('ob')
    self.assertEquals(ob.getProperty('a_dummy_property', 100), 100)
    prop_value = 3
    ob._setProperty('a_dummy_property', prop_value)
    self.assertEquals(ob.getProperty('a_dummy_property', 100), prop_value)

Jérome Perrin's avatar
Jérome Perrin committed
1135 1136 1137 1138 1139
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestBase))
  suite.addTest(unittest.makeSuite(TestERP5PropertyManager))
  return suite