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.

175 lines
7.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Serpent Consulting Services Pvt. Ltd. (support@serpentcs.com)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  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. fields = result.get("fields")
  73. self.assertTrue(fields)
  74. for name, values in fields.items():
  75. self.assertTrue(isinstance(values["views"], dict))
  76. def test_onchange_model(self):
  77. """Test whether onchange model_id returns model_id in list"""
  78. new_mass = self.mass_object_model.new({'model_id': self.user_model.id})
  79. new_mass._onchange_model_id()
  80. self.assertTrue(self.user_model.id in new_mass.model_ids.ids,
  81. 'Onchange model ids 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.")