1# -*- coding: utf-8 -*-
2# Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4import logging
5
6from odoo import exceptions
7from odoo.tests.common import TransactionCase, tagged
8
9_logger = logging.getLogger(__name__)
10
11
12class TestResConfig(TransactionCase):
13
14    def setUp(self):
15        super(TestResConfig, self).setUp()
16        self.ResConfig = self.env['res.config.settings']
17
18        # Define the test values
19        self.menu_xml_id = 'base.menu_action_res_users'
20        self.full_field_name = 'res.partner.lang'
21        self.error_msg = "WarningRedirect test string: %(field:res.partner.lang)s - %(menu:base.menu_action_res_users)s."
22        self.error_msg_wo_menu = "WarningRedirect test string: %(field:res.partner.lang)s."
23        # Note: see the get_config_warning() doc for a better example
24
25        # Fetch the expected values
26        menu = self.env.ref(self.menu_xml_id)
27
28        model_name, field_name = self.full_field_name.rsplit('.', 1)
29
30        self.expected_path = menu.complete_name
31        self.expected_action_id = menu.action.id
32        self.expected_name = self.env[model_name].fields_get([field_name])[field_name]['string']
33        self.expected_final_error_msg = self.error_msg % {
34            'field:res.partner.lang': self.expected_name,
35            'menu:base.menu_action_res_users': self.expected_path
36        }
37        self.expected_final_error_msg_wo_menu = self.error_msg_wo_menu % {
38            'field:res.partner.lang': self.expected_name,
39        }
40
41    def test_00_get_option_path(self):
42        """ The get_option_path() method should return a tuple containing a string and an integer """
43        res = self.ResConfig.get_option_path(self.menu_xml_id)
44
45        # Check types
46        self.assertIsInstance(res, tuple)
47        self.assertEqual(len(res), 2, "The result should contain 2 elements")
48        self.assertIsInstance(res[0], str)
49        self.assertIsInstance(res[1], int)
50
51        # Check returned values
52        self.assertEqual(res[0], self.expected_path)
53        self.assertEqual(res[1], self.expected_action_id)
54
55    def test_10_get_option_name(self):
56        """ The get_option_name() method should return a string """
57        res = self.ResConfig.get_option_name(self.full_field_name)
58
59        # Check type
60        self.assertIsInstance(res, str)
61
62        # Check returned value
63        self.assertEqual(res, self.expected_name)
64
65    def test_20_get_config_warning(self):
66        """ The get_config_warning() method should return a RedirectWarning """
67        res = self.ResConfig.get_config_warning(self.error_msg)
68
69        # Check type
70        self.assertIsInstance(res, exceptions.RedirectWarning)
71
72        # Check returned value
73        self.assertEqual(res.args[0], self.expected_final_error_msg)
74        self.assertEqual(res.args[1], self.expected_action_id)
75
76    def test_30_get_config_warning_wo_menu(self):
77        """ The get_config_warning() method should return a Warning exception """
78        res = self.ResConfig.get_config_warning(self.error_msg_wo_menu)
79
80        # Check type
81        self.assertIsInstance(res, exceptions.UserError)
82
83        # Check returned value
84        self.assertEqual(res.args[0], self.expected_final_error_msg_wo_menu)
85
86
87@tagged('post_install', '-at_install')
88class TestResConfigExecute(TransactionCase):
89
90    def test_01_execute_res_config(self):
91        """
92        Try to create and execute all res_config models. Target settings that can't be
93        loaded or saved and avoid remaining methods `get_default_foo` or `set_foo` that
94        won't be executed is foo != `fields`
95        """
96        all_config_settings = self.env['ir.model'].search([('name', 'like', 'config.settings')])
97        for config_settings in all_config_settings:
98            _logger.info("Testing %s" % (config_settings.name))
99            self.env[config_settings.name].create({}).execute()
100