cubicweb/web/test/unittest_form.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 07 Mar 2017 12:08:39 +0100
changeset 12006 c2230b7f6358
parent 12005 dcf80f5b7b63
child 12567 26744ad37953
permissions -rw-r--r--
[web] Don't raise ProcessFormError if there is no value specified a03376213747 introduced a slight modification in None / empty string values and with it a regression where an exception is raised on empty values, while we should simply consider no value is specified and return None.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11240
diff changeset
     1
# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4678
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
    18
10016
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
    19
import time
11240
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
    20
from datetime import datetime
2145
529867c301fc add datetime form test
Florent <florent@secondweb.fr>
parents: 2005
diff changeset
    21
10786
2ef74a6e6785 [web] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents: 10782
diff changeset
    22
from six import text_type
2ef74a6e6785 [web] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents: 10782
diff changeset
    23
12005
dcf80f5b7b63 [test] Cleanup imports in unittest_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11929
diff changeset
    24
import pytz
dcf80f5b7b63 [test] Cleanup imports in unittest_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11929
diff changeset
    25
dcf80f5b7b63 [test] Cleanup imports in unittest_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11929
diff changeset
    26
from lxml import html
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
    27
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7161
diff changeset
    28
from cubicweb import Binary, ValidationError
11013
645fcb5bcc39 [web] Make tests pass if tal isn't available
Rémi Cardona <remi.cardona@free.fr>
parents: 10786
diff changeset
    29
from cubicweb.mttransforms import HAS_TAL
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    30
from cubicweb.devtools.testlib import CubicWebTC
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    31
from cubicweb.web.formfields import (IntField, StringField, RichTextField,
4390
0285c1fa1459 test fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    32
                                     PasswordField, DateTimeField,
11240
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
    33
                                     FileField, EditableFileField,
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
    34
                                     TZDatetimeField)
12006
c2230b7f6358 [web] Don't raise ProcessFormError if there is no value specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12005
diff changeset
    35
from cubicweb.web.formwidgets import DateTimePicker, JQueryDateTimePicker
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
    36
from cubicweb.web.views.forms import EntityFieldsForm, FieldsForm
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    37
from cubicweb.web.views.workflow import ChangeStateForm
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1983
diff changeset
    38
from cubicweb.web.views.formrenderers import FormRenderer
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    39
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
    40
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    41
class FieldsFormTC(CubicWebTC):
1359
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    42
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    43
    def test_form_field_format(self):
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    44
        with self.admin_access.web_request() as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    45
            form = FieldsForm(req, None)
