diff --git a/partner_firstname/models/res_user.py b/partner_firstname/models/res_user.py index 5b0f7c8fc..efe6f9a9f 100644 --- a/partner_firstname/models/res_user.py +++ b/partner_firstname/models/res_user.py @@ -28,3 +28,10 @@ class ResUser(models.Model): result[field] = inverted.get(field) return result + + @api.onchange("firstname", "lastname") + def _compute_name(self): + """Write the 'name' field according to splitted data.""" + for rec in self: + rec.name = rec.partner_id._get_computed_name( + rec.lastname, rec.firstname) diff --git a/partner_firstname/tests/__init__.py b/partner_firstname/tests/__init__.py index 2692c082c..1fed38100 100644 --- a/partner_firstname/tests/__init__.py +++ b/partner_firstname/tests/__init__.py @@ -8,5 +8,6 @@ from . import ( test_delete, test_empty, test_name, - test_onchange + test_onchange, + test_user_onchange ) diff --git a/partner_firstname/tests/test_user_onchange.py b/partner_firstname/tests/test_user_onchange.py new file mode 100644 index 000000000..46fe41b74 --- /dev/null +++ b/partner_firstname/tests/test_user_onchange.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# © 2016 Yannick Vaucher (Camptocamp SA) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp.tests.common import TransactionCase + + +class UserOnchangeCase(TransactionCase): + + def test_create_from_form_only_firstname(self): + """In a new users form, a user set only the firstname.""" + firstname = u"Zoë" + with self.env.do_in_onchange(): + # Changes firstname, which triggers onchanges + self.user.firstname = firstname + self.user._compute_name() + + self.assertEqual(self.user.lastname, False) + self.assertEqual(self.user.firstname, firstname) + self.assertEqual(self.user.name, firstname) + + def test_create_from_form_only_lastname(self): + """In a new user form, a user set only the lastname.""" + lastname = u"Żywioł" + with self.env.do_in_onchange(): + # Changes lastname, which triggers onchanges + self.user.lastname = lastname + self.user._compute_name() + + self.assertEqual(self.user.firstname, False) + self.assertEqual(self.user.lastname, lastname) + self.assertEqual(self.user.name, lastname) + + def test_create_from_form_all(self): + """In a new user form, a user set all names.""" + firstname = u"Zoë" + lastname = u"Żywioł" + with self.env.do_in_onchange(): + # Changes firstname, which triggers onchanges + self.user.firstname = firstname + self.user._compute_name() + + # Changes lastname, which triggers onchanges + self.user.lastname = lastname + self.user._compute_name() + + self.assertEqual(self.user.lastname, lastname) + self.assertEqual(self.user.firstname, firstname) + self.assertEqual(self.user.name, u" ".join((lastname, firstname))) + + def setUp(self): + super(UserOnchangeCase, self).setUp() + self.user = self.env["res.users"].new()