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.

240 lines
9.9 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 openerp.tests import common
  6. from openerp.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_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. fields = result.get("fields")
  103. self.assertTrue(fields)
  104. for name, values in fields.items():
  105. self.assertTrue(isinstance(values["views"], dict))
  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_mass_edit_partner_title(self):
  114. """Test Case for MASS EDITING which will check if translation
  115. was loaded for new partner title, and if they are removed
  116. as well as the value for the abbreviation for the partner title."""
  117. search_domain = [('res_id', '=', self.partner_title.id),
  118. ('type', '=', 'model'),
  119. ('name', '=', 'res.partner.title,shortcut'),
  120. ('lang', '=', 'de_DE')]
  121. translation_ids = self.ir_translation_model.search(search_domain)
  122. self.assertEqual(len(translation_ids), 1,
  123. 'Translation for Partner Title\'s Abbreviation '
  124. 'was not loaded properly.')
  125. vals = {
  126. 'selection__shortcut': 'remove',
  127. }
  128. self._apply_action(self.partner_title, vals)
  129. self.assertEqual(self.partner_title.shortcut, False,
  130. 'Partner Title\'s Abbreviation should be removed.')
  131. translation_ids = self.ir_translation_model.search(search_domain)
  132. self.assertEqual(len(translation_ids), 0,
  133. 'Translation for Partner Title\'s Abbreviation '
  134. 'was not removed properly.')
  135. def test_mass_edit_email(self):
  136. """Test Case for MASS EDITING which will remove and after add
  137. Partner's email and will assert the same."""
  138. # Remove email address
  139. vals = {
  140. 'selection__email': 'remove',
  141. 'selection__phone': 'remove',
  142. }
  143. self._apply_action(self.partner, vals)
  144. self.assertEqual(self.partner.email, False,
  145. 'Partner\'s Email should be removed.')
  146. # Set email address
  147. vals = {
  148. 'selection__email': 'set',
  149. 'email': 'sample@mycompany.com',
  150. }
  151. self._apply_action(self.partner, vals)
  152. self.assertNotEqual(self.partner.email, False,
  153. 'Partner\'s Email should be set.')
  154. def test_mass_edit_m2m_categ(self):
  155. """Test Case for MASS EDITING which will remove and add
  156. Partner's category m2m."""
  157. # Remove m2m categories
  158. vals = {
  159. 'selection__category_id': 'remove_m2m',
  160. }
  161. self._apply_action(self.partner, vals)
  162. self.assertNotEqual(self.partner.category_id, False,
  163. 'Partner\'s category should be removed.')
  164. # Add m2m categories
  165. dist_categ_id = self.env.ref('base.res_partner_category_13').id
  166. vals = {
  167. 'selection__category_id': 'add',
  168. 'category_id': [[6, 0, [dist_categ_id]]],
  169. }
  170. wiz_action = self._apply_action(self.partner, vals)
  171. self.assertTrue(dist_categ_id in self.partner.category_id.ids,
  172. 'Partner\'s category should be added.')
  173. # Check window close action
  174. res = wiz_action.action_apply()
  175. self.assertTrue(res['type'] == 'ir.actions.act_window_close',
  176. 'IR Action must be window close.')
  177. def test_mass_edit_copy(self):
  178. """Test if fields one2many field gets blank when mass editing record
  179. is copied.
  180. """
  181. self.assertEqual(self.copy_mass.field_ids.ids, [],
  182. 'Fields must be blank.')
  183. def test_sidebar_action(self):
  184. """Test if Sidebar Action is added / removed to / from give object."""
  185. action = self.mass.ref_ir_act_window_id and self.mass.ref_ir_value_id
  186. self.assertTrue(action, 'Sidebar action must be exists.')
  187. # Remove the sidebar actions
  188. self.mass.unlink_action()
  189. action = self.mass.ref_ir_act_window_id and self.mass.ref_ir_value_id
  190. self.assertFalse(action, 'Sidebar action must be removed.')
  191. def test_unlink_mass(self):
  192. """Test if related actions are removed when mass editing
  193. record is unlinked."""
  194. mass_action_id = "ir.actions.act_window," + str(self.mass.id)
  195. self.mass.unlink()
  196. value_cnt = self.env['ir.values'].search([('value', '=',
  197. mass_action_id)],
  198. count=True)
  199. self.assertTrue(value_cnt == 0,
  200. "Sidebar action must be removed when mass"
  201. " editing is unlinked.")
  202. def test_uninstall_hook(self):
  203. """Test if related actions are removed when mass editing
  204. record is uninstalled."""
  205. uninstall_hook(self.cr, registry)
  206. mass_action_id = "ir.actions.act_window," + str(self.mass.id)
  207. value_cnt = self.env['ir.values'].search([('value', '=',
  208. mass_action_id)],
  209. count=True)
  210. self.assertTrue(value_cnt == 0,
  211. "Sidebar action must be removed when mass"
  212. " editing module is uninstalled.")