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.

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