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.

247 lines
9.2 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(
  13. '%s.res_partner_contact1' % current_module)
  14. self.bob_job1 = self.env.ref(
  15. '%s.res_partner_contact1_work_position1' % current_module)
  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. def test_00_show_only_standalone_contact(self):
  20. """Check that only standalone contact are shown if context
  21. explicitly state to not display all positions
  22. """
  23. ctx = {'search_show_all_positions': {'is_set': True,
  24. 'set_value': False
  25. }}
  26. partner_ids = self.partner.with_context(ctx).search([])
  27. self.assertTrue(self.bob_job1 not in partner_ids)
  28. self.assertTrue(self.roger_job2 not in partner_ids)
  29. def test_01_show_all_positions(self):
  30. """Check that all contact are show if context is empty or
  31. explicitly state to display all positions or the "is_set"
  32. value has been set to False.
  33. """
  34. partner_ids = self.partner.search([])
  35. self.assertTrue(self.bob_job1 in partner_ids)
  36. self.assertTrue(self.roger_job2 in partner_ids)
  37. ctx = {'search_show_all_positions': {'is_set': False}}
  38. partner_ids = self.partner.with_context(ctx).search([])
  39. self.assertTrue(self.bob_job1 in partner_ids)
  40. self.assertTrue(self.roger_job2 in partner_ids)
  41. ctx = {'search_show_all_positions': {'is_set': True,
  42. 'set_value': True
  43. }}
  44. partner_ids = self.partner.with_context(ctx).search([])
  45. self.assertTrue(self.bob_job1 in partner_ids)
  46. self.assertTrue(self.roger_job2 in partner_ids)
  47. def test_02_reading_other_contact_one2many_show_all_positions(self):
  48. """Check that readonly partner's ``other_contact_ids`` return
  49. all values whatever the context
  50. """
  51. ctx = {}
  52. self.assertEqual(
  53. self.bob_job1, self.bob_contact.with_context(ctx).other_contact_ids
  54. )
  55. ctx = {'search_show_all_positions': {'is_set': False}}
  56. self.assertEqual(
  57. self.bob_job1, self.bob_contact.with_context(ctx).other_contact_ids
  58. )
  59. ctx = {'search_show_all_positions': {'is_set': True,
  60. 'set_value': False,
  61. }}
  62. self.assertEqual(
  63. self.bob_job1, self.bob_contact.with_context(ctx).other_contact_ids
  64. )
  65. ctx = {'search_show_all_positions': {'is_set': True,
  66. 'set_value': True,
  67. }}
  68. self.assertEqual(
  69. self.bob_job1, self.bob_contact.with_context(ctx).other_contact_ids
  70. )
  71. ctx = {}
  72. self.assertIn(
  73. self.bob_job1,
  74. self.main_partner.with_context(ctx).child_ids)
  75. ctx = {'search_show_all_positions': {'is_set': False}}
  76. self.assertIn(
  77. self.bob_job1,
  78. self.main_partner.with_context(ctx).child_ids)
  79. ctx = {'search_show_all_positions': {'is_set': True,
  80. 'set_value': False,
  81. }}
  82. self.assertIn(
  83. self.bob_job1,
  84. self.main_partner.with_context(ctx).child_ids)
  85. ctx = {'search_show_all_positions': {'is_set': True,
  86. 'set_value': True,
  87. }}
  88. self.assertIn(
  89. self.bob_job1,
  90. self.main_partner.with_context(ctx).child_ids)
  91. def test_03_search_match_attached_contacts(self):
  92. """Check that searching partner also return partners having
  93. attached contacts matching search criteria
  94. """
  95. # Bob's contact has one other position which is related to
  96. # 'YourCompany'
  97. # so search for all contacts working for 'YourCompany'
  98. # should contain Bob position.
  99. partner_ids = self.partner.search(
  100. [('parent_id', 'ilike', 'YourCompany')])
  101. self.assertTrue(self.bob_job1 in partner_ids)
  102. # but when searching without 'all positions',
  103. # we should get the position standalone contact instead.
  104. ctx = {'search_show_all_positions': {'is_set': True,
  105. 'set_value': False,
  106. }}
  107. partner_ids = self.partner.with_context(ctx).search(
  108. [('parent_id', 'ilike', 'YourCompany')])
  109. self.assertTrue(self.bob_contact in partner_ids)
  110. def test_04_contact_creation(self):
  111. """Check that we're begin to create a contact"""
  112. # Create a contact using only name
  113. new_contact = self.partner.create({'name': 'Bob Egnops'})
  114. self.assertEqual(
  115. new_contact.contact_type,
  116. 'standalone',
  117. )
  118. # Create a contact with only contact_id
  119. new_contact = self.partner.create(
  120. {'contact_id': self.bob_contact.id}
  121. )
  122. self.assertEqual(new_contact.name, 'Bob Egnops')
  123. self.assertEqual(new_contact.contact_type, 'attached')
  124. # Create a contact with both contact_id and name;
  125. # contact's name should override provided value in that case
  126. new_contact = self.partner.create(
  127. {'contact_id': self.bob_contact.id, 'name': 'Rob Egnops'}
  128. )
  129. self.assertEqual(
  130. new_contact.name,
  131. 'Bob Egnops'
  132. )
  133. # Reset contact to standalone
  134. new_contact.write({'contact_id': False})
  135. self.assertEqual(
  136. new_contact.contact_type,
  137. 'standalone',
  138. )
  139. # Reset contact to attached, and ensure only it is unlinked (i.e.
  140. # context is ignored).
  141. new_contact.write({'contact_id': self.bob_contact.id})
  142. ctx = {'search_show_all_positions': {'is_set': True,
  143. 'set_value': True
  144. }}
  145. new_contact.with_context(ctx).unlink()
  146. partner_ids = self.partner.with_context(ctx).search(
  147. [('id', 'in', [new_contact.id, self.bob_contact.id])])
  148. self.assertIn(self.bob_contact, partner_ids)
  149. self.assertNotIn(new_contact, partner_ids)
  150. def test_05_contact_fields_sync(self):
  151. """Check that contact's fields are correctly synced between
  152. parent contact or related contacts
  153. """
  154. # Test DOWNSTREAM sync
  155. self.bob_contact.write(
  156. {'name': 'Rob Egnops'}
  157. )
  158. self.assertEqual(
  159. self.bob_job1.name,
  160. 'Rob Egnops',
  161. )
  162. # Test UPSTREAM sync
  163. self.bob_job1.write({'name': 'Bob Egnops'})
  164. self.assertEqual(
  165. self.bob_contact.name,
  166. 'Bob Egnops',
  167. )
  168. def test_06_ir_action(self):
  169. """Check ir_action context is auto updated.
  170. """
  171. new_context_val = "'search_show_all_positions': " \
  172. "{'is_set': True, 'set_value': False}"
  173. details = self.env['ir.actions.act_window'].for_xml_id(
  174. 'base',
  175. 'action_partner_form')
  176. self.assertIn(
  177. new_context_val,
  178. details['context'],
  179. msg='Default actions not updated with new context'
  180. )
  181. details = self.env['ir.actions.act_window'].for_xml_id(
  182. 'partner_contact_in_several_companies',
  183. 'action_partner_form')
  184. self.assertNotIn(
  185. new_context_val,
  186. details['context'],
  187. msg='Custom actions incorrectly updated with new context'
  188. )
  189. def test_07_onchange(self):
  190. """Check onchange method
  191. """
  192. new_contact = self.partner.create({'name': 'Bob before onchange'})
  193. new_contact.write({'contact_id': self.bob_contact.id})
  194. new_contact._onchange_contact_id()
  195. self.assertEqual(
  196. new_contact.name,
  197. 'Bob Egnops',
  198. )
  199. new_contact.write({'contact_type': 'standalone'})
  200. new_contact._onchange_contact_type()
  201. self.assertEqual(
  202. new_contact.contact_id,
  203. self.partner,
  204. )
  205. def test_08_commercial_partner_compute(self):
  206. new_contact = self.partner.create({'name': 'Bob before onchange'})
  207. new_contact.write({'contact_id': self.bob_contact.id,
  208. 'parent_id': False})
  209. new_contact._compute_commercial_partner()
  210. self.assertEqual(
  211. new_contact.commercial_partner_id,
  212. self.bob_contact,
  213. )