testPreferences.py 29.6 KB
Newer Older
Łukasz Nowak's avatar
Łukasz Nowak committed
1
# -*- coding: utf-8 -*-
2 3
##############################################################################
#
Łukasz Nowak's avatar
Łukasz Nowak committed
4
# Copyright (c) 2005-2009 Nexedi SA and Contributors. All Rights Reserved.
5
#                    Jerome Perrin <jerome@nexedi.com>
Łukasz Nowak's avatar
Łukasz Nowak committed
6
#                    Łukasz Nowak <luke@nexedi.com>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#
# 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.
#
##############################################################################

31
import unittest
32
from unittest import expectedFailure
33

34 35
from AccessControl.SecurityManagement import noSecurityManager
from AccessControl.SecurityManagement import getSecurityManager
36
from zExceptions import Unauthorized
37
from AccessControl.ZopeGuards import guarded_hasattr
38
from DateTime import DateTime
39

40
from Products.ERP5Type.tests.testERP5Type import PropertySheetTestCase
41
from Products.ERP5Type.tests.utils import createZODBPythonScript
42
from Products.ERP5Form.PreferenceTool import Priority
43

44 45
# should match what's configured by default in HtmlStylePreference
default_large_image_height = 768
46 47


48
class TestPreferences(PropertySheetTestCase):
Łukasz Nowak's avatar
Łukasz Nowak committed
49

50
  def getTitle(self):
51
    return "Portal Preferences"
52 53 54

  def afterSetUp(self):
    uf = self.getPortal().acl_users
55
    uf._doAddUser('manager', '', ['Manager', 'Assignor', ], [])
56
    self.loginByUserName('manager')
57
    self.createPreferences()
58

59
  def beforeTearDown(self):
60
    self.abort()
61 62
    portal_preferences = self.getPreferenceTool()
    portal_preferences.manage_delObjects(list(portal_preferences.objectIds()))
63
    super(TestPreferences, self).beforeTearDown()
64

Łukasz Nowak's avatar
Łukasz Nowak committed
65
  def createPreferences(self):
66 67 68 69 70 71 72 73 74 75 76 77 78
    """ create some preferences objects  """
    portal_preferences = self.getPreferenceTool()
    ## create initial preferences
    person1 = portal_preferences.newContent(
        id='person1', portal_type='Preference')
    person2 = portal_preferences.newContent(
        id='person2', portal_type='Preference')
    group = portal_preferences.newContent(
        id='group', portal_type='Preference')
    group.setPriority(Priority.GROUP)
    site = portal_preferences.newContent(
        id='site', portal_type='Preference')
    site.setPriority(Priority.SITE)
Łukasz Nowak's avatar
Łukasz Nowak committed
79

80
    self.tic()
Łukasz Nowak's avatar
Łukasz Nowak committed
81

82
    # check preference levels are Ok
83 84 85 86
    self.assertEqual(person1.getPriority(), Priority.USER)
    self.assertEqual(person2.getPriority(), Priority.USER)
    self.assertEqual(group.getPriority(),   Priority.GROUP)
    self.assertEqual(site.getPriority(),    Priority.SITE)
87
    # check initial states
88 89 90 91
    self.assertEqual(person1.getPreferenceState(), 'disabled')
    self.assertEqual(person2.getPreferenceState(), 'disabled')
    self.assertEqual(group.getPreferenceState(),   'disabled')
    self.assertEqual(site.getPreferenceState(),    'disabled')
Łukasz Nowak's avatar
Łukasz Nowak committed
92

93 94 95
  def test_PreferenceToolTitle(self):
    """Tests that the title of the preference tool is correct.
    """
96
    self.assertEqual('Preferences', self.getPreferenceTool().Title())
97

Łukasz Nowak's avatar
Łukasz Nowak committed
98
  def test_AllowedContentTypes(self):
99 100
    """Tests Preference can be added in Preference Tool.
    """
101
    self.assertTrue('Preference' in [x.getId() for x in
102
           self.getPortal().portal_preferences.allowedContentTypes()])
103
    self.assertTrue('System Preference' in [x.getId() for x in
104
           self.getPortal().portal_preferences.allowedContentTypes()])
105

Łukasz Nowak's avatar
Łukasz Nowak committed
106
  def test_EnablePreferences(self):
