[CMF-checkins] SVN: CMF/branches/tseaver-metadatatool_refactoring/ Branch for refactoring the metadata tool to permit alternative schemas.

Tres Seaver tseaver at palladion.com
Tue Sep 6 22:10:30 EDT 2005


Log message for revision 38332:
  Branch for refactoring the metadata tool to permit alternative schemas.
  

Changed:
  A   CMF/branches/tseaver-metadatatool_refactoring/
  U   CMF/branches/tseaver-metadatatool_refactoring/CMFCore/interfaces/portal_metadata.py
  U   CMF/branches/tseaver-metadatatool_refactoring/CMFDefault/MetadataTool.py
  U   CMF/branches/tseaver-metadatatool_refactoring/CMFDefault/tests/test_MetadataTool.py

-=-
Copied: CMF/branches/tseaver-metadatatool_refactoring (from rev 38331, CMF/branches/1.5)

Modified: CMF/branches/tseaver-metadatatool_refactoring/CMFCore/interfaces/portal_metadata.py
===================================================================
--- CMF/branches/1.5/CMFCore/interfaces/portal_metadata.py	2005-09-06 23:57:24 UTC (rev 38331)
+++ CMF/branches/tseaver-metadatatool_refactoring/CMFCore/interfaces/portal_metadata.py	2005-09-07 02:10:28 UTC (rev 38332)
@@ -44,45 +44,58 @@
     #
     #   Content-specific queries, for Dublin Core metadata.
     #
-    def listAllowedSubjects(content=None):
+    def listAllowedSubjects(content=None, content_type=None):
         """ List the allowed values of the 'Subject' DCMI element.
 
         o 'Subject' elements should be keywords categorizing their resource.
 
-        o Return only values appropriate for content's type, or all values
-          if content is None.
+        o Return only values appropriate for content's type, or all values if
+          both 'content' and 'content_type' are None.
         """
 
-    def listAllowedFormats(content=None):
+    def listAllowedFormats(content=None, content_type=None):
         """ List the allowed values of the 'Format' DCMI element.
 
         o These items should be usable as HTTP 'Content-type' values.
 
-        o Return only values appropriate for content's type, or all values
-          if content is None.
+        o Return only values appropriate for content's type, or all values if
+          both 'content' and 'content_type' are None.
         """
 
-    def listAllowedLanguages(content=None):
+    def listAllowedLanguages(content=None, content_type=None):
         """ List the allowed values of the 'Language' DCMI element.
 
         o 'Language' element values should be suitable for generating
           HTTP headers.
 
         o Return only values appropriate for content's type, or all values if
-          content is None.
+          both 'content' and 'content_type' are None.
         """
 
-    def listAllowedRights(content=None):
+    def listAllowedRights(content=None, content_type=None):
         """ List the allowed values of the 'Rights' DCMI element.
 
         o The 'Rights' element describes copyright or other IP declarations
           pertaining to a resource.
 
         o Return only values appropriate for content's type, or all values if
-          content is None.
+          both 'content' and 'content_type' are None.
         """
 
     #
+    #   Content-specific queries, for generic metadata.
+    #
+    def listAllowedVocabulary( schema
+                             , element
+                             , content=None
+                             , content_type=None
+                             ):
+        """ List allowed values for a given schema element and content object.
+        
+        o List possible keywords if both 'content' and 'content_type' are None.
+        """
+
+    #
     #   Validation policy hooks.
     #
     def setInitialMetadata(content):

Modified: CMF/branches/tseaver-metadatatool_refactoring/CMFDefault/MetadataTool.py
===================================================================
--- CMF/branches/1.5/CMFDefault/MetadataTool.py	2005-09-06 23:57:24 UTC (rev 38331)
+++ CMF/branches/tseaver-metadatatool_refactoring/CMFDefault/MetadataTool.py	2005-09-07 02:10:28 UTC (rev 38332)
@@ -20,6 +20,7 @@
 from Globals import DTMLFile
 from Globals import InitializeClass
 from Globals import PersistentMapping
+from OFS.Folder import Folder
 from OFS.SimpleItem import SimpleItem
 
 from Products.CMFCore.ActionProviderBase import ActionProviderBase
@@ -115,15 +116,7 @@
 InitializeClass( MetadataElementPolicy )
 
 
-DEFAULT_ELEMENT_SPECS = ( ( 'Title', 0 )
-                        , ( 'Description', 0 )
-                        , ( 'Subject', 1 )
-                        , ( 'Format', 0 )
-                        , ( 'Language', 0 )
-                        , ( 'Rights', 0 )
-                        )
 
