[Zope3-checkins] CVS: Zope3/src/zope/app/container/browser/tests - __init__.py:1.1 test_adding.py:1.1 test_contents.py:1.1 test_directive.py:1.1

Stephan Richter srichter at cosmos.phy.tufts.edu
Sat Mar 13 21:17:11 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/container/browser/tests
In directory cvs.zope.org:/tmp/cvs-serv8281/src/zope/app/container/browser/tests

Added Files:
	__init__.py test_adding.py test_contents.py test_directive.py 
Log Message:
Moved zope.app.browser.container to zope.app.container.browser.


=== Added File Zope3/src/zope/app/container/browser/tests/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/app/container/browser/tests/test_adding.py ===
##############################################################################
#
# Copyright (c) 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.
#
##############################################################################
"""Adding implementation tests

$Id: test_adding.py,v 1.1 2004/03/14 02:17:03 srichter Exp $
"""
import unittest
from zope.testing.doctestunit import DocTestSuite
from zope.app import zapi
from zope.app.tests import ztapi
from zope.app.browser.absoluteurl import AbsoluteURL
from zope.app.container.browser.adding import Adding
from zope.app.container.interfaces import IAdding
from zope.app.container.interfaces import IObjectAddedEvent
from zope.app.interfaces.exceptions import UserError
from zope.app.traversing.interfaces import IContainmentRoot
from zope.app.tests.placelesssetup import PlacelessSetup, setUp, tearDown
from zope.component.interfaces import IFactory
from zope.component.exceptions import ComponentLookupError
from zope.interface import implements, Interface, directlyProvides
from zope.publisher.browser import TestRequest, BrowserView
from zope.app.container.contained import contained
import zope.security.checker
from zope.exceptions import ForbiddenAttribute
from zope.app.container.interfaces import IWriteContainer
from zope.app.container.interfaces import IContainerNamesContainer
import zope.interface
from zope.app.container.interfaces import INameChooser
from zope.app.container.interfaces import IContainer

class Root:
    implements(IContainmentRoot)

class Container(dict):
    implements(IWriteContainer)

class CreationView(BrowserView):

    def action(self):
        return 'been there, done that'


class Content:
    pass

class Factory:

    implements(IFactory)

    title = ''
    description = ''

    def getInterfaces(self):
        return ()

    def __call__(self):
        return Content()


class AbsoluteURL(BrowserView):

    def __str__(self):
        if IContainmentRoot.providedBy(self.context):
            return ''
        name = self.context.__name__
        url = str(zapi.getView(
            zapi.getParent(self.context), 'absolute_url', self.request))
        url += '/' + name
        return url


