Skip to content

Commit

Permalink
Add test for Macro
Browse files Browse the repository at this point in the history
  • Loading branch information
1letter committed Apr 29, 2024
1 parent b04b9f3 commit 1131008
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/collective/z3cform/norobots/plone_forms/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
permission="zope2.View"
/>

<browser:page
name="simple-form-with-macro-view"
for="plone.base.interfaces.siteroot.IPloneSiteRoot"
class=".simpleform.SimpleFormView"
permission="zope2.View"
/>

<!-- Norobots validator -->
<adapter factory=".contact_info.NorobotsValidator" />

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
xmlns:metal="http://xml.zope.org/namespaces/metal"
xmlns:tal="http://xml.zope.org/namespaces/tal"
lang="en"
metal:use-macro="context/@@main_template/macros/master"
xml:lang="en"
i18n:domain="plone"
>

<body>
<metal:main fill-slot="content">


<metal:block fill-slot="content-core">

<form enctype="multipart/form-data"
data-pat-autotoc="levels: legend; section: fieldset; className: autotabs"
tal:attributes="
id view/form_instance/id;
action view/form_instance/action;
method view/form_instance/method;
"
>

<div>
<tal:field tal:replace="structure view/form_instance/widgets/fullname/@@ploneform-render-widget" />
</div>

<div metal:use-macro="container/norobots_macro/macros/norobots_macro">
Norobots Macro goes here
</div>

<div class="formControls"
tal:condition="view/form_instance/actions/values|nothing"
>
<tal:block repeat="action view/form_instance/actions/values">
<input type="submit"
tal:replace="structure action/render"
/>
</tal:block>
</div>


<tal:block tal:condition="view/form_instance/enableCSRFProtection|nothing"
tal:replace="structure context/@@authenticator/authenticator"
/>

</form>


</metal:block>



</metal:main>
</body>

</html>
33 changes: 33 additions & 0 deletions src/collective/z3cform/norobots/plone_forms/simpleform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from collective.z3cform.norobots.i18n import norobotsMessageFactory as _
from plone.z3cform.layout import FormWrapper
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from z3c.form import button
from z3c.form import field
from z3c.form import form
from zope import schema
from zope.interface import Interface


class ISimpleForm(Interface):
fullname = schema.TextLine(
title=_("Name"),
description=_("Please enter your full name."),
required=False,
)


class SimpleForm(form.Form):
fields = field.Fields(ISimpleForm)
ignoreContext = True # don't use context to get widget data
id = "simpleform"

@button.buttonAndHandler(_("Send"))
def handle_send(self, action):
data, errors = self.extractData()

Check warning on line 26 in src/collective/z3cform/norobots/plone_forms/simpleform.py

View check run for this annotation

Codecov / codecov/patch

src/collective/z3cform/norobots/plone_forms/simpleform.py#L26

Added line #L26 was not covered by tests


class SimpleFormView(FormWrapper):

form = SimpleForm

index = ViewPageTemplateFile("simpleform-with-macro.pt")
37 changes: 37 additions & 0 deletions src/collective/z3cform/norobots/tests/test_sample_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,40 @@ def test_sample_package(self):
browser.contents,
"the error message is missing!",
)


class TestFunctionalMacroView(unittest.TestCase):

layer = NOROBOTS_FUNCTIONNAL_TESTING

def _manager_browser(self):
transaction.commit()
# Set up browser
browser = Browser(self.layer["app"])
browser.handleErrors = False
browser.addHeader(
"Authorization",
"Basic {}:{}".format(
SITE_OWNER_NAME,
SITE_OWNER_PASSWORD,
),
)
return browser

def setUp(self):
self.portal = self.layer["portal"]
setRoles(self.portal, TEST_USER_ID, ["Manager"])

def test_sample_package(self):
from io import StringIO
from lxml import etree

browser = self._manager_browser()
browser.open(f"{self.portal.absolute_url()}/simple-form-with-macro-view")
tree = etree.parse(StringIO(browser.contents), etree.HTMLParser())

result = tree.xpath("//input[@name='form.widgets.fullname']")
self.assertEqual(1, len(result), "fullname widget is not present")

result = tree.xpath("//input[@name='norobots']")
self.assertEqual(1, len(result), "norobots widget is not present")

0 comments on commit 1131008

Please sign in to comment.