107 108 109 110 111 112
    """ tests preference workflow """
    portal_workflow = self.getWorkflowTool()
    person1 = self.getPreferenceTool()['person1']
    person2 = self.getPreferenceTool()['person2']
    group = self.getPreferenceTool()['group']
    site = self.getPreferenceTool()['site']
Łukasz Nowak's avatar
Łukasz Nowak committed
113

114
    self.assertEqual(None, self.getPreferenceTool().getActivePreference())
115
    self.assertEqual(None, self.getPreferenceTool().getActiveUserPreference())
116 117
    self.assertEqual(None,
        self.getPreferenceTool().getActiveSystemPreference())
118

119 120
    person1.portal_workflow.doActionFor(
       person1, 'enable_action', wf_id='preference_workflow')
121
    self.commit()
122
    self.assertEqual(person1.getPreferenceState(), 'enabled')
Łukasz Nowak's avatar
Łukasz Nowak committed
123

124
    self.assertEqual( person1, self.getPreferenceTool().getActivePreference())
125
    self.assertEqual( person1, self.getPreferenceTool().getActiveUserPreference())
126 127
    self.assertEqual(None,
        self.getPreferenceTool().getActiveSystemPreference())
128

129 130
    portal_workflow.doActionFor(
       site, 'enable_action', wf_id='preference_workflow')
131 132
    self.assertEqual(person1.getPreferenceState(), 'enabled')
    self.assertEqual(site.getPreferenceState(),    'global')
133

134
    self.assertEqual(person1, self.getPreferenceTool().getActivePreference())
135
    self.assertEqual(person1, self.getPreferenceTool().getActiveUserPreference())
136 137
    self.assertEqual(None,
        self.getPreferenceTool().getActiveSystemPreference())
138

139 140
    portal_workflow.doActionFor(
       group, 'enable_action', wf_id='preference_workflow')
141 142 143
    self.assertEqual(person1.getPreferenceState(), 'enabled')
    self.assertEqual(group.getPreferenceState(),   'enabled')
    self.assertEqual(site.getPreferenceState(),    'global')
Łukasz Nowak's avatar
Łukasz Nowak committed
144

145
    self.assertEqual(person1, self.getPreferenceTool().getActivePreference())
146
    self.assertEqual(person1, self.getPreferenceTool().getActiveUserPreference())
147 148
    self.assertEqual(None,
        self.getPreferenceTool().getActiveSystemPreference())
149

150 151
    portal_workflow.doActionFor(
       person2, 'enable_action', wf_id='preference_workflow')
152
    self.commit()
153
    self.assertEqual(person2, self.getPreferenceTool().getActivePreference())
154
    self.assertEqual(person2, self.getPreferenceTool().getActiveUserPreference())
155 156
    self.assertEqual(None,
        self.getPreferenceTool().getActiveSystemPreference())
157
    self.assertEqual(person2.getPreferenceState(), 'enabled')
158
    self.tic()
159
    # enabling a preference disable all other of the same level
160 161 162
    self.assertEqual(person1.getPreferenceState(), 'disabled')
    self.assertEqual(group.getPreferenceState(),   'enabled')
    self.assertEqual(site.getPreferenceState(),    'global')
163

Łukasz Nowak's avatar
Łukasz Nowak committed
164
  def test_GetPreference(self):
165 166 167 168 169 170
    """ checks that getPreference returns the good preferred value"""
    portal_workflow = self.getWorkflowTool()
    pref_tool = self.getPreferenceTool()
    person1 = self.getPreferenceTool()['person1']
    group = self.getPreferenceTool()['group']
    site = self.getPreferenceTool()['site']
Łukasz Nowak's avatar
Łukasz Nowak committed
171

172 173 174 175 176 177
    portal_workflow.doActionFor(
       person1, 'enable_action', wf_id='preference_workflow')
    portal_workflow.doActionFor(
       group, 'enable_action', wf_id='preference_workflow')
    portal_workflow.doActionFor(
       site, 'enable_action', wf_id='preference_workflow')
178 179 180
    self.assertEqual(person1.getPreferenceState(), 'enabled')
    self.assertEqual(group.getPreferenceState(),   'enabled')
    self.assertEqual(site.getPreferenceState(),    'global')
