Back to index

plone3  3.1.7
test_Portal.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Unit / functional tests for a CMFSite.
00014 
00015 $Id: test_Portal.py 76996 2007-06-24 00:18:49Z hannosch $
00016 """
00017 
00018 import unittest
00019 from Testing import ZopeTestCase
00020 
00021 from AccessControl.SecurityManagement import newSecurityManager
00022 from AccessControl.User import UnrestrictedUser
00023 from Acquisition import aq_base
00024 from zope.app.component.hooks import setSite
00025 
00026 from Products.CMFDefault.testing import FunctionalLayer
00027 
00028 
00029 class CMFSiteTests(ZopeTestCase.FunctionalTestCase):
00030 
00031     layer = FunctionalLayer
00032 
00033     def _makeContent( self, site, portal_type, id='document', **kw ):
00034         newSecurityManager(None, UnrestrictedUser('god', '', ['Manager'], ''))
00035 
00036         site.invokeFactory( type_name=portal_type, id=id )
00037         content = getattr( site, id )
00038 
00039         if getattr( aq_base( content ), 'editMetadata', None ) is not None:
00040             content.editMetadata( **kw )
00041 
00042         return content
00043 
00044     def afterSetUp(self):
00045         setSite(self.app.site)
00046 
00047     def test_new( self ):
00048         catalog = self.app.site.portal_catalog
00049 
00050         self.assertEqual(len(catalog), 0)
00051 
00052     def test_MetadataCataloguing( self ):
00053         site = self.app.site
00054         catalog = site.portal_catalog
00055         ttool = site.portal_types
00056         uid_handler = getattr(site, 'portal_uidhandler', None)
00057 
00058         portal_types = [ x for x in ttool.listContentTypes()
00059                            if x not in ( 'Discussion Item'
00060                                        , 'CMF BTree Folder'
00061                                        , 'Folder'
00062                                        , 'Topic'
00063                                        ) ]
00064 
00065         self.assertEqual( len( catalog ), 0 )
00066 
00067         for portal_type in portal_types:
00068 
00069             doc = self._makeContent( site
00070                                    , portal_type=portal_type
00071                                    , title='Foo' )
00072 
00073             # in case of the CMFUid beeing installed this test
00074             # indexes also the site root because the 'Favorite'
00075             # references it by unique id
00076             isUidEnabledFavorite = uid_handler and portal_type == 'Favorite'
00077             if isUidEnabledFavorite:
00078                 self.assertEqual( len( catalog ), 2 )
00079             else:
00080                 self.assertEqual( len( catalog ), 1 )
00081 
00082             # find the right brain
00083             rid = catalog._catalog.paths.keys()[0]
00084             title = _getMetadata( catalog, rid )
00085             if isUidEnabledFavorite and title == 'Portal':
00086                 rid = catalog._catalog.paths.keys()[1]
00087                 title = _getMetadata( catalog, rid )
00088             self.assertEqual( title, 'Foo' )
00089 
00090             doc.editMetadata( title='Bar' )
00091             self.assertEqual( _getMetadata( catalog, rid ), 'Bar' )
00092 
00093             site._delObject( doc.getId() )
00094 
00095             if isUidEnabledFavorite:
00096                 # unindex the site root by hand
00097                 catalog.unindexObject(site)
00098 
00099             self.assertEqual( len( catalog ), 0 )
00100 
00101     def test_DocumentEditCataloguing( self ):
00102         site = self.app.site
00103         catalog = site.portal_catalog
00104 
00105         doc = self._makeContent( site
00106                                , portal_type='Document'
00107                                , title='Foo' )
00108 
00109         rid = catalog._catalog.paths.keys()[0]
00110 
00111         doc.setTitle( 'Bar' )   # doesn't reindex
00112         self.assertEqual( _getMetadata( catalog, rid ), 'Foo' )
00113 
00114         doc.edit( text_format='structured-text'
00115                 , text='Some Text Goes Here\n\n   A paragraph\n   for you.'
00116                 )
00117         self.assertEqual( _getMetadata( catalog, rid ), 'Bar' )
00118 
00119     def test_ImageEditCataloguing( self ):
00120         site = self.app.site
00121         catalog = site.portal_catalog
00122 
00123         doc = self._makeContent( site
00124                                , portal_type='Image'
00125                                , title='Foo' )
00126 
00127         rid = catalog._catalog.paths.keys()[0]
00128 
00129         doc.setTitle( 'Bar' )   # doesn't reindex
00130         self.assertEqual( _getMetadata( catalog, rid ), 'Foo' )
00131 
00132         doc.edit( 'GIF89a' )
00133         self.assertEqual( _getMetadata( catalog, rid ), 'Bar' )
00134 
00135     def test_FileEditCataloguing( self ):
00136         site = self.app.site
00137         catalog = site.portal_catalog
00138 
00139         doc = self._makeContent( site
00140                                , portal_type='File'
00141                                , title='Foo' )
00142 
00143         rid = catalog._catalog.paths.keys()[0]
00144 
00145         doc.setTitle( 'Bar' )   # doesn't reindex
00146         self.assertEqual( _getMetadata( catalog, rid ), 'Foo' )
00147 
00148         doc.edit( '%PDF-1.2\r' )
00149         self.assertEqual( _getMetadata( catalog, rid ), 'Bar' )
00150 
00151     def test_LinkEditCataloguing( self ):
00152         site = self.app.site
00153         catalog = site.portal_catalog
00154 
00155         doc = self._makeContent( site
00156                                , portal_type='Link'
00157                                , title='Foo' )
00158 
00159         rid = catalog._catalog.paths.keys()[0]
00160 
00161         doc.setTitle( 'Bar' )   # doesn't reindex
00162         self.assertEqual( _getMetadata( catalog, rid ), 'Foo' )
00163 
00164         doc.edit( 'http://www.example.com' )
00165         self.assertEqual( _getMetadata( catalog, rid ), 'Bar' )
00166 
00167     def test_NewsItemEditCataloguing( self ):
00168         site = self.app.site
00169         catalog = site.portal_catalog
00170 
00171         doc = self._makeContent( site
00172                                , portal_type='News Item'
00173                                , title='Foo' )
00174 
00175         rid = catalog._catalog.paths.keys()[0]
00176 
00177         doc.setTitle( 'Bar' )   # doesn't reindex
00178         self.assertEqual( _getMetadata( catalog, rid ), 'Foo' )
00179 
00180         doc.edit( '<h1>Extra!</h1>' )
00181         self.assertEqual( _getMetadata( catalog, rid ), 'Bar' )
00182 
00183 
00184 def _getMetadata( catalog, rid, field='Title' ):
00185     md = catalog.getMetadataForRID( rid )
00186     return md[ field ]
00187 
00188 
00189 def test_suite():
00190     return unittest.TestSuite((
00191         unittest.makeSuite(CMFSiteTests),
00192         ))
00193 
00194 if __name__ == '__main__':
00195     from Products.CMFCore.testing import run
00196     run(test_suite())