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.

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