diff --git a/tests/forms_tests/field_tests/test_choicefield.py b/tests/forms_tests/field_tests/test_choicefield.py index 69b4d9e342..4c70177dac 100644 --- a/tests/forms_tests/field_tests/test_choicefield.py +++ b/tests/forms_tests/field_tests/test_choicefield.py @@ -1,7 +1,8 @@ +# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.forms import ChoiceField, Form, ValidationError -from django.test import SimpleTestCase +from django.test import SimpleTestCase, ignore_warnings from . import FormFieldAssertionsMixin @@ -84,3 +85,16 @@ class ChoiceFieldTest(FormFieldAssertionsMixin, SimpleTestCase): '' ) + + @ignore_warnings(category=UnicodeWarning) + def test_utf8_bytesrings(self): + # Choice validation with UTF-8 bytestrings as input (these are the + # Russian abbreviations "мес." and "шт.". + f = ChoiceField( + choices=( + (b'\xd0\xbc\xd0\xb5\xd1\x81.', b'\xd0\xbc\xd0\xb5\xd1\x81.'), + (b'\xd1\x88\xd1\x82.', b'\xd1\x88\xd1\x82.'), + ), + ) + self.assertEqual(f.clean('\u0448\u0442.'), '\u0448\u0442.') + self.assertEqual(f.clean(b'\xd1\x88\xd1\x82.'), '\u0448\u0442.') diff --git a/tests/forms_tests/models.py b/tests/forms_tests/models.py index 511a3d0d44..cf770abb02 100644 --- a/tests/forms_tests/models.py +++ b/tests/forms_tests/models.py @@ -143,9 +143,5 @@ class Group(models.Model): return '%s' % self.name -class Cheese(models.Model): - name = models.CharField(max_length=100) - - class Article(models.Model): content = models.TextField() diff --git a/tests/forms_tests/tests/test_error_messages.py b/tests/forms_tests/tests/test_error_messages.py index 315e72604d..4ae550efc2 100644 --- a/tests/forms_tests/tests/test_error_messages.py +++ b/tests/forms_tests/tests/test_error_messages.py @@ -9,10 +9,13 @@ from django.forms import ( ModelMultipleChoiceField, MultipleChoiceField, RegexField, SplitDateTimeField, TimeField, URLField, ValidationError, utils, ) +from django.template import Context, Template from django.test import SimpleTestCase, TestCase from django.utils.encoding import python_2_unicode_compatible from django.utils.safestring import mark_safe +from ..models import ChoiceModel + class AssertFormErrorsMixin(object): def assertFormErrors(self, expected, the_callable, *args, **kwargs): @@ -242,11 +245,50 @@ class FormsErrorMessagesTestCase(SimpleTestCase, AssertFormErrorsMixin): self.assertHTMLEqual(str(form2['last_name'].errors), '

This field is required.

') self.assertHTMLEqual(str(form2.errors['__all__']), '

I like to be awkward.

') + def test_error_messages_escaping(self): + # The forms layer doesn't escape input values directly because error + # messages might be presented in non-HTML contexts. Instead, the + # message is marked for escaping by the template engine, so a template + # is needed to trigger the escaping. + t = Template('{{ form.errors }}') + + class SomeForm(Form): + field = ChoiceField(choices=[('one', 'One')]) + + f = SomeForm({'field': '