testBase.py 36.3 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 30 31 32 33 34
#          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.
#
##############################################################################

#
# Skeleton ZopeTestCase
#

from random import randint
35 36 37 38
import os
import sys
import unittest
import time
Romain Courteaud's avatar
Romain Courteaud committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))

# Needed in order to have a log file inside the current folder
os.environ['EVENT_LOG_FILE'] = os.path.join(os.getcwd(), 'zLOG.log')
os.environ['EVENT_LOG_SEVERITY'] = '-300'

from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from AccessControl.SecurityManagement import newSecurityManager, \
                                             noSecurityManager
from DateTime import DateTime
from Acquisition import aq_base, aq_inner
from zLOG import LOG
from Products.ERP5Type.DateUtils import addToDate
from Products.ERP5Type.tests.Sequence import Sequence, SequenceList
56
from zExceptions import BadRequest
Romain Courteaud's avatar
Romain Courteaud committed
57 58
from Products.ERP5Type import product_path
from Products.CMFCore.utils import getToolByName
59
from Products.ERP5Type.Tool.ClassTool import _aq_reset
Romain Courteaud's avatar
Romain Courteaud committed
60 61 62

class TestBase(ERP5TypeTestCase):

Romain Courteaud's avatar
Romain Courteaud committed
63
  run_all_test = 1
Jérome Perrin's avatar
Jérome Perrin committed
64 65
  quiet = 1

Romain Courteaud's avatar
Romain Courteaud committed
66
  object_portal_type = "Organisation"
67 68
  not_defined_property_id = "azerty_qwerty"
  not_defined_property_value = "qwerty_azerty"
Romain Courteaud's avatar
Romain Courteaud committed
69

70 71 72
  temp_class = "Amount"
  defined_property_id = "title"
  defined_property_value = "a_wonderful_title"
73
  not_related_to_temp_object_property_id = "string_index"
74 75
  not_related_to_temp_object_property_value = "a_great_index"

Romain Courteaud's avatar
Romain Courteaud committed
76 77 78 79 80 81
  def getTitle(self):
    return "Base"

  def getBusinessTemplateList(self):
    """
    """
Sebastien Robin's avatar
Sebastien Robin committed
82
    return ('erp5_base',)
Romain Courteaud's avatar
Romain Courteaud committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

  def login(self, quiet=0, run=run_all_test):
    uf = self.getPortal().acl_users
    uf._doAddUser('rc', '', ['Manager'], [])
    user = uf.getUserById('rc').__of__(uf)
    newSecurityManager(None, user)

  def enableLightInstall(self):
    """
    You can override this. 
    Return if we should do a light install (1) or not (0)
    """
    return 1

  def enableActivityTool(self):
    """
    You can override this.
    Return if we should create (1) or not (0) an activity tool.
    """
    return 1

  def afterSetUp(self, quiet=1, run=run_all_test):
    self.login()
    portal = self.getPortal()
    self.category_tool = self.getCategoryTool()
    portal_catalog = self.getCatalogTool()
    #portal_catalog.manage_catalogClear()
    self.createCategories()

  def createCategories(self):
    """ 
      Light install create only base categories, so we create 
      some categories for testing them
    """
Romain Courteaud's avatar
Romain Courteaud committed
117 118 119 120 121
    category_list = ['testGroup1', 'testGroup2']
    if len(self.category_tool.group.contentValues()) == 0 :
      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
122 123 124 125

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

Romain Courteaud's avatar
Romain Courteaud committed
126 127
  def stepRemoveWorkflowsRelated(self, sequence=None, sequence_list=None, 
                                 **kw):
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    """
      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
143 144
  def stepAssociateWorkflowsExcludingEdit(self, sequence=None, 
                                          sequence_list=None, **kw):
145 146 147 148 149 150 151
    """
      Associate workflow to the portal type
    """
    self.getWorkflowTool().setChainForPortalTypes(
        ['Organisation'], ('validation_workflow',))
    _aq_reset()

Romain Courteaud's avatar
Romain Courteaud committed
152 153
  def stepCreateObject(self, sequence=None, sequence_list=None, **kw):
    """
Romain Courteaud's avatar
Romain Courteaud committed
154
      Create a object_instance which will be tested.
