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.

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