[Zope3-checkins] CVS: Zope3/src/zope/app/presentation/tests - __init__.py:1.1 test_pagefolder.py:1.1 test_presentation.py:1.1 test_zpt.py:1.1

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Mar 11 05:18:38 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/presentation/tests
In directory cvs.zope.org:/tmp/cvs-serv8352/src/zope/app/presentation/tests

Added Files:
	__init__.py test_pagefolder.py test_presentation.py 
	test_zpt.py 
Log Message:


Moved Page Folder and it Page Templates into zope.app.presentation.




=== Added File Zope3/src/zope/app/presentation/tests/__init__.py ===
# import this.


=== Added File Zope3/src/zope/app/presentation/tests/test_pagefolder.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Page folder tests.

$Id: test_pagefolder.py,v 1.1 2004/03/11 10:18:37 srichter Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from zope.app.tests import ztapi
from zope.app.tests import setup
from zope.app.services.tests.placefulsetup import PlacefulSetup
from zope.app.presentation.pagefolder import PageFolder, IPageFolder
from zope.app.presentation.zpt import ZPTTemplate
from zope.app.presentation import LocalPresentationService
from zope.app.interfaces.services.registration import ActiveStatus
from zope.interface import Interface
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.app.services.tests.test_registrationmanager \
     import RegistrationManagerContainerTests
from zope.component.servicenames import Presentation

from zope.app.interfaces.dependable import IDependable
from zope.app.interfaces.annotation import IAttributeAnnotatable
from zope.app.dependable import Dependable
from zope.app import zapi
from zope.app.interfaces.annotation import IAnnotations, IAnnotatable
from zope.app.attributeannotations import AttributeAnnotations



class I(Interface):
    pass

class I2(Interface):
    pass

class Test(RegistrationManagerContainerTests, PlacefulSetup, TestCase):

    def setUp(self):
        sm = PlacefulSetup.setUp(self, site=True)
        zapi.getService(None, Presentation).defineLayer('debug')
        setup.addService(sm, Presentation, LocalPresentationService(),
                         suffix='service')
        default = zapi.traverse(self.rootFolder, '++etc++site/default')

        ztapi.provideAdapter(IAnnotatable, IAnnotations,
                         AttributeAnnotations)

        ztapi.provideAdapter(IAnnotatable, IDependable,
                         Dependable)

        default["PF"] = PageFolder()
        pagefolder = zapi.traverse(default, "PF")

        pagefolder.required = I
        pagefolder.factoryName = None
        pagefolder.permission = 'zope.View'

        self.__pagefolder = pagefolder



    def test___setitem__(self):
        
        pagefolder = self.__pagefolder

        pagefolder['foo.html'] = ZPTTemplate()

        rm = pagefolder.getRegistrationManager()
        name = rm.keys()[-1]
        registration = zapi.traverse(pagefolder.getRegistrationManager(),
                                     name)
        self.assertEqual(registration.status, ActiveStatus)
        self.assertEqual(registration.required, I)
        self.assertEqual(registration.requestType, IBrowserRequest)
        self.assertEqual(registration.name, u'foo.html')
        self.assertEqual(registration.layer, 'default')
        self.assertEqual(registration.factoryName, None)
        self.assertEqual(registration.permission, 'zope.View')
        self.assertEqual(registration.attribute, None)

        self.assertRaises(TypeError,
                          pagefolder.__setitem__, 'bar.html', PageFolder())

    def test_applyDefaults(self):

        pagefolder = self.__pagefolder

        pagefolder['foo.html'] = ZPTTemplate()

        rm = pagefolder.getRegistrationManager()
        name = rm.keys()[-1]
        registration = zapi.traverse(pagefolder.getRegistrationManager(), name)
        self.assertEqual(registration.status, ActiveStatus)
        self.assertEqual(registration.required, I)
        self.assertEqual(registration.requestType, IBrowserRequest)
        self.assertEqual(registration.name, u'foo.html')
        self.assertEqual(registration.layer, 'default')
        self.assertEqual(registration.factoryName, None)
        self.assertEqual(registration.permission, 'zope.View')
        self.assertEqual(registration.attribute, None)

        pagefolder.required = I2
        pagefolder.permission = 'zope.ManageContent'
        pagefolder.layer = 'debug'

        pagefolder.applyDefaults()

        registration = zapi.traverse(pagefolder.getRegistrationManager(), name)
        self.assertEqual(registration.status, ActiveStatus)
        self.assertEqual(registration.required, I2)
        self.assertEqual(registration.requestType, IBrowserRequest)
        self.assertEqual(registration.name, u'foo.html')
        self.assertEqual(registration.layer, 'debug')
        self.assertEqual(registration.factoryName, None)
        self.assertEqual(registration.permission, 'zope.ManageContent')
        self.assertEqual(registration.attribute, None)

        