class Test(PlacelessSetup, unittest.TestCase):

    def setUp(self):
        super(Test, self).setUp()

    def test(self):
        container = Container()
        request = TestRequest()
        adding = Adding(container, request)
        ztapi.browserView(IAdding, "Thing", CreationView)
        self.assertEqual(adding.contentName, None)
        view = adding.publishTraverse(request, 'Thing=foo')
        self.assertEqual(view.action(), 'been there, done that')
        self.assertEqual(adding.contentName, 'foo')

        o = object()
        result = adding.add(o)

        # Check the state of the container and result
        self.assertEqual(container["foo"], o)
        self.assertEqual(result, o)

    def testNoNameGiven(self):
        container = Container()
        request = TestRequest()
        adding = Adding(container, request)
        ztapi.browserView(IAdding, "Thing", CreationView)

        self.assertEqual(adding.contentName, None)
        view = adding.publishTraverse(request, 'Thing=')
        self.assertEqual(adding.contentName, '')

    def testAction(self):
        # make a private factory
        ztapi.provideUtility(IFactory, Factory(), 'fooprivate')

        factory = Factory()
        factory.__Security_checker__ = zope.security.checker.NamesChecker(      
            ['__call__'])
        ztapi.provideUtility(IFactory, factory, 'foo')

        container = Container()
        adding = Adding(container, TestRequest())
        adding.nextURL = lambda: '.'
        adding.namesAccepted = lambda: True

        # we can't use a private factory:
        self.assertRaises(ForbiddenAttribute, 
                          adding.action, type_name='fooprivate', id='bar')

        # typical add - id is provided by user
        adding.action(type_name='foo', id='bar')
        self.assert_('bar' in container)

        # missing type_name
        self.assertRaises(UserError, adding.action, id='bar')

        # missing id
        self.assertRaises(UserError, adding.action, type_name='foo')

        # bad type_name
        self.assertRaises(ComponentLookupError, adding.action, 
            type_name='***', id='bar')

        # alternative add - id is provided internally instead of from user
        adding.namesAccepted = lambda: False
        adding.contentName = 'baz'
        adding.action(type_name='foo')
        self.assert_('baz' in container)

        # alternative add w/missing contentName
        # Note: Passing is None as object name might be okay, if the container
        #       is able to hand out ids itself. Let's not require a content
        #       name to be specified!
        # For the container, (or really, the chooser, to choose, we have to
        # marke the container as a ContainerNamesContainer
        directlyProvides(container, IContainerNamesContainer)
        adding.contentName = None
        adding.action(type_name='foo')
        self.assert_('Content' in container)
        

    def test_action(self):
        container = Container()
        container = contained(container, Root(), "container")
        request = TestRequest()
        adding = Adding(container, request)
        adding.__name__ = '+'
        ztapi.browserView(IAdding, "Thing", CreationView)
        ztapi.browserView(Interface, "absolute_url", AbsoluteURL)
        self.assertRaises(UserError, adding.action, '', 'foo')
        adding.action('Thing', 'foo')
        self.assertEqual(adding.request.response._headers['location'],
                         '/container/+/Thing=foo')
        adding.action('Thing/screen1', 'foo')
        self.assertEqual(adding.request.response._headers['location'],
                         '/container/+/Thing/screen1=foo')



def test_constraint_driven_adding():
    """
    >>> setUp()
    >>> serviceService = zapi.getService(None, zapi.servicenames.Services)
    >>> from zope.app.publisher.interfaces.browser import IBrowserMenuService
    >>> serviceService.defineService(zapi.servicenames.BrowserMenu,
    ...                              IBrowserMenuService)
    >>> from zope.app.publisher.browser.globalbrowsermenuservice """ \
                             """import globalBrowserMenuService
    >>> serviceService.provideService(zapi.servicenames.BrowserMenu,
    ...                               globalBrowserMenuService)

    >>> menuService = zapi.getService(None, zapi.servicenames.BrowserMenu)
    >>> menuService.menu('test', '')
    >>> menuService.menuItem('test', IAdding, '', 'item1', None)
    >>> menuService.menuItem('test', IAdding, '', 'item2', None)
    >>> menuService.menu('zope.app.container.add', '')
    >>> menuService.menuItem('zope.app.container.add', IAdding, '', 'item3',
    ...                      None, extra={'factory': 'f1'})
    >>> menuService.menuItem('zope.app.container.add', IAdding, '', 'item4',
    ...                      None, extra={'factory': 'f2'})

    >>> class F1:
    ...     pass

    >>> class F2:
    ...     pass

    >>> def pre(container, name, object):
    ...     if not isinstance(object, F1):
    ...         raise zope.interface.Invalid()
    >>> def prefactory(container, name, factory):
    ...     if factory._callable is not F1:
    ...         raise zope.interface.Invalid()
    >>> pre.factory = prefactory


    >>> class IContainer(zope.interface.Interface):
    ...     def __setitem__(name, object):
    ...         pass
    ...     __setitem__.precondition = pre


    >>> class Container:
    ...     zope.interface.implements(IContainer)

    >>> from zope.component.factory import Factory
    >>> ztapi.provideUtility(IFactory, Factory(F1), 'f1')
    >>> ztapi.provideUtility(IFactory, Factory(F2), 'f2')

    >>> from zope.app.container.browser.adding import Adding
    >>> adding = Adding(Container(), TestRequest())
    >>> items = adding.addingInfo()
    >>> len(items)
    1
    >>> items[0]['title']
    'item3'
    
    >>> adding.menu_id = 'test'
    >>> items = adding.addingInfo()
    >>> len(items)
    3
    >>> items[0]['title']
    'item1'
    >>> items[1]['title']
    'item2'
    >>> items[2]['title']
    'item3'
    >>> tearDown()    
    """