181
    person1.setPreferredAccountingTransactionSimulationState([])
182
    self.assertEqual(
183
      person1.getPreferredAccountingTransactionSimulationState(), None)
184
    group.setPreferredAccountingTransactionSimulationState([])
185
    self.assertEqual(
186
      group.getPreferredAccountingTransactionSimulationState(), None)
187
    site.setPreferredAccountingTransactionSimulationState([])
188
    self.assertEqual(
189
      site.getPreferredAccountingTransactionSimulationState(), None)
190

191
    self.assertEqual(len(pref_tool.getPreference(
192
      'preferred_accounting_transaction_simulation_state_list')), 0)
Łukasz Nowak's avatar
Łukasz Nowak committed
193

194 195 196
    site.edit(
      preferred_accounting_transaction_simulation_state_list=
      ['stopped', 'delivered'])
197
    self.assertEqual(list(pref_tool.getPreference(
198
      'preferred_accounting_transaction_simulation_state_list')),
199
      list(site.getPreferredAccountingTransactionSimulationStateList()))
Łukasz Nowak's avatar
Łukasz Nowak committed
200

201 202 203 204 205
    # getPreference on the tool has the same behaviour as getProperty
    # on the preference (unless property is unset on this pref)
    for prop in ['preferred_accounting_transaction_simulation_state',
            'preferred_accounting_transaction_simulation_state_list']:

206
      self.assertEqual(pref_tool.getPreference(prop),
207
                        site.getProperty(prop))
Łukasz Nowak's avatar
Łukasz Nowak committed
208

209 210
    group.edit(
      preferred_accounting_transaction_simulation_state_list=['draft'])
211
    self.assertEqual(list(pref_tool.getPreference(
212
      'preferred_accounting_transaction_simulation_state_list')),
213
      list(group.getPreferredAccountingTransactionSimulationStateList()))
Łukasz Nowak's avatar
Łukasz Nowak committed
214

215
    person1.edit(preferred_accounting_transaction_simulation_state_list=
216
              ['cancelled'])
217
    self.assertEqual(list(pref_tool.getPreference(
218
      'preferred_accounting_transaction_simulation_state_list')),
219 220 221 222
      list(person1.getPreferredAccountingTransactionSimulationStateList()))
    # disable person -> group is selected
    self.getWorkflowTool().doActionFor(person1,
            'disable_action', wf_id='preference_workflow')
223
    self.commit()
224
    self.assertEqual(list(pref_tool.getPreference(
225
      'preferred_accounting_transaction_simulation_state_list')),
226 227
      list(group.getPreferredAccountingTransactionSimulationStateList()))

228
    self.assertEqual('default', pref_tool.getPreference(
229 230 231
                                        'this_does_not_exists', 'default'))


Łukasz Nowak's avatar
Łukasz Nowak committed
232
  def test_GetAttr(self):
233 234 235 236 237 238 239
    """ checks that preference methods can be called directly
      on portal_preferences """
    portal_workflow = self.getWorkflowTool()
    pref_tool = self.getPreferenceTool()
    person1 = self.getPreferenceTool()['person1']
    group = self.getPreferenceTool()['group']
    site = self.getPreferenceTool()['site']
240
    self.assertEqual(person1.getPreferenceState(), 'disabled')
241 242
    portal_workflow.doActionFor(
       group, 'enable_action', wf_id='preference_workflow')
243
    self.assertEqual(group.getPreferenceState(),    'enabled')
244 245
    portal_workflow.doActionFor(
       site, 'enable_action', wf_id='preference_workflow')
246
    self.assertEqual(site.getPreferenceState(),     'global')
247
    group.setPreferredAccountingTransactionSimulationStateList(['cancelled'])
Łukasz Nowak's avatar
Łukasz Nowak committed
248

249 250 251 252
    self.assertNotEquals( None,
      pref_tool.getPreferredAccountingTransactionSimulationStateList())
    self.assertNotEquals( [],
      list(pref_tool.getPreferredAccountingTransactionSimulationStateList()))
253
    self.assertEqual(
254 255
      list(pref_tool.getPreferredAccountingTransactionSimulationStateList()),
      list(pref_tool.getPreference(
256
         'preferred_accounting_transaction_simulation_state_list')))
