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.

205 lines
7.7 KiB

  1. # -*- coding: utf-8 ⁻*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Business Applications
  5. # Copyright (C) 2013-TODAY OpenERP S.A. (<http://openerp.com>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.tests import common
  22. class PartnerContactInSeveralCompaniesCase(common.TransactionCase):
  23. def setUp(self):
  24. """*****setUp*****"""
  25. super(PartnerContactInSeveralCompaniesCase, self).setUp()
  26. cr, uid = self.cr, self.uid
  27. ModelData = self.registry('ir.model.data')
  28. self.partner = self.registry('res.partner')
  29. # Get test records reference
  30. for attr, module, name in [
  31. ('main_partner_id', 'base', 'main_partner'),
  32. ('bob_contact_id',
  33. 'partner_contact_in_several_companies',
  34. 'res_partner_contact1'),
  35. ('bob_job1_id',
  36. 'partner_contact_in_several_companies',
  37. 'res_partner_contact1_work_position1'),
  38. ('roger_contact_id', 'base', 'res_partner_main2'),
  39. ('roger_job2_id',
  40. 'partner_contact_in_several_companies',
  41. 'res_partner_main2_position_consultant')]:
  42. r = ModelData.get_object_reference(cr, uid, module, name)
  43. setattr(self, attr, r[1] if r else False)
  44. def test_00_show_only_standalone_contact(self):
  45. """Check that only standalone contact are shown if context
  46. explicitly state to not display all positions
  47. """
  48. cr, uid = self.cr, self.uid
  49. ctx = {'search_show_all_positions': False}
  50. partner_ids = self.partner.search(cr, uid, [], context=ctx)
  51. partner_ids.sort()
  52. self.assertTrue(self.bob_job1_id not in partner_ids)
  53. self.assertTrue(self.roger_job2_id not in partner_ids)
  54. def test_01_show_all_positions(self):
  55. """Check that all contact are show if context is empty or
  56. explicitly state to display all positions
  57. """
  58. cr, uid = self.cr, self.uid
  59. partner_ids = self.partner.search(cr, uid, [], context=None)
  60. self.assertTrue(self.bob_job1_id in partner_ids)
  61. self.assertTrue(self.roger_job2_id in partner_ids)
  62. ctx = {'search_show_all_positions': True}
  63. partner_ids = self.partner.search(cr, uid, [], context=ctx)
  64. self.assertTrue(self.bob_job1_id in partner_ids)
  65. self.assertTrue(self.roger_job2_id in partner_ids)
  66. def test_02_reading_other_contact_one2many_show_all_positions(self):
  67. """Check that readonly partner's ``other_contact_ids`` return
  68. all values whatever the context
  69. """
  70. cr, uid = self.cr, self.uid
  71. def read_other_contacts(pid, context=None):
  72. return self.partner.read(
  73. cr, uid, [pid], ['other_contact_ids'],
  74. context=context)[0]['other_contact_ids']
  75. def read_contacts(pid, context=None):
  76. return self.partner.read(
  77. cr, uid, [pid], ['child_ids'], context=context)[0]['child_ids']
  78. ctx = None
  79. self.assertEqual(
  80. read_other_contacts(self.bob_contact_id, context=ctx),
  81. [self.bob_job1_id],
  82. )
  83. ctx = {'search_show_all_positions': False}
  84. self.assertEqual(read_other_contacts(
  85. self.bob_contact_id, context=ctx),
  86. [self.bob_job1_id],
  87. )
  88. ctx = {'search_show_all_positions': True}
  89. self.assertEqual(
  90. read_other_contacts(self.bob_contact_id, context=ctx),
  91. [self.bob_job1_id],
  92. )
  93. ctx = None
  94. self.assertIn(
  95. self.bob_job1_id,
  96. read_contacts(self.main_partner_id, context=ctx),
  97. )
  98. ctx = {'search_show_all_positions': False}
  99. self.assertIn(
  100. self.bob_job1_id,
  101. read_contacts(self.main_partner_id, context=ctx),
  102. )
  103. ctx = {'search_show_all_positions': True}
  104. self.assertIn(
  105. self.bob_job1_id,
  106. read_contacts(self.main_partner_id, context=ctx),
  107. )
  108. def test_03_search_match_attached_contacts(self):
  109. """Check that searching partner also return partners having
  110. attached contacts matching search criteria
  111. """
  112. cr, uid = self.cr, self.uid
  113. # Bob's contact has one other position which is related to
  114. # 'YourCompany'
  115. # so search for all contacts working for 'YourCompany' should contain
  116. # bob position.
  117. partner_ids = self.partner.search(
  118. cr, uid,
  119. [('parent_id', 'ilike', 'YourCompany')],
  120. context=None
  121. )
  122. self.assertIn(self.bob_job1_id, partner_ids, )
  123. # but when searching without 'all positions',
  124. # we should get the position standalone contact instead.
  125. ctx = {'search_show_all_positions': False}
  126. partner_ids = self.partner.search(
  127. cr, uid,
  128. [('parent_id', 'ilike', 'YourCompany')],
  129. context=ctx
  130. )
  131. self.assertIn(self.bob_contact_id, partner_ids, )
  132. def test_04_contact_creation(self):
  133. """Check that we're begin to create a contact"""
  134. cr, uid = self.cr, self.uid
  135. # Create a contact using only name
  136. new_contact_id = self.partner.create(cr, uid, {'name': 'Bob Egnops'})
  137. self.assertEqual(
  138. self.partner.browse(cr, uid, new_contact_id).contact_type,
  139. 'standalone',
  140. )
  141. # Create a contact with only contact_id
  142. new_contact_id = self.partner.create(
  143. cr, uid, {'contact_id': self.bob_contact_id}
  144. )
  145. new_contact = self.partner.browse(cr, uid, new_contact_id)
  146. self.assertEqual(new_contact.name, 'Bob Egnops')
  147. self.assertEqual(new_contact.contact_type, 'attached')
  148. # Create a contact with both contact_id and name;
  149. # contact's name should override provided value in that case
  150. new_contact_id = self.partner.create(
  151. cr, uid, {'contact_id': self.bob_contact_id, 'name': 'Rob Egnops'}
  152. )
  153. self.assertEqual(
  154. self.partner.browse(cr, uid, new_contact_id).name,
  155. 'Bob Egnops'
  156. )
  157. # Reset contact to standalone
  158. self.partner.write(cr, uid, [new_contact_id], {'contact_id': False})
  159. self.assertEqual(
  160. self.partner.browse(cr, uid, new_contact_id).contact_type,
  161. 'standalone',
  162. )
  163. def test_05_contact_fields_sync(self):
  164. """Check that contact's fields are correctly synced between
  165. parent contact or related contacts
  166. """
  167. cr, uid = self.cr, self.uid
  168. # Test DOWNSTREAM sync
  169. self.partner.write(
  170. cr, uid, [self.bob_contact_id], {'name': 'Rob Egnops'}
  171. )
  172. self.assertEqual(
  173. self.partner.browse(cr, uid, self.bob_job1_id).name,
  174. 'Rob Egnops',
  175. )
  176. # Test UPSTREAM sync
  177. self.partner.write(cr, uid, [self.bob_job1_id], {'name': 'Bob Egnops'})
  178. self.assertEqual(
  179. self.partner.browse(cr, uid, self.bob_contact_id).name,
  180. 'Bob Egnops',
  181. )