|
13 | 13 | from roundup.cgi import client, actions, exceptions |
14 | 14 | from roundup.cgi.exceptions import FormError |
15 | 15 | from roundup.cgi.templating import HTMLItem, HTMLRequest, NoTemplate |
| 16 | +from roundup.cgi.templating import HTMLProperty, _HTMLItem |
16 | 17 | from roundup.cgi.form_parser import FormParser |
17 | 18 | from roundup import init, instance, password, hyperdb, date |
18 | 19 |
|
| 20 | +# For testing very simple rendering |
| 21 | +from roundup.cgi.engine_zopetal import RoundupPageTemplate |
| 22 | + |
19 | 23 | from mocknull import MockNull |
20 | 24 |
|
21 | 25 | import db_test_base |
@@ -117,13 +121,20 @@ def setUp(self): |
117 | 121 | self.FV_SPECIAL = re.compile(FormParser.FV_LABELS%classes, |
118 | 122 | re.VERBOSE) |
119 | 123 |
|
120 | | - def parseForm(self, form, classname='test', nodeid=None): |
| 124 | + def setupClient(self, form, classname, nodeid=None, template='item'): |
121 | 125 | cl = client.Client(self.instance, None, {'PATH_INFO':'/', |
122 | 126 | 'REQUEST_METHOD':'POST'}, makeForm(form)) |
123 | 127 | cl.classname = classname |
124 | 128 | cl.nodeid = nodeid |
125 | 129 | cl.language = ('en',) |
| 130 | + cl.userid = '1' |
126 | 131 | cl.db = self.db |
| 132 | + cl.user = 'admin' |
| 133 | + cl.template = template |
| 134 | + return cl |
| 135 | + |
| 136 | + def parseForm(self, form, classname='test', nodeid=None): |
| 137 | + cl = self.setupClient(form, classname, nodeid) |
127 | 138 | return cl.parsePropsFromForm(create=1) |
128 | 139 |
|
129 | 140 | def tearDown(self): |
@@ -772,6 +783,76 @@ def testBackwardsCompat(self): |
772 | 783 | 'name': 'foo.txt', 'type': 'text/plain'}}, |
773 | 784 | [('issue', None, 'files', [('file', '-1')])])) |
774 | 785 |
|
| 786 | + def testFormValuePreserveOnError(self): |
| 787 | + page_template = """ |
| 788 | + <html> |
| 789 | + <body> |
| 790 | + <p tal:condition="options/error_message|nothing" |
| 791 | + tal:repeat="m options/error_message" |
| 792 | + tal:content="structure m"/> |
| 793 | + <p tal:content="context/title/plain"/> |
| 794 | + <p tal:content="context/priority/plain"/> |
| 795 | + <p tal:content="context/status/plain"/> |
| 796 | + <p tal:content="context/nosy/plain"/> |
| 797 | + <p tal:content="context/keyword/plain"/> |
| 798 | + <p tal:content="structure context/superseder/field"/> |
| 799 | + </body> |
| 800 | + </html> |
| 801 | + """.strip () |
| 802 | + self.db.keyword.create (name = 'key1') |
| 803 | + self.db.keyword.create (name = 'key2') |
| 804 | + nodeid = self.db.issue.create (title = 'Title', priority = '1', |
| 805 | + status = '1', nosy = ['1'], keyword = ['1']) |
| 806 | + self.db.commit () |
| 807 | + form = {':note': 'msg-content', 'title': 'New title', |
| 808 | + 'priority': '2', 'status': '2', 'nosy': '1,2', 'keyword': '', |
| 809 | + 'superseder': '5000', ':action': 'edit'} |
| 810 | + cl = self.setupClient(form, 'issue', '1') |
| 811 | + pt = RoundupPageTemplate() |
| 812 | + pt.pt_edit(page_template, 'text/html') |
| 813 | + out = [] |
| 814 | + def wh(s): |
| 815 | + out.append(s) |
| 816 | + cl.write_html = wh |
| 817 | + # Enable the following if we get a templating error: |
| 818 | + #def send_error (*args, **kw): |
| 819 | + # import pdb; pdb.set_trace() |
| 820 | + #cl.send_error_to_admin = send_error |
| 821 | + # Need to rollback the database on error -- this usually happens |
| 822 | + # in web-interface (and for other databases) anyway, need it for |
| 823 | + # testing that the form values are really used, not the database! |
| 824 | + # We do this together with the setup of the easy template above |
| 825 | + def load_template(x): |
| 826 | + cl.db.rollback() |
| 827 | + return pt |
| 828 | + cl.instance.templates.load = load_template |
| 829 | + cl.selectTemplate = MockNull() |
| 830 | + cl.determine_context = MockNull () |
| 831 | + def hasPermission(s, p, classname=None, d=None, e=None, **kw): |
| 832 | + return True |
| 833 | + actions.Action.hasPermission = hasPermission |
| 834 | + e1 = _HTMLItem.is_edit_ok |
| 835 | + _HTMLItem.is_edit_ok = lambda x : True |
| 836 | + e2 = HTMLProperty.is_edit_ok |
| 837 | + HTMLProperty.is_edit_ok = lambda x : True |
| 838 | + cl.inner_main() |
| 839 | + _HTMLItem.is_edit_ok = e1 |
| 840 | + HTMLProperty.is_edit_ok = e2 |
| 841 | + self.assertEqual(len(out), 1) |
| 842 | + self.assertEqual(out [0].strip (), """ |
| 843 | + <html> |
| 844 | + <body> |
| 845 | + <p>Edit Error: issue has no node 5000</p> |
| 846 | + <p>New title</p> |
| 847 | + <p>urgent</p> |
| 848 | + <p>deferred</p> |
| 849 | + <p>admin, anonymous</p> |
| 850 | + <p></p> |
| 851 | + <p><input type="text" name="superseder" value="5000" size="30"></p> |
| 852 | + </body> |
| 853 | + </html> |
| 854 | + """.strip ()) |
| 855 | + |
775 | 856 | # |
776 | 857 | # SECURITY |
777 | 858 | # |
|
0 commit comments