You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
7.4 KiB

  1. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  2. # Copyright 2015 Grupo ESOC Ingeniería de Servicios, S.L.U.
  3. # Copyright 2015 Antiun Ingenieria S.L. - Antonio Espinosa
  4. """These tests try to mimic the behavior of the UI form.
  5. The form operates in onchange mode, with its limitations.
  6. """
  7. from odoo.tests.common import TransactionCase
  8. class OnChangeCase(TransactionCase):
  9. is_company = False
  10. def setUp(self):
  11. super(OnChangeCase, self).setUp()
  12. self.env['ir.config_parameter'].set_param(
  13. 'partner_names_order', 'last_first_comma')
  14. def new_partner(self):
  15. """Create an empty partner. Ensure it is (or not) a company."""
  16. new = self.env["res.partner"].new()
  17. new.is_company = self.is_company
  18. return new
  19. class PartnerCompanyCase(OnChangeCase):
  20. is_company = True
  21. def tearDown(self):
  22. """Companies never have ``firstname`` nor ``lastname2``."""
  23. super(PartnerCompanyCase, self).tearDown()
  24. self.assertEqual(self.partner.firstname, False)
  25. self.assertEqual(self.partner.lastname2, False)
  26. def set_name(self, value):
  27. self.partner.name = value
  28. # It triggers onchange
  29. self.partner._onchange_name()
  30. # Ensure it's properly set
  31. self.assertEqual(self.partner.name, value)
  32. def test_create_from_form(self):
  33. """A user creates a company from the form."""
  34. name = "Sôme company"
  35. with self.env.do_in_onchange():
  36. # User presses ``new``
  37. self.partner = self.new_partner()
  38. # User changes fields
  39. self.set_name(name)
  40. self.assertEqual(self.partner.lastname, name)
  41. def test_empty_name_and_subnames(self):
  42. """If the user empties ``name``, subnames must be ``False``.
  43. Otherwise, the ``required`` attr will not work as expected.
  44. """
  45. with self.env.do_in_onchange():
  46. # User presses ``new``
  47. self.partner = self.new_partner()
  48. # User changes fields
  49. self.set_name("Fóo")
  50. self.set_name("")
  51. self.assertEqual(self.partner.lastname, False)
  52. class PartnerContactCase(OnChangeCase):
  53. def set_field(self, field, value):
  54. # Changes the field
  55. setattr(self.partner, field, value)
  56. if field in ("firstname", "lastname", "lastname2"):
  57. # Trigger onchanges
  58. self.partner._onchange_subnames()
  59. self.partner._onchange_name()
  60. # Check it's set OK
  61. self.assertEqual(getattr(self.partner, field), value)
  62. def test_create_from_form_empty(self):
  63. """A user creates a contact from the form.
  64. All subfields must be false, or the ``required`` attr will not work as
  65. expected.
  66. """
  67. with self.env.do_in_onchange():
  68. # User presses ``new``
  69. self.partner = self.new_partner()
  70. # Odoo tries to compute the name
  71. self.partner._compute_name()
  72. # This is then triggered
  73. self.partner._onchange_name()
  74. # Subnames must start as False to make the UI work fine
  75. self.assertEqual(self.partner.firstname, False)
  76. self.assertEqual(self.partner.lastname, False)
  77. self.assertEqual(self.partner.lastname2, False)
  78. # ``name`` cannot be False, or upstream Odoo will fail
  79. self.assertEqual(self.partner.name, "")
  80. def test_create_from_form_only_firstname(self):
  81. """A user creates a contact with only the firstname from the form."""
  82. firstname = "Fïrst"
  83. with self.env.do_in_onchange():
  84. # User presses ``new``
  85. self.partner = self.new_partner()
  86. # User changes fields
  87. self.set_field("firstname", firstname)
  88. self.assertEqual(self.partner.lastname, False)
  89. self.assertEqual(self.partner.lastname2, False)
  90. self.assertEqual(self.partner.name, firstname)
  91. def test_create_from_form_only_lastname(self):
  92. """A user creates a contact with only the lastname from the form."""
  93. lastname = "Läst"
  94. with self.env.do_in_onchange():
  95. # User presses ``new``
  96. self.partner = self.new_partner()
  97. # User changes fields
  98. self.set_field("lastname", lastname)
  99. self.assertEqual(self.partner.firstname, False)
  100. self.assertEqual(self.partner.lastname2, False)
  101. self.assertEqual(self.partner.name, lastname)
  102. def test_create_from_form_only_lastname2(self):
  103. """A user creates a contact with only the lastname2 from the form."""
  104. lastname2 = "Läst2"
  105. with self.env.do_in_onchange():
  106. # User presses ``new``
  107. self.partner = self.new_partner()
  108. # User changes fields
  109. self.set_field("lastname2", lastname2)
  110. self.assertEqual(self.partner.firstname, False)
  111. self.assertEqual(self.partner.lastname, False)
  112. self.assertEqual(self.partner.name, lastname2)
  113. def test_create_from_without_firstname(self):
  114. """A user creates a contact without firstname from the form."""
  115. lastname = "Läst"
  116. lastname2 = "Läst2"
  117. with self.env.do_in_onchange():
  118. # User presses ``new``
  119. self.partner = self.new_partner()
  120. # User changes fields
  121. self.set_field("lastname", lastname)
  122. self.set_field("lastname2", lastname2)
  123. self.assertEqual(self.partner.firstname, False)
  124. self.assertEqual(
  125. self.partner.name,
  126. "%s %s" % (lastname, lastname2))
  127. def test_create_from_without_lastname(self):
  128. """A user creates a contact without lastname from the form."""
  129. firstname = "Fïrst"
  130. lastname2 = "Läst2"
  131. with self.env.do_in_onchange():
  132. # User presses ``new``
  133. self.partner = self.new_partner()
  134. # User changes fields
  135. self.set_field("firstname", firstname)
  136. self.set_field("lastname2", lastname2)
  137. self.assertEqual(self.partner.lastname, False)
  138. self.assertEqual(
  139. self.partner.name,
  140. "%s, %s" % (lastname2, firstname))
  141. def test_create_from_without_lastname2(self):
  142. """A user creates a contact without lastname2 from the form."""
  143. firstname = "Fïrst"
  144. lastname = "Läst"
  145. with self.env.do_in_onchange():
  146. # User presses ``new``
  147. self.partner = self.new_partner()
  148. # User changes fields
  149. self.set_field("firstname", firstname)
  150. self.set_field("lastname", lastname)
  151. self.assertEqual(self.partner.lastname2, False)
  152. self.assertEqual(
  153. self.partner.name,
  154. "%s, %s" % (lastname, firstname))
  155. def test_create_from_form_all(self):
  156. """A user creates a contact with all names from the form."""
  157. firstname = "Fïrst"
  158. lastname = "Läst"
  159. lastname2 = "Läst2"
  160. with self.env.do_in_onchange():
  161. # User presses ``new``
  162. self.partner = self.new_partner()
  163. # User changes fields
  164. self.set_field("firstname", firstname)
  165. self.set_field("lastname", lastname)
  166. self.set_field("lastname2", lastname2)
  167. self.assertEqual(
  168. self.partner.name,
  169. "%s %s, %s" % (lastname, lastname2, firstname))