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.

205 lines
8.4 KiB

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