-
 class ElementSpec( SimpleItem ):
     """
         Represent all the tool knows about a single metadata element.
@@ -199,90 +192,32 @@
 InitializeClass( ElementSpec )
 
 
-class MetadataTool( UniqueObject, SimpleItem, ActionProviderBase ):
+class MetadataSchema( SimpleItem ):
 
-    __implements__ = (IMetadataTool, ActionProviderBase.__implements__)
+    """ Describe a metadata schema.
+    """
+    security = ClassSecurityInfo()
 
-    id = 'portal_metadata'
-    meta_type = 'Default Metadata Tool'
-    _actions = ()
-
-    #
-    #   Default values.
-    #
+    meta_type = 'Metadata Schema'
     publisher           = ''
-    element_specs       = None
-    #initial_values_hook = None
-    #validation_hook     = None
 
-    security = ClassSecurityInfo()
-
-    def __init__( self
-                , publisher=None
-               #, initial_values_hook=None
-               #, validation_hook=None
-                , element_specs=DEFAULT_ELEMENT_SPECS
-                ):
-
-        self.editProperties( publisher
-                          #, initial_values_hook
-                          #, validation_hook
-                           )
-
+    def __init__( self, element_specs=() ):
         self.element_specs = PersistentMapping()
-
         for name, is_multi_valued in element_specs:
             self.element_specs[ name ] = ElementSpec( is_multi_valued )
 
+
     #
     #   ZMI methods
     #
-    manage_options = ( ActionProviderBase.manage_options +
-                     ( { 'label'      : 'Overview'
-                         , 'action'     : 'manage_overview'
-                         }
-                       , { 'label'      : 'Properties'
-                         , 'action'     : 'propertiesForm'
-                         }
-                       , { 'label'      : 'Elements'
+    manage_options = ( ( { 'label'      : 'Elements'
                          , 'action'     : 'elementPoliciesForm'
                          }
-            # TODO     , { 'label'      : 'Types'
-            #            , 'action'     : 'typesForm'
-            #            }
+                       ,
                        )
                      + SimpleItem.manage_options
                      )
 
-    security.declareProtected(ManagePortal, 'manage_overview')
-    manage_overview = DTMLFile( 'explainMetadataTool', _dtmldir )
-
-    security.declareProtected(ManagePortal, 'propertiesForm')
-    propertiesForm = DTMLFile( 'metadataProperties', _dtmldir )
-
-    security.declareProtected(ManagePortal, 'editProperties')
-    def editProperties( self
-                      , publisher=None
-               # TODO , initial_values_hook=None
-               # TODO , validation_hook=None
-                      , REQUEST=None
-                      ):
-        """
-            Form handler for "tool-wide" properties (including list of
-            metadata elements).
-        """
-        if publisher is not None:
-            self.publisher = publisher
-
-        # TODO self.initial_values_hook = initial_values_hook
-        # TODO self.validation_hook = validation_hook
-
-        if REQUEST is not None:
-            REQUEST[ 'RESPONSE' ].redirect( self.absolute_url()
-                                        + '/propertiesForm'
-                                        + '?manage_tabs_message=Tool+updated.'
-                                        )
-
     security.declareProtected(ManagePortal, 'elementPoliciesForm')
     elementPoliciesForm = DTMLFile( 'metadataElementPolicies', _dtmldir )
 
@@ -436,114 +371,195 @@
             result.append( ( element, spec.getPolicy( typ ) ) )
         return result
 
+InitializeClass(MetadataSchema)
+
+
+_DCMI_ELEMENT_SPECS = ( ( 'Title', 0 )
+                      , ( 'Description', 0 )
+                      , ( 'Subject', 1 )
+                      , ( 'Format', 0 )
+                      , ( 'Language', 0 )
+                      , ( 'Rights', 0 )
+                      )
+
+class MetadataTool( UniqueObject, Folder, ActionProviderBase ):
+
+    __implements__ = (IMetadataTool, ActionProviderBase.__implements__)
+
+    id = 'portal_metadata'
+    meta_type = 'Default Metadata Tool'
+    _actions = ()
+
+    _DCMI = None
+    def _get_DCMI( self ):
+        if self._DCMI is None:
+            self._DCMI = MetadataSchema( _DCMI_ELEMENT_SPECS )
+
+        return self._DCMI
+
+    DCMI = property(_get_DCMI, None)
+
     #
-    #   'portal_metadata' interface
+    #   Default values.
     #
+    publisher           = ''
+
+    security = ClassSecurityInfo()
+
+    def __init__( self, publisher=None ):
+
+        self.editProperties( publisher )
+
+    #
+    #   ZMI methods
+    #
+    manage_options = ( Folder.manage_options[:1]
+                     + ActionProviderBase.manage_options +
+                     ( { 'label'      : 'Overview'
+                         , 'action'     : 'manage_overview'
+                         }
+                       , { 'label'      : 'Properties'
+                         , 'action'     : 'propertiesForm'
+                         }
+                       )
+                     + Folder.manage_options[1:]
+                     )
+
+    security.declareProtected(ManagePortal, 'manage_overview')
+    manage_overview = DTMLFile( 'explainMetadataTool', _dtmldir )
+
+    security.declareProtected(ManagePortal, 'propertiesForm')
+    propertiesForm = DTMLFile( 'metadataProperties', _dtmldir )
+
+    security.declareProtected(ManagePortal, 'editProperties')
+    def editProperties( self
+                      , publisher=None
+                      , REQUEST=None
+                      ):
+        """
+            Form handler for "tool-wide" properties (including list of
+            metadata elements).
+        """
+        if publisher is not None:
+            self.publisher = publisher
+
+        if REQUEST is not None:
+            REQUEST[ 'RESPONSE' ].redirect( self.absolute_url()
+                                        + '/propertiesForm'
+                                        + '?manage_tabs_message=Tool+updated.'
+                                        )
+
     security.declarePrivate( 'getFullName' )
     def getFullName( self, userid ):
+        """ See IMetadataTool.
         """
-            Convert an internal userid to a "formal" name, if
-            possible, perhaps using the 'portal_membership' tool.
-
-            Used to map userid's for Creator, Contributor DCMI
-            queries.
-        """
         return userid   # TODO: do lookup here
 
     security.declarePublic( 'getPublisher' )
     def getPublisher( self ):
+        """ See IMetadataTool.
         """
-            Return the "formal" name of the publisher of the
-            portal.
-        """
         return self.publisher
 
-    security.declarePublic( 'listAllowedVocabulary' )
-    def listAllowedVocabulary( self, element, content=None, content_type=None ):
-        """
-            List allowed keywords for a given portal_type, or all
-            possible keywords if none supplied.
-        """
-        spec = self.getElementSpec( element )
-        if content_type is None and content:
-            content_type = content.getPortalTypeName()
-        return spec.getPolicy( content_type ).allowedVocabulary()
-
     security.declarePublic( 'listAllowedSubjects' )
     def listAllowedSubjects( self, content=None, content_type=None ):
+        """ See IMetadataTool.
         """
-            List allowed keywords for a given portal_type, or all
-            possible keywords if none supplied.
-        """
-        return self.listAllowedVocabulary( 'Subject', content, content_type )
+        return self.listAllowedVocabulary( 'DCMI'
+                                         , 'Subject'
+                                         , content
+                                         , content_type
+                                         )
 
     security.declarePublic( 'listAllowedFormats' )
     def listAllowedFormats( self, content=None, content_type=None ):