def test_renderAddButton():
    """
    Test for renderAddButton in adding.py 
    
    >>> setUp()
    >>> from zope.app.container.browser.adding import Adding
    >>> from zope.app.container.interfaces import IContainerNamesContainer

    Class implements IContainerNamesContainer
    
    >>> class FakeContainer:
    ...    zope.interface.implements(IContainerNamesContainer)

    renderAddButton returns only 'Add' button if the class imlement
    IContainerNamesContainer
    
    >>> adding = Adding(FakeContainer(),TestRequest())
    >>> adding.renderAddButton()
    u" <input type='submit' name='UPDATE_SUBMIT' value='Add'>"

    Fake class without IContainerNamesContainer
    
    >>> class Fake:
    ...    pass

    renderAddButton returns only 'Add' and 'inputbox' if the class
    doest imlement IContainerNamesContainer

    >>> adding = Adding(Fake(),TestRequest())
    >>> adding.renderAddButton()
    u"&nbsp;&nbsp;<input type='submit' name='UPDATE_SUBMIT' value='Add'>&nbsp;&nbsp;<b>Object Name:</b>&nbsp;<input type='text' name='add_input_name' value=''>"
    >>> adding.contentName='myname'
    >>> adding.renderAddButton()
    u"&nbsp;&nbsp;<input type='submit' name='UPDATE_SUBMIT' value='Add'>&nbsp;&nbsp;<b>Object Name:</b>&nbsp;<input type='text' name='add_input_name' value='myname'>"
    >>> adding = Adding(Fake(),TestRequest())     

    To check request variable

    >>> from zope.app.container.interfaces import IContainer
    >>> from zope.app.publisher.browser import BrowserView

    >>> class MyContainer:
    ...    zope.interface.implements(INameChooser, IContainer)
    ...    def chooseName(self, name, object):
    ...        return "foo"
    ...    def checkName(self, name, object):
    ...        return "foo"
    ...    def __setitem__(self, name, object):
    ...        setattr(self, name, object)
    ...        self.name=name
    ...    def __getitem__(self, key):
    ...        return self

    >>> request = TestRequest()
    >>> request.form.update({'add_input_name': 'reqname'})
    >>> mycontainer = MyContainer()
    >>> adding = Adding(mycontainer, request)
    >>> o = object()
    >>> add_obj = adding.add(o)
    >>> add_obj.name
    'reqname'
    >>> mycontainer.reqname is o
    True
    >>> tearDown()

    """

def test_chooseName():
    """If user don't enter name, pick one
    
    >>> class MyContainer:
    ...    zope.interface.implements(INameChooser, IContainer)
    ...    def chooseName(self, name, object):
    ...        return 'pickone'
    ...    def checkName(self, name, object):
    ...        pass
    ...    def __setitem__(self, name, object):
    ...        setattr(self, name, object)
    ...        self.name = name
    ...    def __getitem__(self, key):
    ...        return getattr(self, key)

    >>> request = TestRequest()
    >>> mycontainer = MyContainer()
    >>> adding = Adding(mycontainer, request)
    >>> o = object()
    >>> add_obj = adding.add(o)
    >>> mycontainer.name
    'pickone'
    >>> add_obj is o
    True
    """
    


