Skip to content

Commit

Permalink
fixing tests for py3
Browse files Browse the repository at this point in the history
  • Loading branch information
petschki committed Feb 7, 2019
1 parent 6ceb71b commit 253ff83
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 57 deletions.
18 changes: 11 additions & 7 deletions plone/app/blocks/layoutbehavior.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# -*- coding: utf-8 -*-
import json
import logging
import six
import zope.deferredimport

from Acquisition import aq_base
from Acquisition import aq_inner
from Acquisition import aq_parent
from lxml import etree
from lxml import html
from plone.app.blocks.interfaces import _
from plone.app.blocks.interfaces import DEFAULT_AJAX_LAYOUT_REGISTRY_KEY
from plone.app.blocks.interfaces import DEFAULT_CONTENT_LAYOUT_REGISTRY_KEY
from plone.app.blocks.interfaces import DEFAULT_SITE_LAYOUT_REGISTRY_KEY
from plone.app.blocks.interfaces import ILayoutField
from plone.app.blocks.interfaces import _
from plone.app.blocks.utils import applyTilePersistent
from plone.app.blocks.utils import resolveResource
from plone.autoform.directives import omitted
Expand All @@ -19,25 +24,22 @@
from plone.memoize import view
from plone.registry.interfaces import IRegistry
from plone.rfc822.interfaces import IPrimaryField
from plone.supermodel.directives import fieldset
from plone.supermodel import model
from plone.supermodel.directives import fieldset
from plone.tiles.data import defaultTileDataStorage
from plone.tiles.interfaces import ITile
from plone.tiles.interfaces import ITileDataStorage
from plone.tiles.interfaces import ITileType
from repoze.xmliter.utils import getHTMLSerializer
from zExceptions import NotFound
from zope import schema
from zope.component import adapter
from zope.component import getUtility
from zope.component import queryUtility
from zope.deprecation import deprecate
from zope import schema
from zope.interface import implementer
from zope.interface import Interface
from zope.interface import implementer
from zope.interface import provider
import json
import logging
import zope.deferredimport

logger = logging.getLogger('plone.app.blocks')

Expand Down Expand Up @@ -175,6 +177,8 @@ def content_layout(self):
path = self.content_layout_path()
try:
resolved = resolveResource(path)
if isinstance(resolved, six.text_type):
resolved = resolved.encode('utf-8')
layout = applyTilePersistent(path, resolved)
except (NotFound, RuntimeError, IOError):
pass
Expand Down
3 changes: 3 additions & 0 deletions plone/app/blocks/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ def layout(self):
if pathContext is None:
break

if isinstance(layout, six.binary_type):
layout = layout.decode()

