Back to index

plone3  3.1.7
test_baseobject.py
Go to the documentation of this file.
00001 ################################################################################
00002 #
00003 # Copyright (c) 2002-2005, Benjamin Saller <bcsaller@ideasuite.com>, and
00004 #                              the respective authors. All rights reserved.
00005 # For a list of Archetypes contributors see docs/CREDITS.txt.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions are met:
00009 #
00010 # * Redistributions of source code must retain the above copyright notice, this
00011 #   list of conditions and the following disclaimer.
00012 # * Redistributions in binary form must reproduce the above copyright notice,
00013 #   this list of conditions and the following disclaimer in the documentation
00014 #   and/or other materials provided with the distribution.
00015 # * Neither the name of the author nor the names of its contributors may be used
00016 #   to endorse or promote products derived from this software without specific
00017 #   prior written permission.
00018 #
00019 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00020 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00022 # FOR A PARTICULAR PURPOSE.
00023 #
00024 ################################################################################
00025 
00026 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00027 from Products.Archetypes.tests.utils import mkDummyInContext
00028 
00029 from Products.Archetypes import PloneMessageFactory as _
00030 from Products.Archetypes.atapi import *
00031 
00032 class DummyDiscussionTool:
00033     def isDiscussionAllowedFor( self, content ):
00034         return False
00035     def overrideDiscussionFor(self, content, allowDiscussion):
00036         pass
00037 
00038 MULTIPLEFIELD_LIST = DisplayList(
00039     (
00040     ('1', _(u'Option 1 : printemps')),
00041     ('2', unicode('Option 2 : \xc3\xa9t\xc3\xa9', 'utf-8')), # e-acute t e-acute
00042     ('3', u'Option 3 : automne'),
00043     ('4', _(u'option3', default=u'Option 3 : hiver')),
00044     ))
00045 
00046 schema = BaseSchema + Schema((
00047     LinesField(
00048         'MULTIPLEFIELD',
00049         searchable = 1,
00050         vocabulary = MULTIPLEFIELD_LIST,
00051         widget = MultiSelectionWidget(
00052             i18n_domain = 'plone',
00053             ),
00054         ), 
00055     TextField(
00056         'TEXTFIELD',
00057         primary=True,
00058         ),
00059 ))
00060 
00061 class Dummy(BaseContent):
00062    
00063     portal_discussion = DummyDiscussionTool()
00064 
00065     def getCharset(self):
00066          return 'utf-8'
00067          
00068 class BaseObjectTest(ATSiteTestCase):
00069 
00070     def afterSetUp(self):
00071         ATSiteTestCase.afterSetUp(self)
00072         self._dummy = mkDummyInContext(Dummy, oid='dummy', context=self.portal,
00073                                       schema=schema)
00074     
00075     def test_searchableText(self):
00076         """
00077         Fix bug [ 951955 ] BaseObject/SearchableText and list of Unicode stuffs
00078         """
00079         dummy = self._dummy
00080 
00081         # Set a multiple field
00082         dummy.setMULTIPLEFIELD(['1','2'])
00083         searchable = dummy.SearchableText()
00084 
00085         self.failUnless(isinstance(searchable, basestring))
00086         self.assertEquals(searchable, '1 2 Option 1 : printemps Option 2 : \xc3\xa9t\xc3\xa9')
00087 
00088         dummy.setMULTIPLEFIELD(['3','4'])
00089         searchable = dummy.SearchableText()
00090 
00091         self.assertEquals(searchable, '3 4 Option 3 : automne option3')
00092 
00093     def test_searchableTextUsesIndexMethod(self):
00094         """See http://dev.plone.org/archetypes/ticket/645
00095 
00096         We want SearchableText to use the ``index_method`` attribute
00097         of fields to determine which is the accessor it should use
00098         while gathering values.
00099         """
00100         dummy = self._dummy
00101         
00102         # This is where we left off in the previous test
00103         dummy.setMULTIPLEFIELD(['1','2'])
00104         searchable = dummy.SearchableText()
00105         self.failUnless(searchable.startswith('1 2 Option 1 : printemps'))
00106 
00107         # Now we set another index_method and expect it to be used:
00108         dummy.getField('MULTIPLEFIELD').index_method = 'myMethod'
00109         def myMethod(self):
00110             return "What do you expect of a Dummy?"
00111         Dummy.myMethod = myMethod
00112         searchable = dummy.SearchableText()
00113         self.failUnless(searchable.startswith("What do you expect of a Dummy"))
00114         del Dummy.myMethod
00115         
00116     def test_authenticatedContentType(self):
00117         """See https://dev.plone.org/archetypes/ticket/712
00118         
00119         content_type should not be protected by a security declaration, as
00120         it is usually an attribute. If a security declaration *is* set (in
00121         BaseObject or one of it's base classes) non-anonymous access from
00122         protected code (guarded_getattr) will fail.
00123         
00124         """
00125         from AccessControl.unauthorized import Unauthorized
00126         from AccessControl.Permissions import view
00127         from AccessControl.ZopeGuards import guarded_getattr
00128         
00129         dummy = self._dummy
00130         dummy.manage_permission(view, ('Manager',), False)
00131         # dummy.content_type in a Python Script
00132         self.assertRaises(Unauthorized, guarded_getattr, dummy, 'content_type')
00133         
00134         self.setRoles(('Manager',))
00135         # dummy.content_type in a Python Script
00136         self.assertEqual(guarded_getattr(dummy, 'content_type'), 'text/html')
00137         
00138 
00139 def test_suite():
00140     from unittest import TestSuite, makeSuite
00141     suite = TestSuite()
00142     suite.addTest(makeSuite(BaseObjectTest))
00143     return suite