Back to index

plone3  3.1.7
Public Member Functions | Private Member Functions
CMFDefault.tests.test_MetadataTool.TestMetadataTool Class Reference

List of all members.

Public Member Functions

def test_z2interfaces
def test_z3interfaces
def test_empty
def test_DCMI_addElementSpec
def test_DCMI_removeElementSpec
def test_simplePolicies
def test_multiValuedPolicies
def test_vocabularies
def test_initialValues_defaults
def test_initialValues_implicit
def test_initialValues_explicit_raises_if_constraint_fails
def test_initialValues_explicit_mutliple_types
def test_validation
def test_addSchema_normal
def test_addSchema_duplicate
def test_removeSchema_normal
def test_removeSchema_invalid
def test_migration

Private Member Functions

def _getTargetClass
def _makeOne
def _makeTestObjects

Detailed Description

Definition at line 111 of file test_MetadataTool.py.


Member Function Documentation

Definition at line 113 of file test_MetadataTool.py.

00113 
00114     def _getTargetClass( self ):
00115         from Products.CMFDefault.MetadataTool import MetadataTool
00116         return MetadataTool

def CMFDefault.tests.test_MetadataTool.TestMetadataTool._makeOne (   self,
  args,
  kw 
) [private]

Definition at line 117 of file test_MetadataTool.py.

00117 
00118     def _makeOne( self, *args, **kw ):
00119         return self._getTargetClass()( *args, **kw )

Definition at line 120 of file test_MetadataTool.py.

00120 
00121     def _makeTestObjects( self ):
00122 
00123         from Products.CMFDefault.DublinCore import DefaultDublinCoreImpl
00124         class Foo( DefaultDublinCoreImpl ):
00125 
00126             description = title = language = format = rights = ''
00127             subject = ()
00128 
00129             def __init__( self ):
00130                 pass # skip DDCI's default values
00131 
00132             def getPortalTypeName( self ):
00133                 return 'Foo'
00134 
00135 
00136         class Bar( Foo ):
00137 
00138             def getPortalTypeName( self ):
00139                 return 'Bar'
00140 
00141         return Foo(), Bar()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 451 of file test_MetadataTool.py.

00451 
00452     def test_addSchema_duplicate( self ):
00453         tool = self._makeOne()
00454         before = tool.listSchemas()
00455         tool.addSchema( 'Arbitrary' )
00456         self.assertRaises( KeyError, tool.addSchema, 'Arbitrary' )
00457         self.assertRaises( KeyError, tool.addSchema, 'DCMI' )

Definition at line 433 of file test_MetadataTool.py.

00433 
00434     def test_addSchema_normal( self ):
00435         from Products.CMFDefault.MetadataTool import MetadataSchema
00436 
00437         tool = self._makeOne()
00438         before = tool.listSchemas()
00439         self.assertEqual( len( before ), 1 )
00440         self.assertEqual( before[0][0], 'DCMI' )
00441         self.failUnless( isinstance( before[0][1], MetadataSchema ) )
00442 
00443         tool.addSchema( 'Arbitrary' )
00444 
00445         after = tool.listSchemas()
00446         self.assertEqual( len( after ), 2 )
00447         self.assertEqual( after[0][0], 'DCMI' )
00448         self.failUnless( isinstance( after[0][1], MetadataSchema ) )
00449         self.assertEqual( after[1][0], 'Arbitrary' )
00450         self.failUnless( isinstance( after[1][1], MetadataSchema ) )

Definition at line 194 of file test_MetadataTool.py.

00194 
00195     def test_DCMI_addElementSpec( self ):
00196         from Products.CMFDefault.MetadataTool import _DCMI_ELEMENT_SPECS
00197 
00198         tool = self._makeOne()
00199         dcmi = tool.DCMI
00200         dcmi.addElementSpec( 'Rating', 1 )
00201         self.assertEqual( len( dcmi.listElementSpecs() )
00202                         , len( _DCMI_ELEMENT_SPECS ) + 1 )
00203         rating = dcmi.getElementSpec( 'Rating' )
00204         self.failUnless( rating.isMultiValued() )

Definition at line 205 of file test_MetadataTool.py.

