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.

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