Back to index

plone3  3.1.7
test_plonedrawer.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 """Tests for the drawer support code
00011 
00012 $Id$
00013 """
00014 
00015 import os, sys
00016 if __name__ == '__main__':
00017     execfile(os.path.join(sys.path[0], 'framework.py'))
00018 
00019 from Products.PloneTestCase import PloneTestCase
00020 from Products.PloneTestCase.ptc import portal_owner
00021 
00022 PloneTestCase.setupPloneSite(products=['kupu'])
00023 
00024 from AccessControl.SecurityManagement import newSecurityManager
00025 try:
00026     from Products.ATContentTypes.lib import constraintypes
00027 except ImportError:
00028     constraintypes = None
00029 
00030 from Products.kupu.plone.tests import TestContent
00031 
00032 RESOURCES = dict(
00033     linkable = ('Document', 'Image', 'File', 'Folder'),
00034     mediaobject = ('Image',),
00035     collection = ('Folder',),
00036     containsanchors = ('Document',),
00037     )
00038 
00039 # Type names vary according to the version of Plone and/or
00040 # ATContentTypes. Map the new names to the old ones here, and
00041 # turn it into an identity mapping later if we can.
00042 TypeMapping = {
00043     'Document': 'ATDocument',
00044     'Image': 'ATImage',
00045     'Link': 'ATLink',
00046     'Folder': 'ATFolder',
00047     'File': 'ATFile',
00048     'News Item': 'ATNewsItem',
00049     'Event': 'ATEvent',
00050 }
00051 def MapType(typename):
00052     return TypeMapping[typename]
00053 
00054 class TestPloneDrawer(PloneTestCase.PloneTestCase):
00055     """Test the implementation of the PloneDrawer class"""
00056 
00057     def afterSetUp(self):
00058         portal = self.portal
00059         self.setRoles(['Manager',])
00060         self.kupu = portal.kupu_library_tool
00061         typestool = self.portal.portal_types
00062         if not hasattr(typestool, 'ATDocument'):
00063             # Use the type names without the AT prefix
00064             for k in TypeMapping:
00065                 TypeMapping[k] = k
00066 
00067     def loginPortalOwner(self):
00068         '''Use if you need to manipulate the portal itself.'''
00069         uf = self.app.acl_users
00070         user = uf.getUserById(portal_owner).__of__(uf)
00071         newSecurityManager(None, user)
00072 
00073     def create(self, id, metatype='ATDocument', folder=None, **kwds):
00074         '''Create an object in the cms portal'''
00075         if folder is None:
00076             folder = self.portal
00077 
00078         folder.invokeFactory(MapType(metatype), id)
00079         obj = getattr(folder, id)
00080 
00081         if metatype=='Folder' and constraintypes:
00082             obj.setConstrainTypesMode(constraintypes.DISABLED)
00083 
00084         if metatype=='Document':
00085             obj.setTitle('Simple document')
00086             obj.setText('Sample document text')
00087             for k, v in kwds.items():
00088                 field = obj.getField(k)
00089                 mutator = field.getMutator(obj)(v)
00090 
00091             obj.reindexObject()
00092         return obj
00093 
00094     def setup_content(self):
00095         self.setRoles(['Manager',])
00096         self.loginPortalOwner()
00097         f = self.create('folder', 'Folder')
00098 
00099         for id in ('alpha', 'beta'):
00100             self.create(id, 'Document', f, subject=['aspidistra'])
00101         self.create('gamma', 'Document', f)
00102 
00103         sub1 = self.create('sub1', 'Folder', f)
00104         sub1.setSubject(['aspidistra'])
00105         sub1.reindexObject()
00106         sub2 = self.create('sub2', 'Folder', f)
00107         self.create('delta', 'Folder', sub2)
00108 
00109         portal = self.portal
00110         tool = self.portal.kupu_library_tool
00111         types = tool.zmi_get_resourcetypes()
00112         #tool.deleteResource([ t.name for t in types])
00113         for k,v in RESOURCES.items():
00114             tool.addResourceType(k, [MapType(t) for t in v])
00115 
00116     def test_FolderContents1(self):
00117         def assertKey(item, key, expected):
00118             actual =  item.get(key, None)
00119             self.assertEquals(expected, actual,
00120                 "Item %s.%s expected %s got %s" % (UIDS[item['id']], key, expected, actual))
00121 
00122         self.setup_content()
00123         portal = self.portal
00124         folder = portal['folder']
00125         UIDS = {}
00126         for id in folder.objectIds():
00127             UIDS[folder[id].UID()] = id
00128 
00129         # Allows browse: docs + folders
00130         items = self.kupu.getFolderItems(folder, resource_type='TestContent.multiRef')
00131         ids = [UIDS[t['id']] for t in items]
00132         self.assertEquals(['alpha', 'beta', 'gamma', 'sub1', 'sub2'], ids)
00133 
00134         # No browse: docs only.
00135         items = self.kupu.getFolderItems(folder, resource_type='TestContent.multiRef2')
00136         ids = [UIDS[t['id']] for t in items]
00137         self.assertEquals(['alpha', 'beta', 'gamma'], ids)
00138 
00139     def test_FolderWithKeywords(self):
00140         def assertKey(item, key, expected):
00141             actual =  item.get(key, None)
00142             self.assertEquals(expected, actual,
00143                 "Item %s.%s expected %s got %s" % (UIDS[item['id']], key, expected, actual))
00144 
00145         self.setup_content()
00146         portal = self.portal
00147         folder = portal['folder']
00148         UIDS = {}
00149         for id in folder.objectIds():
00150             UIDS[folder[id].UID()] = id
00151 
00152         # Query restriction: keyword match only, but should include
00153         # both folders.
00154         items = self.kupu.getFolderItems(folder, resource_type='TestContent.multiRef3')
00155         ids = [UIDS[t['id']] for t in items]
00156         self.assertEquals(['alpha', 'beta', 'sub1', 'sub2'], ids)
00157         # Now check that only the first of the two folders is linkable...
00158         for t in items[:-1]:
00159             assertKey(t, 'linkable', True)
00160         assertKey(items[-1], 'linkable', None)
00161 
00162         for t in items[:-2]:
00163             assertKey(t, 'anchor', True)
00164         assertKey(items[-2], 'anchor', False)
00165         assertKey(items[-1], 'anchor', False)
00166 
00167         # Both folders should contain a src element.
00168         self.assert_(items[-1]['src'])
00169         self.assert_(items[-2]['src'])
00170 
00171     def test_NoBrowseWithKeywords(self):
00172         def assertKey(item, key, expected):
00173             actual =  item.get(key, None)
00174             self.assertEquals(expected, actual,
00175                 "Item %s.%s expected %s got %s" % (UIDS[item['id']], key, expected, actual))
00176 
00177         self.setup_content()
00178         portal = self.portal
00179         folder = portal['folder']
00180         UIDS = {}
00181         for id in folder.objectIds():
00182             UIDS[folder[id].UID()] = id
00183 
00184         # Query restriction: keyword match only, and since we don't
00185         # allow browse only 1 folder (non-browsable)
00186         items = self.kupu.getFolderItems(folder, resource_type='TestContent.multiRef4')
00187         ids = [UIDS[t['id']] for t in items]
00188         self.assertEquals(['alpha', 'beta', 'sub1'], ids)
00189         # Now check that all are linkable and non browsable.
00190         for t in items[:-1]:
00191             assertKey(t, 'linkable', True)
00192             assertKey(t, 'src', None)
00193 
00194     def test_ResourceType(self):
00195         # Some tests that the ResourceType class is roughly working.
00196         kupu = self.kupu
00197         r = kupu.getResourceType('TestContent.multiRef2')
00198         self.assertEquals(r.allow_browse, False)
00199 
00200         r = kupu.getResourceType('linkable')
00201         self.assertEquals(r.allow_browse, True)
00202         
00203 
00204 if __name__ == '__main__':
00205     framework()
00206 else:
00207     # While framework.py provides its own test_suite()
00208     # method the testrunner utility does not.
00209     from unittest import TestSuite, makeSuite
00210     def test_suite():
00211         suite = TestSuite()
00212         suite.addTest(makeSuite(TestPloneDrawer))
00213         return suite