257 258 259
    # standards attributes must not be looked up on Preferences
    self.assertNotEquals(pref_tool.getTitleOrId(), group.getTitleOrId())
    self.assertNotEquals(pref_tool.objectValues(), group.objectValues())
260
    self.assertNotEquals(pref_tool.getParentValue(), group.getParentValue())
261 262
    try :
      pref_tool.getPreferredNotExistingPreference()
263
      self.fail('Attribute error should be raised for dummy methods')
264 265
    except AttributeError :
      pass
Łukasz Nowak's avatar
Łukasz Nowak committed
266 267 268

  def test_SetPreference(self):
    """ check setting a preference modifies
269 270 271 272
     the first enabled user preference """
    portal_workflow = self.getWorkflowTool()
    pref_tool = self.getPreferenceTool()
    person1 = self.getPreferenceTool()['person1']
Łukasz Nowak's avatar
Łukasz Nowak committed
273

274 275
    portal_workflow.doActionFor(
       person1, 'enable_action', wf_id='preference_workflow')
276
    self.assertEqual(person1.getPreferenceState(),    'enabled')
277 278 279 280
    person1.setPreferredAccountingTransactionAtDate(DateTime(2005, 01, 01))
    pref_tool.setPreference(
      'preferred_accounting_transaction_at_date', DateTime(2004, 12, 31))
    self.tic()
281
    self.assertEqual(
282 283
      pref_tool.getPreferredAccountingTransactionAtDate(),
      DateTime(2004, 12, 31))
284
    self.assertEqual(
285 286 287
      person1.getPreferredAccountingTransactionAtDate(),
      DateTime(2004, 12, 31))

Łukasz Nowak's avatar
Łukasz Nowak committed
288
  def test_UserIndependance(self):
289 290 291 292 293
    """ check that the preferences are related to the user. """
    portal_workflow = self.getWorkflowTool()
    portal_preferences = self.getPreferenceTool()
    # create 2 users: user_a and user_b
    uf = self.getPortal().acl_users
294 295
    uf._doAddUser('user_a', '', ['Member', ], [])
    uf._doAddUser('user_b', '', ['Member', ], [])
Łukasz Nowak's avatar
Łukasz Nowak committed
296

297
    self.loginByUserName('user_a')
Łukasz Nowak's avatar
Łukasz Nowak committed
298

299 300 301 302 303
    # create 2 prefs as user_a
    user_a_1 = portal_preferences.newContent(
        id='user_a_1', portal_type='Preference')
    user_a_2 = portal_preferences.newContent(
        id='user_a_2', portal_type='Preference')
304
    self.tic()
305 306 307 308

    # enable a pref
    portal_workflow.doActionFor(
       user_a_1, 'enable_action', wf_id='preference_workflow')
309 310
    self.assertEqual(user_a_1.getPreferenceState(), 'enabled')
    self.assertEqual(user_a_2.getPreferenceState(), 'disabled')
Łukasz Nowak's avatar
Łukasz Nowak committed
311

312
    self.loginByUserName('user_b')
Łukasz Nowak's avatar
Łukasz Nowak committed
313

314 315 316 317
    # create a pref for user_b
    user_b_1 = portal_preferences.newContent(
        id='user_b_1', portal_type='Preference')
    user_b_1.setPreferredAccountingTransactionAtDate(DateTime(2002, 02, 02))
318
    self.tic()
Łukasz Nowak's avatar
Łukasz Nowak committed
319

320 321 322
    # enable this preference
    portal_workflow.doActionFor(
       user_b_1, 'enable_action', wf_id='preference_workflow')
323
    self.assertEqual(user_b_1.getPreferenceState(), 'enabled')
Łukasz Nowak's avatar
Łukasz Nowak committed
324

325
    # check user_a's preference is still enabled
326 327
    self.assertEqual(user_a_1.getPreferenceState(), 'enabled')
    self.assertEqual(user_a_2.getPreferenceState(), 'disabled')
Łukasz Nowak's avatar
Łukasz Nowak committed
328

329 330
    # Checks that a manager preference doesn't disable any other user
    # preferences
331
    self.loginByUserName('manager')
Łukasz Nowak's avatar
Łukasz Nowak committed
332

333 334
    self.assert_('Manager' in
      getSecurityManager().getUser().getRolesInContext(portal_preferences))