+        """ See IMetadataTool.
         """
-            List the allowed 'Content-type' values for a particular
-            portal_type, or all possible formats if none supplied.
-        """
-        return self.listAllowedVocabulary( 'Format', content, content_type )
+        return self.listAllowedVocabulary( 'DCMI'
+                                         , 'Format'
+                                         , content
+                                         , content_type
+                                         )
 
     security.declarePublic( 'listAllowedLanguages' )
     def listAllowedLanguages( self, content=None, content_type=None ):
+        """ See IMetadataTool.
         """
-            List the allowed language values.
-        """
-        return self.listAllowedVocabulary( 'Language', content, content_type )
+        return self.listAllowedVocabulary( 'DCMI'
+                                         , 'Language'
+                                         , content
+                                         , content_type
+                                         )
 
     security.declarePublic( 'listAllowedRights' )
     def listAllowedRights( self, content=None, content_type=None ):
+        """ See IMetadata Tool.
         """
-            List the allowed values for a "Rights"
-            selection list;  this gets especially important where
-            syndication is involved.
+        return self.listAllowedVocabulary( 'DCMI'
+                                         , 'Rights'
+                                         , content
+                                         , content_type
+                                         )
+
+    security.declarePublic( 'listAllowedVocabulary' )
+    def listAllowedVocabulary( self
+                             , schema
+                             , element
+                             , content=None
+                             , content_type=None
+                             ):
+        """ See IMetadataTool.
         """
-        return self.listAllowedVocabulary( 'Rights', content, content_type )
+        schema_def = getattr( self, schema )
+        spec = schema_def.getElementSpec( element )
+        if content_type is None and content:
+            content_type = content.getPortalTypeName()
+        return spec.getPolicy( content_type ).allowedVocabulary()
 
+    security.declarePublic( 'listSchemas' )
+    def listSchemas( self ):
+        """ See IMetadataTool.
+        """
+        result = [ ( 'DCMI', self.DCMI ) ]
+        result.extend( self.objectItems( [ ElementSpec.meta_type ] ) )
+        return result
+
     security.declareProtected(ModifyPortalContent, 'setInitialMetadata')
     def setInitialMetadata( self, content ):
+        """ See IMetadataTool.
         """
-            Set initial values for content metatdata, supplying
-            any site-specific defaults.
-        """
-        for element, policy in self.listPolicies(content.getPortalTypeName()):
+        for schema_id, schema in self.listSchemas():
+            for element, policy in schema.listPolicies(
+                                    content.getPortalTypeName()):
 
-            if not getattr( content, element )():
+                if not getattr( content, element )():
 
-                if policy.supplyDefault():
-                    setter = getattr( content, 'set%s' % element )
-                    setter( policy.defaultValue() )
-                elif policy.isRequired():
-                    raise MetadataError, \
-                          'Metadata element %s is required.' % element
+                    if policy.supplyDefault():
+                        setter = getattr( content, 'set%s' % element )
+                        setter( policy.defaultValue() )
+                    elif policy.isRequired():
+                        raise MetadataError, \
+                            'Metadata element %s is required.' % element
 
-        # TODO:  Call initial_values_hook, if present
-
-
     security.declareProtected(View, 'validateMetadata')
     def validateMetadata( self, content ):
+        """ See IMetadataTool.
         """
-            Enforce portal-wide policies about DCI, e.g.,
-            requiring non-empty title/description, etc.  Called
-            by the CMF immediately before saving changes to the
-            metadata of an object.
-        """
-        for element, policy in self.listPolicies(content.getPortalTypeName()):
+        for schema_id, schema in self.listSchemas():
+            for element, policy in schema.listPolicies(
+                                    content.getPortalTypeName()):
 
-            value = getattr( content, element )()
-            if not value and policy.isRequired():
-                raise MetadataError, \
-                        'Metadata element %s is required.' % element
+                value = getattr( content, element )()
+                if not value and policy.isRequired():
+                    raise MetadataError, \
+                            'Metadata element %s is required.' % element
 
-            if value and policy.enforceVocabulary():
-                values = policy.isMultiValued() and value or [ value ]
-                for value in values:
-                    if not value in policy.allowedVocabulary():
-                        raise MetadataError, \
-                        'Value %s is not in allowed vocabulary for ' \
-                        'metadata element %s.' % ( value, element )
+                if value and policy.enforceVocabulary():
+                    values = policy.isMultiValued() and value or [ value ]
+                    for value in values:
+                        if not value in policy.allowedVocabulary():
+                            raise MetadataError, \
+                            'Value %s is not in allowed vocabulary for ' \
+                            'metadata element %s.' % ( value, element )
 
-        # TODO:  Call validation_hook, if present
-
 InitializeClass( MetadataTool )

