# Copyright 2016 Serpent Consulting Services Pvt. Ltd. (support@serpentcs.com) # Copyright 2018 Aitor Bouzas # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from ast import literal_eval from flectra.exceptions import ValidationError from flectra.tests import Form, common, new_test_user from flectra.addons.base.models.ir_actions import IrActionsServer def fake_onchange_model_id(self): result = { "warning": { "title": "This is a fake onchange", }, } return result @common.tagged("-at_install", "post_install") class TestMassEditing(common.TransactionCase): def setUp(self): super().setUp() self.MassEditingWizard = self.env["mass.editing.wizard"] self.ResPartnerTitle = self.env["res.partner.title"] self.ResLang = self.env["res.lang"] self.IrActionsActWindow = self.env["ir.actions.act_window"] self.mass_editing_user = self.env.ref( "server_action_mass_edit.mass_editing_user" ) self.mass_editing_partner_title = self.env.ref( "server_action_mass_edit.mass_editing_partner_title" ) user_admin = self.env.ref("base.user_admin") user_demo = self.env.ref("base.user_demo") self.users = self.env["res.users"].search( [("id", "not in", (user_admin.id, user_demo.id))] ) self.user = new_test_user( self.env, login="test-mass_editing-user", groups="base.group_system", ) self.partner_title = self._create_partner_title() def _create_partner_title(self): """Create a Partner Title.""" # Loads German to work with translations self.ResLang._activate_lang("de_DE") # Creating the title in English partner_title = self.ResPartnerTitle.create( {"name": "Ambassador", "shortcut": "Amb."} ) # Adding translated terms partner_title.with_context(lang="de_DE").write( {"name": "Botschafter", "shortcut": "Bots."} ) return partner_title def _create_wizard_and_apply_values(self, server_action, items, vals): action = server_action.with_context( active_model=items._name, active_ids=items.ids, ).run() wizard = ( self.env[action["res_model"]] .with_context( **literal_eval(action["context"]), ) .create(vals) ) wizard.button_apply() return wizard def test_wzd_default_get(self): """Test whether `operation_description_danger` is correct""" wzd_obj = self.MassEditingWizard.with_context( server_action_id=self.mass_editing_user.id, active_ids=[1], original_active_ids=[1], ) result = wzd_obj.default_get( fields=[], ) self.assertEqual( result["operation_description_info"], "The treatment will be processed on the 1 selected record(s).", ) self.assertFalse( result["operation_description_warning"], ) self.assertFalse( result["operation_description_danger"], ) result = wzd_obj.with_context(active_ids=[]).default_get( fields=[], ) self.assertFalse( result["operation_description_info"], ) self.assertEqual( result["operation_description_warning"], ( "You have selected 1 record(s) that can not be processed.\n" "Only 0 record(s) will be processed." ), ) self.assertFalse( result["operation_description_danger"], ) result = wzd_obj.with_context(original_active_ids=[]).default_get( fields=[], ) self.assertFalse( result["operation_description_info"], ) self.assertFalse( result["operation_description_warning"], ) self.assertEqual( result["operation_description_danger"], "None of the 1 record(s) you have selected can be processed.", ) def test_wiz_fields_view_get(self): """Test whether fields_view_get method returns arch. with dynamic fields. """ view_id = self.env.ref("server_action_mass_edit.view_mass_editing_wizard_form") view_id.mass_server_action_id = False result = self.MassEditingWizard.with_context( active_ids=[], ).get_view(view_id=view_id.id) arch = result.get("arch", "") self.assertTrue( "selection__email" not in arch, "Fields view get must return architecture w/o fields" "created dynamicaly", ) view_id.mass_server_action_id = self.mass_editing_user result = self.MassEditingWizard.with_context( server_action_id=self.mass_editing_user.id, active_ids=[], ).get_view(view_id=view_id.id) arch = result.get("arch", "") self.assertTrue( "selection__email" in arch, "Fields view get must return architecture with fields" "created dynamicaly", ) # test the code path where we extract an embedded tree for o2m fields self.env["ir.ui.view"].search( [ ("model", "in", ("res.partner.bank", "res.partner", "res.users")), ("id", "!=", self.env.ref("base.res_partner_view_form_private").id), ] ).unlink() self.env.ref("base.res_partner_view_form_private").model = "res.users" result = self.MassEditingWizard.with_context( server_action_id=self.mass_editing_user.id, active_ids=[], ).get_view(view_id=view_id.id) arch = result.get("arch", "") self.assertIn( "