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.

176 lines
7.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Serpent Consulting Services Pvt. Ltd. (support@serpentcs.com)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import ast
  5. from odoo.tests import common
  6. from odoo.modules import registry
  7. from odoo.addons.mass_editing.hooks import uninstall_hook
  8. @common.at_install(False)
  9. @common.post_install(True)
  10. class TestMassEditing(common.TransactionCase):
  11. def setUp(self):
  12. super(TestMassEditing, self).setUp()
  13. model_obj = self.env['ir.model']
  14. self.mass_wiz_obj = self.env['mass.editing.wizard']
  15. self.mass_object_model = self.env['mass.object']
  16. self.res_partner_model = self.env['res.partner']
  17. self.partner = self._create_partner()
  18. self.partner_model = model_obj.\
  19. search([('model', '=', 'res.partner')])
  20. self.user_model = model_obj.search([('model', '=', 'res.users')])
  21. self.fields_model = self.env['ir.model.fields'].\
  22. search([('model_id', '=', self.partner_model.id),
  23. ('name', 'in', ['email', 'phone', 'category_id', 'comment',
  24. 'country_id', 'customer', 'child_ids',
  25. 'title'])])
  26. self.mass = self._create_mass_editing(self.partner_model,
  27. self.fields_model)
  28. self.copy_mass = self.mass.copy()
  29. self.user = self._create_user()
  30. def _create_partner(self):
  31. """Create a Partner."""
  32. categ_ids = self.env['res.partner.category'].search([]).ids
  33. return self.res_partner_model.create({
  34. 'name': 'Test Partner',
  35. 'email': 'example@yourcompany.com',
  36. 'phone': 123456,
  37. 'category_id': [(6, 0, categ_ids)],
  38. })
  39. def _create_user(self):
  40. return self.env['res.users'].create({
  41. 'name': 'Test User',
  42. 'login': 'test_login',
  43. 'email': 'test@test.com',
  44. })
  45. def _create_mass_editing(self, model, fields):
  46. """Create a Mass Editing with Partner as model and
  47. email field of partner."""
  48. mass = self.mass_object_model.create({
  49. 'name': 'Mass Editing for Partner',
  50. 'model_id': model.id,
  51. 'field_ids': [(6, 0, fields.ids)]
  52. })
  53. mass.create_action()
  54. return mass
  55. def _apply_action(self, partner, vals):
  56. """Create Wizard object to perform mass editing to
  57. REMOVE field's value."""
  58. ctx = {
  59. 'active_id': partner.id,
  60. 'active_ids': partner.ids,
  61. 'active_model': 'res.partner',
  62. }
  63. return self.mass_wiz_obj.with_context(ctx).create(vals)
  64. def test_wiz_fields_view_get(self):
  65. """Test whether fields_view_get method returns arch or not."""
  66. ctx = {
  67. 'mass_editing_object': self.mass.id,
  68. 'active_id': self.partner.id,
  69. 'active_ids': self.partner.ids,
  70. 'active_model': 'res.partner',
  71. }
  72. result = self.mass_wiz_obj.with_context(ctx).fields_view_get()
  73. self.assertTrue(result.get('arch'),
  74. 'Fields view get must return architecture.')
  75. def test_onchange_model(self):
  76. """Test whether onchange model_id returns model_id in list"""
  77. new_mass = self.mass_object_model.new({'model_id': self.user_model.id})
  78. new_mass._onchange_model_id()
  79. model_list = ast.literal_eval(new_mass.model_list)
  80. self.assertTrue(self.user_model.id in model_list,
  81. 'Onchange model list must contains model_id.')
  82. def test_mass_edit_email(self):
  83. """Test Case for MASS EDITING which will remove and after add
  84. Partner's email and will assert the same."""
  85. # Remove email address
  86. vals = {
  87. 'selection__email': 'remove',
  88. 'selection__phone': 'remove',
  89. }
  90. self._apply_action(self.partner, vals)
  91. self.assertEqual(self.partner.email, False,
  92. 'Partner\'s Email should be removed.')
  93. # Set email address
  94. vals = {
  95. 'selection__email': 'set',
  96. 'email': 'sample@mycompany.com',
  97. }
  98. self._apply_action(self.partner, vals)
  99. self.assertNotEqual(self.partner.email, False,
  100. 'Partner\'s Email should be set.')
  101. def test_mass_edit_m2m_categ(self):
  102. """Test Case for MASS EDITING which will remove and add
  103. Partner's category m2m."""
  104. # Remove m2m categories
  105. vals = {
  106. 'selection__category_id': 'remove_m2m',
  107. }
  108. self._apply_action(self.partner, vals)
  109. self.assertNotEqual(self.partner.category_id, False,
  110. 'Partner\'s category should be removed.')
  111. # Add m2m categories
  112. dist_categ_id = self.env.ref('base.res_partner_category_13').id
  113. vals = {
  114. 'selection__category_id': 'add',
  115. 'category_id': [[6, 0, [dist_categ_id]]],
  116. }
  117. wiz_action = self._apply_action(self.partner, vals)
  118. self.assertTrue(dist_categ_id in self.partner.category_id.ids,
  119. 'Partner\'s category should be added.')
  120. # Check window close action
  121. res = wiz_action.action_apply()
  122. self.assertTrue(res['type'] == 'ir.actions.act_window_close',
  123. 'IR Action must be window close.')
  124. def test_mass_edit_copy(self):
  125. """Test if fields one2many field gets blank when mass editing record
  126. is copied.
  127. """
  128. self.assertEqual(self.copy_mass.field_ids.ids, [],
  129. 'Fields must be blank.')
  130. def test_sidebar_action(self):
  131. """Test if Sidebar Action is added / removed to / from give object."""
  132. action = self.mass.ref_ir_act_window_id and self.mass.ref_ir_value_id
  133. self.assertTrue(action, 'Sidebar action must be exists.')
  134. # Remove the sidebar actions
  135. self.mass.unlink_action()
  136. action = self.mass.ref_ir_act_window_id and self.mass.ref_ir_value_id
  137. self.assertFalse(action, 'Sidebar action must be removed.')
  138. def test_unlink_mass(self):
  139. """Test if related actions are removed when mass editing
  140. record is unlinked."""
  141. mass_action_id = "ir.actions.act_window," + str(self.mass.id)
  142. self.mass.unlink()
  143. value_cnt = self.env['ir.values'].search([('value', '=',
  144. mass_action_id)],
  145. count=True)
  146. self.assertTrue(value_cnt == 0,
  147. "Sidebar action must be removed when mass"
  148. " editing is unlinked.")
  149. def test_uninstall_hook(self):
  150. """Test if related actions are removed when mass editing
  151. record is uninstalled."""
  152. uninstall_hook(self.cr, registry)
  153. mass_action_id = "ir.actions.act_window," + str(self.mass.id)
  154. value_cnt = self.env['ir.values'].search([('value', '=',
  155. mass_action_id)],
  156. count=True)
  157. self.assertTrue(value_cnt == 0,
  158. "Sidebar action must be removed when mass"
  159. " editing module is uninstalled.")