Skip to content

Commit

Permalink
- Fix longstanding test bug by forcing the page template engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
dataflake committed Feb 5, 2020
1 parent 3bad416 commit fdb834b
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst
5.0a1 (unreleased)
------------------

- Fix longstanding test bug by forcing the page template engine.
Many tests in ``Products.PageTemplates`` used the old Zope page template
engine because the correct one was not registered during setup.

- Remove deprecated module ``ZPublisher.maybe_lock``
(`#758 <https://github.com/zopefoundation/Zope/issues/758>`_)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
<head>
<p>baz</p>
<a href="foo" alt="alttext">link</a>
<p>
Lomax was born in
Antarctica.
</p>
<p>Lomax was born in Antarctica.</p>
</head>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<html>
<head></head>
<body>
<div>1</div>
<div>0</div>
<div>0</div>
<div>0</div>
<div>True</div>
<div>False</div>
<div>False</div>
<div>False</div>
</body>
</html>
2 changes: 1 addition & 1 deletion src/Products/PageTemplates/tests/output/CheckPathAlt.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<p name="">Z</p>
<p>Z</p>

<p>'c'</p>
<p>c</p>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
objects and not be subject to variations in the str() of the
exception object. -->
Error:10
Error:11
Error:12
<hr />
</body>
</html>
6 changes: 6 additions & 0 deletions src/Products/PageTemplates/tests/testDTMLTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
from zope.component import provideUtility
from zope.traversing.adapters import DefaultTraversable

from .util import useChameleonEngine
from .util import useOldZopeEngine


class AqPageTemplate(Implicit, PageTemplate):
pass
Expand Down Expand Up @@ -55,6 +58,7 @@ class DTMLTests(zope.component.testing.PlacelessSetup, unittest.TestCase):