def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')


=== Added File Zope3/src/zope/app/presentation/tests/test_presentation.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Test the presentation module

$Id: test_presentation.py,v 1.1 2004/03/11 10:18:37 srichter Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from zope.testing.doctestunit import DocTestSuite
from zope.app.tests.placelesssetup import setUp, tearDown

from zope.app import zapi
from zope.interface import Interface, directlyProvides, implements
from zope.interface.verify import verifyObject

from zope.app.folder import rootFolder
from zope.app.presentation.zpt import IZPTTemplate
from zope.app.services.service import ServiceManager
from zope.app.services.servicenames import Presentation
from zope.app.services.tests.iregistry import TestingIRegistry
from zope.app.services.tests.placefulsetup import PlacefulSetup
from zope.app.presentation.presentation import ViewRegistration
from zope.app.presentation.presentation import PageRegistration
from zope.app.presentation.presentation import BoundTemplate
from zope.app.presentation.presentation import LocalPresentationService
from zope.app.tests import setup
from zope.app.traversing import traverse

from zope.component.exceptions import ComponentLookupError
from zope.component.interfaces import IServiceService
from zope.component.interfaces import IPresentationService
from zope.app.tests import ztapi
from zope.configuration.exceptions import ConfigurationError

from zope.proxy import removeAllProxies

from zope.publisher.browser import TestRequest
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.app.container.contained import contained, uncontained, setitem
from zope.app.container.interfaces import IContained, ILocation

from zope.app.interfaces.dependable import IDependable
from zope.app.interfaces.annotation import IAttributeAnnotatable
from zope.app.interfaces.services.registration import IRegistered
from zope.app.interfaces.traversing import IPhysicallyLocatable
from zope.app.dependable import Dependable

class I1(Interface):
    pass

class I1E(I1):
    pass

I2 = IBrowserRequest

class I3(Interface):
    pass

class I4(Interface):
    pass


class Registration:
    required = I1
    requestType = I2
    name = 'test'
    layer = 'default'
    serviceType = Presentation
    provided = Interface

    with = property(lambda self: (self.requestType, ))
    factories = property(lambda self: (self.factory, ))

    def activated(self): pass
    def deactivated(self): pass

class C: pass

class PhonyTemplate:
    __name__ = __parent__ = None
    implements(IZPTTemplate, IDependable, IRegistered)

    _dependents = ()
    _usages = ()

    def addDependent(self, location):
        self._dependents = tuple(
            [d for d in self._dependents if d != location]
            +
            [location]
            )

    def removeDependent(self, location):
        self._dependents = tuple(
            [d for d in self._dependents if d != location]
            )

    def dependents(self):
        return self._dependents

    def addUsage(self, location):
        self._usages = tuple(
            [d for d in self._usages if d != location]
            +
            [location]
            )

    def removeUsage(self, location):
        self._usages = tuple(
            [d for d in self._usages if d != location]
            )

    def usages(self):
        return self._usages
 

class A:
    def __init__(self, object, request):
        self.context = object
        self.request = request

    run = PhonyTemplate()