def test_SingleMenuItem_and_CustomAddView_NonICNC():
    """
    This tests the condition if the content has Custom Add views and
    the container contains only a single content object
    
    >>> setUp()
    >>> serviceService = zapi.getService(None, zapi.servicenames.Services)
    >>> from zope.app.publisher.interfaces.browser import IBrowserMenuService
    >>> serviceService.defineService(zapi.servicenames.BrowserMenu,
    ...                              IBrowserMenuService)
    >>> from zope.app.publisher.browser.globalbrowsermenuservice """ \
                             """import globalBrowserMenuService
    >>> serviceService.provideService(zapi.servicenames.BrowserMenu,
    ...                               globalBrowserMenuService)

    >>> menuService = zapi.getService(None, zapi.servicenames.BrowserMenu)
    >>> menuService.menu('zope.app.container.add', '')
    >>> menuService.menuItem('zope.app.container.add', IAdding, '', 'item3',
    ...                      None, extra={'factory': 'f1'})

    >>> class F1:
    ...     pass

    >>> class F2:
    ...     pass

    >>> def pre(container, name, object):
    ...     if not isinstance(object, F1):
    ...         raise zope.interface.Invalid()
    >>> def prefactory(container, name, factory):
    ...     if factory._callable is not F1:
    ...         raise zope.interface.Invalid()
    >>> pre.factory = prefactory


    >>> class IContainer(zope.interface.Interface):
    ...     def __setitem__(name, object):
    ...         pass
    ...     __setitem__.precondition = pre


    >>> class Container:
    ...     zope.interface.implements(IContainer)

    >>> from zope.component.factory import Factory
    >>> ztapi.provideUtility(IFactory, Factory(F1), 'f1')
    >>> ztapi.provideUtility(IFactory, Factory(F2), 'f2')

    >>> from zope.app.container.browser.adding import Adding
    >>> adding = Adding(Container(), TestRequest())
    >>> items = adding.addingInfo()
    >>> len(items)
    1

    isSingleMenuItem returns True if there is only one content class
    inside the Container
    
    >>> adding.isSingleMenuItem()
    True

    hasCustomAddView will return False as the content does not have
    a custom Add View
    
    >>> adding.hasCustomAddView()
    True
    
    >>> tearDown()    
    """

def test_SingleMenuItem_and_NoCustomAddView_NonICNC():
    """
    
    This function checks the case where there is a single content object
    and there is non custom add view . Also the container does not
    implement IContainerNamesContainer
    
    >>> setUp()
    >>> serviceService = zapi.getService(None, zapi.servicenames.Services)
    >>> from zope.app.publisher.interfaces.browser import IBrowserMenuService
    >>> serviceService.defineService(zapi.servicenames.BrowserMenu,
    ...                              IBrowserMenuService)
    >>> from zope.app.publisher.browser.globalbrowsermenuservice """ \
                             """import globalBrowserMenuService
    >>> serviceService.provideService(zapi.servicenames.BrowserMenu,
    ...                               globalBrowserMenuService)

    >>> menuService = zapi.getService(None, zapi.servicenames.BrowserMenu)
    >>> menuService.menu('zope.app.container.add', '')
    >>> menuService.menuItem('zope.app.container.add', None, '', 'item3',
    ...                      None, extra={'factory': ''})
    >>> class F1:
    ...     pass

    >>> class F2:
    ...     pass

    >>> def pre(container, name, object):
    ...     if not isinstance(object, F1):
    ...         raise zope.interface.Invalid()
    >>> def prefactory(container, name, factory):
    ...     if factory._callable is not F1:
    ...         raise zope.interface.Invalid()
    >>> pre.factory = prefactory


    >>> class IContainer(zope.interface.Interface):
    ...     def __setitem__(name, object):
    ...         pass
    ...     __setitem__.precondition = pre


    >>> class Container:
    ...     zope.interface.implements(IContainer)

    >>> from zope.component.factory import Factory
    >>> ztapi.provideUtility(IFactory, Factory(F1), 'f1')
    >>> ztapi.provideUtility(IFactory, Factory(F2), 'f2')
    
    >>> from zope.app.container.browser.adding import Adding
    >>> adding = Adding(Container(), TestRequest())
    >>> items = adding.addingInfo()
    >>> len(items)
    1

    The isSingleMenuItem will return True if there is one single content
    that can be added inside the Container
    
    >>> adding.isSingleMenuItem()
    True

    hasCustomAddView will return False as the content does not have
    a custom Add View

    >>> adding.hasCustomAddView()
    False
    
    >>> tearDown()
    """