path = layout
if pathContext is not None:
path = parse.urljoin(pathContext.absolute_url_path(), layout)
Expand Down
2 changes: 1 addition & 1 deletion plone/app/blocks/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ def tearDown(self):
bases=(BLOCKS_FIXTURE,), name="Blocks:Functional")
BLOCKS_FUNCTIONAL_TESTING_PRETTY_PRINT = FunctionalTesting(
bases=(PRETTY_PRINT_FIXTURE, BLOCKS_FIXTURE,),
name="Blocks:Functional Pretty Printing")
name="Blocks:FunctionalPrettyPrinting")
2 changes: 2 additions & 0 deletions plone/app/blocks/tests/context.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,7 @@ the default title of the portal object, ``Plone site`` will be used::
>>> browser.open(portal.absolute_url() + '/@@page-layout')
>>> print(browser.contents)
<!DOCTYPE html...
...
<title>Plone site</title>...
...
<div data-panel="content">Plone site</div>...
2 changes: 1 addition & 1 deletion plone/app/blocks/tests/rendering.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ By using one of these views to reference the layout of a given page, we can mana
>>> from zope.component import getUtility
>>> from plone.registry.interfaces import IRegistry
>>> registry = getUtility(IRegistry)
>>> registry['plone.defaultSiteLayout'] = '/++sitelayout++mylayout/site.html'
>>> registry['plone.defaultSiteLayout'] = b'/++sitelayout++mylayout/site.html'
>>> transaction.commit()
Creating a page layout and tiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ <h1>Welcome to my layout 1!</h1>
<div id="panel1">Layout panel 1</div>
<div id="panel2">
Layout panel 2
<div id="layout-tile1" class="tile-placeholder" data-tile="./@@test.tile1/tile1">Layout tile 1 placeholder</div>
<div id="layout-tile1" class="tile-placeholder" data-tile="./@@test.tile1/tile1">My Layout 1 tile 1 placeholder</div>
</div>
<div id="panel3">
Layout panel 3
<div id="layout-tile2" class="tile-placeholder" data-tile="./@@test.tile1/tile2">Layout tile 2 placeholder</div>
<div id="layout-tile2" class="tile-placeholder" data-tile="./@@test.tile1/tile2">My Layout 1 tile 2 placeholder</div>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ <h1>Welcome to my layout 2!</h1>
<div id="panel1">Layout panel 1</div>
<div id="panel2">
Layout panel 2
<div id="layout-tile1" class="tile-placeholder" data-tile="./@@test.tile1/tile1">Layout tile 1 placeholder</div>
<div id="layout-tile1" class="tile-placeholder" data-tile="./@@test.tile1/tile1">My Layout 2 tile 1 placeholder</div>
</div>
<div id="panel3">
Layout panel 3
<div id="layout-tile2" class="tile-placeholder" data-tile="./@@test.tile1/tile2">Layout tile 2 placeholder</div>
<div id="layout-tile2" class="tile-placeholder" data-tile="./@@test.tile1/tile2">My Layout 2 tile 2 placeholder</div>
</div>
</body>
</html>
25 changes: 9 additions & 16 deletions plone/app/blocks/tests/test_page_sitelayout.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
# -*- coding: utf-8 -*-
import transaction
import unittest

from plone.app.blocks.interfaces import DEFAULT_SITE_LAYOUT_REGISTRY_KEY
from plone.app.blocks.layoutbehavior import ILayoutAware
from plone.app.blocks.layoutbehavior import LayoutAwareBehavior
from plone.app.blocks.testing import BLOCKS_FUNCTIONAL_TESTING
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.app.testing import setRoles
from plone.registry.interfaces import IRegistry
from zExceptions import NotFound
from zope.component import getGlobalSiteManager
from zope.component import getMultiAdapter
from zope.component import getUtility
import pkg_resources
import transaction
import unittest

try:
pkg_resources.get_distribution('plone.app.contenttypes')
except pkg_resources.DistributionNotFound:
HAS_PLONE_APP_CONTENTTYPES = False
else:
HAS_PLONE_APP_CONTENTTYPES = True


class TestPageSiteLayout(unittest.TestCase):
Expand All @@ -38,7 +31,7 @@ def setUp(self):

# setup default behavior
self.registry[DEFAULT_SITE_LAYOUT_REGISTRY_KEY] =\
'/++sitelayout++testlayout1/site.html'
b'/++sitelayout++testlayout1/site.html'
iface = self.portal['f1']['d1'].__class__
sm = getGlobalSiteManager()
sm.registerAdapter(LayoutAwareBehavior, [iface])
Expand Down Expand Up @@ -66,7 +59,7 @@ def test_page_site_layout_no_registry_key(self):

def test_page_site_layout_default(self):
self.registry[DEFAULT_SITE_LAYOUT_REGISTRY_KEY] =\
'/++sitelayout++testlayout1/site.html'
b'/++sitelayout++testlayout1/site.html'
view = getMultiAdapter((self.portal['f1']['d1'], self.request,),
name=u'page-site-layout')
rendered = view()
Expand Down Expand Up @@ -190,7 +183,7 @@ def test_page_site_layout_cache_invalidate_registry_key(self):

# Trigger invalidation by modifying the global registry key
self.registry[DEFAULT_SITE_LAYOUT_REGISTRY_KEY] =\
'/++sitelayout++testlayout2/mylayout.html'
b'/++sitelayout++testlayout2/mylayout.html'

# Change the section value
self.behavior.sectionSiteLayout = \
Expand Down Expand Up @@ -223,7 +216,7 @@ def setUp(self):

# setup default behaviors
self.registry[DEFAULT_SITE_LAYOUT_REGISTRY_KEY] = \
'/++sitelayout++testlayout1/site.html'
b'/++sitelayout++testlayout1/site.html'