class TestLocalPresentationService(PlacefulSetup, TestingIRegistry, TestCase):

    def setUp(self):
        sm = PlacefulSetup.setUp(self, site=True)
        self._service = setup.addService(sm, Presentation,
                                         LocalPresentationService())

    def test_defaultSkin(self):
        # XXX we don't let people set the default skin locally yet.
        # So just test that we can get the default from the global service
        zapi.getService(None, Presentation).defineSkin('bob', ['default'])
        zapi.getService(None, Presentation).setDefaultSkin('bob')
        self.assertEqual(self._service.defaultSkin, 'bob')

    def test_querySkin(self):
        # XXX we don't let people define skins locally yet.
        # So just test that we can get the defs from the global service
        globalService = zapi.getService(None, Presentation)
        globalService.defineLayer('bob')
        globalService.defineSkin('bob', ['bob', 'default'])
        self.assertEqual(self._service.querySkin('bob'), ('bob', 'default'))
        
    def test_queryLayer(self):
        # XXX we don't let people define layers locally yet.
        # So just test that we can get the them from the global service
        globalService = zapi.getService(None, Presentation)
        layer = self._service.queryLayer('default')
        self.assertEqual(layer.__parent__, globalService)
        self.test_queryView()
        layer = self._service.queryLayer('default')
        self.assertEqual(layer.__parent__, self._service)

    def test_queryDefaultViewName(self):
        # XXX we don't let people define the default view name locally
        # yet.  So just test that we can get it from the global
        # service
        class O:
            implements(I1)
        o = O()
        r = TestRequest()
        self.assertEqual(self._service.queryDefaultViewName(o, r),
                         None)
        globalService = zapi.getService(None, Presentation)
        globalService.setDefaultViewName(I1, IBrowserRequest, 'foo.html')
        self.assertEqual(self._service.queryDefaultViewName(o, r),
                         'foo.html')

    def test_queryMultiView(self):
        # XXX that we don't let people define multiviews locally yet.
        # So just test that we can get them from the global service
        class X:
            implements(I1)
        class Y:
            implements(I3)
        x = X()
        y = Y()
        r = TestRequest()
        self.assertEqual(self._service.queryMultiView((x, y), 'foo.html', r),
                         None)
        globalService = zapi.getService(None, Presentation)

        class MV:
            def __init__(self, x, y, request):
                self.x, self.y, self.request = x, y, request
                
        globalService.provideAdapter(IBrowserRequest, [MV], 'foo.html',
                                     contexts=(I1, I3))
        v = self._service.queryMultiView((x, y), 'foo.html', r)
        self.assertEqual(v.__class__, MV)
        self.assertEqual(v.request, r)
        self.assertEqual(v.x, x)
        self.assertEqual(v.y, y)
        

    def test_queryResource(self):
        # XXX that we don't let people define resources locally yet.
        # So just test that we can get them from the global service

        r = TestRequest()
        self.assertEqual(self._service.queryResource('logo.gif', r),
                         None)

        class Resource:
            def __init__(self, request):
                self.request = request

        globalService = zapi.getService(None, Presentation)
        globalService.provideResource('logo.gif', IBrowserRequest, Resource)
        
        resource = self._service.queryResource('logo.gif', r)
        self.assertEqual(resource.__class__, Resource)
        self.assertEqual(resource.request, r)

    def test_implements_IPresentationService(self):
        from zope.component.interfaces import IPresentationService

        verifyObject(IPresentationService, self._service)

    def createTestingRegistry(self):
        return contained(LocalPresentationService(), C())

    def createTestingRegistration(self):
        return Registration()

    def test_implements_IPresentationService(self):
        verifyObject(IPresentationService, LocalPresentationService())

    def test_queryView_no_view(self):
        service = self._service
        class O:
            implements(I1)

        o = O()
        request = TestRequest()
        self.assertEqual(service.queryView(o, 'test', request), None)
        self.assertEqual(service.queryView(o, 'test', request, default=42), 42)

    def test_queryView(self):
        sm = traverse(self.rootFolder, '++etc++site')

        registration_manager = traverse(sm, 'default').getRegistrationManager()
        key = registration_manager.addRegistration(Registration())
        registration = traverse(registration_manager, key)

        class O:
            implements(I1)

        registration.factory = A

        registry = self._service.createRegistrationsFor(registration)
        registry.register(registration)
        registry.activate(registration)

        o = O()
        request = TestRequest()

        for r in I1, I1E:
            o = O()
            directlyProvides(o, r)

            view = self._service.queryView(o, 'test', request)
            self.assertEqual(view.__class__, A)
            self.assertEqual(view.context, o)
            self.assertEqual(view.request, request)

    def test_queryView_delegation(self):
        service = self._service

        sm = self.buildFolders(site=True)
        registration_manager = traverse(sm, 'default').getRegistrationManager()
        registration = Registration()
        name = registration_manager.addRegistration(registration)
        registration = traverse(registration_manager, name)

        class O:
            implements(I1)

        o = O()
        request = TestRequest()

        class A2(A): pass

        ztapi.browserView(I1, 'test', A2)

        view = service.queryView(o, 'test', request)
        self.assertEqual(view.__class__, A2)
        self.assertEqual(view.context, o)
        self.assertEqual(view.request, request)

    def test_getRegistrationsForInterface(self):
        self.test_queryView()
        for reg in self._service.getRegistrationsForInterface(I1):
            if reg.required is None:
                continue
            self.assertEqual(reg.required, I1)

        for reg in self._service.getRegistrationsForInterface(I1E):
            if reg.required is None:
                continue
            self.assertEqual(reg.required, I1)


