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.

136 lines
6.9 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', 'res_partner_contact1_work_position1'),
  34. ('roger_contact_id', 'base', 'res_partner_main2'),
  35. ('roger_job2_id', 'base_contact', 'res_partner_main2_position_consultant')]:
  36. r = ModelData.get_object_reference(cr, uid, module, name)
  37. setattr(self, attr, r[1] if r else False)
  38. def test_00_show_only_standalone_contact(self):
  39. """Check that only standalone contact are shown if context explicitly state to not display all positions"""
  40. cr, uid = self.cr, self.uid
  41. ctx = {'search_show_all_positions': False}
  42. partner_ids = self.partner.search(cr, uid, [], context=ctx)
  43. partner_ids.sort()
  44. self.assertTrue(self.bob_job1_id not in partner_ids)
  45. self.assertTrue(self.roger_job2_id not in partner_ids)
  46. def test_01_show_all_positions(self):
  47. """Check that all contact are show if context is empty or explicitly state to display all positions"""
  48. cr, uid = self.cr, self.uid
  49. partner_ids = self.partner.search(cr, uid, [], context=None)
  50. self.assertTrue(self.bob_job1_id in partner_ids)
  51. self.assertTrue(self.roger_job2_id in partner_ids)
  52. ctx = {'search_show_all_positions': True}
  53. partner_ids = self.partner.search(cr, uid, [], context=ctx)
  54. self.assertTrue(self.bob_job1_id in partner_ids)
  55. self.assertTrue(self.roger_job2_id in partner_ids)
  56. def test_02_reading_other_contact_one2many_show_all_positions(self):
  57. """Check that readonly partner's ``other_contact_ids`` return all values whatever the context"""
  58. cr, uid = self.cr, self.uid
  59. def read_other_contacts(pid, context=None):
  60. return self.partner.read(cr, uid, [pid], ['other_contact_ids'], context=context)[0]['other_contact_ids']
  61. def read_contacts(pid, context=None):
  62. return self.partner.read(cr, uid, [pid], ['child_ids'], context=context)[0]['child_ids']
  63. ctx = None
  64. self.assertEqual(read_other_contacts(self.bob_contact_id, context=ctx), [self.bob_job1_id])
  65. ctx = {'search_show_all_positions': False}
  66. self.assertEqual(read_other_contacts(self.bob_contact_id, context=ctx), [self.bob_job1_id])
  67. ctx = {'search_show_all_positions': True}
  68. self.assertEqual(read_other_contacts(self.bob_contact_id, context=ctx), [self.bob_job1_id])
  69. ctx = None
  70. self.assertTrue(self.bob_job1_id in read_contacts(self.main_partner_id, context=ctx))
  71. ctx = {'search_show_all_positions': False}
  72. self.assertTrue(self.bob_job1_id in read_contacts(self.main_partner_id, context=ctx))
  73. ctx = {'search_show_all_positions': True}
  74. self.assertTrue(self.bob_job1_id in read_contacts(self.main_partner_id, context=ctx))
  75. def test_03_search_match_attached_contacts(self):
  76. """Check that searching partner also return partners having attached contacts matching search criteria"""
  77. cr, uid = self.cr, self.uid
  78. # Bob's contact has one other position which is related to 'Your Company'
  79. # so search for all contacts working for 'Your Company' should contain bob position.
  80. partner_ids = self.partner.search(cr, uid, [('parent_id', 'ilike', 'Your Company')], context=None)
  81. self.assertTrue(self.bob_job1_id in partner_ids)
  82. # but when searching without 'all positions', we should get the position standalone contact instead.
  83. ctx = {'search_show_all_positions': False}
  84. partner_ids = self.partner.search(cr, uid, [('parent_id', 'ilike', 'Your Company')], context=ctx)
  85. self.assertTrue(self.bob_contact_id in partner_ids)
  86. def test_04_contact_creation(self):
  87. """Check that we're begin to create a contact"""
  88. cr, uid = self.cr, self.uid
  89. # Create a contact using only name
  90. new_contact_id = self.partner.create(cr, uid, {'name': 'Bob Egnops'})
  91. self.assertEqual(self.partner.browse(cr, uid, new_contact_id).contact_type, 'standalone')
  92. # Create a contact with only contact_id
  93. new_contact_id = self.partner.create(cr, uid, {'contact_id': self.bob_contact_id})
  94. new_contact = self.partner.browse(cr, uid, new_contact_id)
  95. self.assertEqual(new_contact.name, 'Bob Egnops')
  96. self.assertEqual(new_contact.contact_type, 'attached')
  97. # Create a contact with both contact_id and name;
  98. # contact's name should override provided value in that case
  99. new_contact_id = self.partner.create(cr, uid, {'contact_id': self.bob_contact_id, 'name': 'Rob Egnops'})
  100. self.assertEqual(self.partner.browse(cr, uid, new_contact_id).name, 'Bob Egnops')
  101. # Reset contact to standalone
  102. self.partner.write(cr, uid, [new_contact_id], {'contact_id': False})
  103. self.assertEqual(self.partner.browse(cr, uid, new_contact_id).contact_type, 'standalone')
  104. def test_05_contact_fields_sync(self):
  105. """Check that contact's fields are correctly synced between parent contact or related contacts"""
  106. cr, uid = self.cr, self.uid
  107. # Test DOWNSTREAM sync
  108. self.partner.write(cr, uid, [self.bob_contact_id], {'name': 'Rob Egnops'})
  109. self.assertEqual(self.partner.browse(cr, uid, self.bob_job1_id).name, 'Rob Egnops')
  110. # Test UPSTREAM sync
  111. self.partner.write(cr, uid, [self.bob_job1_id], {'name': 'Bob Egnops'})
  112. self.assertEqual(self.partner.browse(cr, uid, self.bob_contact_id).name, 'Bob Egnops')