def test_isSingleMenuItem_with_ICNC():
    """
    This test checks for whether there is a single content that can be added
    and the container uses IContainerNamesContaienr

    
    >>> setUp()
    >>> serviceService = zapi.getService(None, zapi.servicenames.Services)
    >>> from zope.app.publisher.interfaces.browser import IBrowserMenuService
    >>> serviceService.defineService(zapi.servicenames.BrowserMenu,
    ...                              IBrowserMenuService)
    >>> from zope.app.publisher.browser.globalbrowsermenuservice \
                      import globalBrowserMenuService
    >>> serviceService.provideService(zapi.servicenames.BrowserMenu,
    ...                               globalBrowserMenuService)
    
    >>> menuService = zapi.getService(None, zapi.servicenames.BrowserMenu)
    >>> menuService.menu('zope.app.container.add', '')
    >>> menuService.menuItem('zope.app.container.add', None, '', 'item3',
    ...                      None, extra={'factory': ''})
    
    >>> class F1:
    ...     pass

    >>> class F2:
    ...     pass

    >>> def pre(container, name, object):
    ...     if not isinstance(object, F1):
    ...         raise zope.interface.Invalid()
    >>> def prefactory(container, name, factory):
    ...     if factory._callable is not F1:
    ...         raise zope.interface.Invalid()
    >>> pre.factory = prefactory


    >>> class IContainer(zope.interface.Interface):
    ...     def __setitem__(name, object):
    ...         pass
    ...     __setitem__.precondition = pre


    >>> class Container:
    ...     zope.interface.implements(IContainer, IContainerNamesContainer)

    >>> from zope.app.container.browser.adding import Adding
    >>> adding = Adding(Container(), TestRequest())
    >>> items = adding.addingInfo()
    >>> len(items)
    1
    >>> adding.isSingleMenuItem()
    True
    >>> adding.hasCustomAddView()
    False
    
    >>> tearDown()
    
    """

def test_suite():
    return unittest.TestSuite((
        unittest.makeSuite(Test),
        DocTestSuite(),
        ))

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



=== Added File Zope3/src/zope/app/container/browser/tests/test_contents.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 Container Contents

$Id: test_contents.py,v 1.1 2004/03/14 02:17:03 srichter Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from zope.app.tests import ztapi
from zope.app.site.tests.placefulsetup import PlacefulSetup

from zope.app.copypastemove.interfaces import IObjectMover

from zope.app.traversing import traverse
from zope.app.copypastemove.interfaces import IObjectMover, IObjectCopier

from zope.app.copypastemove import ObjectMover, ObjectCopier

from zope.app.container.interfaces import IObjectRemovedEvent
from zope.interface import Interface, implements

from zope.app.copypastemove.interfaces import IPrincipalClipboard
from zope.app.copypastemove import PrincipalClipboard
from zope.component import getServiceManager
from zope.app.principalannotation import PrincipalAnnotationService
from zope.app.principalannotation.interfaces import IPrincipalAnnotationService
from zope.app.annotation.interfaces import IAnnotations
from zope.app.container.contained import contained