iface = self.portal['f1'].__class__
sm.registerAdapter(LayoutAwareBehavior, [iface])
Expand All @@ -245,7 +238,7 @@ def setUp(self):

def test_page_site_layout_is_not_acquired(self):
self.registry[DEFAULT_SITE_LAYOUT_REGISTRY_KEY] = \
'/++sitelayout++testlayout1/site.html'
b'/++sitelayout++testlayout1/site.html'

a1 = ILayoutAware(self.portal['f1'])
a2 = ILayoutAware(self.portal['f1']['d1'])
Expand Down
38 changes: 21 additions & 17 deletions plone/app/blocks/tests/test_sitelayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
from plone.app.testing import setRoles
from plone.memoize.volatile import ATTR
from plone.registry.interfaces import IRegistry
from six import StringIO
from zExceptions import NotFound
from zope.component import adapter
from zope.component import getMultiAdapter
from zope.component import getSiteManager
from zope.component import getUtility
from zope.interface import implementer

import six
import transaction
import unittest

Expand Down Expand Up @@ -55,7 +55,7 @@ def test_default_site_layout(self):
delattr(self.portal, ATTR)

self.registry[DEFAULT_SITE_LAYOUT_REGISTRY_KEY] = \
'/++sitelayout++testlayout1/site.html'
b'/++sitelayout++testlayout1/site.html'

view = getMultiAdapter((self.portal, self.request,),
name=u'default-site-layout')
Expand All @@ -73,14 +73,14 @@ def test_no_default_site_layout(self):
rendered = view()

# Should render main_template with template-layout in body class
rendered_tree = etree.parse(StringIO(rendered), etree.HTMLParser())
rendered_tree = etree.parse(six.StringIO(rendered), etree.HTMLParser())
xpath_body = etree.XPath('/html/body')
body_tag = xpath_body(rendered_tree)[0]
self.assertIn(u'template-layout', body_tag.attrib['class'])

def test_default_site_layout_section_override(self):
self.registry[DEFAULT_SITE_LAYOUT_REGISTRY_KEY] = \
'/++sitelayout++testlayout1/site.html'
b'/++sitelayout++testlayout1/site.html'

from plone.app.blocks.layoutbehavior import ILayoutAware

Expand Down Expand Up @@ -108,7 +108,7 @@ def __init__(self, context):

def test_default_site_layout_section_no_override(self):
self.registry[DEFAULT_SITE_LAYOUT_REGISTRY_KEY] = \
'/++sitelayout++testlayout1/site.html'
b'/++sitelayout++testlayout1/site.html'

view = getMultiAdapter((self.portal['f1']['d1'], self.request,),
name=u'default-site-layout')
Expand All @@ -127,12 +127,13 @@ def test_default_site_layout_cache(self):
resources['sitelayout']._setOb('testlayout3',
BTreeFolder2('testlayout3'))
resources['sitelayout']['testlayout3']._setOb(
'site.html', File('site.html', 'site.html', StringIO(
'<html><head><title>ZODB test</title></head></html>'))
'site.html', File(
'site.html', 'site.html',
b'<html><head><title>ZODB test</title></head></html>')
)

self.registry[DEFAULT_SITE_LAYOUT_REGISTRY_KEY] = \
'/++sitelayout++testlayout3/site.html'
b'/++sitelayout++testlayout3/site.html'

view = getMultiAdapter((self.portal, self.request,),
name=u'default-site-layout')
Expand All @@ -142,8 +143,9 @@ def test_default_site_layout_cache(self):

resources['sitelayout']['testlayout3']._delOb('site.html')
resources['sitelayout']['testlayout3']._setOb(
'site.html', File('site.html', 'site.html', StringIO(
'<html><head><title>Cache test</title></head></html>'))
'site.html', File(
'site.html', 'site.html',
b'<html><head><title>Cache test</title></head></html>')
)