00205 
00206     def test_DCMI_removeElementSpec( self ):
00207         from Products.CMFDefault.MetadataTool import _DCMI_ELEMENT_SPECS
00208 
00209         tool = self._makeOne()
00210         dcmi = tool.DCMI
00211         dcmi.removeElementSpec( 'Rights' )
00212 
00213         self.assertEqual( len( dcmi.listElementSpecs() )
00214                         , len( _DCMI_ELEMENT_SPECS ) - 1
00215                         )
00216 
00217         self.assertRaises( KeyError, dcmi.getElementSpec, 'Rights' )
00218         self.assertRaises( KeyError, dcmi.removeElementSpec, 'Foo' )

Definition at line 155 of file test_MetadataTool.py.

00155 
00156     def test_empty( self ):
00157         from Products.CMFDefault.MetadataTool import _DCMI_ELEMENT_SPECS
00158 
00159         tool = self._makeOne()
00160         self.failIf( tool.getPublisher() )
00161         self.assertEqual( tool.getFullName( 'foo' ), 'foo' )
00162 
00163         dcmi = tool.DCMI
00164         specs = list( dcmi.DCMI.listElementSpecs() )
00165         defaults = list( _DCMI_ELEMENT_SPECS )
00166         specs.sort(); defaults.sort()
00167 
00168         self.assertEqual( len( specs ), len( defaults ) )
00169         for i in range( len( specs ) ):
00170             self.assertEqual( specs[i][0], defaults[i][0] )
00171             self.assertEqual( specs[i][1].isMultiValued(), defaults[i][1] )
00172             policies = specs[i][1].listPolicies()
00173             self.assertEqual( len( policies ), 1 )
00174             self.failUnless( policies[0][0] is None )
00175 
00176         self.failIf( dcmi.getElementSpec( 'Title' ).isMultiValued() )
00177         self.failIf( dcmi.getElementSpec( 'Description' ).isMultiValued() )
00178         self.failUnless( dcmi.getElementSpec( 'Subject' ).isMultiValued() )
00179         self.failIf( dcmi.getElementSpec( 'Format' ).isMultiValued() )
00180         self.failIf( dcmi.getElementSpec( 'Language' ).isMultiValued() )
00181         self.failIf( dcmi.getElementSpec( 'Rights' ).isMultiValued() )
00182 
00183         try:
00184             dummy = dcmi.getElementSpec( 'Foo' )
00185         except KeyError:
00186             pass
00187         else:
00188             self.failUnless( 0, "Expected KeyError" )
00189 
00190         self.failIf( dcmi.listAllowedSubjects() )
00191         self.failIf( dcmi.listAllowedFormats() )
00192         self.failIf( dcmi.listAllowedLanguages() )
00193         self.failIf( dcmi.listAllowedRights() )

Definition at line 338 of file test_MetadataTool.py.

00338 
00339     def test_initialValues_defaults( self ):
00340         tool = self._makeOne()
00341         foo, bar = self._makeTestObjects()
00342         self.failIf( foo.Title() )
00343         self.failIf( foo.Description() )
00344         self.failIf( foo.Subject() )
00345         self.failIf( foo.Format(), foo.Format() )
00346         self.failIf( foo.Language() )
00347         self.failIf( foo.Rights() )
00348 
00349         tool.setInitialMetadata( foo )
00350         self.failIf( foo.Title() )
00351         self.failIf( foo.Description() )
00352         self.failIf( foo.Subject() )
00353         self.failIf( foo.Format() )
00354         self.failIf( foo.Language() )
00355         self.failIf( foo.Rights() )

Definition at line 386 of file test_MetadataTool.py.

00386 
00387     def test_initialValues_explicit_mutliple_types( self ):
00388         tool = self._makeOne()
00389         dcmi = tool.DCMI
00390         foo, bar = self._makeTestObjects()
00391         foo.setTitle( 'Foo title' )
00392 
00393         fSpec   = dcmi.getElementSpec( 'Format' )
00394         fSpec.addPolicy( foo.getPortalTypeName() )
00395         fPolicy = fSpec.getPolicy( foo.getPortalTypeName() )
00396         fPolicy.edit( 0, 1, 'text/plain', 0, () )
00397 
00398         tool.setInitialMetadata( foo )
00399         self.assertEqual( foo.Title(), 'Foo title' )
00400         self.failIf( foo.Description() )
00401         self.failIf( foo.Subject() )
00402         self.assertEqual( foo.Format(), 'text/plain' )
00403         self.failIf( foo.Language() )
00404         self.failIf( foo.Rights() )
00405 
00406         #   Ensure Foo's policy doesn't interfere with other types.
00407         tool.setInitialMetadata( bar )
00408         self.failIf( bar.Title() )
00409         self.failIf( bar.Description() )
00410         self.failIf( bar.Subject() )
00411         self.assertEqual( bar.Format(), '' )
00412         self.failIf( bar.Language() )
00413         self.failIf( bar.Rights() )

