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.

174 lines
7.1 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. def test_onchange_model(self):
  74. """Test whether onchange model_id returns model_id in list"""
  75. new_mass = self.mass_object_model.new({'model_id': self.user_model.id})
  76. new_mass._onchange_model_id()
  77. model_list = ast.literal_eval(new_mass.model_list)
  78. self.assertTrue(self.user_model.id in model_list,
  79. 'Onchange model list must contains model_id.')
  80. def test_mass_edit_email(self):
  81. """Test Case for MASS EDITING which will remove and after add
  82. Partner's email and will assert the same."""
  83. # Remove email address
  84. vals = {
  85. 'selection__email': 'remove',
  86. 'selection__phone': 'remove',
  87. }
  88. self._apply_action(self.partner, vals)
  89. self.assertEqual(self.partner.email, False,
  90. 'Partner\'s Email should be removed.')
  91. # Set email address
  92. vals = {
  93. 'selection__email': 'set',
  94. 'email': 'sample@mycompany.com',
  95. }
  96. self._apply_action(self.partner, vals)
  97. self.assertNotEqual(self.partner.email, False,
  98. 'Partner\'s Email should be set.')
  99. def test_mass_edit_m2m_categ(self):
  100. """Test Case for MASS EDITING which will remove and add
  101. Partner's category m2m."""
  102. # Remove m2m categories
  103. vals = {
  104. 'selection__category_id': 'remove_m2m',
  105. }
  106. self._apply_action(self.partner, vals)
  107. self.assertNotEqual(self.partner.category_id, False,
  108. 'Partner\'s category should be removed.')
  109. # Add m2m categories
  110. dist_categ_id = self.env.ref('base.res_partner_category_13').id
  111. vals = {
  112. 'selection__category_id': 'add',
  113. 'category_id': [[6, 0, [dist_categ_id]]],
  114. }
  115. wiz_action = self._apply_action(self.partner, vals)
  116. self.assertTrue(dist_categ_id in self.partner.category_id.ids,
  117. 'Partner\'s category should be added.')
  118. # Check window close action
  119. res = wiz_action.action_apply()
  120. self.assertTrue(res['type'] == 'ir.actions.act_window_close',
  121. 'IR Action must be window close.')
  122. def test_mass_edit_copy(self):
  123. """Test if fields one2many field gets blank when mass editing record
  124. is copied.
  125. """
  126. self.assertEqual(self.copy_mass.field_ids.ids, [],
  127. 'Fields must be blank.')
  128. def test_sidebar_action(self):
  129. """Test if Sidebar Action is added / removed to / from give object."""
  130. action = self.mass.ref_ir_act_window_id and self.mass.ref_ir_value_id
  131. self.assertTrue(action, 'Sidebar action must be exists.')
  132. # Remove the sidebar actions
  133. self.mass.unlink_action()
  134. action = self.mass.ref_ir_act_window_id and self.mass.ref_ir_value_id
  135. self.assertFalse(action, 'Sidebar action must be removed.')
  136. def test_unlink_mass(self):
  137. """Test if related actions are removed when mass editing
  138. record is unlinked."""
  139. mass_action_id = "ir.actions.act_window," + str(self.mass.id)
  140. self.mass.unlink()
  141. value_cnt = self.env['ir.values'].search([('value', '=',
  142. mass_action_id)],
  143. count=True)
  144. self.assertTrue(value_cnt == 0,
  145. "Sidebar action must be removed when mass"
  146. " editing is unlinked.")
  147. def test_uninstall_hook(self):
  148. """Test if related actions are removed when mass editing
  149. record is uninstalled."""
  150. uninstall_hook(self.cr, registry)
  151. mass_action_id = "ir.actions.act_window," + str(self.mass.id)
  152. value_cnt = self.env['ir.values'].search([('value', '=',
  153. mass_action_id)],
  154. count=True)
  155. self.assertTrue(value_cnt == 0,
  156. "Sidebar action must be removed when mass"
  157. " editing module is uninstalled.")