class PhonyServiceManager(ServiceManager):

    implements(IServiceService)

class ModuleFinder:

    implements(IContained)

    __parent__ = __name__ = None

    def __init__(self):
        self._dict = {}

    def resolve(self, name):
        if name == "Foo.Bar.A":
            return A
        raise ImportError(name)

    def __setitem__(self, key, ob):
        setitem(self, self.__setitem, key, ob)
    
    def __setitem(self, key, ob):
        self._dict[key] = ob

    def get(self, key, default=None):
        return self._dict.get(key, default)


class PhonyPathAdapter:
    implements(IPhysicallyLocatable)

    def __init__(self, context):
        self.context = context

    def getPath(self):
        return self.context.__name__

    def getRoot(self):
        root = self.context
        while root.__parent__ is not None:
            root = root.__parent__
        return root


class TestViewRegistration(PlacefulSetup, TestCase):

    def test_factories(self):
        folder = ModuleFinder()
        folder = contained(folder, folder)
        registration = contained(
            ViewRegistration(I1, 'test', I2, "Foo.Bar.A", 'zope.View'),
            folder,
            )

        self.assertEqual(registration.required, I1)
        self.assertEqual(registration.requestType, I2)

        factory, = registration.factories
        self.assertEqual(factory, A)


class TestPageRegistration(PlacefulSetup, TestCase):

    def setUp(self):
        PlacefulSetup.setUp(self)
        self.rootFolder = rootFolder()
        self.rootFolder.setSiteManager(PhonyServiceManager(self.rootFolder))
        default = traverse(self.rootFolder, '++etc++site/default')
        self.__template = PhonyTemplate()
        default['t'] = self.__template
        self.folder = contained(ModuleFinder(), self.rootFolder)
        self.folder = contained(ModuleFinder(), self.folder)

    def test_factories_template(self):
        registration = contained(
            PageRegistration(I1, 'test', 'zope.View',
                              "Foo.Bar.A",
                              template='/++etc++site/default/t',
                              ),
            self.folder,
            )

        c = C()
        request = TestRequest()
        factory, = registration.factories
        view = factory(c, request)
        self.assertEqual(view.__class__, BoundTemplate)
        self.assertEqual(removeAllProxies(view).template, self.__template)

        view = removeAllProxies(view).view
        self.assert_(issubclass(view.__class__, A))
        self.assertEqual(view.context, c)
        self.assertEqual(view.request, request)
        self.assertEqual(registration.required, I1)
        self.assertEqual(registration.requestType, I2)

    def test_factories_attribute(self):
        registration = contained(
            PageRegistration(
                I1, 'test', 'zope.View', "Foo.Bar.A", attribute='run'),
            self.folder,6
            )
        c = C()
        request = TestRequest()
        factory, = registration.factories
        view = factory(c, request)
        self.assertEquals(view, A.run)

    def test_factories_errors(self):
        registration = contained(
            PageRegistration(I1, 'test', 'zope.View', "Foo.Bar.A"),
            self.folder,
            )
        c = C()
        request = TestRequest()
        self.assertRaises(ConfigurationError, lambda: registration.factories)
        registration.template = '/++etc++site/default/t'
        registration.attribute = 'run'
        self.assertRaises(ConfigurationError, lambda: registration.factories)

    def test_addremoveNotify_template(self):
        ztapi.provideAdapter(ILocation, IPhysicallyLocatable,
                             PhonyPathAdapter)
        registration = PageRegistration(I1, 'test', 'zope.View', "Foo.Bar.A",
                                        template='/++etc++site/default/t')
        # Test addNotify
        self.folder['test'] = registration
        dependents = zapi.getAdapter(self.__template, IDependable)
        self.assert_('test' in dependents.dependents())
        usages = zapi.getAdapter(self.__template, IRegistered)
        self.assert_('test' in usages.usages())

        # Test removeNotify
        uncontained(registration, self.folder, 'test')
        dependents = zapi.getAdapter(self.__template, IDependable)
        self.assert_('test' not in dependents.dependents())
        usages = zapi.getAdapter(self.__template, IRegistered)
        self.assert_('test' not in usages.usages())
        
    def test_addremoveNotify_attribute(self):
        ztapi.provideAdapter(ILocation, IPhysicallyLocatable,
                             PhonyPathAdapter)
        registration = PageRegistration(I1, 'test', 'zope.View',
                                        "Foo.Bar.A", attribute='run')
        # Just add and remove registration to see that no errors occur
        self.folder['test'] = registration
        uncontained(registration, self.folder, 'test')