class BaseTestContentsBrowserView(PlacefulSetup):
    """Base class for testing browser contents.

    Subclasses need to define a method, '_TestView__newContext', that
    takes no arguments and that returns a new empty test view context.

    Subclasses need to define a method, '_TestView__newView', that
    takes a context object and that returns a new test view.
    """

    def setUp(self):
        PlacefulSetup.setUp(self)
        PlacefulSetup.buildFolders(self)
        
        ztapi.provideAdapter(None, IObjectCopier, ObjectCopier)
        ztapi.provideAdapter(None, IObjectMover, ObjectMover)

        ztapi.provideAdapter(IAnnotations, IPrincipalClipboard,
                             PrincipalClipboard)
        root_sm = getServiceManager(None)
        svc = PrincipalAnnotationService()
        root_sm.defineService("PrincipalAnnotation", \
            IPrincipalAnnotationService)
        root_sm.provideService("PrincipalAnnotation", svc)

    def testInfo(self):
        # Do we get the correct information back from ContainerContents?
        container = self._TestView__newContext()
        subcontainer = self._TestView__newContext()
        container['subcontainer'] = subcontainer
        document = Document()
        container['document'] = document

        fc = self._TestView__newView(container)
        info_list = fc.listContentInfo()

        self.assertEquals(len(info_list), 2)

        ids = map(lambda x: x['id'], info_list)
        self.assert_('subcontainer' in ids)

        objects = map(lambda x: x['object'], info_list)
        self.assert_(subcontainer in objects)

        urls = map(lambda x: x['url'], info_list)
        self.assert_('subcontainer' in urls)

        self.failIf(filter(None, map(lambda x: x['icon'], info_list)))

    def testInfoWDublinCore(self):
        container = self._TestView__newContext()
        document = Document()
        container['document'] = document

        from datetime import datetime
        from zope.app.dublincore.interfaces import IZopeDublinCore
        class FauxDCAdapter:
            implements(IZopeDublinCore)

            def __init__(self, context):
                pass
            title = 'faux title'
            size = 1024
            created = datetime(2001, 1, 1, 1, 1, 1)
            modified = datetime(2002, 2, 2, 2, 2, 2)

        ztapi.provideAdapter(IDocument, IZopeDublinCore, FauxDCAdapter)

        fc = self._TestView__newView(container)
        info = fc.listContentInfo()[0]

        self.assertEqual(info['id'], 'document')
        self.assertEqual(info['url'], 'document')
        self.assertEqual(info['object'], document)
        self.assertEqual(info['title'], 'faux title')
        self.assertEqual(info['created'], '01/01/01 01:01')
        self.assertEqual(info['modified'], '02/02/02 02:02')

    def testRemove(self):
        container = self._TestView__newContext()
        subcontainer = self._TestView__newContext()
        container['subcontainer'] = subcontainer
        document = Document()
        container['document'] = document
        document2 = Document()
        container['document2'] = document2

        fc = self._TestView__newView(container)

        fc.request.form.update({'ids': ['document2']})

        fc.removeObjects()

        info_list = fc.listContentInfo()

        self.assertEquals(len(info_list), 2)

        ids = map(lambda x: x['id'], info_list)
        self.assert_('subcontainer' in ids)

        objects = map(lambda x: x['object'], info_list)
        self.assert_(subcontainer in objects)

        urls = map(lambda x: x['url'], info_list)
        self.assert_('subcontainer' in urls)

class IDocument(Interface):
    pass

class Document:
    implements(IDocument)


class Principal:
    
    id = 'bob'