Definition at line 372 of file test_MetadataTool.py.

00372 
00373     def test_initialValues_explicit_raises_if_constraint_fails( self ):
00374         from Products.CMFDefault.exceptions import MetadataError
00375 
00376         # Test type-specific policy.
00377         tool = self._makeOne()
00378         dcmi = tool.DCMI
00379         foo, bar = self._makeTestObjects()
00380         tSpec   = dcmi.getElementSpec( 'Title' )
00381         tSpec.addPolicy( 'Foo' )
00382         tPolicy = tSpec.getPolicy( foo.getPortalTypeName() )
00383         tPolicy.edit( 1, 0, '', 0, () )
00384 
00385         self.assertRaises( MetadataError, tool.setInitialMetadata, foo )

Definition at line 356 of file test_MetadataTool.py.

00356 
00357     def test_initialValues_implicit( self ):
00358         # Test default policy.
00359         tool = self._makeOne()
00360         dcmi = tool.DCMI
00361         foo, bar = self._makeTestObjects()
00362         fSpec   = dcmi.getElementSpec( 'Format' )
00363         fPolicy = fSpec.getPolicy()
00364         fPolicy.edit( 0, 1, 'text/plain', 0, () )
00365         tool.setInitialMetadata( foo )
00366         self.failIf( foo.Title() )
00367         self.failIf( foo.Description() )
00368         self.failIf( foo.Subject() )
00369         self.assertEqual( foo.Format(), 'text/plain' )
00370         self.failIf( foo.Language() )
00371         self.failIf( foo.Rights() )

Definition at line 490 of file test_MetadataTool.py.

00490 
00491     def test_migration( self ):
00492         # Test that we forward-migrate old-style DCMI policies.
00493         from Products.CMFDefault.MetadataTool import ElementSpec
00494 
00495         tool = self._makeOne()
00496         tool.element_specs = { 'Title' : ElementSpec( 0 )
00497                              , 'Description' : ElementSpec( 0 )
00498                              , 'Subject' : ElementSpec( 1 )
00499                              , 'Format' : ElementSpec( 0 )
00500                              , 'Language' : ElementSpec( 0 )
00501                              , 'Rights' : ElementSpec( 0 )
00502                              }
00503         subj = tool.element_specs[ 'Subject' ]
00504         subj.addPolicy( 'Foo' )
00505         subj.getPolicy( 'Foo' ).edit( False
00506                                     , False
00507                                     , None
00508                                     , True
00509                                     , ( 'bar', 'baz' )
00510                                     )
00511 
00512         dcmi = tool.DCMI
00513 
00514         self.assertEqual( dcmi.getId(), 'DCMI' )
00515 
00516         # Accessing the DCMI property converts and clears 'element_specs'
00517         self.assertRaises(AttributeError, lambda: tool.element_specs )
00518 
00519         subj2 = dcmi.getElementSpec( 'Subject' )
00520         subj_default = subj2.getPolicy( None )
00521         subj_foo = subj2.getPolicy( 'Foo' )
00522 
00523         self.assertEqual( subj_foo.isRequired(), False )
00524         self.assertEqual( subj_foo.supplyDefault(), False )
00525         self.assertEqual( subj_foo.defaultValue(), None )
00526         self.assertEqual( subj_foo.enforceVocabulary(), True )
00527         self.assertEqual( len( subj_foo.allowedVocabulary() ), 2 )
00528         self.failUnless( 'bar' in subj_foo.allowedVocabulary() )
00529         self.failUnless( 'baz' in subj_foo.allowedVocabulary() )
00530 

Definition at line 260 of file test_MetadataTool.py.