def test_suite():
    return TestSuite([
        makeSuite(TestLocalPresentationService),
        makeSuite(TestViewRegistration),
        makeSuite(TestPageRegistration),
        ])

if __name__ == '__main__':
    main(defaultTest='test_suite')




=== Added File Zope3/src/zope/app/presentation/tests/test_zpt.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""
$Id: test_zpt.py,v 1.1 2004/03/11 10:18:37 srichter Exp $
"""
from unittest import TestCase, TestSuite, makeSuite
from zope.app.presentation.zpt import ZPTTemplate, ZPTFactory
from zope.app.presentation.zpt import ReadFile, WriteFile
from zope.publisher.browser import BrowserView, TestRequest

class Test(TestCase):

    # XXX We need tests for the template class itself and for the
    # SearchableText adapter.

    def test_ReadFile(self):
        template = ZPTTemplate()
        source = '<p>Test content</p>'
        template.source = source
        adapter = ReadFile(template)
        self.assertEqual(adapter.read(), source)
        self.assertEqual(adapter.size(), len(source))

    def test_WriteFile(self):
        template = ZPTTemplate()
        source = '<p>Test content</p>'
        template.source = '<p>Old content</p>'
        adapter = WriteFile(template)
        adapter.write(source)        
        self.assertEqual(template.source, source)

    def test_ZPTFactory(self):
        factory = ZPTFactory(None)
        source = '<p>Test content</p>'
        template = factory('foo', 'text/html', source)
        self.assertEqual(template.source, source)

    def test_usage(self):
        template = ZPTTemplate()
        template.source = ('usage: <span tal:replace="usage" />\n'
                           'options: <span tal:replace="options" />'
                           )
        view = BrowserView(42, TestRequest())

        self.assertEqual(template.render(view),
                         'usage: \n'
                         'options: {}\n'
                         )

        self.assertEqual(template.render(view, template_usage=u"spam"),
                         "usage: spam\n"
                         "options: {'template_usage': u'spam'}\n"
                         )
                           
        template.usage = u'eggs'

        self.assertEqual(template.render(view, template_usage=u"spam"),
                         "usage: spam\n"
                         "options: {'template_usage': u'spam'}\n"
                         )

        self.assertEqual(template.render(view),
                         "usage: eggs\n"
                         "options: {'template_usage': u'eggs'}\n"
                         )
                           

def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))




More information about the Zope3-Checkins mailing list