class TestCutCopyPaste(PlacefulSetup, TestCase):

    def setUp(self):
        PlacefulSetup.setUp(self)
        PlacefulSetup.buildFolders(self)
        ztapi.provideAdapter(None, IObjectCopier, ObjectCopier)
        ztapi.provideAdapter(None, IObjectMover, ObjectMover)

        ztapi.provideAdapter(IAnnotations, IPrincipalClipboard,
                             PrincipalClipboard)
        root_sm = getServiceManager(None)
        svc = PrincipalAnnotationService()
        root_sm.defineService("PrincipalAnnotation", \
            IPrincipalAnnotationService)
        root_sm.provideService("PrincipalAnnotation", svc)

    def testRename(self):
        container = traverse(self.rootFolder, 'folder1')
        fc = self._TestView__newView(container)
        ids=['document1', 'document2']
        for id in ids:
            document = Document()
            container[id] = document
        fc.request.form.update({'rename_ids': ids,
                                'new_value': ['document1_1', 'document2_2']
                                })
        fc.renameObjects()
        self.failIf('document1_1' not in container)
        self.failIf('document1' in container)

    def testCopyPaste(self):
        container = traverse(self.rootFolder, 'folder1')
        fc = self._TestView__newView(container)
        ids=['document1', 'document2']
        for id in ids:
            document = Document()
            container[id] = document

        fc.request.form['ids'] = ids
        fc.copyObjects()
        fc.pasteObjects()
        self.failIf('document1' not in container)
        self.failIf('document2' not in container)
        self.failIf('document1-2' not in container)
        self.failIf('document2-2' not in container)

    def testCopyFolder(self):
        container = traverse(self.rootFolder, 'folder1')
        fc = self._TestView__newView(container)
        ids = ['folder1_1']
        fc.request.form['ids'] = ids
        fc.copyObjects()
        fc.pasteObjects()
        self.failIf('folder1_1' not in container)
        self.failIf('folder1_1-2' not in container)

    def testCopyFolder2(self):
        container = traverse(self.rootFolder, '/folder1/folder1_1')
        fc = self._TestView__newView(container)
        ids = ['folder1_1_1']
        fc.request.form['ids'] = ids
        fc.copyObjects()
        fc.pasteObjects()
        self.failIf('folder1_1_1' not in container)
        self.failIf('folder1_1_1-2' not in container)

    def testCopyFolder3(self):
        container = traverse(self.rootFolder, '/folder1/folder1_1')
        target = traverse(self.rootFolder, '/folder2/folder2_1')
        fc = self._TestView__newView(container)
        tg = self._TestView__newView(target)
        ids = ['folder1_1_1']
        fc.request.form['ids'] = ids
        fc.copyObjects()
        tg.pasteObjects()
        self.failIf('folder1_1_1' not in container)
        self.failIf('folder1_1_1' not in target)

    def testCutPaste(self):
        container = traverse(self.rootFolder, 'folder1')
        fc = self._TestView__newView(container)
        ids=['document1', 'document2']
        for id in ids:
            document = Document()
            container[id] = document
        fc.request.form['ids'] = ids
        fc.cutObjects()
        fc.pasteObjects()
        self.failIf('document1' not in container)
        self.failIf('document2' not in container)

    def testCutFolder(self):
        container = traverse(self.rootFolder, 'folder1')
        fc = self._TestView__newView(container)
        ids = ['folder1_1']
        fc.request.form['ids'] = ids
        fc.cutObjects()
        fc.pasteObjects()
        self.failIf('folder1_1' not in container)

    def testCutFolder2(self):
        container = traverse(self.rootFolder, '/folder1/folder1_1')
        fc = self._TestView__newView(container)
        ids = ['folder1_1_1']
        fc.request.form['ids'] = ids
        fc.cutObjects()
        fc.pasteObjects()
        self.failIf('folder1_1_1' not in container)

    def testCutFolder3(self):
        container = traverse(self.rootFolder, '/folder1/folder1_1')
        target = traverse(self.rootFolder, '/folder2/folder2_1')
        fc = self._TestView__newView(container)
        tg = self._TestView__newView(target)
        ids = ['folder1_1_1']
        fc.request.form['ids'] = ids
        fc.cutObjects()
        tg.pasteObjects()
        self.failIf('folder1_1_1' in container)
        self.failIf('folder1_1_1' not in target)

    def _TestView__newView(self, container):
        from zope.app.container.browser.contents import Contents
        from zope.publisher.browser import TestRequest
        request = TestRequest()
        request.setUser(Principal())
        return Contents(container, request)

class Test(BaseTestContentsBrowserView, TestCase):

    def _TestView__newContext(self):
        from zope.app.container.sample import SampleContainer
        from zope.app.folder import rootFolder
        root = rootFolder()
        container = SampleContainer()
        return contained(container, root, 'sample')

    def _TestView__newView(self, container):
        from zope.app.container.browser.contents import Contents
        from zope.publisher.browser import TestRequest
        request = TestRequest()
        request.setUser(Principal())
        return Contents(container, request)

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

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


=== Added File Zope3/src/zope/app/container/browser/tests/test_directive.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.
#
##############################################################################

import re
import pprint
import cStringIO

import unittest
from zope.interface import Interface
from zope.testing.doctestunit import DocTestSuite
from zope.app.container.browser.metaconfigure import containerViews

atre = re.compile(' at [0-9a-fA-Fx]+')