Łukasz Nowak's avatar
Łukasz Nowak committed
335

336 337 338 339 340
    # create a pref for manager
    manager_pref = portal_preferences.newContent(
        id='manager_pref', portal_type='Preference')
    manager_pref.setPreferredAccountingTransactionAtDate(
                                DateTime(2012, 12, 12))
341
    self.tic()
342 343 344
    # enable this preference
    portal_workflow.doActionFor(
       manager_pref, 'enable_action', wf_id='preference_workflow')
345
    self.assertEqual(manager_pref.getPreferenceState(), 'enabled')
346
    self.tic()
Łukasz Nowak's avatar
Łukasz Nowak committed
347

348
    # check users preferences are still enabled
349 350 351
    self.assertEqual(user_a_1.getPreferenceState(), 'enabled')
    self.assertEqual(user_b_1.getPreferenceState(), 'enabled')
    self.assertEqual(user_a_2.getPreferenceState(), 'disabled')
352

353 354 355 356
    # A user with Manager and Owner can view all preferences, because this user
    # is Manager and Owner, but for Manager, we have an exception, only
    # preferences actually owned by the user are taken into account.
    uf._doAddUser('manager_and_owner', '', ['Manager', 'Owner'], [])
357
    self.loginByUserName('manager_and_owner')
358 359
    self.assert_('Owner' in
      getSecurityManager().getUser().getRolesInContext(manager_pref))
360
    self.assertEqual(None,
361 362
        portal_preferences.getPreferredAccountingTransactionAtDate())

363 364 365 366 367 368 369 370 371
  def test_proxy_roles(self):
    # make sure we can get preferences in a script with proxy roles
    portal_workflow = self.getWorkflowTool()
    portal_preferences = self.getPreferenceTool()
    # create 2 users: user_a and user_b
    uf = self.portal.acl_users
    uf._doAddUser('user_a', '', ['Member', ], [])
    uf._doAddUser('user_b', '', ['Member', ], [])

372
    self.loginByUserName('user_a')
373 374 375 376 377 378 379

    user_a = portal_preferences.newContent(
        id='user_a', portal_type='Preference',
        # this preference have group priority, so preference for user_b would get
        # picked first
        priority=Priority.GROUP,
        preferred_accounting_transaction_simulation_state_list=['user_a'])
380
    self.tic()
381 382 383 384 385

    # enable a pref
    portal_workflow.doActionFor(
       user_a, 'enable_action', wf_id='preference_workflow')

386
    self.loginByUserName('user_b')
387 388 389 390
    # create a pref for user_b
    user_b = portal_preferences.newContent(
        id='user_b', portal_type='Preference',
        preferred_accounting_transaction_simulation_state_list=['user_b'])
391
    self.tic()
392 393 394 395

    # enable this preference
    portal_workflow.doActionFor(
       user_b, 'enable_action', wf_id='preference_workflow')
396

397
    self.login()
398 399 400 401 402
    script = createZODBPythonScript(
     self.portal.portal_skins.custom,
     'PreferenceTool_testPreferencesProxyRole', '',
     'return context.getPreferredAccountingTransactionSimulationStateList()')
    script.manage_proxy(['Manager'])
403

404
    self.loginByUserName('user_a')
405
    self.assertEqual(['user_a'],
406
        portal_preferences.PreferenceTool_testPreferencesProxyRole())
407

408 409 410 411 412 413 414
  def test_GlobalPreference(self):
    # globally enabled preference are preference for anonymous users.
    ptool = self.getPreferenceTool()
    ptool.site.setPreferredAccountingTransactionSimulationStateList(
                          ['this_is_visible_by_anonymous'])
    self.getPortal().portal_workflow.doActionFor(
                  ptool.site, 'enable_action', wf_id='preference_workflow')
415
    self.assertEqual('global', ptool.site.getPreferenceState())
416 417
    self.tic()
    noSecurityManager()
418
    self.assertEqual(['this_is_visible_by_anonymous'],
419
        ptool.getPreferredAccountingTransactionSimulationStateList())
420 421 422 423 424 425 426

  def test_GetDefault(self):
    portal_workflow = self.getWorkflowTool()
    pref_tool = self.getPreferenceTool()
    site = self.getPreferenceTool()['site']
    portal_workflow.doActionFor(
       site, 'enable_action', wf_id='preference_workflow')
