Skip to content

Commit

Permalink
Added tests for validators and defaults both in and out of a fieldset.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauritsvanrees committed Aug 5, 2019
1 parent c85584d commit 1022c72
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<model xmlns:easyform="http://namespaces.plone.org/supermodel/easyform" xmlns:form="http://namespaces.plone.org/supermodel/form" xmlns:i18n="http://xml.zope.org/namespaces/i18n" xmlns:indexer="http://namespaces.plone.org/supermodel/indexer" xmlns:lingua="http://namespaces.plone.org/supermodel/lingua" xmlns:marshal="http://namespaces.plone.org/supermodel/marshal" xmlns:security="http://namespaces.plone.org/supermodel/security" xmlns:users="http://namespaces.plone.org/supermodel/users" xmlns="http://namespaces.plone.org/supermodel/schema">
<schema>
<fieldset name="fs1" label="Fieldset 1">
<field name="replyto" type="zope.schema.TextLine" easyform:TDefault="python:'foo@example.org'" easyform:serverSide="False" easyform:validators="isValidEmail">
<description/>
<title>Your E-Mail Address</title>
</field>
</fieldset>
</schema>
</model>
8 changes: 8 additions & 0 deletions src/collective/easyform/tests/fixtures/single_field.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<model xmlns:easyform="http://namespaces.plone.org/supermodel/easyform" xmlns:form="http://namespaces.plone.org/supermodel/form" xmlns:i18n="http://xml.zope.org/namespaces/i18n" xmlns:indexer="http://namespaces.plone.org/supermodel/indexer" xmlns:lingua="http://namespaces.plone.org/supermodel/lingua" xmlns:marshal="http://namespaces.plone.org/supermodel/marshal" xmlns:security="http://namespaces.plone.org/supermodel/security" xmlns:users="http://namespaces.plone.org/supermodel/users" xmlns="http://namespaces.plone.org/supermodel/schema">
<schema>
<field name="replyto" type="zope.schema.TextLine" easyform:TDefault="python:'foo@example.org'" easyform:serverSide="False" easyform:validators="isValidEmail">
<description/>
<title>Your E-Mail Address</title>
</field>
</schema>
</model>
72 changes: 72 additions & 0 deletions src/collective/easyform/tests/testValidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
from collective.easyform import validators
from collective.easyform.api import get_schema
from collective.easyform.api import set_fields
from collective.easyform.browser.view import EasyFormForm
from collective.easyform.browser.view import ValidateFile
from collective.easyform.interfaces import IFieldExtender
from collective.easyform.tests import base
from os.path import dirname
from os.path import join
from plone import api
from plone.namedfile.file import NamedFile
from plone.namedfile.interfaces import INamed
from Products.CMFPlone.RegistrationTool import EmailAddressInvalid
from Products.validation import validation
Expand Down Expand Up @@ -96,6 +101,73 @@ def test_talvalidator2(self):
self.assertEqual(len(errors), 1)


class TestSingleFieldValidator(base.EasyFormTestCase):

""" test validator in form outside of fieldset
The test methods are reused in TestFieldsetValidator.
They use the same field, except that one has it in a fieldset.
"""
schema_fixture = "single_field.xml"

def afterSetUp(self):
self.folder.invokeFactory("EasyForm", "ff1")
self.ff1 = getattr(self.folder, "ff1")
self.ff1.CSRFProtection = False # no csrf protection
self.ff1.showAll = True
field_template = api.content.create(
self.layer["portal"], "File", id="easyform_default_fields.xml"
)
with open(join(dirname(__file__), "fixtures", self.schema_fixture)) as f:
filecontent = NamedFile(f.read(), contentType="application/xml")
field_template.file = filecontent
classImplements(BaseRequest, IFormLayer)
validators.update_validators()

def LoadRequestForm(self, **kwargs):
request = self.layer["request"]
request.form.clear()
prefix = "form.widgets."
for key in kwargs.keys():
request.form[prefix + key] = kwargs[key]
return request

def test_get_default(self):
# With a GET, we should see the default value in the form.
request = self.LoadRequestForm()
request.method = "GET"
form = EasyFormForm(self.ff1, request)()
self.assertNotIn('Required input is missing.', form)
self.assertIn('value="foo@example.org"', form)

def test_required(self):
data = {"replyto": ""}
request = self.LoadRequestForm(**data)
request.method = "POST"
form = EasyFormForm(self.ff1, request)()
self.assertIn('Required input is missing.', form)
self.assertNotIn('Invalid email address.', form)

def test_validator_in_fieldset(self):
data = {
"replyto": "bad email address",
}
request = self.LoadRequestForm(**data)
request.method = "POST"
form = EasyFormForm(self.ff1, request)()
self.assertNotIn('Required input is missing.', form)
self.assertIn('Invalid email address.', form)


class TestFieldsetValidator(TestSingleFieldValidator):

""" test validator in fieldset
This reuses the test methods from TestSingleFieldValidator.
"""
schema_fixture = "fieldset_with_single_field.xml"


class TestCustomValidators(base.EasyFormTestCase):

""" test our validators """
Expand Down

0 comments on commit 1022c72

Please sign in to comment.