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.

221 lines
7.4 KiB

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