Back to index

plone3  3.1.7
dummy.py
Go to the documentation of this file.
00001 #
00002 # Helper objects for the Plone test suite
00003 #
00004 
00005 # $Id: dummy.py 19550 2008-02-26 06:25:58Z alecm $
00006 
00007 import os
00008 
00009 from zope.interface import Interface, implements, implementer
00010 from zope.interface import Interface
00011 
00012 from Products.CMFPlone.interfaces import INonStructuralFolder
00013 from Products.CMFPlone.interfaces import IWorkflowChain
00014 
00015 from ComputedAttribute import ComputedAttribute
00016 from OFS.SimpleItem import SimpleItem
00017 from OFS.Folder import Folder as SimpleFolder
00018 from ZPublisher.HTTPRequest import FileUpload
00019 
00020 from Globals import package_home
00021 from Products.CMFPlone.tests import GLOBALS
00022 PACKAGE_HOME = package_home(GLOBALS)
00023 
00024 TEXT = 'file data'
00025 UTEXT = u'file data'
00026 GIF = open(os.path.join(PACKAGE_HOME, os.pardir, 'tool.gif')).read()
00027 
00028 
00029 class Dummy:
00030     '''General purpose dummy object'''
00031 
00032     def __init__(self, **kw):
00033         self.__dict__.update(kw)
00034 
00035 
00036 class Item(SimpleItem):
00037     '''Dummy Item
00038        First-class Zope object. Can be _setObject'ed.
00039     '''
00040 
00041     id = 'dummy'
00042     meta_type = 'Dummy Item'
00043 
00044     def __init__(self, id=None, title=None, **kw):
00045         self.__dict__.update(kw)
00046         if id is not None:
00047             self.id = id
00048         if title is not None:
00049             self.title = title
00050 
00051 class SizedItem(Item):
00052     '''Item maintaining a size'''
00053 
00054     size = 0
00055 
00056     def set_size(self, size):
00057         self.size = size
00058 
00059     def get_size(self):
00060         return self.size
00061 
00062 
00063 class FieldStorage(object):
00064     def __init__(self, file, filename='testfile', headers=None):
00065         self.file = file
00066         if headers is None:
00067             headers = {}
00068         self.headers = headers
00069         self.filename = filename
00070 
00071 
00072 class File(FileUpload):
00073     '''Dummy upload object
00074        Used to fake uploaded files.
00075     '''
00076 
00077     __allow_access_to_unprotected_subobjects__ = 1
00078     filename = 'dummy.txt'
00079     data = TEXT
00080     headers = {}
00081 
00082     def __init__(self, filename=None, data=None, headers=None):
00083         if filename is not None:
00084             self.filename = filename
00085         if data is not None:
00086             self.data = data
00087         if headers is not None:
00088             self.headers = headers
00089 
00090     def seek(self, *args): pass
00091     def tell(self, *args): return 1
00092     def read(self, *args): return self.data
00093 
00094 
00095 class Image(File):
00096     '''Dummy image upload object
00097        Contains valid image data by default.
00098     '''
00099 
00100     filename = 'dummy.gif'
00101     data = GIF
00102 
00103 
00104 class Folder(SimpleFolder):
00105     '''Dummy Folder
00106        First-class Zope object. Can be _setObject'ed.
00107     '''
00108 
00109     id = 'dummy_folder'
00110     meta_type = 'Dummy Folder'
00111 
00112     def __init__(self, id=None, title=None, **kw):
00113         self.__dict__.update(kw)
00114         if id is not None:
00115             self.id = id
00116         if title is not None:
00117             self.title = title
00118 
00119 
00120 class NonStructuralFolder(Folder):
00121     '''Folder implementing the INonStructuralFolder interface'''
00122     implements(INonStructuralFolder)
00123 
00124 
00125 class Error(Exception):
00126     '''Dummy exception'''
00127 
00128 
00129 class Raiser(SimpleItem):
00130     '''Raises the stored exception when called'''
00131 
00132     exception = Error
00133 
00134     def __init__(self, exception=None):
00135         if exception is not None:
00136             self.exception = exception
00137 
00138     def __call__(self, *args, **kw):
00139         raise self.exception
00140 
00141 
00142 class DefaultPage(Item):
00143     '''Helper for browserDefault tests'''
00144 
00145     def __init__(self, default=['test'], keys=['index_html']):
00146         self.keys = keys
00147         self.isPrincipiaFolderish = 1 # fake a folder
00148         self.set_default(default)
00149 
00150     def set_default(self, default, has_key=0):
00151         self.default_page = default
00152         if has_key:
00153             if type(default) == type(''):
00154                 self.keys = [default]
00155             self.keys = default
00156 
00157     def has_key(self, key):
00158         return key in self.keys
00159 
00160 
00161 class ImageComputedProps(Item):
00162     '''Helper for testing the imagePatch interaction with
00163     ComputedAttributes (which are used in Archetypes ImageField).
00164     '''
00165 
00166     def get_title(self):
00167         return getattr(self, '_title', '')
00168 
00169     title = ComputedAttribute(get_title, 1)
00170 
00171     def get_alt(self):
00172         return getattr(self, '_alt', '')
00173 
00174     alt = ComputedAttribute(get_alt, 1)
00175 
00176     def get_longdesc(self):
00177         return getattr(self, '_longdesc', '')
00178 
00179     longdesc = ComputedAttribute(get_longdesc, 1)
00180 
00181 
00182 class IDummyUtility(Interface):
00183     pass
00184 
00185 
00186 class DummyUtility(SimpleItem):
00187     implements(IDummyUtility)
00188 
00189 
00190 class ICantBeDeleted(Interface):
00191     """A marker indicating that an object can't be deleted"""
00192 
00193 
00194 def disallow_delete_handler(obj, event):
00195     obj.delete_attempted = True
00196     raise Exception, "You can't delete this!"
00197 
00198 
00199 class DummyContent(Dummy):
00200     """Dummy DynamicType object"""
00201 
00202     def getPortalTypeName(self):
00203         return getattr(self, 'portal_type')
00204 
00205 class DummyWorkflowTool(object):
00206     """A dummy workflow tool for testing adaptation based workflow"""
00207 
00208     def __init__(self, id='portal_workflow'):
00209         self._chains_by_type = {}
00210 
00211     def setChainForPortalTypes(self, types, chain):
00212         for ptype in types:
00213             self._chains_by_type[ptype] = chain
00214 
00215     def getDefaultChainFor(self, context):
00216         return ('Default Workflow',)
00217 
00218 @implementer(IWorkflowChain)
00219 def DummyWorkflowChainAdapter(context, tool):
00220     """A dummy adapter to IWorkflowChain"""
00221     return ('Static Workflow',)