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.

236 lines
9.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Serpent Consulting Services Pvt. Ltd. (support@serpentcs.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. @common.at_install(False)
  9. @common.post_install(True)
  10. class TestMassEditing(common.TransactionCase):
  11. def setUp(self):
  12. super(TestMassEditing, self).setUp()
  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. self.partner = self._create_partner()
  20. self.partner_model = model_obj.\
  21. search([('model', '=', 'res.partner')])
  22. self.user_model = model_obj.search([('model', '=', 'res.users')])
  23. self.fields_model = self.env['ir.model.fields'].\
  24. search([('model_id', '=', self.partner_model.id),
  25. ('name', 'in', ['email', 'phone', 'category_id', 'comment',
  26. 'country_id', 'customer', 'child_ids',
  27. 'title'])])
  28. self.mass = self._create_mass_editing(self.partner_model,
  29. self.fields_model,
  30. 'Partner')
  31. self.copy_mass = self.mass.copy()
  32. self.user = self._create_user()
  33. self.res_partner_title_model = self.env['res.partner.title']
  34. self.partner_title = self._create_partner_title()
  35. self.partner_title_model = model_obj.\
  36. search([('model', '=', 'res.partner.title')])
  37. self.fields_partner_title_model = self.env['ir.model.fields']. \
  38. search([('model_id', '=', self.partner_title_model.id),
  39. ('name', 'in', ['abbreviation'])])
  40. self.mass_partner_title = self._create_mass_editing(
  41. self.partner_title_model, 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. partner_title = self.res_partner_title_model.create({
  58. 'name': 'Ambassador',
  59. 'shortcut': 'Amb.',
  60. })
  61. # Adding translated terms
  62. ctx = {'lang': 'de_DE'}
  63. partner_title.with_context(ctx).write({'name': 'Botschafter',
  64. 'shortcut': 'Bots.'})
  65. return partner_title
  66. def _create_user(self):
  67. return self.env['res.users'].create({
  68. 'name': 'Test User',
  69. 'login': 'test_login',
  70. 'email': 'test@test.com',
  71. })
  72. def _create_mass_editing(self, model, fields, model_name):
  73. """Create a Mass Editing with Partner as model and
  74. email field of partner."""
  75. mass = self.mass_object_model.create({
  76. 'name': u'Mass Editing for {0}'.format(model_name),
  77. 'model_id': model.id,
  78. 'field_ids': [(6, 0, fields.ids)]
  79. })
  80. mass.create_action()
  81. return mass
  82. def _apply_action(self, obj, vals):
  83. """Create Wizard object to perform mass editing to
  84. REMOVE field's value."""
  85. ctx = {
  86. 'active_id': obj.id,
  87. 'active_ids': obj.ids,
  88. 'active_model': obj._name,
  89. }
  90. return self.mass_wiz_obj.with_context(ctx).create(vals)
  91. def test_wiz_fields_view_get(self):
  92. """Test whether fields_view_get method returns arch or not."""
  93. ctx = {
  94. 'mass_editing_object': self.mass.id,
  95. 'active_id': self.partner.id,
  96. 'active_ids': self.partner.ids,
  97. 'active_model': 'res.partner',
  98. }
  99. result = self.mass_wiz_obj.with_context(ctx).fields_view_get()
  100. self.assertTrue(result.get('arch'),
  101. 'Fields view get must return architecture.')
  102. def test_onchange_model(self):
  103. """Test whether onchange model_id returns model_id in list"""
  104. new_mass = self.mass_object_model.new({'model_id': self.user_model.id})
  105. new_mass._onchange_model_id()
  106. model_list = ast.literal_eval(new_mass.model_list)
  107. self.assertTrue(self.user_model.id in model_list,
  108. 'Onchange model list must contains model_id.')
  109. def test_mass_edit_partner_title(self):
  110. """Test Case for MASS EDITING which will check if translation
  111. was loaded for new partner title, and if they are removed
  112. as well as the value for the abbreviation for the partner title."""
  113. search_domain = [('res_id', '=', self.partner_title.id),
  114. ('type', '=', 'model'),
  115. ('name', '=', 'res.partner.title,shortcut'),
  116. ('lang', '=', 'de_DE')]
  117. translation_ids = self.ir_translation_model.search(search_domain)
  118. self.assertEqual(len(translation_ids), 1,
  119. 'Translation for Partner Title\'s Abbreviation '
  120. 'was not loaded properly.')
  121. vals = {
  122. 'selection__shortcut': 'remove',
  123. }
  124. self._apply_action(self.partner_title, vals)
  125. self.assertEqual(self.partner_title.shortcut, False,
  126. 'Partner Title\'s Abbreviation should be removed.')
  127. translation_ids = self.ir_translation_model.search(search_domain)
  128. self.assertEqual(len(translation_ids), 0,
  129. 'Translation for Partner Title\'s Abbreviation '
  130. 'was not removed properly.')
  131. def test_mass_edit_email(self):
  132. """Test Case for MASS EDITING which will remove and after add
  133. Partner's email and will assert the same."""
  134. # Remove email address
  135. vals = {
  136. 'selection__email': 'remove',
  137. 'selection__phone': 'remove',
  138. }
  139. self._apply_action(self.partner, vals)
  140. self.assertEqual(self.partner.email, False,
  141. 'Partner\'s Email should be removed.')
  142. # Set email address
  143. vals = {
  144. 'selection__email': 'set',
  145. 'email': 'sample@mycompany.com',
  146. }
  147. self._apply_action(self.partner, vals)
  148. self.assertNotEqual(self.partner.email, False,
  149. 'Partner\'s Email should be set.')
  150. def test_mass_edit_m2m_categ(self):
  151. """Test Case for MASS EDITING which will remove and add
  152. Partner's category m2m."""
  153. # Remove m2m categories
  154. vals = {
  155. 'selection__category_id': 'remove_m2m',
  156. }
  157. self._apply_action(self.partner, vals)
  158. self.assertNotEqual(self.partner.category_id, False,
  159. 'Partner\'s category should be removed.')
  160. # Add m2m categories
  161. dist_categ_id = self.env.ref('base.res_partner_category_13').id
  162. vals = {
  163. 'selection__category_id': 'add',
  164. 'category_id': [[6, 0, [dist_categ_id]]],
  165. }
  166. wiz_action = self._apply_action(self.partner, vals)
  167. self.assertTrue(dist_categ_id in self.partner.category_id.ids,
  168. 'Partner\'s category should be added.')
  169. # Check window close action
  170. res = wiz_action.action_apply()
  171. self.assertTrue(res['type'] == 'ir.actions.act_window_close',
  172. 'IR Action must be window close.')
  173. def test_mass_edit_copy(self):
  174. """Test if fields one2many field gets blank when mass editing record
  175. is copied.
  176. """
  177. self.assertEqual(self.copy_mass.field_ids.ids, [],
  178. 'Fields must be blank.')
  179. def test_sidebar_action(self):
  180. """Test if Sidebar Action is added / removed to / from give object."""
  181. action = self.mass.ref_ir_act_window_id and self.mass.ref_ir_value_id
  182. self.assertTrue(action, 'Sidebar action must be exists.')
  183. # Remove the sidebar actions
  184. self.mass.unlink_action()
  185. action = self.mass.ref_ir_act_window_id and self.mass.ref_ir_value_id
  186. self.assertFalse(action, 'Sidebar action must be removed.')
  187. def test_unlink_mass(self):
  188. """Test if related actions are removed when mass editing
  189. record is unlinked."""
  190. mass_action_id = "ir.actions.act_window," + str(self.mass.id)
  191. self.mass.unlink()
  192. value_cnt = self.env['ir.values'].search([('value', '=',
  193. mass_action_id)],
  194. count=True)
  195. self.assertTrue(value_cnt == 0,
  196. "Sidebar action must be removed when mass"
  197. " editing is unlinked.")
  198. def test_uninstall_hook(self):
  199. """Test if related actions are removed when mass editing
  200. record is uninstalled."""
  201. uninstall_hook(self.cr, registry)
  202. mass_action_id = "ir.actions.act_window," + str(self.mass.id)
  203. value_cnt = self.env['ir.values'].search([('value', '=',
  204. mass_action_id)],
  205. count=True)
  206. self.assertTrue(value_cnt == 0,
  207. "Sidebar action must be removed when mass"
  208. " editing module is uninstalled.")