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.

196 lines
8.1 KiB

  1. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  2. from odoo.tests import common
  3. class PartnerContactInSeveralCompaniesCase(common.TransactionCase):
  4. def setUp(self):
  5. """*****setUp*****"""
  6. super(PartnerContactInSeveralCompaniesCase, self).setUp()
  7. self.partner = self.env["res.partner"]
  8. self.action = self.env["ir.actions.act_window"]
  9. current_module = "partner_contact_in_several_companies"
  10. # Get test records reference
  11. self.main_partner = self.env.ref("base.main_partner")
  12. self.bob_contact = self.env.ref("%s.res_partner_contact1" % current_module)
  13. self.bob_job1 = self.env.ref(
  14. "%s.res_partner_contact1_work_position1" % current_module
  15. )
  16. self.roger_contact = self.env.ref("base.res_partner_main2")
  17. self.roger_job2 = self.env.ref(
  18. "%s.res_partner_main2_position_consultant" % current_module
  19. )
  20. def test_00_show_only_standalone_contact(self):
  21. """Check that only standalone contact are shown if context
  22. explicitly state to not display all positions
  23. """
  24. ctx = {"search_show_all_positions": {"is_set": True, "set_value": False}}
  25. partner_ids = self.partner.with_context(ctx).search([])
  26. self.assertTrue(self.bob_job1 not in partner_ids)
  27. self.assertTrue(self.roger_job2 not in partner_ids)
  28. def test_01_show_all_positions(self):
  29. """Check that all contact are show if context is empty or
  30. explicitly state to display all positions or the "is_set"
  31. value has been set to False.
  32. """
  33. partner_ids = self.partner.search([])
  34. self.assertTrue(self.bob_job1 in partner_ids)
  35. self.assertTrue(self.roger_job2 in partner_ids)
  36. ctx = {"search_show_all_positions": {"is_set": False}}
  37. partner_ids = self.partner.with_context(ctx).search([])
  38. self.assertTrue(self.bob_job1 in partner_ids)
  39. self.assertTrue(self.roger_job2 in partner_ids)
  40. ctx = {"search_show_all_positions": {"is_set": True, "set_value": True}}
  41. partner_ids = self.partner.with_context(ctx).search([])
  42. self.assertTrue(self.bob_job1 in partner_ids)
  43. self.assertTrue(self.roger_job2 in partner_ids)
  44. def test_02_reading_other_contact_one2many_show_all_positions(self):
  45. """Check that readonly partner's ``other_contact_ids`` return
  46. all values whatever the context
  47. """
  48. ctx = {}
  49. self.assertEqual(
  50. self.bob_job1, self.bob_contact.with_context(ctx).other_contact_ids
  51. )
  52. ctx = {"search_show_all_positions": {"is_set": False}}
  53. self.assertEqual(
  54. self.bob_job1, self.bob_contact.with_context(ctx).other_contact_ids
  55. )
  56. ctx = {"search_show_all_positions": {"is_set": True, "set_value": False}}
  57. self.assertEqual(
  58. self.bob_job1, self.bob_contact.with_context(ctx).other_contact_ids
  59. )
  60. ctx = {"search_show_all_positions": {"is_set": True, "set_value": True}}
  61. self.assertEqual(
  62. self.bob_job1, self.bob_contact.with_context(ctx).other_contact_ids
  63. )
  64. ctx = {}
  65. self.assertIn(self.bob_job1, self.main_partner.with_context(ctx).child_ids)
  66. ctx = {"search_show_all_positions": {"is_set": False}}
  67. self.assertIn(self.bob_job1, self.main_partner.with_context(ctx).child_ids)
  68. ctx = {"search_show_all_positions": {"is_set": True, "set_value": False}}
  69. self.assertIn(self.bob_job1, self.main_partner.with_context(ctx).child_ids)
  70. ctx = {"search_show_all_positions": {"is_set": True, "set_value": True}}
  71. self.assertIn(self.bob_job1, self.main_partner.with_context(ctx).child_ids)
  72. def test_03_search_match_attached_contacts(self):
  73. """Check that searching partner also return partners having
  74. attached contacts matching search criteria
  75. """
  76. # Bob's contact has one other position which is related to
  77. # 'YourCompany'
  78. # so search for all contacts working for 'YourCompany'
  79. # should contain Bob position.
  80. partner_ids = self.partner.search([("parent_id", "ilike", "YourCompany")])
  81. self.assertTrue(self.bob_job1 in partner_ids)
  82. # but when searching without 'all positions',
  83. # we should get the position standalone contact instead.
  84. ctx = {"search_show_all_positions": {"is_set": True, "set_value": False}}
  85. partner_ids = self.partner.with_context(ctx).search(
  86. [("parent_id", "ilike", "YourCompany")]
  87. )
  88. self.assertTrue(self.bob_contact in partner_ids)
  89. def test_04_contact_creation(self):
  90. """Check that we're begin to create a contact"""
  91. # Create a contact using only name
  92. new_contact = self.partner.create({"name": "Bob Egnops"})
  93. self.assertEqual(new_contact.contact_type, "standalone")
  94. # Create a contact with only contact_id
  95. new_contact = self.partner.create({"contact_id": self.bob_contact.id})
  96. self.assertEqual(new_contact.name, "Bob Egnops")
  97. self.assertEqual(new_contact.contact_type, "attached")
  98. # Create a contact with both contact_id and name;
  99. # contact's name should override provided value in that case
  100. new_contact = self.partner.create(
  101. {"contact_id": self.bob_contact.id, "name": "Rob Egnops"}
  102. )
  103. self.assertEqual(new_contact.name, "Bob Egnops")
  104. # Reset contact to standalone
  105. new_contact.write({"contact_id": False})
  106. self.assertEqual(new_contact.contact_type, "standalone")
  107. # Reset contact to attached, and ensure only it is unlinked (i.e.
  108. # context is ignored).
  109. new_contact.write({"contact_id": self.bob_contact.id})
  110. ctx = {"search_show_all_positions": {"is_set": True, "set_value": True}}
  111. new_contact.with_context(ctx).unlink()
  112. partner_ids = self.partner.with_context(ctx).search(
  113. [("id", "in", [new_contact.id, self.bob_contact.id])]
  114. )
  115. self.assertIn(self.bob_contact, partner_ids)
  116. self.assertNotIn(new_contact, partner_ids)
  117. def test_05_contact_fields_sync(self):
  118. """Check that contact's fields are correctly synced between
  119. parent contact or related contacts
  120. """
  121. # Test DOWNSTREAM sync
  122. self.bob_contact.write({"name": "Rob Egnops"})
  123. self.assertEqual(self.bob_job1.name, "Rob Egnops")
  124. # Test UPSTREAM sync
  125. self.bob_job1.write({"name": "Bob Egnops"})
  126. self.assertEqual(
  127. self.bob_contact.name,
  128. "Bob Egnops",
  129. )
  130. def test_06_ir_action(self):
  131. """Check ir_action context is auto updated."""
  132. new_context_val = (
  133. "'search_show_all_positions': " "{'is_set': True, 'set_value': False}"
  134. )
  135. xmlid = "base.action_partner_form"
  136. details = self.env["ir.actions.act_window"]._for_xml_id(xmlid)
  137. self.assertIn(
  138. new_context_val,
  139. details["context"],
  140. msg="Default actions not updated with new context",
  141. )
  142. xmlid = "partner_contact_in_several_companies.action_partner_form"
  143. details = self.env["ir.actions.act_window"]._for_xml_id(xmlid)
  144. self.assertNotIn(
  145. new_context_val,
  146. details["context"],
  147. msg="Custom actions incorrectly updated with new context",
  148. )
  149. def test_07_onchange(self):
  150. """Check onchange method"""
  151. new_contact = self.partner.create({"name": "Bob before onchange"})
  152. new_contact.write({"contact_id": self.bob_contact.id})
  153. new_contact._onchange_contact_id()
  154. self.assertEqual(new_contact.name, "Bob Egnops")
  155. new_contact.write({"contact_type": "standalone"})
  156. new_contact._onchange_contact_type()
  157. self.assertEqual(new_contact.contact_id, self.partner)
  158. def test_08_commercial_partner_compute(self):
  159. new_contact = self.partner.create({"name": "Bob before onchange"})
  160. new_contact.write({"contact_id": self.bob_contact.id, "parent_id": False})
  161. new_contact._compute_commercial_partner()
  162. self.assertEqual(
  163. new_contact.commercial_partner_id,
  164. self.bob_contact,
  165. )