class Context:
    actions = ()
    
    def action(self, discriminator, callable, args):
        self.actions += ((discriminator, callable, args), )

    def __repr__(self):
        stream = cStringIO.StringIO()
        pprinter = pprint.PrettyPrinter(stream=stream, width=60)
        pprinter.pprint(self.actions)
        r = stream.getvalue()
        return (''.join(atre.split(r))).strip()

class I(Interface):
    pass

def test_containerViews():
    """
    >>> context = Context()
    >>> containerViews(context, for_=I, contents='zope.ManageContent',
    ...                add='zope.ManageContent', index='zope.View')
    >>> context
    ((('browser:menuItem',
       'zmi_views',
       <InterfaceClass zope.app.container.browser.tests.test_directive.I>,
       u'Contents'),
      <bound method GlobalBrowserMenuService.menuItem of <zope.app.publisher.browser.globalbrowsermenuservice.GlobalBrowserMenuService object>>,
      ('zmi_views',
       <InterfaceClass zope.app.container.browser.tests.test_directive.I>,
       '@@contents.html',
       u'Contents',
       '',
       None,
       'zope.ManageContent',
       None)),
     (None,
      <function checkPermission>,
      (None, 'zope.ManageContent')),
     (None,
      <function provideInterface>,
      (None,
       <InterfaceClass zope.app.container.browser.tests.test_directive.I>)),
     (('view',
       <InterfaceClass zope.app.container.browser.tests.test_directive.I>,
       'contents.html',
       <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>,
       'default'),
      <function handler>,
      ('Presentation',
       'provideView',
       <InterfaceClass zope.app.container.browser.tests.test_directive.I>,
       'contents.html',
       <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>,
       [<class 'zope.app.publisher.browser.viewmeta.Contents'>],
       'default')),
     (None,
      <function _handle_usage_from_menu>,
      (<class 'zope.app.publisher.browser.viewmeta.Contents'>,
       'zmi_views')),
     (None,
      <function checkPermission>,
      (None, 'zope.View')),
     (None,
      <function provideInterface>,
      (None,
       <InterfaceClass zope.app.container.browser.tests.test_directive.I>)),
     (('view',
       <InterfaceClass zope.app.container.browser.tests.test_directive.I>,
       'index.html',
       <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>,
       'default'),
      <function handler>,
      ('Presentation',
       'provideView',
       <InterfaceClass zope.app.container.browser.tests.test_directive.I>,
       'index.html',
       <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>,
       [<class 'zope.app.publisher.browser.viewmeta.Contents'>],
       'default')),
     (('browser:menuItem',
       'zmi_actions',
       <InterfaceClass zope.app.container.browser.tests.test_directive.I>,
       u'Add'),
      <bound method GlobalBrowserMenuService.menuItem of <zope.app.publisher.browser.globalbrowsermenuservice.GlobalBrowserMenuService object>>,
      ('zmi_actions',
       <InterfaceClass zope.app.container.browser.tests.test_directive.I>,
       '@@+',
       u'Add',
       '',
       None,
       'zope.ManageContent',
       None)),
     (None,
      <function checkPermission>,
      (None, 'zope.ManageContent')),
     (None,
      <function provideInterface>,
      (None,
       <InterfaceClass zope.app.container.browser.tests.test_directive.I>)),
     (None,
      <function provideInterface>,
      ('zope.interface.Interface',
       <InterfaceClass zope.interface.Interface>)),
     (('view',
       <InterfaceClass zope.app.container.browser.tests.test_directive.I>,
       '+',
       <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>,
       'default',
       <InterfaceClass zope.interface.Interface>),
      <function handler>,
      ('Presentation',
       'provideView',
       <InterfaceClass zope.app.container.browser.tests.test_directive.I>,
       '+',
       <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>,
       [<class 'zope.app.publisher.browser.viewmeta.+'>],
       'default',
       <InterfaceClass zope.interface.Interface>)))

    """
    
       
def test_suite():
    return unittest.TestSuite((
        DocTestSuite(),
        ))

if __name__ == '__main__':
    unittest.main()




More information about the Zope3-Checkins mailing list