427
    self.assertEqual(site.getPreferenceState(),    'global')
428 429 430

    method = pref_tool.getPreferredAccountingTransactionSimulationState
    state = method()
431
    self.assertEqual(state, [])
432
    state = method('default')
433
    self.assertEqual(state, 'default')
434

435 436
    method = lambda *args: pref_tool.getPreference('preferred_accounting_transaction_simulation_state', *args)
    state = method()
437
    self.assertEqual(state, [])
438
    state = method('default')
439
    self.assertEqual(state, 'default')
440

441 442
    method = pref_tool.getPreferredAccountingTransactionSimulationStateList
    state_list = method()
443
    self.assertEqual(state_list, [])
444
    state_list = method(('default',))
445 446
    # Initially, tuples were always casted to lists. This is not the case
    # anymore when preference_tool.getXxxList returns the default value.
447
    self.assertEqual(state_list, ('default',))
Łukasz Nowak's avatar
Łukasz Nowak committed
448

449 450
    method = lambda *args: pref_tool.getPreference('preferred_accounting_transaction_simulation_state_list', *args)
    state_list = method()
451
    self.assertEqual(state_list, [])
452
    state_list = method(('default',))
453
    self.assertEqual(state_list, ('default',))
454

455 456
  def test_Permissions(self):
    # create a new site preference for later
457 458
    preference_tool = self.portal.portal_preferences
    site_pref = preference_tool.newContent(
459 460 461
                          portal_type='Preference',
                          priority=Priority.SITE)
    self.portal.portal_workflow.doActionFor(site_pref, 'enable_action')
462
    self.assertEqual(site_pref.getPreferenceState(), 'global')
Łukasz Nowak's avatar
Łukasz Nowak committed
463

464 465 466 467
    # Members can add new preferences
    uf = self.getPortal().acl_users
    uf._doAddUser('member', '', ['Member', ], [])
    member = uf.getUserById('member').__of__(uf)
468
    self.loginByUserName('member')
469
    user_pref = preference_tool.newContent(portal_type='Preference')
470 471 472

    # Members can copy & paste existing preferences
    user_pref.Base_createCloneDocument()
473 474 475 476
    # note that copy & pasting a site preference reset the priority to USER
    # preference.
    cp_data = preference_tool.manage_copyObjects(ids=[site_pref.getId()])
    copy_id = preference_tool.manage_pasteObjects(cp_data)[0]['new_id']
477
    self.assertEqual(Priority.USER, preference_tool[copy_id].getPriority())
478

479 480 481
    # Globally enabled preferences can be viewed by Members
    self.assertTrue(member.has_permission('View', site_pref))

482
    # Member does not have Manage properties on their own preferences,
483 484 485
    # otherwise the "Metadata" tab is shown
    self.assertFalse(member.has_permission(
                         'Manage properties', user_pref))
486

Łukasz Nowak's avatar
Łukasz Nowak committed
487

488
  def test_SystemPreference(self):
489 490 491 492
    # We want to test a property with a default value defined
    self.assertTrue(default_large_image_height > 0)
    large_image_height = default_large_image_height + 1

493
    preference_tool = self.portal.portal_preferences
494
    system_pref = preference_tool.newContent(
495
                          portal_type='System Preference',
496
                          preferred_document_conversion_server_url='http://127.0.0.1',
497
                          priority=Priority.SITE)
498
    # check not taken into account if not enabled
499
    self.assertEqual(None,
500 501 502
                     preference_tool.getPreferredDocumentConversionServerUrl())
    self.assertEqual('http://localhost',
                     preference_tool.getPreferredDocumentConversionServerUrl('http://localhost'))
503 504
    self.assertEqual(default_large_image_height,
                     preference_tool.getPreferredLargeImageHeight())
505
    # enable it and check preference is returned
506 507
    self.portal.portal_workflow.doActionFor(system_pref, 'enable_action')
    self.assertEqual(system_pref.getPreferenceState(), 'global')
508
    self.tic()
509 510 511 512
    self.assertEqual('http://127.0.0.1',
                     preference_tool.getPreferredDocumentConversionServerUrl())
    self.assertEqual('http://127.0.0.1',
                     preference_tool.getPreferredDocumentConversionServerUrl('http://localhost'))
