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.

248 lines
9.2 KiB

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