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.

159 lines
7.3 KiB

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