513 514
    self.assertEqual(default_large_image_height,
                     preference_tool.getPreferredLargeImageHeight())
515 516 517 518
    # Default value passed by parameter has priority over the default in the
    # property sheet.
    self.assertEqual(large_image_height,
                     preference_tool.getPreferredLargeImageHeight(large_image_height))
Łukasz Nowak's avatar
Łukasz Nowak committed
519

520 521 522
    # Members can't add new system preferences
    uf = self.getPortal().acl_users
    uf._doAddUser('member', '', ['Member', ], [])
523
    self.loginByUserName('member')
524 525
    self.assertRaises(Unauthorized, preference_tool.newContent, portal_type='System Preference')
    # But they can see others
526
    system_pref.view()
527
    # check accessors works
528
    system_pref.setPreferredDocumentConversionServerUrl('http://1.2.3.4')
529
    self.tic()
530 531 532 533
    self.assertEqual('http://1.2.3.4',
                     preference_tool.getPreferredDocumentConversionServerUrl())
    self.assertEqual('http://1.2.3.4',
                     preference_tool.getPreferredDocumentConversionServerUrl('http://localhost'))
534 535
    self.assertEqual(default_large_image_height,
                     preference_tool.getPreferredLargeImageHeight())
536

537
    # create a user pref and check it doesn't outranks the system one if
538 539 540 541
    # they both defined same pref
    user_pref = preference_tool.newContent(
                          portal_type='Preference',
                          priority=Priority.USER)
542
    user_pref.setPreferredLargeImageHeight(large_image_height)
543
    self.portal.portal_workflow.doActionFor(user_pref, 'enable_action')
544
    self.assertEqual(user_pref.getPreferenceState(), 'enabled')
545
    self.tic()
546 547
    self.assertEqual('http://1.2.3.4',
                     preference_tool.getPreferredDocumentConversionServerUrl('http://localhost'))
548 549
    self.assertEqual(large_image_height,
                     preference_tool.getPreferredLargeImageHeight())
550 551
    self.assertEqual(large_image_height,
                     preference_tool.getPreferredLargeImageHeight(0))
552 553

    # check a user can't edit preference which are marked for manager
554
    self.assertRaises(Unauthorized, user_pref.edit, preferred_document_conversion_server_url="http://localhost")
555 556 557
    # even if there is System Preference enabled getActivePreference shall return
    # user preference
    self.assertEqual(user_pref, preference_tool.getActivePreference())
558
    self.assertEqual(user_pref, preference_tool.getActiveUserPreference())
559
    self.assertEqual(system_pref, preference_tool.getActiveSystemPreference())
560

561
  def test_boolean_accessor(self):
562 563 564 565 566 567
    self._addProperty('Preference',
        'test_boolean_accessor Preference',
        portal_type='Standard Property',
        property_id='dummy',
        preference=True,
        elementary_type='boolean')
568 569 570 571 572 573 574 575 576 577 578
    portal_preferences = self.portal.portal_preferences
    self.assertFalse(portal_preferences.getDummy())
    self.assertFalse(portal_preferences.isDummy())

    preference = portal_preferences.newContent(portal_type='Preference',
                                               dummy=True)
    preference.enable()
    self.tic()

    self.assertTrue(portal_preferences.getDummy())
    self.assertTrue(portal_preferences.isDummy())
579 580 581 582 583 584

  def test_property_sheet_security_on_permission(self):
    """ Added a test to make sure permissions are used into portal
        preference level. """
    write_permission = 'Modify portal content'
    read_permission = 'Manage portal'
585

586 587 588 589 590 591 592 593
    self._addProperty('Preference',
        'test_property_sheet_security_on_permission Preference',
        property_id='preferred_toto',
        portal_type='Standard Property',
        preference=1,
        write_permission='Modify portal content',
        read_permission='Manage portal',
        elementary_type='string')
594 595 596 597

    obj = self.portal.portal_preferences.newContent(portal_type='Preference')
    obj.enable()
    self.tic()
