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.

201 lines
7.6 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 Test_Base_Contact(common.TransactionCase):
  23. def setUp(self):
  24. """*****setUp*****"""
  25. super(Test_Base_Contact, 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', 'base_contact', 'res_partner_contact1'),
  33. ('bob_job1_id', 'base_contact',
  34. 'res_partner_contact1_work_position1'),
  35. ('roger_contact_id', 'base', 'res_partner_main2'),
  36. ('roger_job2_id', 'base_contact',
  37. 'res_partner_main2_position_consultant')]:
  38. r = ModelData.get_object_reference(cr, uid, module, name)
  39. setattr(self, attr, r[1] if r else False)
  40. def test_00_show_only_standalone_contact(self):
  41. """Check that only standalone contact are shown if context
  42. explicitly state to not display all positions
  43. """
  44. cr, uid = self.cr, self.uid
  45. ctx = {'search_show_all_positions': False}
  46. partner_ids = self.partner.search(cr, uid, [], context=ctx)
  47. partner_ids.sort()
  48. self.assertTrue(self.bob_job1_id not in partner_ids)
  49. self.assertTrue(self.roger_job2_id not in partner_ids)
  50. def test_01_show_all_positions(self):
  51. """Check that all contact are show if context is empty or
  52. explicitly state to display all positions
  53. """
  54. cr, uid = self.cr, self.uid
  55. partner_ids = self.partner.search(cr, uid, [], context=None)
  56. self.assertTrue(self.bob_job1_id in partner_ids)
  57. self.assertTrue(self.roger_job2_id in partner_ids)
  58. ctx = {'search_show_all_positions': True}
  59. partner_ids = self.partner.search(cr, uid, [], context=ctx)
  60. self.assertTrue(self.bob_job1_id in partner_ids)
  61. self.assertTrue(self.roger_job2_id in partner_ids)
  62. def test_02_reading_other_contact_one2many_show_all_positions(self):
  63. """Check that readonly partner's ``other_contact_ids`` return
  64. all values whatever the context
  65. """
  66. cr, uid = self.cr, self.uid
  67. def read_other_contacts(pid, context=None):
  68. return self.partner.read(
  69. cr, uid, [pid], ['other_contact_ids'],
  70. context=context)[0]['other_contact_ids']
  71. def read_contacts(pid, context=None):
  72. return self.partner.read(
  73. cr, uid, [pid], ['child_ids'], context=context)[0]['child_ids']
  74. ctx = None
  75. self.assertEqual(
  76. read_other_contacts(self.bob_contact_id, context=ctx),
  77. [self.bob_job1_id],
  78. )
  79. ctx = {'search_show_all_positions': False}
  80. self.assertEqual(read_other_contacts(
  81. self.bob_contact_id, context=ctx),
  82. [self.bob_job1_id],
  83. )
  84. ctx = {'search_show_all_positions': True}
  85. self.assertEqual(
  86. read_other_contacts(self.bob_contact_id, context=ctx),
  87. [self.bob_job1_id],
  88. )
  89. ctx = None
  90. self.assertIn(
  91. self.bob_job1_id,
  92. read_contacts(self.main_partner_id, context=ctx),
  93. )
  94. ctx = {'search_show_all_positions': False}
  95. self.assertIn(
  96. self.bob_job1_id,
  97. read_contacts(self.main_partner_id, context=ctx),
  98. )
  99. ctx = {'search_show_all_positions': True}
  100. self.assertIn(
  101. self.bob_job1_id,
  102. read_contacts(self.main_partner_id, context=ctx),
  103. )
  104. def test_03_search_match_attached_contacts(self):
  105. """Check that searching partner also return partners having
  106. attached contacts matching search criteria
  107. """
  108. cr, uid = self.cr, self.uid
  109. # Bob's contact has one other position which is related to
  110. # 'YourCompany'
  111. # so search for all contacts working for 'YourCompany' should contain
  112. # bob position.
  113. partner_ids = self.partner.search(
  114. cr, uid,
  115. [('parent_id', 'ilike', 'YourCompany')],
  116. context=None
  117. )
  118. self.assertIn(self.bob_job1_id, partner_ids, )
  119. # but when searching without 'all positions',
  120. # we should get the position standalone contact instead.
  121. ctx = {'search_show_all_positions': False}
  122. partner_ids = self.partner.search(
  123. cr, uid,
  124. [('parent_id', 'ilike', 'YourCompany')],
  125. context=ctx
  126. )
  127. self.assertIn(self.bob_contact_id, partner_ids, )
  128. def test_04_contact_creation(self):
  129. """Check that we're begin to create a contact"""
  130. cr, uid = self.cr, self.uid
  131. # Create a contact using only name
  132. new_contact_id = self.partner.create(cr, uid, {'name': 'Bob Egnops'})
  133. self.assertEqual(
  134. self.partner.browse(cr, uid, new_contact_id).contact_type,
  135. 'standalone',
  136. )
  137. # Create a contact with only contact_id
  138. new_contact_id = self.partner.create(
  139. cr, uid, {'contact_id': self.bob_contact_id}
  140. )
  141. new_contact = self.partner.browse(cr, uid, new_contact_id)
  142. self.assertEqual(new_contact.name, 'Bob Egnops')
  143. self.assertEqual(new_contact.contact_type, 'attached')
  144. # Create a contact with both contact_id and name;
  145. # contact's name should override provided value in that case
  146. new_contact_id = self.partner.create(
  147. cr, uid, {'contact_id': self.bob_contact_id, 'name': 'Rob Egnops'}
  148. )
  149. self.assertEqual(
  150. self.partner.browse(cr, uid, new_contact_id).name,
  151. 'Bob Egnops'
  152. )
  153. # Reset contact to standalone
  154. self.partner.write(cr, uid, [new_contact_id], {'contact_id': False})
  155. self.assertEqual(
  156. self.partner.browse(cr, uid, new_contact_id).contact_type,
  157. 'standalone',
  158. )
  159. def test_05_contact_fields_sync(self):
  160. """Check that contact's fields are correctly synced between
  161. parent contact or related contacts
  162. """
  163. cr, uid = self.cr, self.uid
  164. # Test DOWNSTREAM sync
  165. self.partner.write(
  166. cr, uid, [self.bob_contact_id], {'name': 'Rob Egnops'}
  167. )
  168. self.assertEqual(
  169. self.partner.browse(cr, uid, self.bob_job1_id).name,
  170. 'Rob Egnops',
  171. )
  172. # Test UPSTREAM sync
  173. self.partner.write(cr, uid, [self.bob_job1_id], {'name': 'Bob Egnops'})
  174. self.assertEqual(
  175. self.partner.browse(cr, uid, self.bob_contact_id).name,
  176. 'Bob Egnops',
  177. )