10273
ef74abcf369d [config] disable fckeditor by default (closes #1368900)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10016
diff changeset
    46
            self.assertEqual(StringField().format(form), 'text/plain')
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    47
            req.cnx.execute('INSERT CWProperty X: X pkey "ui.default-text-format", X value "text/rest", X for_user U WHERE U login "admin"')
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    48
            req.cnx.commit()
11699
b48020a80dc3 Store user groups and properties as session data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
    49
        with self.admin_access.web_request() as req:
b48020a80dc3 Store user groups and properties as session data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
    50
            form = FieldsForm(req, None)
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    51
            self.assertEqual(StringField().format(form), 'text/rest')
1359
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    52
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    53
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7161
diff changeset
    54
    def test_process_posted(self):
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7161
diff changeset
    55
        class AForm(FieldsForm):
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7161
diff changeset
    56
            anint = IntField()
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7161
diff changeset
    57
            astring = StringField()
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    58
        with self.admin_access.web_request(anint='1', astring='2', _cw_fields='anint,astring') as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    59
            form = AForm(req)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    60
            self.assertEqual(form.process_posted(), {'anint': 1, 'astring': '2'})
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    61
        with self.admin_access.web_request(anint='1a', astring='2b', _cw_fields='anint,astring') as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    62
            form = AForm(req)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    63
            self.assertRaises(ValidationError, form.process_posted)
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7161
diff changeset
    64
12006
c2230b7f6358 [web] Don't raise ProcessFormError if there is no value specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12005
diff changeset
    65
    def test_jqdt_process_data(self):
c2230b7f6358 [web] Don't raise ProcessFormError if there is no value specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12005
diff changeset
    66
        widget = JQueryDateTimePicker()
c2230b7f6358 [web] Don't raise ProcessFormError if there is no value specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12005
diff changeset
    67
        field = DateTimeField('jqdt')
c2230b7f6358 [web] Don't raise ProcessFormError if there is no value specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12005
diff changeset
    68
        with self.admin_access.web_request(**{'jqdt-date': '', 'jqdt-time': '00:00'}) as req:
c2230b7f6358 [web] Don't raise ProcessFormError if there is no value specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12005
diff changeset
    69
            self._cw = req
c2230b7f6358 [web] Don't raise ProcessFormError if there is no value specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12005
diff changeset
    70
            self.formvalues = {}
c2230b7f6358 [web] Don't raise ProcessFormError if there is no value specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12005
diff changeset
    71
            date = widget.process_field_data(self, field)
c2230b7f6358 [web] Don't raise ProcessFormError if there is no value specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12005
diff changeset
    72
            self.assertIsNone(date)
c2230b7f6358 [web] Don't raise ProcessFormError if there is no value specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12005
diff changeset
    73
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7161
diff changeset
    74
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    75
class EntityFieldsFormTC(CubicWebTC):
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    76
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    77
    def test_form_field_choices(self):
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    78
        with self.admin_access.web_request() as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    79
            b = req.create_entity('BlogEntry', title=u'di mascii code', content=u'a best-seller')
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    80
            t = req.create_entity('Tag', name=u'x')
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    81
            form1 = self.vreg['forms'].select('edition', req, entity=t)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    82
            choices = [reid for rview, reid in form1.field_by_name('tags', 'subject', t.e_schema).choices(form1)]
10786
2ef74a6e6785 [web] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents: 10782
diff changeset
    83
            self.assertIn(text_type(b.eid), choices)
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    84
            form2 = self.vreg['forms'].select('edition', req, entity=b)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    85
            choices = [reid for rview, reid in form2.field_by_name('tags', 'object', t.e_schema).choices(form2)]
10786
2ef74a6e6785 [web] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents: 10782
diff changeset
    86
            self.assertIn(text_type(t.eid), choices)
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    87
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    88
            b.cw_clear_all_caches()
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    89
            t.cw_clear_all_caches()
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    90
            req.cnx.execute('SET X tags Y WHERE X is Tag, Y is BlogEntry')
8734
3530b7494195 [test/web] fix invisibly bogus test (prepares #2755994)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    91
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    92
            choices = [reid for rview, reid in form1.field_by_name('tags', 'subject', t.e_schema).choices(form1)]
10786
2ef74a6e6785 [web] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents: 10782
diff changeset
    93
            self.assertIn(text_type(b.eid), choices)
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    94
            choices = [reid for rview, reid in form2.field_by_name('tags', 'object', t.e_schema).choices(form2)]
10786
2ef74a6e6785 [web] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents: 10782
diff changeset
    95
            self.assertIn(text_type(t.eid), choices)
3176
b94703b131ad hopefully correctly fix the breadcrumbs escaping
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3002
diff changeset
    96
8734
3530b7494195 [test/web] fix invisibly bogus test (prepares #2755994)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    97
    def test_form_field_choices_new_entity(self):
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    98
        with self.admin_access.web_request() as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
    99
            e = self.vreg['etypes'].etype_class('CWUser')(req)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   100
            form = self.vreg['forms'].select('edition', req, entity=e)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   101
            unrelated = [rview for rview, reid in form.field_by_name('in_group', 'subject').choices(form)]
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   102
            # should be default groups but owners, i.e. managers, users, guests
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   103
            self.assertEqual(unrelated, [u'guests', u'managers', u'users'])
1345
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
   104
1843
646c2dd1f03e #343290: inputs don't use value specified in URL
sylvain.thenault@logilab.fr
parents: 1793
diff changeset
   105
    def test_consider_req_form_params(self):
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   106
        with self.admin_access.web_request() as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   107
            e = self.vreg['etypes'].etype_class('CWUser')(req)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   108
            e.eid = 'A'
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   109
            with self.admin_access.web_request(login=u'toto') as toto_req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   110
                form = EntityFieldsForm(toto_req, None, entity=e)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   111
                field = StringField(name='login', role='subject', eidparam=True)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   112
                form.append_field(field)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   113
                form.build_context({})
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   114
                self.assertEqual(field.widget.values(form, field), (u'toto',))
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   115
9377
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8849
diff changeset
   116
    def test_linkto_field_duplication_inout(self):
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   117
        with self.admin_access.web_request() as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   118
            e = self.vreg['etypes'].etype_class('CWUser')(req)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   119
            e.eid = 'A'
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   120
            e._cw = req
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   121
            geid = req.cnx.execute('CWGroup X WHERE X name "users"')[0][0]
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   122
            req.form['__linkto'] = 'in_group:%s:subject' % geid
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   123
            form = self.vreg['forms'].select('edition', req, entity=e)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   124
            form.content_type = 'text/html'
10006
8391bf718485 remove most 3.10 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9990
diff changeset
   125
            data = []
8391bf718485 remove most 3.10 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9990
diff changeset
   126
            form.render(w=data.append)
8391bf718485 remove most 3.10 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9990
diff changeset
   127
            pageinfo = self._check_html(u'\n'.join(data), form, template=None)
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   128
            inputs = pageinfo.find_tag('select', False)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   129
            ok = False
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   130
            for selectnode in pageinfo.matching_nodes('select', name='from_in_group-subject:A'):
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   131
                for optionnode in selectnode:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   132
                    self.assertEqual(optionnode.get('value'), str(geid))
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   133
                    self.assertEqual(ok, False)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   134
                    ok = True
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   135
            inputs = pageinfo.find_tag('input', False)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   136
            self.assertFalse(list(pageinfo.matching_nodes('input', name='__linkto')))
1947
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
   137
2198
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2174
diff changeset
   138
    def test_reledit_composite_field(self):
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   139
        with self.admin_access.web_request() as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   140
            rset = req.execute('INSERT BlogEntry X: X title "cubicweb.org", X content "hop"')
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   141
            form = self.vreg['views'].select('reledit', req,
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   142
                                             rset=rset, row=0, rtype='content')
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   143
            data = form.render(row=0, rtype='content', formid='base', action='edit_rtype')
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   144
            self.assertIn('content_format', data)
2198
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2174
diff changeset
   145
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   146
10016
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   147
    def test_form_generation_time(self):
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   148
        with self.admin_access.web_request() as req:
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   149
            e = req.create_entity('BlogEntry', title=u'cubicweb.org', content=u"hop")
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   150
            expected_field_name = '__form_generation_time:%d' % e.eid
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   151
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   152
            ts_before = time.time()
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   153
            form = self.vreg['forms'].select('edition', req, entity=e)
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   154
            ts_after = time.time()
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   155
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   156
            data = []
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   157
            form.render(action='edit', w=data.append)
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   158
            html_form = html.fromstring(''.join(data)).forms[0]
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   159
            fields = dict(html_form.form_values())
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   160
            self.assertIn(expected_field_name, fields)
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   161
            ts = float(fields[expected_field_name])
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   162
            self.assertTrue(ts_before < ts  < ts_after)
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   163
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 10012
diff changeset
   164
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   165
    # form tests ##############################################################
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   166
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
   167
    def test_form_inheritance(self):
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   168
        with self.admin_access.web_request() as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   169
            class CustomChangeStateForm(ChangeStateForm):
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   170
                hello = IntField(name='youlou')
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   171
                creation_date = DateTimeField(widget=DateTimePicker)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   172
            form = CustomChangeStateForm(req, redirect_path='perdu.com',
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   173
                                         entity=req.user)
10006
8391bf718485 remove most 3.10 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9990
diff changeset
   174
            data = []
8391bf718485 remove most 3.10 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9990
diff changeset
   175
            form.render(w=data.append,
8391bf718485 remove most 3.10 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9990
diff changeset
   176
                        formvalues=dict(state=123, trcomment=u'',
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   177
                                        trcomment_format=u'text/plain'))
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
   178
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
   179
    def test_change_state_form(self):
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   180
        with self.admin_access.web_request() as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   181
            form = ChangeStateForm(req, redirect_path='perdu.com',
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   182
                                   entity=req.user)
10006
8391bf718485 remove most 3.10 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9990
diff changeset
   183
            data = []
8391bf718485 remove most 3.10 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9990
diff changeset
   184
            form.render(w=data.append,
8391bf718485 remove most 3.10 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9990
diff changeset
   185
                        formvalues=dict(state=123, trcomment=u'',
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   186
                                        trcomment_format=u'text/plain'))
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   187
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   188
    # fields tests ############################################################
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   189
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   190
    def _render_entity_field(self, req, name, form):
3510
bf746bf4a394 rename form_build_context to build_context, and call it from form, not renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3176
diff changeset
   191
        form.build_context({})
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   192
        renderer = FormRenderer(req)
4172
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   193
        return form.field_by_name(name, 'subject').render(form, renderer)
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   194
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   195
    def _test_richtextfield(self, req, expected):
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   196
        class RTFForm(EntityFieldsForm):
4172
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   197
            description = RichTextField(eidparam=True, role='subject')
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   198
        state = self.vreg['etypes'].etype_class('State')(req)
4390
0285c1fa1459 test fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   199
        state.eid = 'S'
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   200
        form = RTFForm(req, redirect_path='perdu.com', entity=state)
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   201
        # make it think it can use fck editor anyway
6637
6bbfc628555b [3.10] more form api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   202
        form.field_by_name('description', 'subject').format = lambda form, field=None: 'text/html'
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   203
        self.assertMultiLineEqual(self._render_entity_field(req, 'description', form),
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   204
                              expected % {'eid': state.eid})
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   205
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   206
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   207
    def test_richtextfield_1(self):
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   208
        with self.admin_access.web_request() as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   209
            req.use_fckeditor = lambda: False
11875
011730a4af73 [web] remove next_tabindex()
Julien Cristau <julien.cristau@logilab.fr>
parents: 11699
diff changeset
   210
            self._test_richtextfield(req, '''<select id="description_format-subject:%(eid)s" name="description_format-subject:%(eid)s" size="1" style="display: block">
11013
645fcb5bcc39 [web] Make tests pass if tal isn't available
Rémi Cardona <remi.cardona@free.fr>
parents: 10786
diff changeset
   211
''' + ('<option value="text/cubicweb-page-template">text/cubicweb-page-template</option>\n'
645fcb5bcc39 [web] Make tests pass if tal isn't available
Rémi Cardona <remi.cardona@free.fr>
parents: 10786
diff changeset
   212
if HAS_TAL else '') +
645fcb5bcc39 [web] Make tests pass if tal isn't available
Rémi Cardona <remi.cardona@free.fr>
parents: 10786
diff changeset
   213
'''<option selected="selected" value="text/html">text/html</option>
10012
8c2c6fdd8d56 [RichString] Add markdown support
Christophe de Vienne <christophe@unlish.com>
parents: 10006
diff changeset
   214
<option value="text/markdown">text/markdown</option>
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   215
<option value="text/plain">text/plain</option>
4172
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   216
<option value="text/rest">text/rest</option>
11875
011730a4af73 [web] remove next_tabindex()
Julien Cristau <julien.cristau@logilab.fr>
parents: 11699
diff changeset
   217
</select><textarea cols="80" id="description-subject:%(eid)s" name="description-subject:%(eid)s" onkeyup="autogrow(this)" rows="2"></textarea>''')
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   218
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   219
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   220
    def test_richtextfield_2(self):
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   221
        with self.admin_access.web_request() as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   222
            req.use_fckeditor = lambda: True
11875
011730a4af73 [web] remove next_tabindex()
Julien Cristau <julien.cristau@logilab.fr>
parents: 11699
diff changeset
   223
            self._test_richtextfield(req, '<input name="description_format-subject:%(eid)s" type="hidden" value="text/html" /><textarea cols="80" cubicweb:type="wysiwyg" id="description-subject:%(eid)s" name="description-subject:%(eid)s" onkeyup="autogrow(this)" rows="2"></textarea>')
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   224
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   225
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   226
    def test_filefield(self):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   227
        class FFForm(EntityFieldsForm):
4172
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   228
            data = FileField(
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   229
                format_field=StringField(name='data_format', max_length=50,
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   230
                                         eidparam=True, role='subject'),
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   231
                encoding_field=StringField(name='data_encoding', max_length=20,
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   232
                                           eidparam=True, role='subject'),
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   233
                eidparam=True, role='subject')
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   234
        with self.admin_access.web_request() as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   235
            file = req.create_entity('File', data_name=u"pouet.txt", data_encoding=u'UTF-8',
10782
65bb683d2a51 [web/test] Binary eats bytes
Julien Cristau <julien.cristau@logilab.fr>
parents: 10572
diff changeset
   236
                                     data=Binary(b'new widgets system'))
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   237
            form = FFForm(req, redirect_path='perdu.com', entity=file)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   238
            self.assertMultiLineEqual(self._render_entity_field(req, 'data', form),
11875
011730a4af73 [web] remove next_tabindex()
Julien Cristau <julien.cristau@logilab.fr>
parents: 11699
diff changeset
   239
                              '''<input id="data-subject:%(eid)s" name="data-subject:%(eid)s" type="file" value="" />
4172
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   240
<a href="javascript: toggleVisibility(&#39;data-subject:%(eid)s-advanced&#39;)" title="show advanced fields"><img src="http://testing.fr/cubicweb/data/puce_down.png" alt="show advanced fields"/></a>
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   241
<div id="data-subject:%(eid)s-advanced" class="hidden">
11875
011730a4af73 [web] remove next_tabindex()
Julien Cristau <julien.cristau@logilab.fr>
parents: 11699
diff changeset
   242
<label for="data_format-subject:%(eid)s">data_format</label><input id="data_format-subject:%(eid)s" maxlength="50" name="data_format-subject:%(eid)s" size="45" type="text" value="text/plain" /><br/>
011730a4af73 [web] remove next_tabindex()
Julien Cristau <julien.cristau@logilab.fr>
parents: 11699
diff changeset
   243
<label for="data_encoding-subject:%(eid)s">data_encoding</label><input id="data_encoding-subject:%(eid)s" maxlength="20" name="data_encoding-subject:%(eid)s" size="20" type="text" value="UTF-8" /><br/>
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   244
</div>
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   245
<br/>
4172
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   246
<input name="data-subject__detach:%(eid)s" type="checkbox" />
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5914
diff changeset
   247
detach attached file''' % {'eid': file.eid})
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   248
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   249
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   250
    def test_editablefilefield(self):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   251
        class EFFForm(EntityFieldsForm):
4172
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   252
            data = EditableFileField(
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   253
                format_field=StringField('data_format', max_length=50,
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   254
                                         eidparam=True, role='subject'),
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   255
                encoding_field=StringField('data_encoding', max_length=20,
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   256
                                           eidparam=True, role='subject'),
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   257
                eidparam=True, role='subject')
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   258
        with self.admin_access.web_request() as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   259
            file = req.create_entity('File', data_name=u"pouet.txt", data_encoding=u'UTF-8',
10782
65bb683d2a51 [web/test] Binary eats bytes
Julien Cristau <julien.cristau@logilab.fr>
parents: 10572
diff changeset
   260
                                     data=Binary(b'new widgets system'))
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   261
            form = EFFForm(req, redirect_path='perdu.com', entity=file)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   262
            self.assertMultiLineEqual(self._render_entity_field(req, 'data', form),
11875
011730a4af73 [web] remove next_tabindex()
Julien Cristau <julien.cristau@logilab.fr>
parents: 11699
diff changeset
   263
                              '''<input id="data-subject:%(eid)s" name="data-subject:%(eid)s" type="file" value="" />
4172
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   264
<a href="javascript: toggleVisibility(&#39;data-subject:%(eid)s-advanced&#39;)" title="show advanced fields"><img src="http://testing.fr/cubicweb/data/puce_down.png" alt="show advanced fields"/></a>
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   265
<div id="data-subject:%(eid)s-advanced" class="hidden">
11875
011730a4af73 [web] remove next_tabindex()
Julien Cristau <julien.cristau@logilab.fr>
parents: 11699
diff changeset
   266
<label for="data_format-subject:%(eid)s">data_format</label><input id="data_format-subject:%(eid)s" maxlength="50" name="data_format-subject:%(eid)s" size="45" type="text" value="text/plain" /><br/>
011730a4af73 [web] remove next_tabindex()
Julien Cristau <julien.cristau@logilab.fr>
parents: 11699
diff changeset
   267
<label for="data_encoding-subject:%(eid)s">data_encoding</label><input id="data_encoding-subject:%(eid)s" maxlength="20" name="data_encoding-subject:%(eid)s" size="20" type="text" value="UTF-8" /><br/>
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   268
</div>
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   269
<br/>
4172
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   270
<input name="data-subject__detach:%(eid)s" type="checkbox" />
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   271
detach attached file
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   272
<p><b>You can either submit a new file using the browse button above, or choose to remove already uploaded file by checking the "detach attached file" check-box, or edit file content online with the widget below.</b></p>
11875
011730a4af73 [web] remove next_tabindex()
Julien Cristau <julien.cristau@logilab.fr>
parents: 11699
diff changeset
   273
<textarea cols="80" name="data-subject:%(eid)s" onkeyup="autogrow(this)" rows="3">new widgets system</textarea>''' % {'eid': file.eid})
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   274
11922
5f886dc718ab [web] Handle unspecified value in TZDatetimeField
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11699
diff changeset
   275
    def _modified_tzdatenaiss(self, eid, date_and_time_str=None):
5f886dc718ab [web] Handle unspecified value in TZDatetimeField
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11699
diff changeset
   276
        ctx = {}
5f886dc718ab [web] Handle unspecified value in TZDatetimeField
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11699
diff changeset
   277
        if date_and_time_str:
5f886dc718ab [web] Handle unspecified value in TZDatetimeField
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11699
diff changeset
   278
            datestr, timestr = date_and_time_str
5f886dc718ab [web] Handle unspecified value in TZDatetimeField
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11699
diff changeset
   279
            ctx['tzdatenaiss-subjectdate:%d' % eid] = datestr
5f886dc718ab [web] Handle unspecified value in TZDatetimeField
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11699
diff changeset
   280
            ctx['tzdatenaiss-subjecttime:%d' % eid] = timestr
11240
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   281
        with self.admin_access.web_request(**ctx) as req:
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   282
            form = EntityFieldsForm(req, None, entity=req.entity_from_eid(eid))
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   283
            field = TZDatetimeField(name='tzdatenaiss', eidparam=True,
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   284
                                    role='subject')
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   285
            form.append_field(field)
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   286
            form.build_context({})
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   287
            return field.has_been_modified(form)
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   288
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   289
    def test_tzdatetimefield(self):
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   290
        """ Comparison of the tz-aware database-stored value and the posted data
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   291
        should not crash, and the posted data should be considered UTC """
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   292
        tzd = datetime.now(pytz.utc).replace(second=0, microsecond=0)
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   293
        datestr, timestr = tzd.strftime('%Y/%m/%d %H:%M').split()
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   294
        with self.admin_access.web_request() as req:
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   295
            eid = req.create_entity('Personne', nom=u'Flo', tzdatenaiss=tzd).eid
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   296
            req.cnx.commit()
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   297
11922
5f886dc718ab [web] Handle unspecified value in TZDatetimeField
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11699
diff changeset
   298
        modified = self._modified_tzdatenaiss(eid, (datestr, timestr))
11240
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   299
        self.assertFalse(modified)
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   300
11922
5f886dc718ab [web] Handle unspecified value in TZDatetimeField
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11699
diff changeset
   301
        modified = self._modified_tzdatenaiss(eid, ('2016/05/04', '15:07'))
5f886dc718ab [web] Handle unspecified value in TZDatetimeField
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11699
diff changeset
   302
        self.assertTrue(modified)
5f886dc718ab [web] Handle unspecified value in TZDatetimeField
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11699
diff changeset
   303
5f886dc718ab [web] Handle unspecified value in TZDatetimeField
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11699
diff changeset
   304
        modified = self._modified_tzdatenaiss(eid, None)
11240
1694e6e9ff94 [forms] Fix edition of TZDatetime attributes
Florent Cayré <florent.cayre@logilab.fr>
parents: 11057
diff changeset
   305
        self.assertTrue(modified)
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   306
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   307
    def test_passwordfield(self):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   308
        class PFForm(EntityFieldsForm):
4172
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   309
            upassword = PasswordField(eidparam=True, role='subject')
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   310
        with self.admin_access.web_request() as req:
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   311
            form = PFForm(req, redirect_path='perdu.com', entity=req.user)
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   312
            self.assertMultiLineEqual(self._render_entity_field(req, 'upassword', form),
11875
011730a4af73 [web] remove next_tabindex()
Julien Cristau <julien.cristau@logilab.fr>
parents: 11699
diff changeset
   313
                                  '''<input id="upassword-subject:%(eid)s" name="upassword-subject:%(eid)s" type="password" value="" />
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   314
<br/>
11875
011730a4af73 [web] remove next_tabindex()
Julien Cristau <julien.cristau@logilab.fr>
parents: 11699
diff changeset
   315
<input name="upassword-subject-confirm:%(eid)s" type="password" value="" />
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2755
diff changeset
   316
&#160;
9646
0e245d071c19 [web/test] port unittest_form to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9377
diff changeset
   317
<span class="emphasis">confirm password</span>''' % {'eid': req.user.eid})
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   318
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   319
4172
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   320
    # def test_datefield(self):
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   321
    #     class DFForm(EntityFieldsForm):
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   322
    #         creation_date = DateTimeField(widget=Input)
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   323
    #     form = DFForm(self.req, entity=self.entity)
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   324
    #     init, cur = (fromstring(self._render_entity_field(attr, form)).get('value')
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   325
    #                  for attr in ('edits-creation_date', 'creation_date'))
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5914
diff changeset
   326
    #     self.assertEqual(init, cur)
2145
529867c301fc add datetime form test
Florent <florent@secondweb.fr>
parents: 2005
diff changeset
   327
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
   328
if __name__ == '__main__':
12005
dcf80f5b7b63 [test] Cleanup imports in unittest_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11929
diff changeset
   329
    import unittest
dcf80f5b7b63 [test] Cleanup imports in unittest_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11929
diff changeset
   330
    unittest.main()