Back to index

plone3  3.1.7
kuputestcase.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2003-2005 Kupu Contributors. All rights reserved.
00004 #
00005 # This software is distributed under the terms of the Kupu
00006 # License. See LICENSE.txt for license text. For a list of Kupu
00007 # Contributors see CREDITS.txt.
00008 #
00009 ##############################################################################
00010 """Base test class for kupu tests"""
00011 
00012 import os, sys
00013 if __name__ == '__main__':
00014     execfile(os.path.join(sys.path[0], 'framework.py'))
00015 
00016 from Products.PloneTestCase import PloneTestCase
00017 from Products.PloneTestCase.ptc import portal_owner
00018 
00019 PloneTestCase.setupPloneSite(products=['ATContentTypes', 'kupu'])
00020 
00021 from AccessControl.SecurityManagement import newSecurityManager
00022 try:
00023     from Products.ATContentTypes.lib import constraintypes
00024 except:
00025     constraintypes = None
00026 
00027 from os.path import join, abspath, dirname
00028 PREFIX = abspath(dirname(__file__))
00029 
00030 RESOURCES = dict(
00031     linkable = ('Document', 'Image', 'File', 'Folder'),
00032     mediaobject = ('Image',),
00033     collection = ('Folder',),
00034     containsanchors = ('Document',),
00035     )
00036 
00037 # Type names vary according to the version of Plone and/or
00038 # ATContentTypes. Map the new names to the old ones here, and
00039 # turn it into an identity mapping later if we can.
00040 TypeMapping = {
00041     'Document': 'ATDocument',
00042     'Image': 'ATImage',
00043     'Link': 'ATLink',
00044     'Folder': 'ATFolder',
00045     'File': 'ATFile',
00046     'News Item': 'ATNewsItem',
00047     'Event': 'ATEvent',
00048 }
00049 def MapType(typename):
00050     return TypeMapping[typename]
00051 
00052 class KupuTestCase(PloneTestCase.PloneTestCase):
00053     """Base class for Kupu tests"""
00054 
00055     def afterSetUp(self):
00056         portal = self.portal
00057         self.setRoles(['Manager',])
00058         self.kupu = portal.kupu_library_tool
00059         self.kupu.configure_kupu(captioning=True, linkbyuid=True)
00060         typestool = self.portal.portal_types
00061         if not hasattr(typestool, 'ATDocument'):
00062             # Use the type names without the AT prefix
00063             for k in TypeMapping:
00064                 TypeMapping[k] = k
00065 
00066     def loginPortalOwner(self):
00067         '''Use if you need to manipulate the portal itself.'''
00068         uf = self.app.acl_users
00069         user = uf.getUserById(portal_owner).__of__(uf)
00070         newSecurityManager(None, user)
00071 
00072     def create(self, id, metatype='ATDocument', folder=None, **kwds):
00073         '''Create an object in the cms portal'''
00074         if folder is None:
00075             folder = self.portal
00076 
00077         folder.invokeFactory(MapType(metatype), id)
00078         obj = getattr(folder, id)
00079 
00080         if metatype=='Folder' and constraintypes:
00081             obj.setConstrainTypesMode(constraintypes.DISABLED)
00082 
00083         if metatype=='Document':
00084             obj.setTitle('Simple document')
00085             obj.setText('Sample document text')
00086             for k, v in kwds.items():
00087                 field = obj.getField(k)
00088                 mutator = field.getMutator(obj)(v)
00089 
00090             obj.reindexObject()
00091 
00092         assert obj is not None
00093         return obj
00094 
00095     def setup_content(self):
00096         self.setRoles(['Manager',])
00097         self.loginPortalOwner()
00098         f = self.create('folder', 'Folder')
00099 
00100         for id in ('alpha', 'beta'):
00101             self.create(id, 'Document', f, subject=['aspidistra'])
00102         self.create('gamma', 'Image', f)
00103         gamma = f.gamma
00104         gamma.setImage(open(join(PREFIX,'image.jpg'),'rb').read())
00105         gamma.setTitle('Kupu Test Image')
00106         gamma.setDescription('Test image caption')
00107         # The image needs a fixed uid for the transform tests.
00108         f.gamma._setUID('104ede98d4c7c8eaeaa3b984f7395979')
00109 
00110         self.create('umlauts', 'Image', f)
00111         umlauts = f.umlauts
00112         umlauts.setImage(open(join(PREFIX,'image.jpg'),'rb').read())
00113         title, description = open(join(PREFIX, 'umlauts-testdata.txt')).read().strip().split(';')
00114         umlauts.setTitle(title)
00115         umlauts.setDescription(description)
00116         # The image needs a fixed uid for the transform tests.
00117         f.umlauts._setUID('215fef98e5d7c9ebebb4c984f7395979')
00118 
00119         f.reindexObject()
00120 
00121         sub1 = self.create('sub1', 'Folder', f)
00122         sub1.setSubject(['aspidistra'])
00123         sub1.reindexObject()
00124         sub2 = self.create('sub2', 'Folder', f)
00125         self.create('delta', 'Folder', sub2)
00126 
00127         portal = self.portal
00128         tool = self.portal.kupu_library_tool
00129         types = tool.zmi_get_resourcetypes()
00130         #tool.deleteResource([ t.name for t in types])
00131         for k,v in RESOURCES.items():
00132             tool.addResourceType(k, [MapType(t) for t in v])
00133 
00134 __all__ = ['KupuTestCase', 'PloneTestCase', 'portal_owner']