598

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    self.assertTrue(guarded_hasattr(obj, 'setPreferredToto'))
    obj.setPreferredToto("A TEST")
    self.assertTrue(guarded_hasattr(obj, 'getPreferredToto'))

    obj.manage_permission(write_permission, [], 0)
    self.assertFalse(guarded_hasattr(obj, 'setPreferredToto'))
    self.assertTrue(guarded_hasattr(obj, 'getPreferredToto'))

    obj.manage_permission(write_permission, ['Manager'], 1)
    obj.manage_permission(read_permission, [], 0)
    self.assertTrue(guarded_hasattr(obj, 'setPreferredToto'))
    self.assertFalse(guarded_hasattr(obj, 'getPreferredToto'))

    obj.manage_permission(read_permission, ['Manager'], 1)

    self.tic()

    preference_tool = self.portal.portal_preferences
    self.assertTrue(guarded_hasattr(preference_tool, 'getPreferredToto'))
618
    self.assertEqual("A TEST", preference_tool.getPreferredToto())
619 620 621 622 623 624 625 626 627 628

    preference_tool.manage_permission(write_permission, [], 0)
    self.assertTrue(guarded_hasattr(preference_tool, 'getPreferredToto'))

    preference_tool.manage_permission(write_permission, ['Manager'], 1)
    preference_tool.manage_permission(read_permission, [], 0)
    obj.manage_permission(read_permission, [], 0)
    self.assertFalse(guarded_hasattr(preference_tool, 'getPreferredToto'))

    preference_tool.manage_permission(read_permission, ['Manager'], 1)
629

630 631 632 633
  def test_system_preference_value_prefererred(self):
    default_preference_string = 'Default Name'
    normal_preference_string = 'Normal Preference'
    system_preference_string = 'System Preference'
634 635 636 637 638 639 640
    self._addProperty('Preference',
        'test_system_preference_value_prefererred Preference',
        portal_type='Standard Property',
        property_id='dummystring',
        property_default='python: "%s"' % default_preference_string,
        preference=True,
        elementary_type='string')
641 642 643 644 645 646 647 648 649 650 651 652 653
    portal_preferences = self.portal.portal_preferences
    self.assertEqual(default_preference_string,
        portal_preferences.getDummystring())

    preference = portal_preferences.newContent(portal_type='Preference',
                                               dummystring=normal_preference_string,
                                               priority=Priority.SITE)
    preference.enable()
    self.tic()

    self.assertEqual(normal_preference_string,
        portal_preferences.getDummystring())

Łukasz Nowak's avatar
Łukasz Nowak committed
654
    system_preference = portal_preferences.newContent(portal_type='System Preference',
655 656 657 658 659 660 661
                                               dummystring=system_preference_string)
    system_preference.enable()
    self.tic()

    self.assertEqual(system_preference_string,
        portal_preferences.getDummystring())

662
  @expectedFailure
663
  def test_system_preference_value_prefererred_clear_cache_disabled(self):
664 665 666
    default_preference_string = 'Default Name'
    normal_preference_string = 'Normal Preference'
    system_preference_string = 'System Preference'
667 668 669 670 671 672 673
    self._addProperty('Preference',
        'test_system_preference_value_prefererred_clear_cache_disabled Preference',
        portal_type='Standard Property',
        property_id='dummystring',
        property_default='python: "%s"' % default_preference_string,
        preference=True,
        elementary_type='string')
674 675 676 677 678 679 680 681 682 683 684 685 686
    portal_preferences = self.portal.portal_preferences
    self.assertEqual(default_preference_string,
        portal_preferences.getDummystring())

    preference = portal_preferences.newContent(portal_type='Preference',
                                               dummystring=normal_preference_string,
                                               priority=Priority.SITE)
    preference.enable()
    self.tic()

    self.assertEqual(normal_preference_string,
        portal_preferences.getDummystring())

687 688 689 690
    # simulate situation when _clearCache does nothing, for example in case
    # if memcached or any other non-deleteable cache is used
    from Products.ERP5Form.Document.Preference import Preference
    Preference._clearCache = lambda *args,**kwargs: None
691 692 693 694 695 696 697
    system_preference = portal_preferences.newContent(portal_type='System Preference',
                                               dummystring=system_preference_string)
    system_preference.enable()
    self.tic()

    self.assertEqual(system_preference_string,
        portal_preferences.getDummystring())
698

699 700 701 702
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestPreferences))
  return suite