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.

276 lines
12 KiB

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