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.

115 lines
4.4 KiB

  1. # coding: utf-8
  2. from odoo import api
  3. from odoo.tests import common
  4. class TestTracking(common.TransactionCase):
  5. def test_message_track(self):
  6. self.user_group_employee = self.env.ref('base.group_user')
  7. users = self.env['res.users'].with_context({
  8. 'no_reset_password': True, 'mail_create_nosubscribe': True})
  9. self.user_employee = users.create({
  10. 'name': 'Ernest Employee',
  11. 'login': 'ernest',
  12. 'email': 'e.e@example.com',
  13. 'signature': '--\nErnest',
  14. 'notification_type': 'email',
  15. 'groups_id': [(6, 0, [self.user_group_employee.id])]
  16. })
  17. test_channel = self.env['mail.channel'].create({
  18. 'name': 'Test',
  19. 'channel_partner_ids': [(4, self.user_employee.partner_id.id)]
  20. })
  21. subtype = self.env['mail.message.subtype']
  22. data = self.env['ir.model.data']
  23. note_subtype = self.env.ref('mail.mt_note')
  24. # mt_private: public field (tracked as onchange) set to 'private'
  25. # (selection)
  26. mt_private = subtype.create({
  27. 'name': 'private',
  28. 'description': 'Public field set to private'
  29. })
  30. data.create({
  31. 'name': 'mt_private',
  32. 'model': 'mail.message.subtype',
  33. 'module': 'mail',
  34. 'res_id': mt_private.id
  35. })
  36. # mt_name_supername: name field (tracked as always) set to 'supername'
  37. # (char)
  38. mt_name_supername = subtype.create({
  39. 'name': 'name_supername',
  40. 'description': 'Name field set to supername'
  41. })
  42. data.create({
  43. 'name': 'mt_name_supername',
  44. 'model': 'mail.message.subtype',
  45. 'module': 'mail',
  46. 'res_id': mt_name_supername.id
  47. })
  48. # mt_group_public_set: group_public field (tracked as onchange) set to
  49. # something (m2o)
  50. mt_group_public_set = subtype.create({
  51. 'name': 'group_public_set',
  52. 'description': 'Group_public field set'
  53. })
  54. data.create({
  55. 'name': 'mt_group_public_set',
  56. 'model': 'mail.message.subtype',
  57. 'module': 'mail',
  58. 'res_id': mt_group_public_set.id
  59. })
  60. # mt_group_public_set: group_public field (tracked as onchange) set to
  61. # nothing (m2o)
  62. mt_group_public_unset = subtype.create({
  63. 'name': 'group_public_unset',
  64. 'description': 'Group_public field unset'
  65. })
  66. data.create({
  67. 'name': 'mt_group_public_unset',
  68. 'model': 'mail.message.subtype',
  69. 'module': 'mail',
  70. 'res_id': mt_group_public_unset.id
  71. })
  72. @api.multi
  73. def _track_subtype(self, init_values):
  74. if 'public' in init_values and self.public == 'private':
  75. return 'mail.mt_private'
  76. elif 'name' in init_values and self.name == 'supername':
  77. return 'mail.mt_name_supername'
  78. elif 'group_public_id' in init_values and self.group_public_id:
  79. return 'mail.mt_group_public_set'
  80. elif 'group_public_id' in init_values and not self.group_public_id:
  81. return 'mail.mt_group_public_unset'
  82. return False
  83. self.registry('mail.channel')._patch_method(
  84. '_track_subtype', _track_subtype)
  85. visibility = {
  86. 'public': 'onchange',
  87. 'name': 'always',
  88. 'group_public_id': 'onchange'
  89. }
  90. channel = type(self.env['mail.channel'])
  91. for key in visibility:
  92. self.assertFalse(
  93. hasattr(getattr(channel, key), 'track_visibility'))
  94. getattr(channel, key).track_visibility = visibility[key]
  95. # Test: change name -> always tracked, not related to a subtype
  96. test_channel.sudo(self.user_employee).write({'name': 'my_name'})
  97. self.assertEqual(len(test_channel.message_ids), 1)
  98. last_msg = test_channel.message_ids[-1]
  99. self.assertEqual(last_msg.subtype_id, note_subtype)
  100. self.assertEqual(len(last_msg.tracking_value_ids), 1)
  101. self.assertEqual(last_msg.tracking_value_ids.field, 'name')
  102. self.assertEqual(last_msg.tracking_value_ids.field_desc, 'Name')
  103. self.assertEqual(last_msg.tracking_value_ids.old_value_char, 'Test')
  104. self.assertEqual(last_msg.tracking_value_ids.new_value_char, 'my_name')