00260 
00261     def test_multiValuedPolicies( self ):
00262 
00263         tool = self._makeOne()
00264         dcmi = tool.DCMI
00265         sSpec = dcmi.getElementSpec( 'Subject' )
00266 
00267         # Fetch default policy.
00268         sDef  = sSpec.getPolicy()
00269         self.failIf( sDef.isRequired() )
00270         self.failIf( sDef.supplyDefault() )
00271         self.failIf( sDef.defaultValue() )
00272         self.failIf( sDef.enforceVocabulary() )
00273         self.failIf( sDef.allowedVocabulary() )
00274 
00275         # Fetch (default) policy for a type.
00276         sDoc  = sSpec.getPolicy( 'Document' )
00277         self.assertEqual(aq_base(sDoc), aq_base(sDef))
00278 
00279         # Changing default changes policies found from there.
00280         sDef.edit( 1, 1, 'xyz', 1, ( 'foo', 'bar' ) )
00281         self.failUnless( sDef.isRequired() )
00282         self.failUnless( sDef.supplyDefault() )
00283         self.assertEqual( sDef.defaultValue(), 'xyz' )
00284         self.failUnless( sDoc.isRequired() )
00285         self.failUnless( sDoc.supplyDefault() )
00286         self.assertEqual( sDoc.defaultValue(), 'xyz' )
00287         self.failUnless( sDef.enforceVocabulary() )
00288         self.assertEqual( len( sDef.allowedVocabulary() ), 2 )
00289         self.failUnless( 'foo' in sDef.allowedVocabulary() )
00290         self.failUnless( 'bar' in sDef.allowedVocabulary() )
00291         self.failUnless( sDoc.enforceVocabulary() )
00292         self.assertEqual( len( sDoc.allowedVocabulary() ), 2 )
00293         self.failUnless( 'foo' in sDoc.allowedVocabulary() )
00294         self.failUnless( 'bar' in sDoc.allowedVocabulary() )
00295 
00296         sSpec.addPolicy( 'Document' )
00297         self.assertEqual( len( sSpec.listPolicies() ), 2 )
00298 
00299         sDoc  = sSpec.getPolicy( 'Document' )
00300         self.assertNotEqual(aq_base(sDoc), aq_base(sDef))
00301         self.failIf( sDoc.isRequired() )
00302         self.failIf( sDoc.supplyDefault() )
00303         self.failIf( sDoc.defaultValue() )
00304         self.failIf( sDoc.enforceVocabulary() )
00305         self.failIf( sDoc.allowedVocabulary() )
00306 
00307         sSpec.removePolicy( 'Document' )
00308         sDoc  = sSpec.getPolicy( 'Document' )
00309         self.assertEqual(aq_base(sDoc), aq_base(sDef))
00310         self.failUnless( sDoc.isRequired() )
00311         self.failUnless( sDoc.supplyDefault() )
00312         self.assertEqual( sDoc.defaultValue(), 'xyz' )
00313         self.failUnless( sDoc.enforceVocabulary() )
00314         self.assertEqual( len( sDoc.allowedVocabulary() ), 2 )
00315         self.failUnless( 'foo' in sDoc.allowedVocabulary() )
00316         self.failUnless( 'bar' in sDoc.allowedVocabulary() )

Definition at line 483 of file test_MetadataTool.py.

00483 
00484     def test_removeSchema_invalid( self ):
00485         tool = self._makeOne()
00486         self.assertRaises( KeyError, tool.removeSchema, 'DCMI' )
00487         tool.addSchema( 'Arbitrary' )
00488         tool.removeSchema( 'Arbitrary' )
00489         self.assertRaises( KeyError, tool.removeSchema, 'Arbitrary' )

Definition at line 458 of file test_MetadataTool.py.

00458 
00459     def test_removeSchema_normal( self ):
00460         tool = self._makeOne()
00461         before = tool.listSchemas()
00462         self.assertEqual( len( before ), 1 )
00463         self.assertEqual( before[0][0], 'DCMI' )
00464 
00465         tool.addSchema( 'Arbitrary' )
00466         tool.addSchema( 'Beneficent' )
00467         tool.addSchema( 'Grouchy' )
00468 
00469         middle = tool.listSchemas()
00470         self.assertEqual( len( middle ), 4 )
00471         self.assertEqual( middle[0][0], 'DCMI' )
00472         self.assertEqual( middle[1][0], 'Arbitrary' )
00473         self.assertEqual( middle[2][0], 'Beneficent' )
00474         self.assertEqual( middle[3][0], 'Grouchy' )
00475 
00476         tool.removeSchema( 'Beneficent' )
00477 
00478         after = tool.listSchemas()
00479         self.assertEqual( len( after ), 3 )
00480         self.assertEqual( after[0][0], 'DCMI' )
00481         self.assertEqual( after[1][0], 'Arbitrary' )
00482         self.assertEqual( after[2][0], 'Grouchy' )

Definition at line 219 of file test_MetadataTool.py.