Romain Courteaud's avatar
Romain Courteaud committed
155 156 157
    """
    portal = self.getPortal()
    module = portal.getDefaultModule(self.object_portal_type)
Romain Courteaud's avatar
Romain Courteaud committed
158
    object_instance = module.newContent(portal_type=self.object_portal_type)
Romain Courteaud's avatar
Romain Courteaud committed
159
    sequence.edit(
Romain Courteaud's avatar
Romain Courteaud committed
160
        object_instance=object_instance,
161
        current_title='',
Romain Courteaud's avatar
Romain Courteaud committed
162
        current_group_value=None
Romain Courteaud's avatar
Romain Courteaud committed
163 164 165 166 167 168
    )

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

Romain Courteaud's avatar
Romain Courteaud committed
173 174
  def stepSetDifferentTitleValueWithEdit(self, sequence=None, 
                                         sequence_list=None, **kw):
Romain Courteaud's avatar
Romain Courteaud committed
175 176 177
    """
      Set a different title value
    """
Romain Courteaud's avatar
Romain Courteaud committed
178
    object_instance = sequence.get('object_instance')
Romain Courteaud's avatar
Romain Courteaud committed
179 180
    current_title = sequence.get('current_title')
    new_title_value = '%s_a' % current_title
Romain Courteaud's avatar
Romain Courteaud committed
181
    object_instance.edit(title=new_title_value)
Romain Courteaud's avatar
Romain Courteaud committed
182 183 184 185 186 187 188 189 190 191 192 193
    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()
194 195 196 197 198 199 200 201
    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
202

Romain Courteaud's avatar
Romain Courteaud committed
203 204
  def stepSetSameTitleValueWithEdit(self, sequence=None, sequence_list=None, 
                                    **kw):
Romain Courteaud's avatar
Romain Courteaud committed
205 206 207
    """
      Set a different title value
    """
Romain Courteaud's avatar
Romain Courteaud committed
208 209
    object_instance = sequence.get('object_instance')
    object_instance.edit(title=object_instance.getTitle())
Romain Courteaud's avatar
Romain Courteaud committed
210 211 212 213 214 215 216 217 218 219

  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)

220 221 222 223 224 225 226 227 228 229
  def stepMakeImmediateReindexObjectCrashing(self, sequence=None, sequence_list=None, **kw):
    """
      Overwrite immediateReindexObject() with a crashing method
    """
    def crashingMethod(self):
      self.ImmediateReindexObjectIsCalled()
    from Products.ERP5Type.Document.Organisation import Organisation
    Organisation.immediateReindexObject = crashingMethod
    Organisation.recursiveImmediateReindexObject = crashingMethod

Jérome Perrin's avatar
Jérome Perrin committed
230
  def test_01_areActivitiesWellLaunchedByPropertyEdit(self, quiet=quiet,
Romain Courteaud's avatar
Romain Courteaud committed
231
                                                      run=run_all_test):
Romain Courteaud's avatar
Romain Courteaud committed
232
    """
233 234
      Test if setter does not call a activity if the attribute 
      value is not changed.
Romain Courteaud's avatar
Romain Courteaud committed
235 236 237
    """
    if not run: return
    sequence_list = SequenceList()
238 239 240 241
    # Test without workflows associated to the portal type
    sequence_string = '\
              RemoveWorkflowsRelated \
              CreateObject \
Romain Courteaud's avatar
Romain Courteaud committed
242
              Tic \
243
              CheckTitleValue \
244
              MakeImmediateReindexObjectCrashing \
Romain Courteaud's avatar
Romain Courteaud committed
245
              SetDifferentTitleValueWithEdit \
246 247 248 249
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
Romain Courteaud's avatar
Romain Courteaud committed
250
              SetSameTitleValueWithEdit \
251
              CheckTitleValue \
252
              CheckIfMessageQueueIsEmpty \
Romain Courteaud's avatar
Romain Courteaud committed
253
              SetDifferentTitleValueWithEdit \
254 255 256 257 258 259 260 261 262 263
              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
264
              Tic \
Romain Courteaud's avatar
Romain Courteaud committed
265
              CheckTitleValue \
266
              MakeImmediateReindexObjectCrashing \
Romain Courteaud's avatar
Romain Courteaud committed
267
              SetDifferentTitleValueWithEdit \
Romain Courteaud's avatar
Romain Courteaud committed
268 269 270 271
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
Romain Courteaud's avatar
Romain Courteaud committed
272
              SetSameTitleValueWithEdit \
273 274 275
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
Romain Courteaud's avatar
Romain Courteaud committed
276
              CheckIfMessageQueueIsEmpty \
Romain Courteaud's avatar
Romain Courteaud committed
277
              SetDifferentTitleValueWithEdit \
Romain Courteaud's avatar
Romain Courteaud committed
278 279 280 281 282 283
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              '
    sequence_list.addSequenceString(sequence_string)
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    # Test with workflows associated to the portal type, excluding edit_workflow
    sequence_string = '\
              AssociateWorkflowsExcludingEdit \
              CreateObject \
              Tic \
              CheckTitleValue \
              MakeImmediateReindexObjectCrashing \
              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
306
    sequence_list.play(self, quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
307

Romain Courteaud's avatar
Romain Courteaud committed
308 309 310 311
  def stepCheckGroupValue(self, sequence=None, sequence_list=None, **kw):
    """
      Check if getTitle return a correect value
    """
Romain Courteaud's avatar
Romain Courteaud committed
312
    object_instance = sequence.get('object_instance')
Romain Courteaud's avatar
Romain Courteaud committed
313
    current_group_value = sequence.get('current_group_value')
Romain Courteaud's avatar
Romain Courteaud committed
314
    self.assertEquals(object_instance.getGroupValue(), current_group_value)
Romain Courteaud's avatar
Romain Courteaud committed
315 316 317 318 319 320

  def stepSetDifferentGroupValueWithEdit(self, sequence=None, 
                                         sequence_list=None, **kw):
    """
      Set a different title value
    """
Romain Courteaud's avatar
Romain Courteaud committed
321
    object_instance = sequence.get('object_instance')
Romain Courteaud's avatar
Romain Courteaud committed
322
    current_group_value = sequence.get('current_group_value')
Romain Courteaud's avatar
Romain Courteaud committed
323 324 325 326
    group1 = object_instance.portal_categories.\
                       restrictedTraverse('group/testGroup1')
    group2 = object_instance.portal_categories.\
                       restrictedTraverse('group/testGroup2')
Romain Courteaud's avatar
Romain Courteaud committed
327 328 329 330 331 332
    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
333
    object_instance.edit(group_value=new_group_value)
Romain Courteaud's avatar
Romain Courteaud committed
334 335 336 337 338 339 340 341 342
    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
343 344
    object_instance = sequence.get('object_instance')
    object_instance.edit(group_value=object_instance.getGroupValue())
Romain Courteaud's avatar
Romain Courteaud committed
345 346


Jérome Perrin's avatar
Jérome Perrin committed
347
  def test_02_areActivitiesWellLaunchedByCategoryEdit(self, quiet=quiet,
Romain Courteaud's avatar
Romain Courteaud committed
348 349 350 351 352 353 354 355 356 357 358 359 360
                                                      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 \
361
              MakeImmediateReindexObjectCrashing \
Romain Courteaud's avatar
Romain Courteaud committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
              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 \
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
              MakeImmediateReindexObjectCrashing \
              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 \
              MakeImmediateReindexObjectCrashing \
Romain Courteaud's avatar
Romain Courteaud committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420
              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
421
    sequence_list.play(self, quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
422 423 424 425 426 427

  def stepSetDifferentTitleValueWithSetter(self, sequence=None, 
                                           sequence_list=None, **kw):
    """
      Set a different title value
    """
Romain Courteaud's avatar
Romain Courteaud committed
428
    object_instance = sequence.get('object_instance')
Romain Courteaud's avatar
Romain Courteaud committed
429 430
    current_title = sequence.get('current_title')
    new_title_value = '%s_a' % current_title
Romain Courteaud's avatar
Romain Courteaud committed
431
    object_instance.setTitle(new_title_value)
Romain Courteaud's avatar
Romain Courteaud committed
432 433 434 435 436 437 438 439 440
    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
441 442
    object_instance = sequence.get('object_instance')
    object_instance.setTitle(object_instance.getTitle())
Romain Courteaud's avatar
Romain Courteaud committed
443

Jérome Perrin's avatar
Jérome Perrin committed
444
  def test_03_areActivitiesWellLaunchedByPropertySetter(self, quiet=quiet,
Romain Courteaud's avatar
Romain Courteaud committed
445 446 447 448 449 450 451 452 453 454 455 456 457
                                                        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 \
458
              MakeImmediateReindexObjectCrashing \
Romain Courteaud's avatar
Romain Courteaud committed
459 460 461 462 463 464
              SetDifferentTitleValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              SetSameTitleValueWithSetter \
465 466 467
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
Romain Courteaud's avatar
Romain Courteaud committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481
              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 \
482
              MakeImmediateReindexObjectCrashing \
Romain Courteaud's avatar
Romain Courteaud committed
483 484 485 486 487 488
              SetDifferentTitleValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              SetSameTitleValueWithSetter \
489 490 491
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
Romain Courteaud's avatar
Romain Courteaud committed
492 493 494 495 496 497 498 499
              CheckIfMessageQueueIsEmpty \
              SetDifferentTitleValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckTitleValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              '
    sequence_list.addSequenceString(sequence_string)
Jérome Perrin's avatar
Jérome Perrin committed
500
    sequence_list.play(self, quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
501 502 503 504 505 506

  def stepSetDifferentGroupValueWithSetter(self, sequence=None, 
                                           sequence_list=None, **kw):
    """
      Set a different title value
    """
Romain Courteaud's avatar
Romain Courteaud committed
507
    object_instance = sequence.get('object_instance')
Romain Courteaud's avatar
Romain Courteaud committed
508
    current_group_value = sequence.get('current_group_value')
Romain Courteaud's avatar
Romain Courteaud committed
509 510 511 512
    group1 = object_instance.portal_categories.\
                                   restrictedTraverse('group/testGroup1')
    group2 = object_instance.portal_categories.\
                                   restrictedTraverse('group/testGroup2')
Romain Courteaud's avatar
Romain Courteaud committed
513 514 515 516 517 518
    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
519
    object_instance.setGroupValue(new_group_value)
Romain Courteaud's avatar
Romain Courteaud committed
520 521 522 523 524 525 526 527 528
    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
529 530
    object_instance = sequence.get('object_instance')
    object_instance.setGroupValue(object_instance.getGroupValue())
Romain Courteaud's avatar
Romain Courteaud committed
531

Jérome Perrin's avatar
Jérome Perrin committed
532
  def test_04_areActivitiesWellLaunchedByCategorySetter(self, quiet=quiet,
Romain Courteaud's avatar
Romain Courteaud committed
533 534 535 536 537 538 539 540 541 542 543 544 545
                                                        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 \
546
              MakeImmediateReindexObjectCrashing \
Romain Courteaud's avatar
Romain Courteaud committed
547 548 549 550 551 552
              SetDifferentGroupValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              SetSameGroupValueWithSetter \
553 554 555
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
Romain Courteaud's avatar
Romain Courteaud committed
556 557 558 559 560 561 562 563 564 565 566 567 568 569
              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 \
570
              MakeImmediateReindexObjectCrashing \
Romain Courteaud's avatar
Romain Courteaud committed
571 572 573 574 575 576
              SetDifferentGroupValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              SetSameGroupValueWithSetter \
577 578 579
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
Romain Courteaud's avatar
Romain Courteaud committed
580 581 582 583 584 585 586 587
              CheckIfMessageQueueIsEmpty \
              SetDifferentGroupValueWithSetter \
              CheckIfActivitiesAreCreated \
              CheckGroupValue \
              Tic \
              CheckIfMessageQueueIsEmpty \
              '
    sequence_list.addSequenceString(sequence_string)
Jérome Perrin's avatar
Jérome Perrin committed
588
    sequence_list.play(self, quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
589

590 591 592
  def stepSetObjectNotDefinedProperty(self, sequence=None, 
                                      sequence_list=None, **kw):
    """
Romain Courteaud's avatar
Romain Courteaud committed
593
    Set a not defined property on the object_instance.
594
    """
Romain Courteaud's avatar
Romain Courteaud committed
595 596
    object_instance = sequence.get('object_instance')
    object_instance.setProperty(self.not_defined_property_id,
597 598 599 600 601
                       self.not_defined_property_value)

  def stepCheckNotDefinedPropertySaved(self, sequence=None, 
                                       sequence_list=None, **kw):
    """
Romain Courteaud's avatar
Romain Courteaud committed
602
    Check if a not defined property is stored on the object_instance.
603
    """
Romain Courteaud's avatar
Romain Courteaud committed
604
    object_instance = sequence.get('object_instance')
605
    self.assertEquals(self.not_defined_property_value,
Romain Courteaud's avatar
Romain Courteaud committed
606
                      getattr(object_instance, self.not_defined_property_id))
607 608 609 610 611 612

  def stepCheckGetNotDefinedProperty(self, sequence=None, 
                                     sequence_list=None, **kw):
    """
    Check getProperty with a not defined property.
    """
Romain Courteaud's avatar
Romain Courteaud committed
613
    object_instance = sequence.get('object_instance')
614
    self.assertEquals(self.not_defined_property_value,
Romain Courteaud's avatar
Romain Courteaud committed
615
                    object_instance.getProperty(self.not_defined_property_id))
616 617 618 619

  def stepCheckObjectPortalType(self, sequence=None, 
                                sequence_list=None, **kw):
    """
Romain Courteaud's avatar
Romain Courteaud committed
620
    Check the portal type of the object_instance.
621
    """
Romain Courteaud's avatar
Romain Courteaud committed
622 623
    object_instance = sequence.get('object_instance')
    object_instance.getPortalType()
624
    self.assertEquals(self.object_portal_type,
Romain Courteaud's avatar
Romain Courteaud committed
625
                      object_instance.getPortalType())
626 627 628

  def stepCreateTempObject(self, sequence=None, sequence_list=None, **kw):
    """
Romain Courteaud's avatar
Romain Courteaud committed
629
      Create a temp object_instance which will be tested.
630 631 632 633 634
    """
    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
635
        object_instance=tmp_object,
636 637 638 639
        current_title='',
        current_group_value=None
    )

Jérome Perrin's avatar
Jérome Perrin committed
640
  def test_05_getPropertyWithoutPropertySheet(self, quiet=quiet, run=run_all_test):
641 642 643 644 645
    """
    Test if set/getProperty work without any property sheet.
    """
    if not run: return
    sequence_list = SequenceList()
Romain Courteaud's avatar
Romain Courteaud committed
646
    # Test on object_instance.
647 648 649 650 651 652 653
    sequence_string = '\
              CreateObject \
              SetObjectNotDefinedProperty \
              CheckNotDefinedPropertySaved \
              CheckGetNotDefinedProperty \
              '
    sequence_list.addSequenceString(sequence_string)
Romain Courteaud's avatar
Romain Courteaud committed
654
    # Test on temp object_instance.
655 656 657 658 659 660 661 662
    sequence_string = '\
              CreateTempObject \
              CheckObjectPortalType \
              SetObjectNotDefinedProperty \
              CheckNotDefinedPropertySaved \
              CheckGetNotDefinedProperty \
              '
    sequence_list.addSequenceString(sequence_string)
Jérome Perrin's avatar
Jérome Perrin committed
663
    sequence_list.play(self, quiet=quiet)
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 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
  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))

748
  def test_06_getPropertyOnTempClass(self, quiet=quiet, run=1):
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
    """
    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
770
    sequence_list.play(self, quiet=quiet)
771

772 773 774 775 776 777
  def stepCheckEditMethod(self, sequence=None, 
                          sequence_list=None, **kw):
    """
    Check if edit method works.
    """
    object_instance = sequence.get('object_instance')
778 779 780 781
    object_instance.edit(title='toto')
    self.assertEquals(object_instance.getTitle(),'toto')
    object_instance.edit(title='tutu')
    self.assertEquals(object_instance.getTitle(),'tutu')
782 783 784 785 786 787 788

  def stepSetEditProperty(self, sequence=None, 
                          sequence_list=None, **kw):
    """
    Check if edit method works.
    """
    object_instance = sequence.get('object_instance')
789 790
    self.assertRaises(BadRequest,object_instance.setProperty, 'edit', 
                      "now this object is 'read only !!!'")
791

Jérome Perrin's avatar
Jérome Perrin committed
792
  def test_07_setEditProperty(self, quiet=quiet, run=run_all_test):
793 794 795 796 797 798 799 800 801 802 803 804 805
    """
    Test if setProperty erase existing accessors/methods.
    """
    if not run: return
    sequence_list = SequenceList()
    # Test on temp tempAmount.
    sequence_string = '\
              CreateObject \
              CheckEditMethod \
              SetEditProperty \
              CheckEditMethod \
              '
    sequence_list.addSequenceString(sequence_string)
Jérome Perrin's avatar
Jérome Perrin committed
806
    sequence_list.play(self, quiet=quiet)
807

808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
  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
851
  def test_07_setEditTalesExpression(self, quiet=quiet, run=run_all_test):
852 853 854 855 856 857 858 859 860 861 862 863 864
    """
    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
865
    sequence_list.play(self, quiet=quiet)
866
  
Jérome Perrin's avatar
Jérome Perrin committed
867
  def test_08_emptyObjectHasNoTitle(self, quiet=quiet, run=run_all_test):
868
    """Test that an empty object has no title.
869 870 871 872 873
    """
    if not run: return
    portal = self.getPortal()
    portal_type = "Organisation"
    module = portal.getDefaultModule(portal_type=portal_type)
874 875 876 877 878
    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 ''
    self.assertEquals('', obj.getProperty("title"))
    self.assertEquals('', obj.getTitle())
879

880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
  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)
  
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
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)

  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)

Romain Courteaud's avatar
Romain Courteaud committed
991 992 993 994 995 996 997
if __name__ == '__main__':
    framework()
else:
    import unittest
    def test_suite():
        suite = unittest.TestSuite()
        suite.addTest(unittest.makeSuite(TestBase))
998
        suite.addTest(unittest.makeSuite(TestERP5PropertyManager))
Romain Courteaud's avatar
Romain Courteaud committed
999
        return suite