Modified: CMF/branches/tseaver-metadatatool_refactoring/CMFDefault/tests/test_MetadataTool.py
===================================================================
--- CMF/branches/1.5/CMFDefault/tests/test_MetadataTool.py	2005-09-06 23:57:24 UTC (rev 38331)
+++ CMF/branches/tseaver-metadatatool_refactoring/CMFDefault/tests/test_MetadataTool.py	2005-09-07 02:10:28 UTC (rev 38332)
@@ -14,8 +14,7 @@
 
 $Id$
 """
-
-from unittest import TestCase, TestSuite, makeSuite, main
+import unittest
 import Testing
 try:
     import Zope2
@@ -25,110 +24,125 @@
 
 from Acquisition import aq_base
 
-from Products.CMFDefault.DublinCore import DefaultDublinCoreImpl
-from Products.CMFDefault.exceptions import MetadataError
-from Products.CMFDefault.MetadataTool import DEFAULT_ELEMENT_SPECS
-from Products.CMFDefault.MetadataTool import ElementSpec
-from Products.CMFDefault.MetadataTool import MetadataElementPolicy
-from Products.CMFDefault.MetadataTool import MetadataTool
 
+class TestMetadataElementPolicy( unittest.TestCase ):
 
-class TestMetadataElementPolicy( TestCase ):
+    def _getTargetClass( self ):
+        from Products.CMFDefault.MetadataTool import MetadataElementPolicy
+        return MetadataElementPolicy
 
-    def setUp( self ):
-        self.sv_policy = MetadataElementPolicy( 0 )
-        self.mv_policy = MetadataElementPolicy( 1 )
+    def _makeOne( self, *args, **kw ):
+        return self._getTargetClass()( *args, **kw )
 
-    def tearDown( self ):
-        del self.sv_policy
-        del self.mv_policy
+    def test_empty_single_valued( self ):
+        sv_policy = self._makeOne( 0 )
+        self.failIf( sv_policy.isMultiValued() )
+        self.failIf( sv_policy.isRequired() )
+        self.failIf( sv_policy.supplyDefault() )
+        self.failIf( sv_policy.defaultValue() )
+        self.failIf( sv_policy.enforceVocabulary() )
+        self.failIf( sv_policy.allowedVocabulary() )
 
-    def test_emptySV( self ):
-        assert not self.sv_policy.isMultiValued()
-        assert not self.sv_policy.isRequired()
-        assert not self.sv_policy.supplyDefault()
-        assert not self.sv_policy.defaultValue()
-        assert not self.sv_policy.enforceVocabulary()
-        assert not self.sv_policy.allowedVocabulary()
+    def test_edit_single_valued( self ):
+        sv_policy = self._makeOne( 0 )
+        sv_policy.edit( 1, 1, 'xxx', 0, '' ) 
+        self.failIf( sv_policy.isMultiValued() )
+        self.failUnless( sv_policy.isRequired() )
+        self.failUnless( sv_policy.supplyDefault() )
+        self.assertEquals( sv_policy.defaultValue(), 'xxx' )
+        self.failIf( sv_policy.enforceVocabulary() )
+        self.failIf( sv_policy.allowedVocabulary() )
 
-    def test_editSV( self ):
-        self.sv_policy.edit( 1, 1, 'xxx', 0, '' )
-        assert not self.sv_policy.isMultiValued()
-        assert self.sv_policy.isRequired()
-        assert self.sv_policy.supplyDefault()
-        assert self.sv_policy.defaultValue() == 'xxx'
-        assert not self.sv_policy.enforceVocabulary()
-        assert not self.sv_policy.allowedVocabulary()
+    def test_empty_multi_valued( self ):
+        mv_policy = self._makeOne( 1 )
+        self.failUnless( mv_policy.isMultiValued() )
+        self.failIf( mv_policy.isRequired() )
+        self.failIf( mv_policy.supplyDefault() )
+        self.failIf( mv_policy.defaultValue() )
+        self.failIf( mv_policy.enforceVocabulary() )
+        self.failIf( mv_policy.allowedVocabulary() )
 
-    def test_emptyMV( self ):
-        assert self.mv_policy.isMultiValued()
-        assert not self.mv_policy.isRequired()
-        assert not self.mv_policy.supplyDefault()
-        assert not self.mv_policy.defaultValue()
-        assert not self.mv_policy.enforceVocabulary()
-        assert not self.mv_policy.allowedVocabulary()
+    def test_edit_multi_valued( self ):
+        mv_policy = self._makeOne( 1 )
+        mv_policy.edit( 1, 1, 'xxx', 1, ( 'xxx', 'yyy' ) )
+        self.failUnless( mv_policy.isMultiValued() )
+        self.failUnless( mv_policy.isRequired() )
+        self.failUnless( mv_policy.supplyDefault() )
+        self.assertEqual( mv_policy.defaultValue(), 'xxx' )
+        self.failUnless( mv_policy.enforceVocabulary() )
+        self.assertEqual( len( mv_policy.allowedVocabulary() ), 2 )
+        self.failUnless( 'xxx' in mv_policy.allowedVocabulary() )
+        self.failUnless( 'yyy' in mv_policy.allowedVocabulary() )
 
-    def test_editMV( self ):
-        self.mv_policy.edit( 1, 1, 'xxx', 1, ( 'xxx', 'yyy' ) )
-        assert self.mv_policy.isMultiValued()
-        assert self.mv_policy.isRequired()
-        assert self.mv_policy.supplyDefault()
-        assert self.mv_policy.defaultValue() == 'xxx'
-        assert self.mv_policy.enforceVocabulary()
-        assert len( self.mv_policy.allowedVocabulary() ) == 2
-        assert 'xxx' in self.mv_policy.allowedVocabulary()
-        assert 'yyy' in self.mv_policy.allowedVocabulary()
 
-class TestElementSpec( TestCase ):
+class TestElementSpec( unittest.TestCase ):
 
-    def setUp( self ):
-        self.sv_spec    = ElementSpec( 0 )
-        self.mv_spec    = ElementSpec( 1 )
+    def _getTargetClass( self ):
+        from Products.CMFDefault.MetadataTool import ElementSpec
+        return ElementSpec
 
-    def tearDown( self ):
-        del self.sv_spec
-        del self.mv_spec
+    def _makeOne( self, *args, **kw ):
+        return self._getTargetClass()( *args, **kw )
 
-    def test_empty( self ):
-        assert not self.sv_spec.isMultiValued()
-        assert self.sv_spec.getPolicy() == self.sv_spec.getPolicy( 'XYZ' )
-        policies = self.sv_spec.listPolicies()
-        assert len( policies ) == 1
-        assert policies[0][0] is None
+    def test_empty_single_valued( self ):
+        sv_spec = self._makeOne( 0 )
+        self.failIf( sv_spec.isMultiValued() )
+        self.assertEqual( sv_spec.getPolicy(), sv_spec.getPolicy( 'XYZ' ) )
+        policies = sv_spec.listPolicies()
+        self.assertEqual( len( policies ), 1 )
+        self.assertEqual( policies[0][0], None )
 
-        assert self.mv_spec.isMultiValued()
-        assert self.mv_spec.getPolicy() == self.mv_spec.getPolicy( 'XYZ' )
-        policies = self.mv_spec.listPolicies()
-        assert len( policies ) == 1
-        assert policies[0][0] is None
+    def test_empty_multi_valued( self ):
+        mv_spec = self._makeOne( 1 )
+        self.failUnless( mv_spec.isMultiValued() )
+        self.assertEqual( mv_spec.getPolicy(), mv_spec.getPolicy( 'XYZ' ) )
+        policies = mv_spec.listPolicies()
+        self.assertEqual( len( policies ), 1 )
+        self.assertEqual( policies[0][0], None )
 
 
-class Foo( DefaultDublinCoreImpl ):
 
-    description = title = language = format = rights = ''
-    subject = ()
+class TestMetadataSchema( unittest.TestCase ):
 
-    def __init__( self ):
-        pass # skip DDCI's default values
+    def _getTargetClass( self ):
+        from Products.CMFDefault.MetadataTool import MetadataSchema
+        return MetadataSchema
 
-    def getPortalTypeName( self ):
-        return 'Foo'
+    def _makeOne( self, *args, **kw ):
+        return self._getTargetClass()( *args, **kw )
 
 
-class Bar( Foo ):
+class TestMetadataTool( unittest.TestCase ):
 
-    def getPortalTypeName( self ):
-        return 'Bar'
+    def _getTargetClass( self ):
+        from Products.CMFDefault.MetadataTool import MetadataTool
+        return MetadataTool
 
+    def _makeOne( self, *args, **kw ):
+        return self._getTargetClass()( *args, **kw )
 
-class TestMetadataTool( TestCase ):
+    def _makeTestObjects( self ):
 
-    def setUp( self ):
-        self.tool = MetadataTool()
+        from Products.CMFDefault.DublinCore import DefaultDublinCoreImpl
+        class Foo( DefaultDublinCoreImpl ):
 
-    def tearDown( self ):
-        del self.tool
+            description = title = language = format = rights = ''
+            subject = ()
 
+            def __init__( self ):
+                pass # skip DDCI's default values
+
+            def getPortalTypeName( self ):
+                return 'Foo'
+
+
+        class Bar( Foo ):
+
+            def getPortalTypeName( self ):
+                return 'Bar'
+
+        return Foo(), Bar()
+
     def test_z2interfaces(self):
         from Interface.Verify import verifyClass
         from Products.CMFCore.interfaces.portal_actions \
@@ -136,8 +150,8 @@
         from Products.CMFCore.interfaces.portal_metadata \
                 import portal_metadata as IMetadataTool
 
-        verifyClass(IActionProvider, MetadataTool)
-        verifyClass(IMetadataTool, MetadataTool)
+        verifyClass(IActionProvider, self._getTargetClass())
+        verifyClass(IMetadataTool, self._getTargetClass())
 
     def test_z3interfaces(self):
         try:
@@ -148,81 +162,84 @@
         from Products.CMFCore.interfaces import IActionProvider
         from Products.CMFCore.interfaces import IMetadataTool
 
-        verifyClass(IActionProvider, MetadataTool)
-        verifyClass(IMetadataTool, MetadataTool)
+        verifyClass(IActionProvider, self._getTargetClass())
+        verifyClass(IMetadataTool, self._getTargetClass())
 
     def test_empty( self ):
+        from Products.CMFDefault.MetadataTool import _DCMI_ELEMENT_SPECS
 
-        assert not self.tool.getPublisher()
-        assert self.tool.getFullName( 'foo' ) == 'foo'
+        tool = self._makeOne()
+        self.failIf( tool.getPublisher() )
+        self.assertEqual( tool.getFullName( 'foo' ), 'foo' )
 
-        specs = list( self.tool.listElementSpecs() )
-        defaults = list( DEFAULT_ELEMENT_SPECS )
+        dcmi = tool.DCMI
+        specs = list( dcmi.DCMI.listElementSpecs() )
+        defaults = list( _DCMI_ELEMENT_SPECS )
         specs.sort(); defaults.sort()
 
-        assert len( specs ) == len( defaults )
+        self.assertEqual( len( specs ), len( defaults ) )
         for i in range( len( specs ) ):
-            assert specs[i][0] == defaults[i][0]
-            assert specs[i][1].isMultiValued() == defaults[i][1]
+            self.assertEqual( specs[i][0], defaults[i][0] )
+            self.assertEqual( specs[i][1].isMultiValued(), defaults[i][1] )
             policies = specs[i][1].listPolicies()
-            assert len( policies ) == 1
-            assert policies[0][0] is None
+            self.assertEqual( len( policies ), 1 )
+            self.failUnless( policies[0][0] is None )
 
-        assert not self.tool.getElementSpec( 'Title'        ).isMultiValued()
-        assert not self.tool.getElementSpec( 'Description'  ).isMultiValued()
-        assert     self.tool.getElementSpec( 'Subject'      ).isMultiValued()
-        assert not self.tool.getElementSpec( 'Format'       ).isMultiValued()
-        assert not self.tool.getElementSpec( 'Language'     ).isMultiValued()
-        assert not self.tool.getElementSpec( 'Rights'       ).isMultiValued()
+        self.failIf( dcmi.getElementSpec( 'Title' ).isMultiValued() )
+        self.failIf( dcmi.getElementSpec( 'Description' ).isMultiValued() )
+        self.failUnless( dcmi.getElementSpec( 'Subject' ).isMultiValued() )
+        self.failIf( dcmi.getElementSpec( 'Format' ).isMultiValued() )
+        self.failIf( dcmi.getElementSpec( 'Language' ).isMultiValued() )
+        self.failIf( dcmi.getElementSpec( 'Rights' ).isMultiValued() )
 
         try:
-            dummy = self.tool.getElementSpec( 'Foo' )
+            dummy = dcmi.getElementSpec( 'Foo' )
         except KeyError:
             pass
         else:
-            assert 0, "Expected KeyError"
+            self.failUnless( 0, "Expected KeyError" )
 
-        assert not self.tool.listAllowedSubjects()
-        assert not self.tool.listAllowedFormats()
-        assert not self.tool.listAllowedLanguages()
-        assert not self.tool.listAllowedRights()
+        self.failIf( dcmi.listAllowedSubjects() )
+        self.failIf( dcmi.listAllowedFormats() )
+        self.failIf( dcmi.listAllowedLanguages() )
+        self.failIf( dcmi.listAllowedRights() )
 
-    def test_add( self ):
-        self.tool.addElementSpec( 'Rating', 1 )
-        assert len( self.tool.listElementSpecs() ) \
-            == len( DEFAULT_ELEMENT_SPECS ) + 1
-        rating = self.tool.getElementSpec( 'Rating' )
-        assert rating.isMultiValued()
+    def test_DCMI_addElementSpec( self ):
+        from Products.CMFDefault.MetadataTool import _DCMI_ELEMENT_SPECS
 
-    def test_remove( self ):
-        self.tool.removeElementSpec( 'Rights' )
+        tool = self._makeOne()
+        dcmi = tool.DCMI
+        dcmi.addElementSpec( 'Rating', 1 )
+        self.assertEqual( len( dcmi.listElementSpecs() )
+                        , len( _DCMI_ELEMENT_SPECS ) + 1 )
+        rating = dcmi.getElementSpec( 'Rating' )
+        self.failUnless( rating.isMultiValued() )
 
-        assert len( self.tool.listElementSpecs() ) \
-            == len( DEFAULT_ELEMENT_SPECS ) - 1
+    def test_DCMI_removeElementSpec( self ):
+        from Products.CMFDefault.MetadataTool import _DCMI_ELEMENT_SPECS
 
-        try:
-            dummy = self.tool.getElementSpec( 'Rights' )
-        except KeyError:
-            pass
-        else:
-            assert 0, "Expected KeyError"
+        tool = self._makeOne()
+        dcmi = tool.DCMI
+        dcmi.removeElementSpec( 'Rights' )
 
-        try:
-            self.tool.removeElementSpec( 'Foo' )
-        except KeyError:
-            pass
-        else:
-            assert 0, "Expected KeyError"
+        self.assertEqual( len( dcmi.listElementSpecs() )
+                        , len( _DCMI_ELEMENT_SPECS ) - 1
+                        )
 
+        self.assertRaises( KeyError, dcmi.getElementSpec, 'Rights' )
+        self.assertRaises( KeyError, dcmi.removeElementSpec, 'Foo' )
+
     def test_simplePolicies( self ):
 
-        tSpec = self.tool.getElementSpec( 'Title' )
+        tool = self._makeOne()
+        dcmi = tool.DCMI
+        tSpec = dcmi.getElementSpec( 'Title' )
 
         # Fetch default policy.
         tDef  = tSpec.getPolicy()
-        assert not tDef.isRequired()
-        assert not tDef.supplyDefault()
-        assert not tDef.defaultValue()
+        self.failIf( tDef.isRequired() )
+        self.failIf( tDef.supplyDefault() )
+        self.failIf( tDef.defaultValue() )
 
         # Fetch (default) policy for a type.
         tDoc  = tSpec.getPolicy( 'Document' )
@@ -230,40 +247,42 @@
 
         # Changing default changes policies found from there.
         tDef.edit( 1, 1, 'xyz', 0, () )
-        assert tDef.isRequired()
-        assert tDef.supplyDefault()
-        assert tDef.defaultValue() == 'xyz'
-        assert tDoc.isRequired()
-        assert tDoc.supplyDefault()
-        assert tDoc.defaultValue() == 'xyz'
+        self.failUnless( tDef.isRequired() )
+        self.failUnless( tDef.supplyDefault() )
+        self.assertEqual( tDef.defaultValue(), 'xyz' )
+        self.failUnless( tDoc.isRequired() )
+        self.failUnless( tDoc.supplyDefault() )
+        self.assertEqual( tDoc.defaultValue(), 'xyz' )
 
         tSpec.addPolicy( 'Document' )
-        assert len( tSpec.listPolicies() ) == 2
+        self.assertEqual( len( tSpec.listPolicies() ), 2 )
 
         tDoc  = tSpec.getPolicy( 'Document' )
         self.assertNotEqual(aq_base(tDoc), aq_base(tDef))
-        assert not tDoc.isRequired()
-        assert not tDoc.supplyDefault()
-        assert not tDoc.defaultValue()
+        self.failIf( tDoc.isRequired() )
+        self.failIf( tDoc.supplyDefault() )
+        self.failIf( tDoc.defaultValue() )
 
         tSpec.removePolicy( 'Document' )
         tDoc  = tSpec.getPolicy( 'Document' )
         self.assertEqual(aq_base(tDoc), aq_base(tDef))
-        assert tDoc.isRequired()
-        assert tDoc.supplyDefault()
-        assert tDoc.defaultValue() == 'xyz'
+        self.failUnless( tDoc.isRequired() )
+        self.failUnless( tDoc.supplyDefault() )
+        self.assertEqual( tDoc.defaultValue(), 'xyz' )
 
     def test_multiValuedPolicies( self ):
 
-        sSpec = self.tool.getElementSpec( 'Subject' )
+        tool = self._makeOne()
+        dcmi = tool.DCMI
+        sSpec = dcmi.getElementSpec( 'Subject' )
 
         # Fetch default policy.
         sDef  = sSpec.getPolicy()
-        assert not sDef.isRequired()
-        assert not sDef.supplyDefault()
-        assert not sDef.defaultValue()
-        assert not sDef.enforceVocabulary()
-        assert not sDef.allowedVocabulary()
+        self.failIf( sDef.isRequired() )
+        self.failIf( sDef.supplyDefault() )
+        self.failIf( sDef.defaultValue() )
+        self.failIf( sDef.enforceVocabulary() )
+        self.failIf( sDef.allowedVocabulary() )
 
         # Fetch (default) policy for a type.
         sDoc  = sSpec.getPolicy( 'Document' )
@@ -271,150 +290,168 @@
 
         # Changing default changes policies found from there.
         sDef.edit( 1, 1, 'xyz', 1, ( 'foo', 'bar' ) )
-        assert sDef.isRequired()
-        assert sDef.supplyDefault()
-        assert sDef.defaultValue() == 'xyz'
-        assert sDoc.isRequired()
-        assert sDoc.supplyDefault()
-        assert sDoc.defaultValue() == 'xyz'
-        assert sDef.enforceVocabulary()
-        assert len( sDef.allowedVocabulary() ) == 2
-        assert 'foo' in sDef.allowedVocabulary()
-        assert 'bar' in sDef.allowedVocabulary()
-        assert sDoc.enforceVocabulary()
-        assert len( sDoc.allowedVocabulary() ) == 2
-        assert 'foo' in sDoc.allowedVocabulary()
-        assert 'bar' in sDoc.allowedVocabulary()
+        self.failUnless( sDef.isRequired() )
+        self.failUnless( sDef.supplyDefault() )
+        self.assertEqual( sDef.defaultValue(), 'xyz' )
+        self.failUnless( sDoc.isRequired() )
+        self.failUnless( sDoc.supplyDefault() )
+        self.assertEqual( sDoc.defaultValue(), 'xyz' )
+        self.failUnless( sDef.enforceVocabulary() )
+        self.assertEqual( len( sDef.allowedVocabulary() ), 2 )
+        self.failUnless( 'foo' in sDef.allowedVocabulary() )
+        self.failUnless( 'bar' in sDef.allowedVocabulary() )
+        self.failUnless( sDoc.enforceVocabulary() )
+        self.assertEqual( len( sDoc.allowedVocabulary() ), 2 )
+        self.failUnless( 'foo' in sDoc.allowedVocabulary() )
+        self.failUnless( 'bar' in sDoc.allowedVocabulary() )
 
         sSpec.addPolicy( 'Document' )
-        assert len( sSpec.listPolicies() ) == 2
+        self.assertEqual( len( sSpec.listPolicies() ), 2 )
 
         sDoc  = sSpec.getPolicy( 'Document' )
         self.assertNotEqual(aq_base(sDoc), aq_base(sDef))
-        assert not sDoc.isRequired()
-        assert not sDoc.supplyDefault()
-        assert not sDoc.defaultValue()
-        assert not sDoc.enforceVocabulary()
-        assert not sDoc.allowedVocabulary()
+        self.failIf( sDoc.isRequired() )
+        self.failIf( sDoc.supplyDefault() )
+        self.failIf( sDoc.defaultValue() )
+        self.failIf( sDoc.enforceVocabulary() )
+        self.failIf( sDoc.allowedVocabulary() )
 
         sSpec.removePolicy( 'Document' )
         sDoc  = sSpec.getPolicy( 'Document' )
         self.assertEqual(aq_base(sDoc), aq_base(sDef))
-        assert sDoc.isRequired()
-        assert sDoc.supplyDefault()
-        assert sDoc.defaultValue() == 'xyz'
-        assert sDoc.enforceVocabulary()
-        assert len( sDoc.allowedVocabulary() ) == 2
-        assert 'foo' in sDoc.allowedVocabulary()
-        assert 'bar' in sDoc.allowedVocabulary()
+        self.failUnless( sDoc.isRequired() )
+        self.failUnless( sDoc.supplyDefault() )
+        self.assertEqual( sDoc.defaultValue(), 'xyz' )
+        self.failUnless( sDoc.enforceVocabulary() )
+        self.assertEqual( len( sDoc.allowedVocabulary() ), 2 )
+        self.failUnless( 'foo' in sDoc.allowedVocabulary() )
+        self.failUnless( 'bar' in sDoc.allowedVocabulary() )
 
     def test_vocabularies( self ):
-        fSpec   = self.tool.getElementSpec( 'Format' )
+        tool = self._makeOne()
+        dcmi = tool.DCMI
+        fSpec   = dcmi.getElementSpec( 'Format' )
         fDef    = fSpec.getPolicy()
         formats = ( 'text/plain', 'text/html' )
         fDef.edit( 0, 0, '', 0, ( 'text/plain', 'text/html' ) )
-        assert self.tool.listAllowedFormats() == formats
+        self.assertEqual( tool.listAllowedFormats(), formats )
 
-        foo = Foo()
-        assert self.tool.listAllowedFormats( foo ) == formats
+        foo, bar = self._makeTestObjects()
+
+        self.assertEqual( tool.listAllowedFormats( foo ), formats )
+
         fSpec.addPolicy( 'Foo' )
-        assert not self.tool.listAllowedFormats( foo )
+        self.failIf( tool.listAllowedFormats( foo ) )
+
         foo_formats = ( 'image/jpeg', 'image/gif', 'image/png' )
         fFoo        = fSpec.getPolicy( 'Foo' )
         fFoo.edit( 0, 0, '', 0, foo_formats )
-        assert self.tool.listAllowedFormats( foo ) == foo_formats
+        self.assertEqual( tool.listAllowedFormats( foo ), foo_formats )
 
-    def test_initialValues( self ):
-        foo = Foo()
-        assert not foo.Title()
-        assert not foo.Description()
-        assert not foo.Subject()
-        assert not foo.Format(), foo.Format()
-        assert not foo.Language()
-        assert not foo.Rights()
+    def test_initialValues_defaults( self ):
+        tool = self._makeOne()
+        foo, bar = self._makeTestObjects()
+        self.failIf( foo.Title() )
+        self.failIf( foo.Description() )
+        self.failIf( foo.Subject() )
+        self.failIf( foo.Format(), foo.Format() )
+        self.failIf( foo.Language() )
+        self.failIf( foo.Rights() )
 
-        self.tool.setInitialMetadata( foo )
-        assert not foo.Title()
-        assert not foo.Description()
-        assert not foo.Subject()
-        assert not foo.Format()
-        assert not foo.Language()
-        assert not foo.Rights()
+        tool.setInitialMetadata( foo )
+        self.failIf( foo.Title() )
+        self.failIf( foo.Description() )
+        self.failIf( foo.Subject() )
+        self.failIf( foo.Format() )
+        self.failIf( foo.Language() )
+        self.failIf( foo.Rights() )
 
+    def test_initialValues_implicit( self ):
         # Test default policy.
-        foo     = Foo()
-        fSpec   = self.tool.getElementSpec( 'Format' )
+        tool = self._makeOne()
+        dcmi = tool.DCMI
+        foo, bar = self._makeTestObjects()
+        fSpec   = dcmi.getElementSpec( 'Format' )
         fPolicy = fSpec.getPolicy()
         fPolicy.edit( 0, 1, 'text/plain', 0, () )
-        self.tool.setInitialMetadata( foo )
-        assert not foo.Title()
-        assert not foo.Description()
-        assert not foo.Subject()
-        assert foo.Format() == 'text/plain'
-        assert not foo.Language()
-        assert not foo.Rights()
+        tool.setInitialMetadata( foo )
+        self.failIf( foo.Title() )
+        self.failIf( foo.Description() )
+        self.failIf( foo.Subject() )
+        self.assertEqual( foo.Format(), 'text/plain' )
+        self.failIf( foo.Language() )
+        self.failIf( foo.Rights() )
 
+    def test_initialValues_explicit_raises_if_constraint_fails( self ):
+        from Products.CMFDefault.exceptions import MetadataError
+
         # Test type-specific policy.
-        foo     = Foo()
-        tSpec   = self.tool.getElementSpec( 'Title' )
+        tool = self._makeOne()
+        dcmi = tool.DCMI
+        foo, bar = self._makeTestObjects()
+        tSpec   = dcmi.getElementSpec( 'Title' )
         tSpec.addPolicy( 'Foo' )
         tPolicy = tSpec.getPolicy( foo.getPortalTypeName() )
         tPolicy.edit( 1, 0, '', 0, () )
 
-        try:
-            self.tool.setInitialMetadata( foo )
-        except MetadataError:
-            pass
-        else:
-            assert 0, "Expected MetadataError"
+        self.assertRaises( MetadataError, tool.setInitialMetadata, foo )
 
+    def test_initialValues_explicit_mutliple_types( self ):
+        from Products.CMFDefault.exceptions import MetadataError
+
+        tool = self._makeOne()
+        dcmi = tool.DCMI
+        foo, bar = self._makeTestObjects()
         foo.setTitle( 'Foo title' )
-        self.tool.setInitialMetadata( foo )
-        assert foo.Title() == 'Foo title'
-        assert not foo.Description()
-        assert not foo.Subject()
-        assert foo.Format() == 'text/plain'
-        assert not foo.Language()
-        assert not foo.Rights()
 
+        fSpec   = dcmi.getElementSpec( 'Format' )
+        fSpec.addPolicy( foo.getPortalTypeName() )
+        fPolicy = fSpec.getPolicy( foo.getPortalTypeName() )
+        fPolicy.edit( 0, 1, 'text/plain', 0, () )
+
+        tool.setInitialMetadata( foo )
+        self.assertEqual( foo.Title(), 'Foo title' )
+        self.failIf( foo.Description() )
+        self.failIf( foo.Subject() )
+        self.assertEqual( foo.Format(), 'text/plain' )
+        self.failIf( foo.Language() )
+        self.failIf( foo.Rights() )
+
         #   Ensure Foo's policy doesn't interfere with other types.
-        bar = Bar()
-        self.tool.setInitialMetadata( bar )
-        assert not bar.Title()
-        assert not bar.Description()
-        assert not bar.Subject()
-        assert bar.Format() == 'text/plain'
-        assert not bar.Language()
-        assert not bar.Rights()
+        tool.setInitialMetadata( bar )
+        self.failIf( bar.Title() )
+        self.failIf( bar.Description() )
+        self.failIf( bar.Subject() )
+        self.assertEqual( bar.Format(), '' )
+        self.failIf( bar.Language() )
+        self.failIf( bar.Rights() )
 
     def test_validation( self ):
+        from Products.CMFDefault.exceptions import MetadataError
 
-        foo = Foo()
-        self.tool.setInitialMetadata( foo )
-        self.tool.validateMetadata( foo )
+        tool = self._makeOne()
+        foo, bar = self._makeTestObjects()
+        tool.setInitialMetadata( foo )
+        tool.validateMetadata( foo )
 
-        tSpec   = self.tool.getElementSpec( 'Title' )
+        dcmi = tool.DCMI
+        tSpec   = dcmi.getElementSpec( 'Title' )
         tSpec.addPolicy( 'Foo' )
         tPolicy = tSpec.getPolicy( foo.getPortalTypeName() )
         tPolicy.edit( 1, 0, '', 0, () )
 
-        try:
-            self.tool.validateMetadata( foo )
-        except MetadataError:
-            pass
-        else:
-            assert 0, "Expected MetadataError"
+        self.assertRaises( MetadataError, tool.validateMetadata, foo )
 
         foo.setTitle( 'Foo title' )
-        self.tool.validateMetadata( foo )
+        tool.validateMetadata( foo )
 
 
 def test_suite():
-    return TestSuite((
-        makeSuite(TestMetadataElementPolicy),
-        makeSuite(TestElementSpec),
-        makeSuite(TestMetadataTool),
+    return unittest.TestSuite((
+        unittest.makeSuite(TestMetadataElementPolicy),
+        unittest.makeSuite(TestElementSpec),
+        unittest.makeSuite(TestMetadataTool),
         ))
 
 if __name__ == '__main__':
-    main(defaultTest='test_suite')
+    unittest.main(defaultTest='test_suite')



More information about the CMF-checkins mailing list