From 09908b736fd9e7ffac08783f9771e7a8ac071d4f Mon Sep 17 00:00:00 2001 From: Nans Lefebvre Date: Mon, 2 Dec 2019 15:43:43 +0000 Subject: [PATCH] [FIX] base: when saving settings, only reset environment if modules were installed/uninstalled Execute the tests from ea574fe9ff644 and 9e9ac5ced80b30, which are in a SavepointCase. The second test fails on a cache_miss. Whats happens is the following: - the second test starts with the environment from the beginning of the first test. However its envs has a reference to environments that were created during the first test. - when computing the field, the ORM finds the value in a zombie environment, so does not recompute it - when trying to get the value, the value is not in cache The issue is that going through res.config.settings's execute resets the envs. One solution is to put: self.addCleanup(lambda: setattr(self.env.envs, 'envs', envs)) in SavepointCase's setup. However in that case the reset is not needed at all: it's necessary only if there were some modules to install. Unfortunately to_install and to_uninstall are not symmetric: to_uninstall contains only modules that should be uninstalled, whereas to_install contains everything, and only filters later what actions really need to be performed. In this case, we can rely on the result of the installation process. closes odoo/odoo#41126 Signed-off-by: Nans Lefebvre (len) --- addons/base_setup/tests/test_res_config.py | 41 ++++++++++++++++++++++ odoo/addons/base/models/res_config.py | 5 ++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/addons/base_setup/tests/test_res_config.py b/addons/base_setup/tests/test_res_config.py index a8352093468..67b32ed189a 100644 --- a/addons/base_setup/tests/test_res_config.py +++ b/addons/base_setup/tests/test_res_config.py @@ -1,11 +1,25 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +from unittest.mock import patch from odoo.tests.common import TransactionCase +def just_raise(*args): + raise Exception("We should not be here.") + + class TestResConfig(TransactionCase): + def setUp(self): + super(TestResConfig, self).setUp() + + self.user = self.env.ref('base.user_admin') + self.company = self.env['res.company'].create({'name': 'oobO'}) + self.user.write({'company_ids': [(4, self.company.id)], 'company_id': self.company.id}) + Settings = self.env['res.config.settings'].with_user(self.user.id) + self.config = Settings.create({}) + def test_multi_company_res_config_group(self): # Enable/Disable a group in a multi-company environment # 1/ All the users should be added/removed from the group @@ -53,3 +67,30 @@ class TestResConfig(TransactionCase): 'partner_id': new_partner.id, }) self.assertTrue(new_user not in self.env.ref('base.group_multi_currency').sudo().users) + + def test_no_install(self): + """Make sure that when saving settings, + no modules are installed if nothing was set to install. + """ + # check that no module should be installed in the first place + config_fields = self.config._get_classified_fields() + for name, module in config_fields['module']: + if int(self.config[name]): + self.assertTrue(module.state != 'uninstalled', + "All set modules should already be installed.") + # if we try to install something, raise; so nothing should be installed + with patch('odoo.addons.base.models.ir_module.Module._button_immediate_function', new=just_raise): + self.config.execute() + + def test_install(self): + """Make sure that the previous test is valid, i.e. when saving settings, + it starts module install if something was set to install. + """ + config_fields = self.config._get_classified_fields() + # set the first uninstalled module to install + module_to_install = next(filter(lambda m: m[1].state == 'uninstalled', config_fields['module'])) + self.config[module_to_install[0]] = True + + with patch('odoo.addons.base.models.ir_module.Module._button_immediate_function', new=just_raise): + with self.assertRaisesRegex(Exception, "We should not be here."): + self.config.execute() diff --git a/odoo/addons/base/models/res_config.py b/odoo/addons/base/models/res_config.py index 48832102825..f10e4574a31 100644 --- a/odoo/addons/base/models/res_config.py +++ b/odoo/addons/base/models/res_config.py @@ -627,10 +627,9 @@ class ResConfigSettings(models.TransientModel, ResConfigModuleInstallationMixin) if to_uninstall_modules: to_uninstall_modules.button_immediate_uninstall() - self._install_modules(to_install) + installation_status = self._install_modules(to_install) - - if to_install or to_uninstall_modules: + if installation_status or to_uninstall_modules: # After the uninstall/install calls, the registry and environments # are no longer valid. So we reset the environment. self.env.reset()