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.

264 lines
11 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 ..hooks import uninstall_hook
  7. class TestMassEditing(common.TransactionCase):
  8. at_install = False
  9. post_install = True
  10. def setUp(self):
  11. super(TestMassEditing, self).setUp()
  12. # Model connections
  13. model_obj = self.env['ir.model']
  14. self.mass_wiz_obj = self.env['mass.editing.wizard']
  15. self.mass_object_model = self.env['mass.object']
  16. self.res_partner_model = self.env['res.partner']
  17. self.ir_translation_model = self.env['ir.translation']
  18. self.lang_model = self.env['res.lang']
  19. # Shared data for test methods
  20. self.partner = self._create_partner()
  21. self.partner_model = model_obj.\
  22. search([('model', '=', 'res.partner')])
  23. self.user_model = model_obj.search([('model', '=', 'res.users')])
  24. self.fields_model = self.env['ir.model.fields'].\
  25. search([('model_id', '=', self.partner_model.id),
  26. ('name', 'in', ['email', 'phone', 'category_id', 'comment',
  27. 'country_id', 'customer', 'child_ids',
  28. 'title'])])
  29. self.mass = self._create_mass_editing(self.partner_model,
  30. self.fields_model,
  31. 'Partner')
  32. self.copy_mass = self.mass.copy()
  33. self.user = self._create_user()
  34. self.res_partner_title_model = self.env['res.partner.title']
  35. self.partner_title = self._create_partner_title()
  36. self.partner_title_model = model_obj.search(
  37. [('model', '=', 'res.partner.title')])
  38. self.fields_partner_title_model = self.env['ir.model.fields'].search(
  39. [('model_id', '=', self.partner_title_model.id),
  40. ('name', 'in', ['abbreviation'])])
  41. self.mass_partner_title = self._create_mass_editing(
  42. self.partner_title_model,
  43. self.fields_partner_title_model,
  44. 'Partner Title')
  45. def _create_partner(self):
  46. """Create a Partner."""
  47. categ_ids = self.env['res.partner.category'].search([]).ids
  48. return self.res_partner_model.create({
  49. 'name': 'Test Partner',
  50. 'email': 'example@yourcompany.com',
  51. 'phone': 123456,
  52. 'category_id': [(6, 0, categ_ids)],
  53. })
  54. def _create_partner_title(self):
  55. """Create a Partner Title."""
  56. # Loads German to work with translations
  57. self.lang_model.load_lang('de_DE')
  58. # Creating the title in English
  59. partner_title = self.res_partner_title_model.create({
  60. 'name': 'Ambassador',
  61. 'shortcut': 'Amb.',
  62. })
  63. # Adding translated terms
  64. ctx = {'lang': 'de_DE'}
  65. partner_title.with_context(ctx).write({
  66. 'name': 'Botschafter',
  67. 'shortcut': 'Bots.'})
  68. return partner_title
  69. def _create_user(self):
  70. return self.env['res.users'].create({
  71. 'name': 'Test User',
  72. 'login': 'test_login',
  73. 'email': 'test@test.com',
  74. })
  75. def _create_mass_editing(self, model, fields, model_name):
  76. """Create a Mass Editing with Partner as model and
  77. email field of partner."""
  78. mass = self.mass_object_model.create({
  79. 'name': 'Mass Editing for {0}'.format(model_name),
  80. 'model_id': model.id,
  81. 'field_ids': [(6, 0, fields.ids)]
  82. })
  83. mass.create_action()
  84. return mass
  85. def _apply_action(self, obj, vals):
  86. """Create Wizard object to perform mass editing to
  87. REMOVE field's value."""
  88. ctx = {
  89. 'active_id': obj.id,
  90. 'active_ids': obj.ids,
  91. 'active_model': obj._name,
  92. }
  93. return self.mass_wiz_obj.with_context(ctx).create(vals)
  94. def test_wiz_fields_view_get(self):
  95. """Test whether fields_view_get method returns arch or not."""
  96. ctx = {
  97. 'mass_editing_object': self.mass.id,
  98. 'active_id': self.partner.id,
  99. 'active_ids': self.partner.ids,
  100. 'active_model': 'res.partner',
  101. }
  102. result = self.mass_wiz_obj.with_context(ctx).fields_view_get()
  103. self.assertTrue(result.get('arch'),
  104. 'Fields view get must return architecture.')
  105. def test_onchange_model(self):
  106. """Test whether onchange model_id returns model_id in list"""
  107. new_mass = self.mass_object_model.new({'model_id': self.user_model.id})
  108. new_mass._onchange_model_id()
  109. model_list = ast.literal_eval(new_mass.model_list)
  110. self.assertTrue(self.user_model.id in model_list,
  111. 'Onchange model list must contains model_id.')
  112. def test_wiz_read_fields(self):
  113. """Test whether read method returns all fields or not."""
  114. ctx = {
  115. 'mass_editing_object': self.mass.id,
  116. 'active_id': self.partner.id,
  117. 'active_ids': self.partner.ids,
  118. 'active_model': 'res.partner',
  119. }
  120. fields_view = self.mass_wiz_obj.with_context(ctx).fields_view_get()
  121. fields = list(fields_view['fields'].keys())
  122. # add a real field
  123. fields.append('display_name')
  124. vals = {
  125. 'selection__email': 'remove',
  126. 'selection__phone': 'remove',
  127. }
  128. mass_wiz_obj = self._apply_action(self.partner, vals)
  129. result = mass_wiz_obj.read(fields)[0]
  130. self.assertTrue(all([field in result for field in fields]),
  131. 'Read must return all fields.')
  132. def test_mass_edit_partner_title(self):
  133. """Test Case for MASS EDITING which will check if translation
  134. was loaded for new partner title, and if they are removed
  135. as well as the value for the abbreviation for the partner title."""
  136. search_domain = [('res_id', '=', self.partner_title.id),
  137. ('type', '=', 'model'),
  138. ('name', '=', 'res.partner.title,shortcut'),
  139. ('lang', '=', 'de_DE')]
  140. translation_ids = self.ir_translation_model.search(search_domain)
  141. self.assertEqual(len(translation_ids), 1,
  142. 'Translation for Partner Title\'s Abbreviation '
  143. 'was not loaded properly.')
  144. # Removing partner title with mass edit action
  145. vals = {
  146. 'selection__shortcut': 'remove',
  147. }
  148. self._apply_action(self.partner_title, vals)
  149. self.assertEqual(self.partner_title.shortcut, False,
  150. 'Partner Title\'s Abbreviation should be removed.')
  151. # Checking if translations were also removed
  152. translation_ids = self.ir_translation_model.search(search_domain)
  153. self.assertEqual(len(translation_ids), 0,
  154. 'Translation for Partner Title\'s Abbreviation '
  155. 'was not removed properly.')
  156. def test_mass_edit_email(self):
  157. """Test Case for MASS EDITING which will remove and after add
  158. Partner's email and will assert the same."""
  159. # Remove email address
  160. vals = {
  161. 'selection__email': 'remove',
  162. 'selection__phone': 'remove',
  163. }
  164. self._apply_action(self.partner, vals)
  165. self.assertEqual(self.partner.email, False,
  166. 'Partner\'s Email should be removed.')
  167. # Set email address
  168. vals = {
  169. 'selection__email': 'set',
  170. 'email': 'sample@mycompany.com',
  171. }
  172. self._apply_action(self.partner, vals)
  173. self.assertNotEqual(self.partner.email, False,
  174. 'Partner\'s Email should be set.')
  175. def test_mass_edit_m2m_categ(self):
  176. """Test Case for MASS EDITING which will remove and add
  177. Partner's category m2m."""
  178. # Remove m2m categories
  179. vals = {
  180. 'selection__category_id': 'remove_m2m',
  181. }
  182. self._apply_action(self.partner, vals)
  183. self.assertNotEqual(self.partner.category_id, False,
  184. 'Partner\'s category should be removed.')
  185. # Add m2m categories
  186. dist_categ_id = self.env.ref('base.res_partner_category_13').id
  187. vend_categ_id = self.env.ref('base.res_partner_category_1').id
  188. vals = {
  189. 'selection__category_id': 'add',
  190. 'category_id': [[6, 0, [dist_categ_id, vend_categ_id]]],
  191. }
  192. wiz_action = self._apply_action(self.partner, vals)
  193. self.assertTrue(all(item in self.partner.category_id.ids
  194. for item in [dist_categ_id, vend_categ_id]),
  195. 'Partner\'s category should be added.')
  196. # Remove one m2m category
  197. vals = {
  198. 'selection__category_id': 'remove_m2m',
  199. 'category_id': [[6, 0, [vend_categ_id]]],
  200. }
  201. wiz_action = self._apply_action(self.partner, vals)
  202. self.assertTrue([dist_categ_id] == self.partner.category_id.ids,
  203. 'Partner\'s category should be removed.')
  204. # Check window close action
  205. res = wiz_action.action_apply()
  206. self.assertTrue(res['type'] == 'ir.actions.act_window_close',
  207. 'IR Action must be window close.')
  208. def test_mass_edit_copy(self):
  209. """Test if fields one2many field gets blank when mass editing record
  210. is copied.
  211. """
  212. self.assertEqual(self.copy_mass.field_ids.ids, [],
  213. 'Fields must be blank.')
  214. def test_sidebar_action(self):
  215. """Test if Sidebar Action is added / removed to / from give object."""
  216. action = self.mass.ref_ir_act_window_id\
  217. and self.mass.ref_ir_act_window_id.binding_model_id
  218. self.assertTrue(action, 'Sidebar action must be exists.')
  219. # Remove the sidebar actions
  220. self.mass.unlink_action()
  221. action = self.mass.ref_ir_act_window_id
  222. self.assertFalse(action, 'Sidebar action must be removed.')
  223. def test_unlink_mass(self):
  224. """Test if related actions are removed when mass editing
  225. record is unlinked."""
  226. mass_action_id = self.mass.ref_ir_act_window_id.id
  227. mass_object_id = self.mass.id
  228. mass_id = self.env['mass.object'].browse(mass_object_id)
  229. mass_id.unlink()
  230. value_cnt = self.env['ir.actions.act_window'].search([
  231. ('id', '=', mass_action_id)], count=True)
  232. self.assertTrue(value_cnt == 0,
  233. "Sidebar action must be removed when mass"
  234. " editing is unlinked.")
  235. def test_uninstall_hook(self):
  236. """Test if related actions are removed when mass editing
  237. record is uninstalled."""
  238. uninstall_hook(self.cr, registry)
  239. mass_action_id = self.mass.ref_ir_act_window_id.id
  240. value_cnt = len(self.env['ir.actions.act_window'].browse(
  241. mass_action_id))
  242. self.assertTrue(value_cnt == 0,
  243. "Sidebar action must be removed when mass"
  244. " editing module is uninstalled.")