Back to index

plone3  3.1.7
atcttestcase.py
Go to the documentation of this file.
00001 # -*- coding: latin1 -*-
00002 #  ATContentTypes http://plone.org/products/atcontenttypes/
00003 #  Archetypes reimplementation of the CMF core types
00004 #  Copyright (c) 2003-2006 AT Content Types development team
00005 #
00006 #  This program is free software; you can redistribute it and/or modify
00007 #  it under the terms of the GNU General Public License as published by
00008 #  the Free Software Foundation; either version 2 of the License, or
00009 #  (at your option) any later version.
00010 #
00011 #  This program is distributed in the hope that it will be useful,
00012 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 #  GNU General Public License for more details.
00015 #
00016 #  You should have received a copy of the GNU General Public License
00017 #  along with this program; if not, write to the Free Software
00018 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 #
00020 """Common imports and declarations
00021 
00022 common includes a set of basic things that every test needs.
00023 """
00024 
00025 __author__ = 'Christian Heimes <tiran@cheimes.de>'
00026 __docformat__ = 'restructuredtext'
00027 
00028 from Testing import ZopeTestCase
00029 from Products.PloneTestCase import PloneTestCase
00030 from Products.PloneTestCase.setup import portal_name
00031 from Products.PloneTestCase.setup import portal_owner
00032 ZopeTestCase.installProduct('SiteAccess')
00033 PloneTestCase.setupPloneSite(extension_profiles=['Products.CMFPlone:testfixture'])
00034 
00035 import os
00036 import transaction
00037 
00038 from Interface.Verify import verifyObject
00039 from Products.CMFCore.permissions import View
00040 from Products.CMFCore.permissions import ModifyPortalContent
00041 from Products.CMFCore.interfaces.DublinCore import DublinCore as IDublinCore
00042 from Products.CMFCore.interfaces.DublinCore import MutableDublinCore as IMutableDublinCore
00043 from Products.Archetypes.interfaces.base import IBaseContent
00044 from Products.Archetypes.atapi import *
00045 from Products.Archetypes.interfaces.layer import ILayerContainer
00046 from Products.Archetypes.interfaces.referenceable import IReferenceable
00047 from Products.Archetypes.interfaces.templatemixin import ITemplateMixin
00048 from Products.CMFDynamicViewFTI.interface import ISelectableBrowserDefault
00049 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00050 from Products.Archetypes.tests.test_baseschema import BaseSchemaTest
00051 from Products.ATContentTypes.config import HAS_LINGUA_PLONE
00052 from Products.ATContentTypes import permission as ATCTPermissions
00053 from Products.ATContentTypes.interfaces import IATContentType
00054 from Products.ATContentTypes.tests.utils import dcEdit
00055 from Products.ATContentTypes.tests.utils import EmptyValidator
00056 from Products.ATContentTypes.tests.utils import idValidator
00057 from Products.ATContentTypes.tests.utils import FakeRequestSession
00058 from Products.ATContentTypes.tests.utils import DummySessionDataManager
00059 from Products.ATReferenceBrowserWidget.ATReferenceBrowserWidget import ReferenceBrowserWidget
00060 from Products.CMFCore.utils import getToolByName
00061 from Products.CMFPlone.interfaces.Translatable import ITranslatable
00062 
00063 # Z3 imports
00064 from Products.ATContentTypes.interface import IATContentType as Z3IATContentType
00065 from zope.interface.verify import verifyObject as Z3verifyObject
00066 
00067 test_home = os.path.dirname(__file__)
00068 
00069 class ATCTSiteTestCase(PloneTestCase.PloneTestCase):
00070     pass
00071 
00072 class ATCTFunctionalSiteTestCase(PloneTestCase.FunctionalTestCase):
00073     pass
00074 
00075 class ATCTTypeTestCase(ATCTSiteTestCase):
00076     """AT Content Types test
00077 
00078     Tests some basics of a type
00079     """
00080 
00081     klass = None
00082     cmf_klass = None
00083     portal_type = ''
00084     cmf_portal_type = ''
00085     title = ''
00086     meta_type = ''
00087     icon = ''
00088 
00089     def afterSetUp(self):
00090         #self.setRoles(['Manager', 'Member'])
00091         self._ATCT = self._createType(self.folder, self.portal_type, 'ATCT')
00092         if self.cmf_klass:
00093             self._cmf = self._createType(self.folder, self.cmf_portal_type, 'cmf')
00094 
00095     def _createType(self, context, portal_type, id, **kwargs):
00096         """Helper method to create a new type
00097         """
00098         ttool = getToolByName(context, 'portal_types')
00099         cat = self.portal.portal_catalog
00100 
00101         fti = ttool.getTypeInfo(portal_type)
00102         fti.constructInstance(context, id, **kwargs)
00103         obj = getattr(context.aq_inner.aq_explicit, id)
00104         cat.indexObject(obj)
00105         return obj
00106 
00107     def test_000testsetup(self):
00108         # test if we really have the right test setup
00109         # vars
00110         self.failUnless(self.klass)
00111         self.failUnless(self.portal_type)
00112         self.failUnless(self.title)
00113         self.failUnless(self.meta_type)
00114         self.failUnless(self.icon)
00115         if self.cmf_klass:
00116             self.failUnless(self.cmf_klass)
00117             self.failUnless(self.cmf_portal_type)
00118 
00119         # portal types
00120         self.failUnlessEqual(self._ATCT.portal_type, self.portal_type)
00121         if self.cmf_klass:
00122             self.failUnlessEqual(self._cmf.portal_type, self.cmf_portal_type)
00123 
00124         # classes
00125         atct_class = self._ATCT.__class__
00126         self.failUnlessEqual(self.klass, atct_class)
00127         if self.cmf_klass:
00128             cmf_class = self._cmf.__class__
00129             self.failUnlessEqual(self.cmf_klass, cmf_class)
00130 
00131     def test_dcEdit(self):
00132         new = self._ATCT
00133         dcEdit(new)
00134         if self.cmf_klass:
00135             old = self._cmf
00136             dcEdit(old)
00137             self.compareDC(old, new)
00138 
00139     def test_typeInfo(self):
00140         ti = self._ATCT.getTypeInfo()
00141         self.failUnlessEqual(ti.getId(), self.portal_type)
00142         self.failUnlessEqual(ti.Title(), self.title)
00143         self.failUnlessEqual(ti.getIcon(), self.icon)
00144         self.failUnlessEqual(ti.Metatype(), self.meta_type)
00145 
00146     def test_doesImplementDC(self):
00147         self.failUnless(IDublinCore.isImplementedBy(self._ATCT))
00148         self.failUnless(IMutableDublinCore.isImplementedBy(self._ATCT))
00149         self.failUnless(verifyObject(IDublinCore, self._ATCT))
00150         self.failUnless(verifyObject(IMutableDublinCore, self._ATCT))
00151 
00152     def test_doesImplementATCT(self):
00153         self.failUnless(IATContentType.isImplementedBy(self._ATCT))
00154         self.failUnless(verifyObject(IATContentType, self._ATCT))
00155 
00156     def test_doesImplementZ3ATCT(self):
00157         iface = Z3IATContentType
00158         self.failUnless(Z3verifyObject(iface, self._ATCT))
00159 
00160     def test_doesImplementAT(self):
00161         self.failUnless(IBaseContent.isImplementedBy(self._ATCT))
00162         self.failUnless(IReferenceable.isImplementedBy(self._ATCT))
00163         self.failUnless(verifyObject(IBaseContent, self._ATCT))
00164         self.failUnless(verifyObject(IReferenceable, self._ATCT))
00165 
00166     def test_implementsTranslateable(self):
00167         # lingua plone is adding the ITranslatable interface to all types
00168         if not HAS_LINGUA_PLONE:
00169             return
00170         self.failUnless(ITranslatable.isImplementedBy(self._ATCT))
00171         self.failUnless(verifyObject(ITranslatable, self._ATCT))
00172 
00173     def test_not_implements_ITemplateMixin(self):
00174         self.failIf(ITemplateMixin.isImplementedBy(self._ATCT))
00175 
00176     def test_implements_ISelectableBrowserDefault(self):
00177         iface = ISelectableBrowserDefault
00178         self.failUnless(iface.providedBy(self._ATCT))
00179         self.failUnless(Z3verifyObject(iface, self._ATCT))
00180 
00181     def compareDC(self, first, second=None, **kwargs):
00182         """
00183         """
00184         if second != None:
00185             title = second.Title()
00186             description = second.Description()
00187         else:
00188             title = kwargs.get('title')
00189             description = kwargs.get('description')
00190 
00191         self.failUnlessEqual(first.Title(), title)
00192         self.failUnlessEqual(first.Description(), description)
00193 
00194     def test_idValidation(self):
00195         self.setRoles(['Manager', 'Member']) # for ATTopic
00196         asdf = self._createType(self.folder, self.portal_type, 'asdf')
00197         asdf2 = self._createType(self.folder, self.portal_type, 'asdf2')
00198         self.setRoles(['Member'])
00199 
00200         request = FakeRequestSession()
00201 
00202         # invalid ids
00203         ids = ['asdf2', '???', '/asdf2', ' asdf2', 'portal_workflow',
00204             'portal_url']
00205         for id in ids:
00206             request.form = {'id':id, 'fieldset':'default'}
00207             self.assertNotEquals(asdf.validate(REQUEST=request), {}, "Not catched id: %s" % id)
00208 
00209         # valid ids
00210         ids = ['', 'abcd', 'blafasel']
00211         for id in ids:
00212             request.form = {'id':id}
00213             self.assertEquals(asdf.validate(REQUEST=request), {})
00214 
00215     def test_getobjpositioninparent(self):
00216         # TODO: not a real test
00217         self._ATCT.getObjPositionInParent()
00218 
00219     def test_schema_marshall(self):
00220         atct = self._ATCT
00221         schema = atct.Schema()
00222         marshall = schema.getLayerImpl('marshall')
00223         marshallers = [RFC822Marshaller]
00224         try:
00225             from Products.Marshall import ControlledMarshaller
00226             marshallers.append(ControlledMarshaller)
00227         except ImportError:
00228             pass
00229         self.failUnless(isinstance(marshall, tuple(marshallers)), marshall)
00230 
00231     def beforeTearDown(self):
00232         self.logout()
00233 
00234 class ATCTFieldTestCase(BaseSchemaTest):
00235     """ ATContentTypes test including AT schema tests """
00236 
00237     def afterSetUp(self):
00238         # initalize the portal but not the base schema test
00239         # because we want to overwrite the dummy and don't need it
00240         ATSiteTestCase.afterSetUp(self)
00241         self.setRoles(['Manager',])
00242 
00243     def createDummy(self, klass, id='dummy'):
00244         portal = self.portal
00245         dummy = klass(oid=id)
00246         # put dummy in context of portal
00247         dummy = dummy.__of__(portal)
00248         portal.dummy = dummy
00249         dummy.initializeArchetype()
00250         return dummy
00251 
00252     def test_description(self):
00253         dummy = self._dummy
00254         field = dummy.getField('description')
00255 
00256         self.failUnless(ILayerContainer.isImplementedBy(field))
00257         self.failUnlessEqual(field.required, False)
00258         self.failUnlessEqual(field.default, '')
00259         self.failUnlessEqual(field.searchable, True)
00260         self.failUnlessEqual(field.primary, False)
00261         vocab = field.vocabulary
00262         self.failUnlessEqual(vocab, ())
00263         self.failUnlessEqual(field.enforceVocabulary, False)
00264         self.failUnlessEqual(field.multiValued, False)
00265         self.failUnlessEqual(field.isMetadata, True)
00266         self.failUnlessEqual(field.accessor, 'Description')
00267         self.failUnlessEqual(field.mutator, 'setDescription')
00268         self.failUnlessEqual(field.edit_accessor, 'getRawDescription')
00269         self.failUnlessEqual(field.read_permission, View)
00270         self.failUnlessEqual(field.write_permission, ModifyPortalContent)
00271         self.failUnlessEqual(field.generateMode, 'mVc')
00272         #self.failUnless(field.generateMode == 'veVc', field.generateMode)
00273         self.failUnlessEqual(field.force, '')
00274         self.failUnlessEqual(field.type, 'text')
00275         self.failUnless(isinstance(field.storage, MetadataStorage))
00276         self.failUnless(field.getLayerImpl('storage') == MetadataStorage())
00277         self.failUnlessEqual(field.validators, EmptyValidator)
00278         self.failUnless(isinstance(field.widget, TextAreaWidget))
00279         vocab = field.Vocabulary(dummy)
00280         self.failUnless(isinstance(vocab, DisplayList))
00281         self.failUnlessEqual(tuple(vocab), ())
00282 
00283     def test_id(self):
00284         dummy = self._dummy
00285         field = dummy.getField('id')
00286 
00287         self.failUnless(ILayerContainer.isImplementedBy(field))
00288         self.failUnlessEqual(field.required, False)
00289         self.failUnlessEqual(field.default, None)
00290         self.failUnlessEqual(field.searchable, True)
00291         self.failUnlessEqual(getattr(field, 'primary', None), None)
00292         vocab = field.vocabulary
00293         self.failUnlessEqual(vocab, ())
00294         self.failUnlessEqual(field.enforceVocabulary, False)
00295         self.failUnlessEqual(field.multiValued, False)
00296         self.failUnlessEqual(field.isMetadata, False)
00297         self.failUnlessEqual(field.accessor, 'getId')
00298         self.failUnlessEqual(field.mutator, 'setId')
00299         self.failUnlessEqual(field.edit_accessor, 'getRawId')
00300         self.failUnlessEqual(field.read_permission, View)
00301         self.failUnlessEqual(field.write_permission, ModifyPortalContent)
00302         self.failUnlessEqual(field.generateMode, 'veVc')
00303         self.failUnlessEqual(field.force, '')
00304         self.failUnlessEqual(field.type, 'string')
00305         self.failUnless(isinstance(field.storage, AttributeStorage))
00306         self.failUnless(field.getLayerImpl('storage') == AttributeStorage())
00307         self.failUnlessEqual(field.validators, idValidator)
00308         self.failUnless(isinstance(field.widget, IdWidget))
00309         vocab = field.Vocabulary(dummy)
00310         self.failUnless(isinstance(vocab, DisplayList))
00311         self.failUnlessEqual(tuple(vocab), ())
00312 
00313     def test_relateditems(self):
00314         dummy = self._dummy
00315         field = dummy.getField('relatedItems')
00316 
00317         self.failUnless(ILayerContainer.isImplementedBy(field))
00318         self.failUnlessEqual(field.required, False)
00319         self.failUnlessEqual(field.default, None)
00320         self.failUnlessEqual(field.searchable, False)
00321         self.failUnlessEqual(getattr(field, 'primary', None), None)
00322         vocab = field.vocabulary
00323         self.failUnlessEqual(vocab, ())
00324         self.failUnlessEqual(field.enforceVocabulary, False)
00325         self.failUnlessEqual(field.multiValued, True)
00326         self.failUnlessEqual(field.isMetadata, True)
00327         self.failUnlessEqual(field.accessor, 'getRelatedItems')
00328         self.failUnlessEqual(field.mutator, 'setRelatedItems')
00329         self.failUnlessEqual(field.edit_accessor, 'getRawRelatedItems')
00330         self.failUnlessEqual(field.read_permission, View)
00331         self.failUnlessEqual(field.write_permission, ModifyPortalContent)
00332         self.failUnlessEqual(field.generateMode, 'veVc')
00333         self.failUnlessEqual(field.force, '')
00334         self.failUnlessEqual(field.type, 'reference')
00335         self.failUnless(isinstance(field.storage, AttributeStorage))
00336         self.failUnless(field.getLayerImpl('storage') == AttributeStorage())
00337         self.failUnlessEqual(field.validators, EmptyValidator)
00338         self.failUnless(isinstance(field.widget, ReferenceBrowserWidget))
00339         vocab = field.Vocabulary(dummy)
00340         self.failUnless(isinstance(vocab, DisplayList))
00341