def setUp(self):
super().setUp()
useChameleonEngine()
zope.component.provideAdapter(DefaultTraversable, (None,))
provideUtility(DefaultUnicodeEncodingConflictResolver,
IUnicodeEncodingConflictResolver)
Expand Down Expand Up @@ -153,6 +157,8 @@ def test_3(self):
def test_on_error_in_slot_filler(self):
# The `here` isn't defined, so the macro definition is
# expected to catch the error that gets raised.
# BBB This only works with the old Zope page template engine
useOldZopeEngine()
text = '''\
<div metal:define-macro="foo">
<div tal:on-error="string:eek">
Expand Down
12 changes: 12 additions & 0 deletions src/Products/PageTemplates/tests/testHTMLTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
from zope.component import provideUtility
from zope.traversing.adapters import DefaultTraversable

from .util import useChameleonEngine
from .util import useOldZopeEngine


class AqPageTemplate(Implicit, PageTemplate):
pass
Expand Down Expand Up @@ -57,6 +60,7 @@ class HTMLTests(zope.component.testing.PlacelessSetup, unittest.TestCase):

def setUp(self):
super().setUp()
useChameleonEngine()
zope.component.provideAdapter(DefaultTraversable, (None,))

provideUtility(DefaultUnicodeEncodingConflictResolver,
Expand Down Expand Up @@ -121,6 +125,10 @@ def testSimpleLoop(self):
self.assert_expected(self.folder.t, 'Loop1.html')

def testFancyLoop(self):
# BBB This only works with the old Zope page template engine.
# It uses attributes ``first`` and ``last`` on a repeat variable,
# which does not work in Chameleon.
useOldZopeEngine()
self.assert_expected(self.folder.t, 'Loop2.html')

def testGlobalsShadowLocals(self):
Expand Down Expand Up @@ -157,6 +165,10 @@ def testImportOldStyleClass(self):
self.assert_expected(self.folder.t, 'CheckImportOldStyleClass.html')

def testRepeatVariable(self):
# This should work with the new Chameleon engine, but apparently there
# is too much futzing around with traversal during test setup, so
# Chameleon fails to traverse to repeat variables. Using old engine.
useOldZopeEngine()
self.assert_expected(self.folder.t, 'RepeatVariable.html')

def testBooleanAttributesAndDefault(self):
Expand Down
32 changes: 27 additions & 5 deletions src/Products/PageTemplates/tests/testZopePageTemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
from zope.publisher.http import HTTPCharsets
from zope.traversing.adapters import DefaultTraversable

from .util import useChameleonEngine
from .util import useOldZopeEngine


ascii_binary = b'<html><body>hello world</body></html>'
iso885915_binary = '<html><body>üöäÜÖÄß</body></html>'.encode('iso-8859-15')
Expand Down Expand Up @@ -85,6 +88,9 @@

class ZPTUtilsTests(unittest.TestCase):

def afterSetUp(self):
useChameleonEngine()

def testExtractEncodingFromXMLPreamble(self):
extract = encodingFromXMLPreamble
self.assertEqual(extract(b'<?xml version="1.0" ?>'), DEFAULT_ENCODING)
Expand Down Expand Up @@ -118,8 +124,11 @@ def testExtractCharsetFromMetaHTTPEquivTag(self):


class ZPTUnicodeEncodingConflictResolution(ZopeTestCase):
# BBB The unicode conflict resolution feature is only available
# for the old Zope page template engine!

def afterSetUp(self):
useOldZopeEngine()
zope.component.provideAdapter(DefaultTraversable, (None,))
zope.component.provideAdapter(HTTPCharsets, (None,))
provideUtility(PreferredCharsetResolver,
Expand All @@ -134,7 +143,7 @@ def testISO_8859_15(self):
self.app.REQUEST.set('HTTP_ACCEPT_CHARSET', 'ISO-8859-15,utf-8')
self.app.REQUEST.set('data', 'üöä'.encode('iso-8859-15'))
result = zpt.pt_render()
self.assertTrue(result.startswith('<div>üöä</div>'))
self.assertIn('<div>üöä</div>', result)

def testUTF8(self):
manage_addPageTemplate(self.app, 'test',
Expand All @@ -145,7 +154,7 @@ def testUTF8(self):
self.app.REQUEST.set('HTTP_ACCEPT_CHARSET', 'utf-8,ISO-8859-15')
self.app.REQUEST.set('data', 'üöä'.encode())
result = zpt.pt_render()
self.assertTrue(result.startswith('<div>üöä</div>'))
self.assertIn('<div>üöä</div>', result)

def testUTF8WrongPreferredCharset(self):
manage_addPageTemplate(self.app, 'test',
Expand All @@ -156,7 +165,7 @@ def testUTF8WrongPreferredCharset(self):
self.app.REQUEST.set('HTTP_ACCEPT_CHARSET', 'iso-8859-15')
self.app.REQUEST.set('data', 'üöä'.encode())
result = zpt.pt_render()
self.assertFalse(result.startswith('<div>üöä</div>'))
self.assertNotIn('<div>üöä</div>', result)

def testStructureWithAccentedChars(self):
raw = '<div tal:content="structure python: \'üöä\'" />'
Expand All @@ -166,7 +175,7 @@ def testStructureWithAccentedChars(self):
zpt = self.app['test']
self.app.REQUEST.set('HTTP_ACCEPT_CHARSET', 'iso-8859-15,utf-8')
result = zpt.pt_render()
self.assertTrue(result.startswith('<div>üöä</div>'))
self.assertIn('<div>üöä</div>', result)

def testBug151020(self):
raw = '<div tal:content="structure python: \'üöä\'" />'
Expand All @@ -177,7 +186,7 @@ def testBug151020(self):
self.app.REQUEST.set('HTTP_ACCEPT_CHARSET',
'x-user-defined, iso-8859-15,utf-8')
result = zpt.pt_render()
self.assertTrue(result.startswith('<div>üöä</div>'))
self.assertIn('<div>üöä</div>', result)

def test_bug_198274(self):
# See https://bugs.launchpad.net/bugs/198274
Expand Down Expand Up @@ -227,6 +236,9 @@ def testDebugFlags(self):

class ZopePageTemplateFileTests(ZopeTestCase):

def afterSetUp(self):
useChameleonEngine()

def test_class_conforms_to_IWriteLock(self):
from zope.interface.verify import verifyClass
from OFS.interfaces import IWriteLock
Expand Down Expand Up @@ -336,6 +348,8 @@ def testXMLAttrsMustNotBeLowercased(self):
self.assertEqual('ATTR' in result, True)

def testHTMLAttrsAreLowerCased(self):
# BBB Only the old Zope page template engine does this munging
useOldZopeEngine()
zpt = self._put(html_with_upper_attr)
self.content_type = 'text/html'
result = zpt.pt_render()
Expand All @@ -359,6 +373,7 @@ def testPreferredCharsetResolverWithoutRequestAndWithoutEncoding(self):
class ZPTRegressions(unittest.TestCase):

def setUp(self):
useChameleonEngine()
transaction.begin()
self.app = makerequest(Zope2.app())
f = self.app.manage_addProduct['PageTemplates'].manage_addPageTemplate
Expand Down Expand Up @@ -406,6 +421,7 @@ class ZPTMacros(zope.component.testing.PlacelessSetup, unittest.TestCase):

def setUp(self):
super().setUp()
useChameleonEngine()
zope.component.provideAdapter(DefaultTraversable, (None,))

transaction.begin()
Expand Down Expand Up @@ -458,6 +474,9 @@ def testPtErrors(self):

class SrcTests(unittest.TestCase):

def setUp(self):
useChameleonEngine()

def _getTargetClass(self):
from Products.PageTemplates.ZopePageTemplate import Src
return Src
Expand Down Expand Up @@ -503,6 +522,9 @@ def test___call__(self):
class ZPTBrowserTests(FunctionalTestCase):
"""Browser testing ZopePageTemplate"""

def afterSetUp(self):
useChameleonEngine()

def setUp(self):
from Products.PageTemplates.ZopePageTemplate import \
manage_addPageTemplate
Expand Down
6 changes: 3 additions & 3 deletions src/Products/PageTemplates/tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
from Testing.ZopeTestCase import ZopeTestCase
from Testing.ZopeTestCase.sandbox import Sandboxed

from .util import useChameleonEngine


path = os.path.dirname(__file__)


class TestPatches(Sandboxed, ZopeTestCase):

def afterSetUp(self):
from Zope2.App import zcml
import Products.PageTemplates
zcml.load_config("configure.zcml", Products.PageTemplates)
useChameleonEngine()

def test_pagetemplate(self):
from Products.PageTemplates.PageTemplate import PageTemplate
Expand Down
7 changes: 4 additions & 3 deletions src/Products/PageTemplates/tests/test_pagetemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from Testing.ZopeTestCase import ZopeTestCase

from .util import useChameleonEngine


path = os.path.dirname(__file__)


class TestPageTemplateFile(ZopeTestCase):

def afterSetUp(self):
from Zope2.App import zcml
import Products.PageTemplates
zcml.load_config("configure.zcml", Products.PageTemplates)
useChameleonEngine()

def _makeOne(self, name):
return PageTemplateFile(os.path.join(path, name)).__of__(self.app)
Expand Down
7 changes: 4 additions & 3 deletions src/Products/PageTemplates/tests/test_persistenttemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from Testing.utils import capture_stdout
from Testing.ZopeTestCase import ZopeTestCase

from .util import useChameleonEngine


macro_outer = """
<metal:defm define-macro="master">
Expand Down Expand Up @@ -109,10 +111,9 @@ def get_editable_content(template):


class TestPersistent(ZopeTestCase):

def afterSetUp(self):
from Zope2.App import zcml
import Products.PageTemplates
zcml.load_config("configure.zcml", Products.PageTemplates)
useChameleonEngine()
self.setRoles(['Manager'])

def _makeOne(self, template_id, source):
Expand Down
4 changes: 4 additions & 0 deletions src/Products/PageTemplates/tests/test_ptfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from Testing.makerequest import makerequest

from .util import useChameleonEngine


class TypeSniffingTestCase(unittest.TestCase):

Expand All @@ -21,6 +23,8 @@ def setUp(self):
IUnicodeEncodingConflictResolver
from Products.PageTemplates.unicodeconflictresolver import \
DefaultUnicodeEncodingConflictResolver
# Make sure we use the new default chameleon engine
useChameleonEngine()
provideUtility(DefaultUnicodeEncodingConflictResolver,
IUnicodeEncodingConflictResolver)

Expand Down
7 changes: 4 additions & 3 deletions src/Products/PageTemplates/tests/test_viewpagetemplatefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Testing.ZopeTestCase import ZopeTestCase

from .util import useChameleonEngine


class SimpleView(BrowserView):
index = ViewPageTemplateFile('simple.pt')
Expand Down Expand Up @@ -38,10 +40,9 @@ class MissingView(BrowserView):


class TestPageTemplateFile(ZopeTestCase):

def afterSetUp(self):
from Zope2.App import zcml
import Products.PageTemplates
zcml.load_config("configure.zcml", Products.PageTemplates)
useChameleonEngine()

def test_simple(self):
view = SimpleView(self.folder, self.folder.REQUEST)
Expand Down
19 changes: 19 additions & 0 deletions src/Products/PageTemplates/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import unittest

from ExtensionClass import Base
from Products.PageTemplates.engine import Program
from zope.component import provideUtility
from zope.pagetemplate.interfaces import IPageTemplateEngine
from zope.pagetemplate.pagetemplate import PageTemplateEngine


# Dummy TestCase to use the assertions outside the actual tests.
Expand Down Expand Up @@ -137,3 +141,18 @@ def read_output(filename):
with _open(filename, 'r') as fd:
data = fd.read()
return data


def useChameleonEngine():
# Force the use of the new chameleon rendering engine (the new default).
# Its use depends on a utility registration that is queried in
# zope.pagetemplate,pagetemplate.PageTemplate's _cook method. Unfortunately
# the fallback is the old Zope engine if there is no registration, so we
# force one here for use by unit tests.
provideUtility(Program, IPageTemplateEngine)


def useOldZopeEngine():
# BBB Force the use of the old Zope page template engine, which is needed
# for some tests that test features only supported by it.
provideUtility(PageTemplateEngine, IPageTemplateEngine)

0 comments on commit fdb834b

Please sign in to comment.