Commit 6b742292 authored by Jérome Perrin's avatar Jérome Perrin Committed by Eteri

PreferenceTool: introduce a new getActiveUserPreference method

This method creates a user preference if no preference exists.
parent 495b9aba
......@@ -1157,7 +1157,7 @@ class TestERP5Base(ERP5TypeTestCase):
def test_user_creation(self):
person = self.portal.person_module.newContent(portal_type='Person')
assignment = person.newContent(portal_type='Assignment',
group='nexedi')
group='nexedi/storever')
self.assertNotEquals(None, assignment.getGroupValue())
assignment.open()
self.portal.portal_workflow.doActionFor(person, 'create_user_action',
......@@ -1170,13 +1170,21 @@ class TestERP5Base(ERP5TypeTestCase):
user = self.portal.acl_users.getUser('user_login')
self.assertNotEquals(None, user)
# and this user has a preference created
# This user does not have a preference created automatically ...
newSecurityManager(None, user.__of__(self.portal.acl_users))
self.assertEquals(None,
self.portal.portal_catalog.getResultValue(portal_type='Preference',
owner=user.getId()))
# ... but only when `getActiveUserPreference`
preference = self.portal.portal_preferences.getActiveUserPreference()
self.assertNotEquals(None, preference)
self.tic()
self.assertNotEquals(None,
self.portal.portal_catalog.getResultValue(portal_type='Preference',
owner=user.getId()))
# for his assignent group
self.assertEqual('group/nexedi',
# for his assignment group
self.assertEqual('group/nexedi/storever',
self.portal.portal_preferences.getPreferredSectionCategory())
def test_default_address_acquisition(self):
......
......@@ -241,19 +241,6 @@ class TestPerson(ERP5TypeTestCase):
self.assertEqual(None, p.getPassword())
self.assertEqual('default', p.getPassword('default'))
def testPreferenceInteractionWorkflow(self):
""" when setting reference, a script create preference is
called by activities, check this behavior. """
person_module = self.getPersonModule()
title = "Séb"
person = person_module.newContent(portal_type='Person', title=title)
person.setReference('test_seb')
self.tic()
portal = self.getPortal()
last_id = portal.portal_preferences.getLastId()
last_preference = portal.portal_preferences[last_id]
self.assertTrue("Séb" in last_preference.getTitle())
def test_suite():
suite = unittest.TestSuite()
......
......@@ -197,6 +197,27 @@ class PreferenceTool(BaseTool):
Note that this preference may be read only. """
return self._getActivePreferenceByPortalType('Preference')
security.declareProtected(Permissions.View, 'getActiveUserPreference')
def getActiveUserPreference(self) :
""" returns the current user preference for the user.
If no preference exists, then try to create one with `createUserPreference`
type based method.
This method returns a preference that the user will be able to edit or
None, if `createUserPreference` refused to create a preference.
It is intendended for "click here to edit your preferences" actions.
"""
active_preference = self.getActivePreference()
if active_preference is None or active_preference.getPriority() != Priority.USER:
# If user does not have a preference, let's try to create one
user = self.getPortalObject().portal_membership.getAuthenticatedMember().getUserValue()
if user is not None:
createUserPreference = user.getTypeBasedMethod('createUserPreference')
if createUserPreference is not None:
active_preference = createUserPreference()
return active_preference
security.declareProtected(Permissions.View, 'getActiveSystemPreference')
def getActiveSystemPreference(self) :
""" returns the current system preference for the user.
......
......@@ -112,6 +112,7 @@ class TestPreferences(PropertySheetTestCase):
site = self.getPreferenceTool()['site']
self.assertEqual(None, self.getPreferenceTool().getActivePreference())
self.assertEqual(None, self.getPreferenceTool().getActiveUserPreference())
self.assertEqual(None,
self.getPreferenceTool().getActiveSystemPreference())
......@@ -121,6 +122,7 @@ class TestPreferences(PropertySheetTestCase):
self.assertEqual(person1.getPreferenceState(), 'enabled')
self.assertEqual( person1, self.getPreferenceTool().getActivePreference())
self.assertEqual( person1, self.getPreferenceTool().getActiveUserPreference())
self.assertEqual(None,
self.getPreferenceTool().getActiveSystemPreference())
......@@ -130,6 +132,7 @@ class TestPreferences(PropertySheetTestCase):
self.assertEqual(site.getPreferenceState(), 'global')
self.assertEqual(person1, self.getPreferenceTool().getActivePreference())
self.assertEqual(person1, self.getPreferenceTool().getActiveUserPreference())
self.assertEqual(None,
self.getPreferenceTool().getActiveSystemPreference())
......@@ -140,6 +143,7 @@ class TestPreferences(PropertySheetTestCase):
self.assertEqual(site.getPreferenceState(), 'global')
self.assertEqual(person1, self.getPreferenceTool().getActivePreference())
self.assertEqual(person1, self.getPreferenceTool().getActiveUserPreference())
self.assertEqual(None,
self.getPreferenceTool().getActiveSystemPreference())
......@@ -147,6 +151,7 @@ class TestPreferences(PropertySheetTestCase):
person2, 'enable_action', wf_id='preference_workflow')
self.commit()
self.assertEqual(person2, self.getPreferenceTool().getActivePreference())
self.assertEqual(person2, self.getPreferenceTool().getActiveUserPreference())
self.assertEqual(None,
self.getPreferenceTool().getActiveSystemPreference())
self.assertEqual(person2.getPreferenceState(), 'enabled')
......@@ -550,6 +555,7 @@ class TestPreferences(PropertySheetTestCase):
# even if there is System Preference enabled getActivePreference shall return
# user preference
self.assertEqual(user_pref, preference_tool.getActivePreference())
self.assertEqual(user_pref, preference_tool.getActiveUserPreference())
self.assertEqual(system_pref, preference_tool.getActiveSystemPreference())
def test_boolean_accessor(self):
......
......@@ -379,6 +379,31 @@ class TestUserManagement(ERP5TypeTestCase):
user_id,
)
def test_Preference_created_for_new_user_on_getActiveUserPreference(self):
# Creating a user will create a preference on the first time `getActiveUserPreference`
# is called
preference_tool = self.portal.portal_preferences
preference_count = len(preference_tool.contentValues())
user_id, login, password = self._makePerson()
# creating a person does not create a preference
self.assertEqual(preference_count, len(preference_tool.contentValues()))
self.loginAsUser(user_id)
# getActiveUserPreference will create a user preference
new_preference = preference_tool.getActiveUserPreference()
self.assertNotEqual(None, new_preference)
self.assertEqual(preference_count+1, len(preference_tool.contentValues()))
self.assertEqual('enabled', new_preference.getPreferenceState())
self.tic()
# subsequent calls to getActiveUserPreference returns the same preference
active_preference = preference_tool.getActiveUserPreference()
self.assertEqual(active_preference, new_preference)
self.assertEqual(preference_count+1, len(preference_tool.contentValues()))
def test_PreferenceTool_setNewPassword(self):
# Preference Tool has an action to change password
user_id, login, password = self._makePerson()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment