cubicweb/web/test/unittest_uicfg.py
changeset 11057 0b59724cb3f2
parent 9341 099a3a33eaaa
child 11876 b35e21fc1f9b
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 import copy
       
    19 from logilab.common.testlib import tag
       
    20 from cubicweb.devtools.testlib import CubicWebTC
       
    21 from cubicweb.web import uihelper, formwidgets as fwdgs
       
    22 from cubicweb.web.views import uicfg
       
    23 
       
    24 abaa = uicfg.actionbox_appearsin_addmenu
       
    25 
       
    26 class UICFGTC(CubicWebTC):
       
    27 
       
    28     def test_default_actionbox_appearsin_addmenu_config(self):
       
    29         self.assertFalse(abaa.etype_get('TrInfo', 'wf_info_for', 'object', 'CWUser'))
       
    30 
       
    31 
       
    32 
       
    33 class DefinitionOrderTC(CubicWebTC):
       
    34     """This test check that when multiple definition could match a key, only
       
    35     the more accurate apply"""
       
    36 
       
    37     def setUp(self):
       
    38         super(DefinitionOrderTC, self).setUp()
       
    39         for rtag in (uicfg.autoform_section, uicfg.autoform_field_kwargs):
       
    40             rtag._old_tagdefs = copy.deepcopy(rtag._tagdefs)
       
    41         new_def = (
       
    42                     (('*', 'login', '*'),
       
    43                          {'formtype':'main', 'section':'hidden'}),
       
    44                     (('*', 'login', '*'),
       
    45                          {'formtype':'muledit', 'section':'hidden'}),
       
    46                     (('CWUser', 'login', '*'),
       
    47                          {'formtype':'main', 'section':'attributes'}),
       
    48                     (('CWUser', 'login', '*'),
       
    49                          {'formtype':'muledit', 'section':'attributes'}),
       
    50                     (('CWUser', 'login', 'String'),
       
    51                          {'formtype':'main', 'section':'inlined'}),
       
    52                     (('CWUser', 'login', 'String'),
       
    53                          {'formtype':'inlined', 'section':'attributes'}),
       
    54                     )
       
    55         for key, kwargs in new_def:
       
    56             uicfg.autoform_section.tag_subject_of(key, **kwargs)
       
    57 
       
    58     def tearDown(self):
       
    59         super(DefinitionOrderTC, self).tearDown()
       
    60         for rtag in (uicfg.autoform_section, uicfg.autoform_field_kwargs):
       
    61             rtag._tagdefs = rtag._old_tagdefs
       
    62 
       
    63     @tag('uicfg')
       
    64     def test_definition_order_hidden(self):
       
    65         result = uicfg.autoform_section.get('CWUser', 'login', 'String', 'subject')
       
    66         expected = set(['main_inlined', 'muledit_attributes', 'inlined_attributes'])
       
    67         self.assertSetEqual(result, expected)
       
    68 
       
    69     @tag('uihelper', 'order', 'func')
       
    70     def test_uihelper_set_fields_order(self):
       
    71         afk_get = uicfg.autoform_field_kwargs.get
       
    72         self.assertEqual(afk_get('CWUser', 'firstname', 'String', 'subject'), {})
       
    73         uihelper.set_fields_order('CWUser', ('login', 'firstname', 'surname'))
       
    74         self.assertEqual(afk_get('CWUser', 'firstname', 'String', 'subject'), {'order': 1})
       
    75 
       
    76     @tag('uicfg', 'order', 'func')
       
    77     def test_uicfg_primaryview_set_fields_order(self):
       
    78         pvdc = uicfg.primaryview_display_ctrl
       
    79         pvdc.set_fields_order('CWUser', ('login', 'firstname', 'surname'))
       
    80         self.assertEqual(pvdc.get('CWUser', 'login', 'String', 'subject'), {'order': 0})
       
    81         self.assertEqual(pvdc.get('CWUser', 'firstname', 'String', 'subject'), {'order': 1})
       
    82         self.assertEqual(pvdc.get('CWUser', 'surname', 'String', 'subject'), {'order': 2})
       
    83 
       
    84     @tag('uihelper', 'kwargs', 'func')
       
    85     def test_uihelper_set_field_kwargs(self):
       
    86         afk_get = uicfg.autoform_field_kwargs.get
       
    87         self.assertEqual(afk_get('CWUser', 'firstname', 'String', 'subject'), {})
       
    88         wdg = fwdgs.TextInput({'size': 30})
       
    89         uihelper.set_field_kwargs('CWUser', 'firstname', widget=wdg)
       
    90         self.assertEqual(afk_get('CWUser', 'firstname', 'String', 'subject'), {'widget': wdg})
       
    91 
       
    92     @tag('uihelper', 'hidden', 'func')
       
    93     def test_uihelper_hide_fields(self):
       
    94         # original conf : in_group is edited in 'attributes' section everywhere
       
    95         section_conf = uicfg.autoform_section.get('CWUser', 'in_group', '*', 'subject')
       
    96         self.assertCountEqual(section_conf, ['main_attributes', 'muledit_attributes'])
       
    97         # hide field in main form
       
    98         uihelper.hide_fields('CWUser', ('login', 'in_group'))
       
    99         section_conf = uicfg.autoform_section.get('CWUser', 'in_group', '*', 'subject')
       
   100         self.assertCountEqual(section_conf, ['main_hidden', 'muledit_attributes'])
       
   101         # hide field in muledit form
       
   102         uihelper.hide_fields('CWUser', ('login', 'in_group'), formtype='muledit')
       
   103         section_conf = uicfg.autoform_section.get('CWUser', 'in_group', '*', 'subject')
       
   104         self.assertCountEqual(section_conf, ['main_hidden', 'muledit_hidden'])
       
   105 
       
   106     @tag('uihelper', 'hidden', 'formconfig')
       
   107     def test_uihelper_formconfig(self):
       
   108         afk_get = uicfg.autoform_field_kwargs.get
       
   109         class CWUserFormConfig(uihelper.FormConfig):
       
   110             etype = 'CWUser'
       
   111             hidden = ('in_group',)
       
   112             fields_order = ('login', 'firstname')
       
   113         section_conf = uicfg.autoform_section.get('CWUser', 'in_group', '*', 'subject')
       
   114         self.assertCountEqual(section_conf, ['main_hidden', 'muledit_attributes'])
       
   115         self.assertEqual(afk_get('CWUser', 'firstname', 'String', 'subject'), {'order': 1})
       
   116 
       
   117 
       
   118 class UicfgRegistryTC(CubicWebTC):
       
   119 
       
   120     def test_default_uicfg_object(self):
       
   121         'CW default ui config objects must be registered in uicfg registry'
       
   122         onames = ('autoform_field', 'autoform_section', 'autoform_field_kwargs')
       
   123         for oname in onames:
       
   124             obj = self.vreg['uicfg'].select_or_none(oname)
       
   125             self.assertTrue(obj is not None, '%s not found in uicfg registry'
       
   126                             % oname)
       
   127 
       
   128     def test_custom_uicfg(self):
       
   129         ASRT = uicfg.AutoformSectionRelationTags
       
   130         custom_afs = ASRT()
       
   131         custom_afs.__select__ = ASRT.__select__ & ASRT.__select__
       
   132         self.vreg['uicfg'].register(custom_afs)
       
   133         obj = self.vreg['uicfg'].select_or_none('autoform_section')
       
   134         self.assertTrue(obj is custom_afs)
       
   135 
       
   136 
       
   137 if __name__ == '__main__':
       
   138     from logilab.common.testlib import unittest_main
       
   139     unittest_main()