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.

158 lines
7.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2018 Therp BV <https://therp.nl>.
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from lxml import etree
  5. from odoo.exceptions import ValidationError
  6. from . import common
  7. from ..tablib import Tab
  8. class TestPartnerTabs(common.TestCommon):
  9. post_install = True
  10. def test_create_tab(self):
  11. self.assertTrue(bool(self.tab_board))
  12. tab_obj = Tab(self.tab_board)
  13. # fields_view_get should force the creation of the new tabs.
  14. view_partner_form = self.env.ref('base.view_partner_form')
  15. view = self.partner_model.with_context().fields_view_get(
  16. view_id=view_partner_form.id, view_type='form')
  17. # The form view for partner should now also contain field 'id'.
  18. tree = etree.fromstring(view['arch'])
  19. field = tree.xpath('//field[@name="id"]')
  20. self.assertTrue(field, 'Id field does not exist.')
  21. # There should now be a field in res_partner for the new tab.
  22. fieldname = tab_obj.get_fieldname()
  23. self.assertTrue(fieldname in self.partner_model._fields)
  24. # And we should have a field for the tab:
  25. field = tree.xpath('//field[@name="%s"]' % fieldname)
  26. self.assertTrue(
  27. field,
  28. 'Tab field %s does not exist in %s.' %
  29. (fieldname, etree.tostring(tree)))
  30. # There should be no effect on the tree view:
  31. view = self. partner_model.with_context().fields_view_get(
  32. view_type='tree')
  33. tree = etree.fromstring(view['arch'])
  34. field = tree.xpath('//field[@name="%s"]' % fieldname)
  35. self.assertFalse(
  36. field,
  37. 'Tab field %s should not exist in %s.' %
  38. (fieldname, etree.tostring(tree)))
  39. def test_view_without_pages(self):
  40. """Check that _add_tab_pages does not effect view without pages."""
  41. # pylint: disable=protected-access
  42. view = etree.Element('view')
  43. extra_fields = self.partner_model._add_tab_pages(view)
  44. self.assertFalse(extra_fields)
  45. def test_tab_modifications(self):
  46. tab_executive = self.tab_model.create({
  47. 'code': 'executive',
  48. 'name': 'Executive members'})
  49. self.assertTrue(bool(tab_executive))
  50. type_chairperson = self.type_model.create({
  51. 'name': 'has chairperson',
  52. 'name_inverse': 'is chairperson for',
  53. 'contact_type_left': 'p', # This emulates a user mistake.
  54. 'contact_type_right': 'p',
  55. 'tab_left_id': tab_executive.id})
  56. self.assertTrue(bool(type_chairperson))
  57. # If we change tab now to be only valid on company partners
  58. # the tab_left_id field should be cleared from the type:
  59. tab_executive.write({'contact_type': 'c'})
  60. self.assertFalse(type_chairperson.tab_left_id.id)
  61. # Trying to set the tab back on type should be impossible:
  62. with self.assertRaises(ValidationError):
  63. type_chairperson.write({'tab_left_id': tab_executive.id})
  64. # We should be able to change tab, if also changing contact type.
  65. type_chairperson.write({
  66. 'contact_type_left': 'c',
  67. 'tab_left_id': tab_executive.id})
  68. self.assertEqual(
  69. type_chairperson.tab_left_id.id,
  70. tab_executive.id)
  71. # Unlinking the tab should reset the tab_left_id on relation type.
  72. tab_executive.unlink()
  73. self.assertEqual(
  74. type_chairperson.tab_left_id.id,
  75. False)
  76. # It should not be possible to add category or contact type to as
  77. # selection criteria to a tab meant for specific partners.
  78. with self.assertRaises(ValidationError):
  79. self.tab_departments.write({'contact_type': 'c'})
  80. with self.assertRaises(ValidationError):
  81. self.tab_departments.write({
  82. 'partner_category_id': self.category_government.id})
  83. def test_type_modifications(self):
  84. self.assertTrue(bool(self.tab_board))
  85. self.assertTrue(bool(self.tab_positions))
  86. self.assertTrue(bool(self.type_chairperson))
  87. # Trying to clear either category should raise ValidationError:
  88. with self.assertRaises(ValidationError):
  89. self.type_chairperson.write({'partner_category_left': False})
  90. with self.assertRaises(ValidationError):
  91. self.type_chairperson.write({'partner_category_right': False})
  92. # Trying to clear either contact type should raise ValidationError:
  93. with self.assertRaises(ValidationError):
  94. self.type_chairperson.write({'contact_type_left': False})
  95. with self.assertRaises(ValidationError):
  96. self.type_chairperson.write({'contact_type_right': False})
  97. def test_relations(self):
  98. """Test relations shown on tab."""
  99. relation_all_model = self.env['res.partner.relation.all']
  100. self.assertTrue(bool(self.tab_board))
  101. self.assertTrue(bool(self.type_ceo))
  102. self.assertTrue(bool(self.partner_big_company))
  103. self.assertTrue(bool(self.partner_important_person))
  104. self.assertTrue(bool(self.relation_company_ceo))
  105. # Now we should be able to find the relation with the tab_id:
  106. board_partners = relation_all_model.search([
  107. ('tab_id', '=', self.tab_board.id)])
  108. self.assertTrue(bool(board_partners))
  109. self.assertIn(
  110. self.partner_big_company,
  111. [relation.this_partner_id for relation in board_partners])
  112. # We should find the company on the partner through tab field:
  113. tab_obj = Tab(self.tab_board)
  114. fieldname = tab_obj.get_fieldname()
  115. self.assertTrue(fieldname in self.partner_model._fields)
  116. board_partners = self.partner_big_company[fieldname]
  117. self.assertEqual(len(board_partners), 1)
  118. self.assertEqual(
  119. board_partners.other_partner_id.id,
  120. self.partner_important_person.id)
  121. # When adding a new relation on a tab, type must be for tab.
  122. onchange_result = board_partners.with_context(
  123. default_tab_id=self.tab_board.id
  124. ).onchange_partner_id()
  125. self.assertTrue(onchange_result)
  126. self.assertIn('domain', onchange_result)
  127. self.assertIn('type_selection_id', onchange_result['domain'])
  128. self.assertEqual(
  129. onchange_result['domain']['type_selection_id'][-1],
  130. ('tab_id', '=', self.tab_board.id))
  131. def test_compute_visibility(self):
  132. """Check the computation of visibility on partners."""
  133. # pylint: disable=protected-access
  134. main_partner = self.env.ref('base.main_partner')
  135. main_partner._compute_tabs_visibility()
  136. tab_obj = Tab(self.tab_departments)
  137. fieldname = tab_obj.get_fieldname()
  138. visible_fieldname = tab_obj.get_visible_fieldname()
  139. self.assertIn(visible_fieldname, main_partner._fields)
  140. self.assertIn(fieldname, main_partner._fields)
  141. self.assertEqual(main_partner[visible_fieldname], True)
  142. department_relations = main_partner[fieldname]
  143. self.assertTrue(len(department_relations) >= 1)
  144. departments = [
  145. relation.other_partner_id for relation in department_relations]
  146. for department in departments:
  147. self.assertIn(
  148. self.category_department, department.category_id)