diff -r 6641e941ef1e -r ff1a9aa48cfd app/django/contrib/formtools/tests.py --- a/app/django/contrib/formtools/tests.py Tue Oct 14 12:36:55 2008 +0000 +++ b/app/django/contrib/formtools/tests.py Tue Oct 14 16:00:59 2008 +0000 @@ -1,33 +1,29 @@ -from django import newforms as forms -from django.contrib.formtools import preview +from django import forms +from django.contrib.formtools import preview, wizard from django import http -from django.conf import settings from django.test import TestCase success_string = "Done was called!" -test_data = {'field1': u'foo', - 'field1_': u'asdf'} - class TestFormPreview(preview.FormPreview): def done(self, request, cleaned_data): return http.HttpResponse(success_string) - class TestForm(forms.Form): field1 = forms.CharField() field1_ = forms.CharField() - + bool1 = forms.BooleanField(required=False) class PreviewTests(TestCase): + urls = 'django.contrib.formtools.test_urls' def setUp(self): - settings.ROOT_URLCONF = 'django.contrib.formtools.test_urls' # Create a FormPreview instance to share between tests self.preview = preview.FormPreview(TestForm) input_template = '' self.input = input_template % (self.preview.unused_name('stage'), "%d") + self.test_data = {'field1':u'foo', 'field1_':u'asdf'} def test_unused_name(self): """ @@ -60,8 +56,8 @@ """ # Pass strings for form submittal and add stage variable to # show we previously saw first stage of the form. - test_data.update({'stage': 1}) - response = self.client.post('/test1/', test_data) + self.test_data.update({'stage': 1}) + response = self.client.post('/test1/', self.test_data) # Check to confirm stage is set to 2 in output form. stage = self.input % 2 self.assertContains(response, stage, 1) @@ -78,11 +74,71 @@ """ # Pass strings for form submittal and add stage variable to # show we previously saw first stage of the form. - test_data.update({'stage': 2}) - response = self.client.post('/test1/', test_data) + self.test_data.update({'stage':2}) + response = self.client.post('/test1/', self.test_data) self.failIfEqual(response.content, success_string) - hash = self.preview.security_hash(None, TestForm(test_data)) - test_data.update({'hash': hash}) - response = self.client.post('/test1/', test_data) + hash = self.preview.security_hash(None, TestForm(self.test_data)) + self.test_data.update({'hash': hash}) + response = self.client.post('/test1/', self.test_data) + self.assertEqual(response.content, success_string) + + def test_bool_submit(self): + """ + Test contrib.formtools.preview form submittal when form contains: + BooleanField(required=False) + + Ticket: #6209 - When an unchecked BooleanField is previewed, the preview + form's hash would be computed with no value for ``bool1``. However, when + the preview form is rendered, the unchecked hidden BooleanField would be + rendered with the string value 'False'. So when the preview form is + resubmitted, the hash would be computed with the value 'False' for + ``bool1``. We need to make sure the hashes are the same in both cases. + + """ + self.test_data.update({'stage':2}) + hash = self.preview.security_hash(None, TestForm(self.test_data)) + self.test_data.update({'hash':hash, 'bool1':u'False'}) + response = self.client.post('/test1/', self.test_data) self.assertEqual(response.content, success_string) +# +# FormWizard tests +# + +class WizardPageOneForm(forms.Form): + field = forms.CharField() + +class WizardPageTwoForm(forms.Form): + field = forms.CharField() + +class WizardClass(wizard.FormWizard): + def render_template(self, *args, **kw): + return "" + + def done(self, request, cleaned_data): + return http.HttpResponse(success_string) + +class DummyRequest(object): + def __init__(self, POST=None): + self.method = POST and "POST" or "GET" + self.POST = POST + +class WizardTests(TestCase): + def test_step_starts_at_zero(self): + """ + step should be zero for the first form + """ + wizard = WizardClass([WizardPageOneForm, WizardPageTwoForm]) + request = DummyRequest() + wizard(request) + self.assertEquals(0, wizard.step) + + def test_step_increments(self): + """ + step should be incremented when we go to the next page + """ + wizard = WizardClass([WizardPageOneForm, WizardPageTwoForm]) + request = DummyRequest(POST={"0-field":"test", "wizard_step":"0"}) + response = wizard(request) + self.assertEquals(1, wizard.step) +