view = getMultiAdapter((self.portal, self.request,),
Expand Down Expand Up @@ -174,12 +176,13 @@ def test_default_site_layout_invalidate_mtime(self):
resources['sitelayout']._setOb('testlayout3',
BTreeFolder2('testlayout3'))
resources['sitelayout']['testlayout3']._setOb(
'site.html', File('site.html', 'site.html', StringIO(
'<html><head><title>ZODB test</title></head></html>'))
'site.html', File(
'site.html', 'site.html',
b'<html><head><title>ZODB test</title></head></html>')
)

self.registry[DEFAULT_SITE_LAYOUT_REGISTRY_KEY] = \
'/++sitelayout++testlayout3/site.html'
b'/++sitelayout++testlayout3/site.html'

view = getMultiAdapter((self.portal, self.request,),
name=u'default-site-layout')
Expand All @@ -194,8 +197,9 @@ def test_default_site_layout_invalidate_mtime(self):
# Modify the site layout
resources['sitelayout']['testlayout3']._delOb('site.html')
resources['sitelayout']['testlayout3']._setOb(
'site.html', File('site.html', 'site.html', StringIO(
'<html><head><title>Cache test</title></head></html>'))
'site.html', File(
'site.html', 'site.html',
b'<html><head><title>Cache test</title></head></html>')
)

view = getMultiAdapter((self.portal, self.request,),
Expand All @@ -211,7 +215,7 @@ def test_default_site_layout_invalidate_registry_key(self):
delattr(self.portal, ATTR)

self.registry[DEFAULT_SITE_LAYOUT_REGISTRY_KEY] = \
'/++sitelayout++testlayout1/site.html'
b'/++sitelayout++testlayout1/site.html'

view = getMultiAdapter((self.portal, self.request,),
name=u'default-site-layout')
Expand All @@ -221,7 +225,7 @@ def test_default_site_layout_invalidate_registry_key(self):

# Trigger invalidation by modifying the global site layout selection
self.registry[DEFAULT_SITE_LAYOUT_REGISTRY_KEY] = \
'/++sitelayout++testlayout2/mylayout.html'
b'/++sitelayout++testlayout2/mylayout.html'

view = getMultiAdapter((self.portal, self.request,),
name=u'default-site-layout')
Expand Down
10 changes: 5 additions & 5 deletions plone/app/blocks/tests/test_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def testRenderTiles(self):
request = self.layer['request']
tree = serializer.tree
renderTiles(request, tree)
result = serializer.serialize()
result = str(serializer)
self.assertIn('This is a demo tile with id tile2', result)
self.assertIn('This is a demo tile with id tile3', result)
self.assertIn('This is a demo tile with id tile4', result)
Expand All @@ -231,7 +231,7 @@ def testRenderTilesError(self):
request = self.layer['request']
tree = serializer.tree
renderTiles(request, tree)
result = serializer.serialize()
result = str(serializer)
self.assertIn('This is a demo tile with id tile2', result)
self.assertNotIn('This is a demo tile with id tile3', result)
self.assertIn('There was an error while rendering this tile', result)
Expand All @@ -244,7 +244,7 @@ def testRenderSubTiles(self):
request = self.layer['request']
tree = serializer.tree
renderTiles(request, tree)
result = serializer.serialize()
result = str(serializer)

self.assertIn("I'm a tile calling another tile as subtile.", result)
self.assertIn("This is a demo tile with id subtile", result)
Expand All @@ -256,7 +256,7 @@ def testRenderStructurelessTiles(self):
request = self.layer['request']
tree = serializer.tree
renderTiles(request, tree)
result = serializer.serialize()
result = str(serializer)

self.assertIn("structureless in head", result)
self.assertIn("structureless in body", result)
Expand All @@ -268,6 +268,6 @@ def testNonExistentAndBrokenTiiles(self):
request = self.layer['request']
tree = serializer.tree
renderTiles(request, tree)
result = serializer.serialize()
result = str(serializer)

self.assertIn("hi there!", result)
3 changes: 2 additions & 1 deletion plone/app/blocks/tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def renderTiles(request, tree):
except RuntimeError:
tileTree = errorTile(request)
except NotFound:
logger.warn('NotFound while trying to render tile: %s', tileHref)
logger.warning(
'NotFound while trying to render tile: %s', tileHref)

if tileTree is None:
utils.remove_element(tileNode)
Expand Down
Loading

0 comments on commit 253ff83

Please sign in to comment.