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.

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