00219 
00220     def test_simplePolicies( self ):
00221 
00222         tool = self._makeOne()
00223         dcmi = tool.DCMI
00224         tSpec = dcmi.getElementSpec( 'Title' )
00225 
00226         # Fetch default policy.
00227         tDef  = tSpec.getPolicy()
00228         self.failIf( tDef.isRequired() )
00229         self.failIf( tDef.supplyDefault() )
00230         self.failIf( tDef.defaultValue() )
00231 
00232         # Fetch (default) policy for a type.
00233         tDoc  = tSpec.getPolicy( 'Document' )
00234         self.assertEqual(aq_base(tDoc), aq_base(tDef))
00235 
00236         # Changing default changes policies found from there.
00237         tDef.edit( 1, 1, 'xyz', 0, () )
00238         self.failUnless( tDef.isRequired() )
00239         self.failUnless( tDef.supplyDefault() )
00240         self.assertEqual( tDef.defaultValue(), 'xyz' )
00241         self.failUnless( tDoc.isRequired() )
00242         self.failUnless( tDoc.supplyDefault() )
00243         self.assertEqual( tDoc.defaultValue(), 'xyz' )
00244 
00245         tSpec.addPolicy( 'Document' )
00246         self.assertEqual( len( tSpec.listPolicies() ), 2 )
00247 
00248         tDoc  = tSpec.getPolicy( 'Document' )
00249         self.assertNotEqual(aq_base(tDoc), aq_base(tDef))
00250         self.failIf( tDoc.isRequired() )
00251         self.failIf( tDoc.supplyDefault() )
00252         self.failIf( tDoc.defaultValue() )
00253 
00254         tSpec.removePolicy( 'Document' )
00255         tDoc  = tSpec.getPolicy( 'Document' )
00256         self.assertEqual(aq_base(tDoc), aq_base(tDef))
00257         self.failUnless( tDoc.isRequired() )
00258         self.failUnless( tDoc.supplyDefault() )
00259         self.assertEqual( tDoc.defaultValue(), 'xyz' )

Definition at line 414 of file test_MetadataTool.py.

00414 
00415     def test_validation( self ):
00416         from Products.CMFDefault.exceptions import MetadataError
00417 
00418         tool = self._makeOne()
00419         foo, bar = self._makeTestObjects()
00420         tool.setInitialMetadata( foo )
00421         tool.validateMetadata( foo )
00422 
00423         dcmi = tool.DCMI
00424         tSpec   = dcmi.getElementSpec( 'Title' )
00425         tSpec.addPolicy( 'Foo' )
00426         tPolicy = tSpec.getPolicy( foo.getPortalTypeName() )
00427         tPolicy.edit( 1, 0, '', 0, () )
00428 
00429         self.assertRaises( MetadataError, tool.validateMetadata, foo )
00430 
00431         foo.setTitle( 'Foo title' )
00432         tool.validateMetadata( foo )

Definition at line 317 of file test_MetadataTool.py.

00317 
00318     def test_vocabularies( self ):
00319         tool = self._makeOne()
00320         dcmi = tool.DCMI
00321         fSpec   = dcmi.getElementSpec( 'Format' )
00322         fDef    = fSpec.getPolicy()
00323         formats = ( 'text/plain', 'text/html' )
00324         fDef.edit( 0, 0, '', 0, ( 'text/plain', 'text/html' ) )
00325         self.assertEqual( tool.listAllowedFormats(), formats )
00326 
00327         foo, bar = self._makeTestObjects()
00328 
00329         self.assertEqual( tool.listAllowedFormats( foo ), formats )
00330 
00331         fSpec.addPolicy( 'Foo' )
00332         self.failIf( tool.listAllowedFormats( foo ) )
00333 
00334         foo_formats = ( 'image/jpeg', 'image/gif', 'image/png' )
00335         fFoo        = fSpec.getPolicy( 'Foo' )
00336         fFoo.edit( 0, 0, '', 0, foo_formats )
00337         self.assertEqual( tool.listAllowedFormats( foo ), foo_formats )

Definition at line 142 of file test_MetadataTool.py.

00142 
00143     def test_z2interfaces(self):
00144         from Interface.Verify import verifyClass
00145         from Products.CMFCore.interfaces.portal_metadata \
00146                 import portal_metadata as IMetadataTool
00147 
00148         verifyClass(IMetadataTool, self._getTargetClass())

Definition at line 149 of file test_MetadataTool.py.

00149 
00150     def test_z3interfaces(self):
00151         from zope.interface.verify import verifyClass
00152         from Products.CMFCore.interfaces import IMetadataTool
00153 
00154         verifyClass(IMetadataTool, self._getTargetClass())


The documentation for this class was generated from the following file: