Back to index

plone3  3.1.7
test_tool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2004 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 tests for GenericSetup tool.
00014 
00015 $Id: test_tool.py 85170 2008-04-08 21:13:10Z wichert $
00016 """
00017 import copy
00018 import os
00019 import unittest
00020 import Testing
00021 
00022 from StringIO import StringIO
00023 
00024 from zope.component import adapter
00025 from zope.component import provideHandler
00026 from zope.component.globalregistry import base as base_registry
00027 
00028 from Acquisition import aq_base
00029 from OFS.Folder import Folder
00030 
00031 from Products.GenericSetup import profile_registry
00032 from Products.GenericSetup.upgrade import listUpgradeSteps
00033 from Products.GenericSetup.upgrade import UpgradeStep
00034 from Products.GenericSetup.upgrade import _registerUpgradeStep
00035 from Products.GenericSetup.testing import ExportImportZCMLLayer
00036 from Products.GenericSetup.tests.test_zcml import dummy_upgrade_handler
00037 
00038 from common import BaseRegistryTests
00039 from common import DummyExportContext
00040 from common import DummyImportContext
00041 from common import FilesystemTestBase
00042 from common import TarballTester
00043 from conformance import ConformsToISetupTool
00044 
00045 from Products.GenericSetup.interfaces import IBeforeProfileImportEvent
00046 from Products.GenericSetup.interfaces import IProfileImportedEvent
00047 
00048 _before_import_events = []
00049 @adapter(IBeforeProfileImportEvent)
00050 def handleBeforeProfileImportEvent(event):
00051     _before_import_events.append(event)
00052 
00053 _after_import_events = []
00054 @adapter(IProfileImportedEvent)
00055 def handleProfileImportedEvent(event):
00056     _after_import_events.append(event)
00057 
00058 _METADATA_XML = """<?xml version="1.0"?>
00059 <metadata>
00060   <version>1.0</version>
00061   <dependencies>
00062     <dependency>profile-other:bar</dependency>
00063   </dependencies>
00064 </metadata>
00065 """
00066 
00067 
00068 class SetupToolTests(FilesystemTestBase, TarballTester, ConformsToISetupTool):
00069 
00070     layer = ExportImportZCMLLayer
00071 
00072     _PROFILE_PATH = '/tmp/STT_test'
00073     _PROFILE_PATH2 = '/tmp/STT_test2'
00074 
00075     def afterSetUp(self):
00076         self._profile_registry_info = profile_registry._profile_info
00077         self._profile_registry_ids = profile_registry._profile_ids
00078         profile_registry.clear()
00079         global _before_import_events
00080         global _after_import_events
00081         _before_import_events = []
00082         provideHandler(handleBeforeProfileImportEvent)
00083         _after_import_events = []
00084         provideHandler(handleProfileImportedEvent)
00085 
00086     def beforeTearDown(self):
00087         profile_registry._profile_info = self._profile_registry_info
00088         profile_registry._profile_ids = self._profile_registry_ids
00089         base_registry.unregisterHandler(handleBeforeProfileImportEvent)
00090         base_registry.unregisterHandler(handleProfileImportedEvent)
00091         FilesystemTestBase.beforeTearDown(self)
00092 
00093     def _getTargetClass(self):
00094         from Products.GenericSetup.tool import SetupTool
00095 
00096         return SetupTool
00097 
00098     def _makeSite( self, title="Don't care" ):
00099 
00100         site = Folder()
00101         site._setId( 'site' )
00102         site.title = title
00103 
00104         self.app._setObject( 'site', site )
00105         return self.app._getOb( 'site' )
00106 
00107     def test_empty( self ):
00108 
00109         tool = self._makeOne('setup_tool')
00110 
00111         self.assertEqual( tool.getBaselineContextID(), '' )
00112 
00113         import_registry = tool.getImportStepRegistry()
00114         self.assertEqual( len( import_registry.listSteps() ), 0 )
00115 
00116         export_registry = tool.getExportStepRegistry()
00117         export_steps = export_registry.listSteps()
00118         self.assertEqual(len(export_steps), 0)
00119 
00120         toolset_registry = tool.getToolsetRegistry()
00121         self.assertEqual( len( toolset_registry.listForbiddenTools() ), 0 )
00122         self.assertEqual( len( toolset_registry.listRequiredTools() ), 0 )
00123 
00124     def test_getBaselineContextID( self ):
00125 
00126         from Products.GenericSetup.tool import IMPORT_STEPS_XML
00127         from Products.GenericSetup.tool import EXPORT_STEPS_XML
00128         from Products.GenericSetup.tool import TOOLSET_XML
00129         from test_registry import _EMPTY_IMPORT_XML
00130         from test_registry import _EMPTY_EXPORT_XML
00131         from test_registry import _EMPTY_TOOLSET_XML
00132 
00133         tool = self._makeOne('setup_tool')
00134 
00135         self._makeFile(IMPORT_STEPS_XML, _EMPTY_IMPORT_XML)
00136         self._makeFile(EXPORT_STEPS_XML, _EMPTY_EXPORT_XML)
00137         self._makeFile(TOOLSET_XML, _EMPTY_TOOLSET_XML)
00138 
00139         profile_registry.registerProfile('foo', 'Foo', '', self._PROFILE_PATH)
00140         tool.setBaselineContext('profile-other:foo')
00141 
00142         self.assertEqual( tool.getBaselineContextID(), 'profile-other:foo' )
00143 
00144     def test_setBaselineContext_invalid( self ):
00145 
00146         tool = self._makeOne('setup_tool')
00147 
00148         self.assertRaises( KeyError
00149                          , tool.setBaselineContext
00150                          , 'profile-foo'
00151                          )
00152 
00153     def test_setBaselineContext_empty_string( self ):
00154 
00155         tool = self._makeOne('setup_tool')
00156 
00157         self.assertRaises( KeyError
00158                          , tool.setBaselineContext
00159                          , ''
00160                          )
00161 
00162     def test_setBaselineContext( self ):
00163 
00164         from Products.GenericSetup.tool import IMPORT_STEPS_XML
00165         from Products.GenericSetup.tool import EXPORT_STEPS_XML
00166         from Products.GenericSetup.tool import TOOLSET_XML
00167         from test_registry import _SINGLE_IMPORT_XML
00168         from test_registry import _SINGLE_EXPORT_XML
00169         from test_registry import _NORMAL_TOOLSET_XML
00170         from test_registry import ONE_FUNC
00171 
00172         tool = self._makeOne('setup_tool')
00173         tool.getExportStepRegistry().clear()
00174 
00175         self._makeFile(IMPORT_STEPS_XML, _SINGLE_IMPORT_XML)
00176         self._makeFile(EXPORT_STEPS_XML, _SINGLE_EXPORT_XML)
00177         self._makeFile(TOOLSET_XML, _NORMAL_TOOLSET_XML)
00178 
00179         profile_registry.registerProfile('foo', 'Foo', '', self._PROFILE_PATH)
00180         tool.setBaselineContext('profile-other:foo')
00181 
00182         self.assertEqual( tool.getBaselineContextID(), 'profile-other:foo' )
00183 
00184         import_registry = tool.getImportStepRegistry()
00185         self.assertEqual( len( import_registry.listSteps() ), 1 )
00186         self.failUnless( 'one' in import_registry.listSteps() )
00187         info = import_registry.getStepMetadata( 'one' )
00188         self.assertEqual( info[ 'id' ], 'one' )
00189         self.assertEqual( info[ 'title' ], 'One Step' )
00190         self.assertEqual( info[ 'version' ], '1' )
00191         self.failUnless( 'One small step' in info[ 'description' ] )
00192         self.assertEqual( info[ 'handler' ]
00193                         , 'Products.GenericSetup.tests.test_registry.ONE_FUNC')
00194 
00195         self.assertEqual( import_registry.getStep( 'one' ), ONE_FUNC )
00196 
00197         export_registry = tool.getExportStepRegistry()
00198         self.assertEqual( len( export_registry.listSteps() ), 1 )
00199         self.failUnless( 'one' in import_registry.listSteps() )
00200         info = export_registry.getStepMetadata( 'one' )
00201         self.assertEqual( info[ 'id' ], 'one' )
00202         self.assertEqual( info[ 'title' ], 'One Step' )
00203         self.failUnless( 'One small step' in info[ 'description' ] )
00204         self.assertEqual( info[ 'handler' ]
00205                         , 'Products.GenericSetup.tests.test_registry.ONE_FUNC')
00206 
00207         self.assertEqual( export_registry.getStep( 'one' ), ONE_FUNC )
00208 
00209     def test_runImportStep_nonesuch( self ):
00210 
00211         site = self._makeSite()
00212 
00213         tool = self._makeOne('setup_tool').__of__( site )
00214 
00215         self.assertRaises( KeyError, tool.runImportStepFromProfile,
00216                            '', 'nonesuch' )
00217 
00218     def test_runImportStep_simple( self ):
00219 
00220         TITLE = 'original title'
00221         site = self._makeSite( TITLE )
00222 
00223         tool = self._makeOne('setup_tool').__of__( site )
00224 
00225         registry = tool.getImportStepRegistry()
00226         registry.registerStep( 'simple', '1', _uppercaseSiteTitle )
00227 
00228         result = tool.runImportStepFromProfile( 'snapshot-dummy', 'simple' )
00229 
00230         self.assertEqual( len( result[ 'steps' ] ), 1 )
00231 
00232         self.assertEqual( result[ 'steps' ][ 0 ], 'simple' )
00233         self.assertEqual( result[ 'messages' ][ 'simple' ]
00234                         , 'Uppercased title' )
00235 
00236         self.assertEqual( site.title, TITLE.upper() )
00237 
00238         global _before_import_events
00239         self.assertEqual( len(_before_import_events), 1)
00240         self.assertEqual(_before_import_events[0].profile_id, 'snapshot-dummy')
00241         self.assertEqual(_before_import_events[0].steps, ['simple'])
00242         self.assertEqual(_before_import_events[0].full_import, False)
00243 
00244         global _after_import_events
00245         self.assertEqual( len(_after_import_events), 1)
00246         self.assertEqual(_after_import_events[0].profile_id, 'snapshot-dummy')
00247         self.assertEqual(_after_import_events[0].steps, ['simple'])
00248         self.assertEqual(_after_import_events[0].full_import, False)
00249 
00250     def test_runImportStep_dependencies( self ):
00251 
00252         TITLE = 'original title'
00253         site = self._makeSite( TITLE )
00254 
00255         tool = self._makeOne('setup_tool').__of__( site )
00256 
00257         registry = tool.getImportStepRegistry()
00258         registry.registerStep( 'dependable', '1', _underscoreSiteTitle )
00259         registry.registerStep( 'dependent', '1'
00260                              , _uppercaseSiteTitle, ( 'dependable', ) )
00261 
00262         result = tool.runImportStepFromProfile( 'snapshot-dummy', 'dependent' )
00263 
00264         self.assertEqual( len( result[ 'steps' ] ), 2 )
00265 
00266         self.assertEqual( result[ 'steps' ][ 0 ], 'dependable' )
00267         self.assertEqual( result[ 'messages' ][ 'dependable' ]
00268                         , 'Underscored title' )
00269 
00270         self.assertEqual( result[ 'steps' ][ 1 ], 'dependent' )
00271         self.assertEqual( result[ 'messages' ][ 'dependent' ]
00272                         , 'Uppercased title' )
00273         self.assertEqual( site.title, TITLE.replace( ' ', '_' ).upper() )
00274 
00275         global _before_import_events
00276         self.assertEqual( len(_before_import_events), 1)
00277         self.assertEqual(_before_import_events[0].profile_id, 'snapshot-dummy')
00278         self.assertEqual(_before_import_events[0].steps, ['dependable', 'dependent'])
00279         self.assertEqual(_before_import_events[0].full_import, False)
00280 
00281         global _after_import_events
00282         self.assertEqual( len(_after_import_events), 1)
00283         self.assertEqual(_after_import_events[0].profile_id, 'snapshot-dummy')
00284         self.assertEqual(_after_import_events[0].steps, ['dependable', 'dependent'])
00285         self.assertEqual(_after_import_events[0].full_import, False)
00286 
00287 
00288     def test_runImportStep_skip_dependencies( self ):
00289 
00290         TITLE = 'original title'
00291         site = self._makeSite( TITLE )
00292 
00293         tool = self._makeOne('setup_tool').__of__( site )
00294 
00295         registry = tool.getImportStepRegistry()
00296         registry.registerStep( 'dependable', '1', _underscoreSiteTitle )
00297         registry.registerStep( 'dependent', '1'
00298                              , _uppercaseSiteTitle, ( 'dependable', ) )
00299 
00300         result = tool.runImportStepFromProfile( 'snapshot-dummy', 'dependent',
00301                                                 run_dependencies=False )
00302 
00303         self.assertEqual( len( result[ 'steps' ] ), 1 )
00304 
00305         self.assertEqual( result[ 'steps' ][ 0 ], 'dependent' )
00306         self.assertEqual( result[ 'messages' ][ 'dependent' ]
00307                         , 'Uppercased title' )
00308 
00309         self.assertEqual( site.title, TITLE.upper() )
00310 
00311         global _before_import_events
00312         self.assertEqual( len(_before_import_events), 1)
00313         self.assertEqual(_before_import_events[0].profile_id, 'snapshot-dummy')
00314         self.assertEqual(_before_import_events[0].steps, ['dependent'])
00315         self.assertEqual(_before_import_events[0].full_import, False)
00316 
00317         global _after_import_events
00318         self.assertEqual( len(_after_import_events), 1)
00319         self.assertEqual(_after_import_events[0].profile_id, 'snapshot-dummy')
00320         self.assertEqual(_after_import_events[0].steps, ['dependent'])
00321         self.assertEqual(_after_import_events[0].full_import, False)
00322 
00323     def test_runImportStep_default_purge( self ):
00324 
00325         site = self._makeSite()
00326 
00327         tool = self._makeOne('setup_tool').__of__( site )
00328         registry = tool.getImportStepRegistry()
00329         registry.registerStep( 'purging', '1', _purgeIfRequired )
00330 
00331         result = tool.runImportStepFromProfile( 'snapshot-dummy', 'purging' )
00332 
00333         self.assertEqual( len( result[ 'steps' ] ), 1 )
00334         self.assertEqual( result[ 'steps' ][ 0 ], 'purging' )
00335         self.assertEqual( result[ 'messages' ][ 'purging' ], 'Purged' )
00336         self.failUnless( site.purged )
00337 
00338     def test_runImportStep_explicit_purge( self ):
00339 
00340         site = self._makeSite()
00341 
00342         tool = self._makeOne('setup_tool').__of__( site )
00343         registry = tool.getImportStepRegistry()
00344         registry.registerStep( 'purging', '1', _purgeIfRequired )
00345 
00346         result = tool.runImportStepFromProfile( 'snapshot-dummy', 'purging',
00347                                                 purge_old=True )
00348 
00349         self.assertEqual( len( result[ 'steps' ] ), 1 )
00350         self.assertEqual( result[ 'steps' ][ 0 ], 'purging' )
00351         self.assertEqual( result[ 'messages' ][ 'purging' ], 'Purged' )
00352         self.failUnless( site.purged )
00353 
00354     def test_runImportStep_skip_purge( self ):
00355 
00356         site = self._makeSite()
00357 
00358         tool = self._makeOne('setup_tool').__of__( site )
00359         registry = tool.getImportStepRegistry()
00360         registry.registerStep( 'purging', '1', _purgeIfRequired )
00361 
00362         result = tool.runImportStepFromProfile( 'snapshot-dummy', 'purging',
00363                                                 purge_old=False )
00364 
00365         self.assertEqual( len( result[ 'steps' ] ), 1 )
00366         self.assertEqual( result[ 'steps' ][ 0 ], 'purging' )
00367         self.assertEqual( result[ 'messages' ][ 'purging' ], 'Unpurged' )
00368         self.failIf( site.purged )
00369 
00370     def test_runImportStep_consistent_context( self ):
00371 
00372         site = self._makeSite()
00373 
00374         tool = self._makeOne('setup_tool').__of__( site )
00375 
00376         registry = tool.getImportStepRegistry()
00377         registry.registerStep( 'purging', '1', _purgeIfRequired )
00378         registry.registerStep( 'dependent', '1'
00379                              , _uppercaseSiteTitle, ( 'purging', ) )
00380 
00381         result = tool.runImportStepFromProfile( 'snapshot-dummy', 'dependent',
00382                                                 purge_old=False )
00383         self.failIf( site.purged )
00384 
00385     def test_runAllImportSteps_empty( self ):
00386 
00387         site = self._makeSite()
00388         tool = self._makeOne('setup_tool').__of__( site )
00389 
00390         result = tool.runAllImportStepsFromProfile('snapshot-dummy')
00391 
00392         self.assertEqual( len(result['steps']), 3 )
00393 
00394     def test_runAllImportSteps_sorted_default_purge( self ):
00395 
00396         TITLE = 'original title'
00397         PROFILE_ID = 'snapshot-testing'
00398         site = self._makeSite( TITLE )
00399         tool = self._makeOne('setup_tool').__of__( site )
00400 
00401         registry = tool.getImportStepRegistry()
00402         registry.registerStep( 'dependable', '1'
00403                              , _underscoreSiteTitle, ( 'purging', ) )
00404         registry.registerStep( 'dependent', '1'
00405                              , _uppercaseSiteTitle, ( 'dependable', ) )
00406         registry.registerStep( 'purging', '1'
00407                              , _purgeIfRequired )
00408 
00409         result = tool.runAllImportStepsFromProfile(PROFILE_ID)
00410 
00411         self.assertEqual( len(result['steps']), 6 )
00412 
00413         self.assertEqual( result['steps'][3], 'purging' )
00414         self.assertEqual( result[ 'messages' ][ 'purging' ]
00415                         , 'Purged' )
00416 
00417         self.assertEqual( result['steps'][4], 'dependable' )
00418         self.assertEqual( result[ 'messages' ][ 'dependable' ]
00419                         , 'Underscored title' )
00420 
00421         self.assertEqual( result['steps'][5], 'dependent' )
00422         self.assertEqual( result[ 'messages' ][ 'dependent' ]
00423                         , 'Uppercased title' )
00424 
00425         self.assertEqual( site.title, TITLE.replace( ' ', '_' ).upper() )
00426         self.failUnless( site.purged )
00427 
00428         prefix = 'import-all-%s' % PROFILE_ID
00429         logged = [x for x in tool.objectIds('File') if x.startswith(prefix)]
00430         self.assertEqual(len(logged), 1)
00431 
00432     def test_runAllImportSteps_unicode_profile_id_creates_reports( self ):
00433 
00434         TITLE = 'original title'
00435         PROFILE_ID = u'snapshot-testing'
00436         site = self._makeSite( TITLE )
00437         tool = self._makeOne('setup_tool').__of__( site )
00438 
00439         registry = tool.getImportStepRegistry()
00440         registry.registerStep( 'dependable', '1'
00441                              , _underscoreSiteTitle, ( 'purging', ) )
00442         registry.registerStep( 'dependent', '1'
00443                              , _uppercaseSiteTitle, ( 'dependable', ) )
00444         registry.registerStep( 'purging', '1'
00445                              , _purgeIfRequired )
00446 
00447         tool.runAllImportStepsFromProfile(PROFILE_ID)
00448 
00449         prefix = ('import-all-%s' % PROFILE_ID).encode('UTF-8')
00450         logged = [x for x in tool.objectIds('File') if x.startswith(prefix)]
00451         self.assertEqual(len(logged), 1)
00452 
00453     def test_runAllImportSteps_sorted_explicit_purge( self ):
00454 
00455         site = self._makeSite()
00456         tool = self._makeOne('setup_tool').__of__( site )
00457 
00458         registry = tool.getImportStepRegistry()
00459         registry.registerStep( 'dependable', '1'
00460                              , _underscoreSiteTitle, ( 'purging', ) )
00461         registry.registerStep( 'dependent', '1'
00462                              , _uppercaseSiteTitle, ( 'dependable', ) )
00463         registry.registerStep( 'purging', '1'
00464                              , _purgeIfRequired )
00465 
00466         result = tool.runAllImportStepsFromProfile( 'snapshot-dummy', purge_old=True )
00467 
00468         self.assertEqual( len(result['steps']), 6 )
00469 
00470         self.assertEqual( result['steps'][3], 'purging' )
00471         self.assertEqual( result[ 'messages' ][ 'purging' ]
00472                         , 'Purged' )
00473 
00474         self.assertEqual( result['steps'][4], 'dependable' )
00475         self.assertEqual( result['steps'][5], 'dependent' )
00476         self.failUnless( site.purged )
00477 
00478     def test_runAllImportSteps_sorted_skip_purge( self ):
00479 
00480         site = self._makeSite()
00481         tool = self._makeOne('setup_tool').__of__( site )
00482 
00483         registry = tool.getImportStepRegistry()
00484         registry.registerStep( 'dependable', '1'
00485                              , _underscoreSiteTitle, ( 'purging', ) )
00486         registry.registerStep( 'dependent', '1'
00487                              , _uppercaseSiteTitle, ( 'dependable', ) )
00488         registry.registerStep( 'purging', '1'
00489                              , _purgeIfRequired )
00490 
00491         result = tool.runAllImportStepsFromProfile( 'snapshot-dummy', purge_old=False )
00492 
00493         self.assertEqual( len(result['steps']), 6 )
00494 
00495         self.assertEqual( result['steps'][3], 'purging' )
00496         self.assertEqual( result[ 'messages' ][ 'purging' ]
00497                         , 'Unpurged' )
00498 
00499         self.assertEqual( result['steps'][4], 'dependable' )
00500         self.assertEqual( result['steps'][5], 'dependent' )
00501         self.failIf( site.purged )
00502 
00503     def test_runAllImportStepsFromProfileWithoutDepends( self ):
00504         from Products.GenericSetup.metadata import METADATA_XML
00505 
00506         self._makeFile(METADATA_XML, _METADATA_XML)
00507 
00508         site = self._makeSite()
00509         tool = self._makeOne('setup_tool').__of__( site )
00510 
00511         profile_registry.registerProfile('foo', 'Foo', '', self._PROFILE_PATH)
00512 
00513         _imported = []
00514         def applyContext(context):
00515             _imported.append(context._profile_path)
00516 
00517         tool.applyContext=applyContext
00518         result = tool.runAllImportStepsFromProfile('profile-other:foo', ignore_dependencies=True)
00519         self.assertEqual(_imported, [self._PROFILE_PATH])
00520 
00521     def test_runAllImportStepsFromProfileWithDepends( self ):
00522         from Products.GenericSetup.metadata import METADATA_XML
00523 
00524         self._makeFile(METADATA_XML, _METADATA_XML)
00525 
00526         site = self._makeSite()
00527         tool = self._makeOne('setup_tool').__of__( site )
00528 
00529         profile_registry.registerProfile('foo', 'Foo', '', self._PROFILE_PATH)
00530         profile_registry.registerProfile('bar', 'Bar', '', self._PROFILE_PATH2)
00531 
00532         _imported = []
00533         def applyContext(context):
00534             _imported.append(context._profile_path)
00535 
00536         tool.applyContext=applyContext
00537         result = tool.runAllImportStepsFromProfile('profile-other:foo',
00538                                                    ignore_dependencies=False)
00539         self.assertEqual(_imported, [self._PROFILE_PATH2, self._PROFILE_PATH])
00540 
00541 
00542     def test_runAllImportStepsFromProfileStepRegistrationWithDepends( self ):
00543         from Products.GenericSetup.metadata import METADATA_XML
00544 
00545         self._makeFile(METADATA_XML, _METADATA_XML)
00546 
00547         _IMPORT_STEPS_XML = """<?xml version="1.0"?>
00548 <import-steps>
00549  <import-step id="one"
00550              version="1"
00551              handler="Products.GenericSetup.tests.common.dummy_handler"
00552              title="One Step">
00553   One small step
00554  </import-step>
00555 </import-steps>
00556 """
00557         self._makeFile('import_steps.xml', _IMPORT_STEPS_XML)
00558 
00559         site = self._makeSite()
00560         tool = self._makeOne('setup_tool').__of__( site )
00561 
00562         profile_registry.registerProfile('foo', 'Foo', '', self._PROFILE_PATH)
00563         profile_registry.registerProfile('bar', 'Bar', '', self._PROFILE_PATH2)
00564 
00565         result = tool.runAllImportStepsFromProfile('profile-other:foo',
00566                                                    ignore_dependencies=False)
00567 
00568         # ensure the additional step on foo was imported
00569         self.failUnless('one' in result['steps'])
00570 
00571 
00572 
00573     def test_runExportStep_nonesuch( self ):
00574 
00575         site = self._makeSite()
00576         tool = self._makeOne('setup_tool').__of__( site )
00577 
00578         self.assertRaises( ValueError, tool.runExportStep, 'nonesuch' )
00579 
00580     def test_runExportStep_step_registry_empty(self):
00581         site = self._makeSite()
00582         site.setup_tool = self._makeOne('setup_tool')
00583         tool = site.setup_tool
00584 
00585         result = tool.runExportStep( 'step_registries' )
00586 
00587         self.assertEqual( len( result[ 'steps' ] ), 1 )
00588         self.assertEqual( result[ 'steps' ][ 0 ], 'step_registries' )
00589         self.assertEqual( result[ 'messages' ][ 'step_registries' ]
00590                         , None
00591                         )
00592 
00593     def test_runExportStep_step_registry_default(self):
00594         site = self._makeSite()
00595         site.setup_tool = self._makeOne('setup_tool')
00596         tool = site.setup_tool
00597         tool._import_registry.registerStep('foo', handler='foo.bar')
00598         tool._export_registry.registerStep('step_registries',
00599                 'Products.GenericSetup.tool.exportStepRegistries',
00600                 'Export import / export steps.')
00601 
00602         result = tool.runExportStep( 'step_registries' )
00603 
00604         self.assertEqual( len( result[ 'steps' ] ), 1 )
00605         self.assertEqual( result[ 'steps' ][ 0 ], 'step_registries' )
00606         self.assertEqual( result[ 'messages' ][ 'step_registries' ]
00607                         , None
00608                         )
00609         fileish = StringIO( result[ 'tarball' ] )
00610 
00611         self._verifyTarballContents( fileish, [ 'import_steps.xml'
00612                                               , 'export_steps.xml'
00613                                               ] )
00614         self._verifyTarballEntryXML( fileish, 'import_steps.xml'
00615                                    , _DEFAULT_STEP_REGISTRIES_IMPORT_XML )
00616         self._verifyTarballEntryXML( fileish, 'export_steps.xml'
00617                                    , _DEFAULT_STEP_REGISTRIES_EXPORT_XML )
00618 
00619     def test_runAllExportSteps_empty(self):
00620         site = self._makeSite()
00621         site.setup_tool = self._makeOne('setup_tool')
00622         tool = site.setup_tool
00623 
00624         result = tool.runAllExportSteps()
00625 
00626         self.assertEqual( len(result['steps'] ), 4 )
00627         self.assertEqual( result['steps'][1], 'step_registries' )
00628         self.assertEqual( result[ 'messages' ][ 'step_registries' ]
00629                         , None
00630                         )
00631 
00632     def test_runAllExportSteps_default(self):
00633         site = self._makeSite()
00634         site.setup_tool = self._makeOne('setup_tool')
00635         tool = site.setup_tool
00636         tool._import_registry.registerStep('foo', handler='foo.bar')
00637         tool._export_registry.registerStep('step_registries',
00638                 'Products.GenericSetup.tool.exportStepRegistries',
00639                 'Export import / export steps.')
00640 
00641         result = tool.runAllExportSteps()
00642 
00643         self.assertEqual( len(result['steps']), 4 )
00644         self.assertEqual( result['steps'][1], 'step_registries' )
00645         self.assertEqual( result[ 'messages' ][ 'step_registries' ]
00646                         , None
00647                         )
00648         fileish = StringIO( result[ 'tarball' ] )
00649 
00650         self._verifyTarballContents( fileish, [ 'componentregistry.xml'
00651                                               , 'import_steps.xml'
00652                                               , 'export_steps.xml'
00653                                               , 'rolemap.xml'
00654                                               , 'toolset.xml'
00655                                               ] )
00656         self._verifyTarballEntryXML( fileish, 'import_steps.xml'
00657                                    , _DEFAULT_STEP_REGISTRIES_IMPORT_XML )
00658         self._verifyTarballEntryXML( fileish, 'export_steps.xml'
00659                                    , _DEFAULT_STEP_REGISTRIES_EXPORT_XML )
00660 
00661     def test_runAllExportSteps_extras( self ):
00662         site = self._makeSite()
00663         site.setup_tool = self._makeOne('setup_tool')
00664         tool = site.setup_tool
00665         tool._export_registry.registerStep('step_registries',
00666                 'Products.GenericSetup.tool.exportStepRegistries',
00667                 'Export import / export steps.')
00668 
00669         import_reg = tool.getImportStepRegistry()
00670         import_reg.registerStep( 'dependable', '1'
00671                                , _underscoreSiteTitle, ( 'purging', ) )
00672         import_reg.registerStep( 'dependent', '1'
00673                                , _uppercaseSiteTitle, ( 'dependable', ) )
00674         import_reg.registerStep( 'purging', '1'
00675                                , _purgeIfRequired )
00676 
00677         export_reg = tool.getExportStepRegistry()
00678         export_reg.registerStep( 'properties'
00679                                , _exportPropertiesINI )
00680 
00681         result = tool.runAllExportSteps()
00682 
00683         self.assertEqual( len(result['steps']), 5 )
00684 
00685         self.failUnless( 'properties' in result[ 'steps' ] )
00686         self.assertEqual( result[ 'messages' ][ 'properties' ]
00687                         , 'Exported properties'
00688                         )
00689 
00690         self.failUnless( 'step_registries' in result[ 'steps' ] )
00691         self.assertEqual( result[ 'messages' ][ 'step_registries' ]
00692                         , None
00693                         )
00694 
00695         fileish = StringIO( result[ 'tarball' ] )
00696 
00697         self._verifyTarballContents( fileish, [ 'componentregistry.xml'
00698                                               , 'import_steps.xml'
00699                                               , 'export_steps.xml'
00700                                               , 'properties.ini'
00701                                               , 'rolemap.xml'
00702                                               , 'toolset.xml'
00703                                               ] )
00704         self._verifyTarballEntryXML( fileish, 'import_steps.xml'
00705                                    , _EXTRAS_STEP_REGISTRIES_IMPORT_XML )
00706         self._verifyTarballEntryXML( fileish, 'export_steps.xml'
00707                                    , _EXTRAS_STEP_REGISTRIES_EXPORT_XML )
00708         self._verifyTarballEntry( fileish, 'properties.ini'
00709                                 , _PROPERTIES_INI % site.title  )
00710 
00711     def test_createSnapshot_default( self ):
00712         _EXPECTED = [('import_steps.xml', _DEFAULT_STEP_REGISTRIES_IMPORT_XML),
00713                      ('export_steps.xml', _DEFAULT_STEP_REGISTRIES_EXPORT_XML),
00714                      ('rolemap.xml', 'dummy'),
00715                      ('toolset.xml', 'dummy'),
00716                      ('componentregistry.xml', 'dummy')]
00717 
00718         site = self._makeSite()
00719         site.setup_tool = self._makeOne('setup_tool')
00720         tool = site.setup_tool
00721         tool._import_registry.registerStep('foo', handler='foo.bar')
00722         tool._export_registry.registerStep('step_registries',
00723                 'Products.GenericSetup.tool.exportStepRegistries',
00724                 'Export import / export steps.')
00725 
00726         self.assertEqual( len( tool.listSnapshotInfo() ), 0 )
00727 
00728         result = tool.createSnapshot( 'default' )
00729 
00730         self.assertEqual( len(result['steps']), 4 )
00731         self.assertEqual( result['steps'][1], 'step_registries' )
00732         self.assertEqual( result[ 'messages' ][ 'step_registries' ]
00733                         , None
00734                         )
00735 
00736         snapshot = result[ 'snapshot' ]
00737 
00738         self.assertEqual( len( snapshot.objectIds() ), len( _EXPECTED ) )
00739 
00740         for id in [ x[0] for x in _EXPECTED ]:
00741             self.failUnless( id in snapshot.objectIds() )
00742 
00743         def normalize_xml(xml):
00744             # using this might mask a real problem on windows, but so far the
00745             # different newlines just caused problems in this test
00746             lines = [ line for line in xml.splitlines() if line ]
00747             return '\n'.join(lines) + '\n'
00748 
00749         fileobj = snapshot._getOb( 'import_steps.xml' )
00750         self.assertEqual( normalize_xml( fileobj.read() ),
00751                           _DEFAULT_STEP_REGISTRIES_IMPORT_XML )
00752 
00753         fileobj = snapshot._getOb( 'export_steps.xml' )
00754         self.assertEqual( normalize_xml( fileobj.read() ),
00755                           _DEFAULT_STEP_REGISTRIES_EXPORT_XML )
00756 
00757         self.assertEqual( len( tool.listSnapshotInfo() ), 1 )
00758 
00759         info = tool.listSnapshotInfo()[ 0 ]
00760 
00761         self.assertEqual( info[ 'id' ], 'default' )
00762         self.assertEqual( info[ 'title' ], 'default' )
00763 
00764     def test_applyContext(self):
00765         from Products.GenericSetup.tool import IMPORT_STEPS_XML
00766         from Products.GenericSetup.tool import EXPORT_STEPS_XML
00767         from Products.GenericSetup.tool import TOOLSET_XML
00768         from test_registry import _SINGLE_IMPORT_XML
00769         from test_registry import _SINGLE_EXPORT_XML
00770         from test_registry import _NORMAL_TOOLSET_XML
00771         from test_registry import ONE_FUNC
00772 
00773         site = self._makeSite()
00774         tool = self._makeOne('setup_tool').__of__(site)
00775         tool.getImportStepRegistry().clear()
00776         tool.getExportStepRegistry().clear()
00777         tool.getToolsetRegistry().clear()
00778 
00779         context = DummyImportContext( site, tool=tool )
00780         context._files[ IMPORT_STEPS_XML ] = _SINGLE_IMPORT_XML
00781         context._files[ EXPORT_STEPS_XML ] = _SINGLE_EXPORT_XML
00782         context._files[ TOOLSET_XML ] = _NORMAL_TOOLSET_XML
00783 
00784         tool.applyContext(context)
00785 
00786         import_registry = tool.getImportStepRegistry()
00787         self.assertEqual( len( import_registry.listSteps() ), 1 )
00788         self.failUnless( 'one' in import_registry.listSteps() )
00789         info = import_registry.getStepMetadata( 'one' )
00790 
00791         self.assertEqual( info[ 'id' ], 'one' )
00792         self.assertEqual( info[ 'title' ], 'One Step' )
00793         self.assertEqual( info[ 'version' ], '1' )
00794         self.failUnless( 'One small step' in info[ 'description' ] )
00795         self.assertEqual( info[ 'handler' ]
00796                         , 'Products.GenericSetup.tests.test_registry.ONE_FUNC' )
00797 
00798         self.assertEqual( import_registry.getStep( 'one' ), ONE_FUNC )
00799 
00800         export_registry = tool.getExportStepRegistry()
00801         self.assertEqual( len( export_registry.listSteps() ), 1 )
00802         self.failUnless( 'one' in import_registry.listSteps() )
00803         info = export_registry.getStepMetadata( 'one' )
00804         self.assertEqual( info[ 'id' ], 'one' )
00805         self.assertEqual( info[ 'title' ], 'One Step' )
00806         self.failUnless( 'One small step' in info[ 'description' ] )
00807         self.assertEqual( info[ 'handler' ]
00808                         , 'Products.GenericSetup.tests.test_registry.ONE_FUNC' )
00809 
00810         self.assertEqual( export_registry.getStep( 'one' ), ONE_FUNC )
00811 
00812     def test_listContextInfos_empty(self):
00813         site = self._makeSite()
00814         site.setup_tool = self._makeOne('setup_tool')
00815         tool = site.setup_tool
00816         infos = tool.listContextInfos()
00817         self.assertEqual(len(infos), 0)
00818 
00819     def test_listContextInfos_with_snapshot(self):
00820         site = self._makeSite()
00821         site.setup_tool = self._makeOne('setup_tool')
00822         tool = site.setup_tool
00823         tool.createSnapshot('testing')
00824         infos = tool.listContextInfos()
00825         self.assertEqual(len(infos), 1)
00826         info = infos[0]
00827         self.assertEqual(info['id'], 'snapshot-testing')
00828         self.assertEqual(info['title'], 'testing')
00829         self.assertEqual(info['type'], 'snapshot')
00830 
00831     def test_listContextInfos_with_registered_base_profile(self):
00832         from Products.GenericSetup.interfaces import BASE
00833         profile_registry.registerProfile('foo', 'Foo', '', self._PROFILE_PATH,
00834                                          'Foo', BASE)
00835         site = self._makeSite()
00836         site.setup_tool = self._makeOne('setup_tool')
00837         tool = site.setup_tool
00838         infos = tool.listContextInfos()
00839         self.assertEqual(len(infos), 1)
00840         info = infos[0]
00841         self.assertEqual(info['id'], 'profile-Foo:foo')
00842         self.assertEqual(info['title'], 'Foo')
00843         self.assertEqual(info['type'], 'base')
00844 
00845     def test_listContextInfos_with_registered_extension_profile(self):
00846         from Products.GenericSetup.interfaces import EXTENSION
00847         profile_registry.registerProfile('foo', 'Foo', '', self._PROFILE_PATH,
00848                                          'Foo', EXTENSION)
00849         site = self._makeSite()
00850         site.setup_tool = self._makeOne('setup_tool')
00851         tool = site.setup_tool
00852         infos = tool.listContextInfos()
00853         self.assertEqual(len(infos), 1)
00854         info = infos[0]
00855         self.assertEqual(info['id'], 'profile-Foo:foo')
00856         self.assertEqual(info['title'], 'Foo')
00857         self.assertEqual(info['type'], 'extension')
00858 
00859     def test_getProfileImportDate_nonesuch(self):
00860         site = self._makeSite()
00861         site.setup_tool = self._makeOne('setup_tool')
00862         tool = site.setup_tool
00863         self.assertEqual(tool.getProfileImportDate('nonesuch'), None)
00864 
00865     def test_getProfileImportDate_simple_id(self):
00866         from OFS.Image import File
00867         site = self._makeSite()
00868         site.setup_tool = self._makeOne('setup_tool')
00869         tool = site.setup_tool
00870         filename = 'import-all-foo-20070315123456.log'
00871         tool._setObject(filename, File(filename, '', ''))
00872         self.assertEqual(tool.getProfileImportDate('foo'),
00873                          '2007-03-15T12:34:56Z')
00874 
00875     def test_getProfileImportDate_id_with_colon(self):
00876         from OFS.Image import File
00877         site = self._makeSite()
00878         site.setup_tool = self._makeOne('setup_tool')
00879         tool = site.setup_tool
00880         filename = 'import-all-foo_bar-20070315123456.log'
00881         tool._setObject(filename, File(filename, '', ''))
00882         self.assertEqual(tool.getProfileImportDate('foo:bar'),
00883                          '2007-03-15T12:34:56Z')
00884 
00885     def test_getProfileImportDate_id_with_prefix(self):
00886         # Test if getProfileImportDate does not fail if there is another
00887         # item id with id with a longer id which starts with the same
00888         # prefix
00889         from OFS.Image import File
00890         site = self._makeSite()
00891         site.setup_tool = self._makeOne('setup_tool')
00892         tool = site.setup_tool
00893         filename = 'import-all-foo_bar-20070315123456.log'
00894         tool._setObject(filename, File(filename, '', ''))
00895         filename2 = 'import-all-foo_bar-boo-20070315123456.log'
00896         tool._setObject(filename2, File(filename2, '', ''))
00897         self.assertEqual(tool.getProfileImportDate('foo:bar'),
00898                          '2007-03-15T12:34:56Z')
00899 
00900     def test_profileVersioning(self):
00901         site = self._makeSite()
00902         site.setup_tool = self._makeOne('setup_tool')
00903         tool = site.setup_tool
00904         profile_id = 'dummy_profile'
00905         product_name = 'GenericSetup'
00906         directory = os.path.split(__file__)[0]
00907         path = os.path.join(directory, 'versioned_profile')
00908 
00909         # register profile
00910         orig_profile_reg = (profile_registry._profile_info.copy(),
00911                             profile_registry._profile_ids[:])
00912         profile_registry.registerProfile(profile_id,
00913                                          'Dummy Profile',
00914                                          'This is a dummy profile',
00915                                          path,
00916                                          product=product_name)
00917 
00918         # register upgrade step
00919         from Products.GenericSetup.upgrade import _upgrade_registry
00920         orig_upgrade_registry = copy.copy(_upgrade_registry._registry)
00921         step = UpgradeStep("Upgrade", "GenericSetup:dummy_profile", '*', '1.1', '',
00922                            dummy_upgrade_handler,
00923                            None, "1")
00924         _registerUpgradeStep(step)
00925 
00926         # test initial states
00927         profile_id = ':'.join((product_name, profile_id))
00928         self.assertEqual(tool.getVersionForProfile(profile_id), '1.1')
00929         self.assertEqual(tool.getLastVersionForProfile(profile_id),
00930                          'unknown')
00931 
00932         # run upgrade steps
00933         request = site.REQUEST
00934         request.form['profile_id'] = profile_id
00935         steps = listUpgradeSteps(tool, profile_id, '1.0')
00936         step_id = steps[0]['id']
00937         request.form['upgrades'] = [step_id]
00938         tool.manage_doUpgrades()
00939         self.assertEqual(tool.getLastVersionForProfile(profile_id),
00940                          ('1', '1'))
00941 
00942         # reset ugprade registry
00943         _upgrade_registry._registry = orig_upgrade_registry
00944 
00945         # reset profile registry
00946         (profile_registry._profile_info,
00947          profile_registry._profile_ids) = orig_profile_reg
00948 
00949     def test_listExportSteps(self):
00950         site = self._makeSite()
00951         site.setup_tool = self._makeOne('setup_tool')
00952         tool = site.setup_tool
00953         result = tool.listExportSteps()
00954         self.assertEqual(len(result), 4)
00955         self.failUnless(u'componentregistry' in result)
00956         self.failUnless(u'rolemap' in result)
00957         self.failUnless(u'step_registries' in result)
00958         self.failUnless(u'toolset' in result)
00959 
00960         tool._export_registry.registerStep(u'foo', handler='foo.export')
00961         tool._export_registry.registerStep(u'toolset',
00962                                            handler='toolset.export')
00963         result = tool.listExportSteps()
00964         self.assertEqual(len(result), 5)
00965         self.failUnless(u'componentregistry' in result)
00966         self.failUnless(u'foo' in result)
00967         self.failUnless(u'rolemap' in result)
00968         self.failUnless(u'step_registries' in result)
00969         self.failUnless(u'toolset' in result)
00970 
00971     def test_getSortedImportSteps(self):
00972         site = self._makeSite()
00973         site.setup_tool = self._makeOne('setup_tool')
00974         tool = site.setup_tool
00975         result = tool.getSortedImportSteps()
00976         self.assertEqual(len(result), 3)
00977         self.failUnless(u'componentregistry' in result)
00978         self.failUnless(u'rolemap' in result)
00979         self.failUnless(u'toolset' in result)
00980         self.failUnless(list(result).index(u'componentregistry') >
00981                         list(result).index(u'toolset'))
00982 
00983         tool._import_registry.registerStep(u'foo', handler='foo.import')
00984         tool._import_registry.registerStep(u'toolset',
00985                                            handler='toolset.import')
00986         result = tool.getSortedImportSteps()
00987         self.assertEqual(len(result), 4)
00988         self.failUnless(u'componentregistry' in result)
00989         self.failUnless(u'foo' in result)
00990         self.failUnless(u'rolemap' in result)
00991         self.failUnless(u'toolset' in result)
00992         self.failUnless(list(result).index(u'componentregistry') >
00993                         list(result).index(u'toolset'))
00994 
00995 
00996 _DEFAULT_STEP_REGISTRIES_EXPORT_XML = """\
00997 <?xml version="1.0"?>
00998 <export-steps>
00999  <export-step id="step_registries"
01000               handler="Products.GenericSetup.tool.exportStepRegistries"
01001               title="Export import / export steps.">
01002   
01003  </export-step>
01004 </export-steps>
01005 """
01006 
01007 _EXTRAS_STEP_REGISTRIES_EXPORT_XML = """\
01008 <?xml version="1.0"?>
01009 <export-steps>
01010  <export-step
01011     id="properties"
01012     handler="Products.GenericSetup.tests.test_tool._exportPropertiesINI"
01013     title="properties">
01014 
01015  </export-step>
01016  <export-step
01017     id="step_registries"
01018     handler="Products.GenericSetup.tool.exportStepRegistries"
01019     title="Export import / export steps.">
01020 
01021  </export-step>
01022 </export-steps>
01023 """
01024 
01025 _DEFAULT_STEP_REGISTRIES_IMPORT_XML = """\
01026 <?xml version="1.0"?>
01027 <import-steps>
01028  <import-step id="foo" handler="foo.bar" title="foo">
01029   
01030  </import-step>
01031 </import-steps>
01032 """
01033 
01034 _EXTRAS_STEP_REGISTRIES_IMPORT_XML = """\
01035 <?xml version="1.0"?>
01036 <import-steps>
01037  <import-step
01038     id="dependable"
01039     version="1"
01040     handler="Products.GenericSetup.tests.test_tool._underscoreSiteTitle"
01041     title="dependable">
01042   <dependency step="purging" />
01043 
01044  </import-step>
01045  <import-step
01046     id="dependent"
01047     version="1"
01048     handler="Products.GenericSetup.tests.test_tool._uppercaseSiteTitle"
01049     title="dependent">
01050   <dependency step="dependable" />
01051 
01052  </import-step>
01053  <import-step
01054     id="purging"
01055     version="1"
01056     handler="Products.GenericSetup.tests.test_tool._purgeIfRequired"
01057     title="purging">
01058 
01059  </import-step>
01060 </import-steps>
01061 """
01062 
01063 _PROPERTIES_INI = """\
01064 [Default]
01065 Title=%s
01066 """
01067 
01068 
01069 def _underscoreSiteTitle( context ):
01070 
01071     site = context.getSite()
01072     site.title = site.title.replace( ' ', '_' )
01073     return 'Underscored title'
01074 
01075 def _uppercaseSiteTitle( context ):
01076 
01077     site = context.getSite()
01078     site.title = site.title.upper()
01079     return 'Uppercased title'
01080 
01081 def _purgeIfRequired( context ):
01082 
01083     site = context.getSite()
01084     purged = site.purged = context.shouldPurge()
01085     return purged and 'Purged' or 'Unpurged'
01086 
01087 def _exportPropertiesINI( context ):
01088 
01089     site = context.getSite()
01090     text = _PROPERTIES_INI % site.title
01091 
01092     context.writeDataFile( 'properties.ini', text, 'text/plain' )
01093 
01094     return 'Exported properties'
01095 
01096 
01097 class _ToolsetSetup(BaseRegistryTests):
01098 
01099     def _initSite( self ):
01100 
01101         from Products.GenericSetup.tool import SetupTool
01102         site = Folder()
01103         site._setId( 'site' )
01104         self.app._setObject( 'site', site )
01105         site = self.app._getOb( 'site' )
01106         site._setObject('setup_tool', SetupTool('setup_tool'))
01107         return site
01108 
01109 
01110 class Test_exportToolset(_ToolsetSetup):
01111 
01112     layer = ExportImportZCMLLayer
01113 
01114     def test_empty( self ):
01115 
01116         from Products.GenericSetup.tool import TOOLSET_XML
01117         from Products.GenericSetup.tool import exportToolset
01118 
01119         site = self._initSite()
01120         context = DummyExportContext( site, tool=site.setup_tool )
01121 
01122         exportToolset( context )
01123 
01124         self.assertEqual( len( context._wrote ), 1 )
01125         filename, text, content_type = context._wrote[ 0 ]
01126         self.assertEqual( filename, TOOLSET_XML )
01127         self._compareDOM( text, _EMPTY_TOOLSET_XML )
01128         self.assertEqual( content_type, 'text/xml' )
01129 
01130     def test_normal( self ):
01131 
01132         from Products.GenericSetup.tool import TOOLSET_XML
01133         from Products.GenericSetup.tool import exportToolset
01134 
01135         site = self._initSite()
01136         toolset = site.setup_tool.getToolsetRegistry()
01137         toolset.addForbiddenTool( 'doomed' )
01138         toolset.addRequiredTool( 'mandatory', 'path.to.one' )
01139         toolset.addRequiredTool( 'obligatory', 'path.to.another' )
01140 
01141         context = DummyExportContext( site, tool=site.setup_tool )
01142 
01143         exportToolset( context )
01144 
01145         self.assertEqual( len( context._wrote ), 1 )
01146         filename, text, content_type = context._wrote[ 0 ]
01147         self.assertEqual( filename, TOOLSET_XML )
01148         self._compareDOM( text, _NORMAL_TOOLSET_XML )
01149         self.assertEqual( content_type, 'text/xml' )
01150 
01151 
01152 class Test_importToolset(_ToolsetSetup):
01153 
01154     layer = ExportImportZCMLLayer
01155 
01156     def test_import_updates_registry(self):
01157         from Products.GenericSetup.tool import TOOLSET_XML
01158         from Products.GenericSetup.tool import importToolset
01159         from test_registry import _NORMAL_TOOLSET_XML
01160 
01161         site = self._initSite()
01162         context = DummyImportContext( site, tool=site.setup_tool )
01163 
01164         # Import forbidden
01165         context._files[ TOOLSET_XML ] = _FORBIDDEN_TOOLSET_XML
01166         importToolset( context )
01167 
01168         tool = context.getSetupTool()
01169         toolset = tool.getToolsetRegistry()
01170 
01171         self.assertEqual( len( toolset.listForbiddenTools() ), 3 )
01172         self.failUnless( 'doomed' in toolset.listForbiddenTools() )
01173         self.failUnless( 'damned' in toolset.listForbiddenTools() )
01174         self.failUnless( 'blasted' in toolset.listForbiddenTools() )
01175 
01176         # Import required
01177         context._files[ TOOLSET_XML ] = _REQUIRED_TOOLSET_XML
01178         importToolset( context )
01179 
01180         self.assertEqual( len( toolset.listRequiredTools() ), 2 )
01181         self.failUnless( 'mandatory' in toolset.listRequiredTools() )
01182         info = toolset.getRequiredToolInfo( 'mandatory' )
01183         self.assertEqual( info[ 'class' ],
01184                           'Products.GenericSetup.tests.test_tool.DummyTool' )
01185         self.failUnless( 'obligatory' in toolset.listRequiredTools() )
01186         info = toolset.getRequiredToolInfo( 'obligatory' )
01187         self.assertEqual( info[ 'class' ],
01188                           'Products.GenericSetup.tests.test_tool.DummyTool' )
01189 
01190     def test_tool_ids( self ):
01191         # The tool import mechanism used to rely on the fact that all tools
01192         # have unique IDs set at the class level and that you can call their
01193         # constructor with no arguments. However, there might be tools
01194         # that need IDs set.
01195         from Products.GenericSetup.tool import TOOLSET_XML
01196         from Products.GenericSetup.tool import importToolset
01197 
01198         site = self._initSite()
01199         context = DummyImportContext( site, tool=site.setup_tool )
01200         context._files[ TOOLSET_XML ] = _REQUIRED_TOOLSET_XML
01201 
01202         importToolset( context )
01203 
01204         for tool_id in ( 'mandatory', 'obligatory' ):
01205             tool = getattr( site, tool_id )
01206             self.assertEqual( tool.getId(), tool_id )
01207 
01208     def test_tool_id_required(self):
01209         # Tests that tool creation will still work when an id is required
01210         # by the tool constructor.
01211         from Products.GenericSetup.tool import TOOLSET_XML
01212         from Products.GenericSetup.tool import importToolset
01213 
01214         site = self._initSite()
01215         context = DummyImportContext( site, tool=site.setup_tool )
01216         context._files[ TOOLSET_XML ] = _WITH_ID_TOOLSET_XML
01217 
01218         importToolset( context )
01219 
01220         for tool_id in ( 'mandatory', 'requires_id' ):
01221             tool = getattr( site, tool_id )
01222             self.assertEqual( tool.getId(), tool_id )
01223 
01224     def test_forbidden_tools( self ):
01225 
01226         from Products.GenericSetup.tool import TOOLSET_XML
01227         from Products.GenericSetup.tool import importToolset
01228         TOOL_IDS = ( 'doomed', 'blasted', 'saved' )
01229 
01230         site = self._initSite()
01231 
01232         for tool_id in TOOL_IDS:
01233             pseudo = Folder()
01234             pseudo._setId( tool_id )
01235             site._setObject( tool_id, pseudo )
01236 
01237         self.assertEqual( len( site.objectIds() ), len( TOOL_IDS ) + 1 )
01238 
01239         for tool_id in TOOL_IDS:
01240             self.failUnless( tool_id in site.objectIds() )
01241 
01242         context = DummyImportContext( site, tool=site.setup_tool )
01243         context._files[ TOOLSET_XML ] = _FORBIDDEN_TOOLSET_XML
01244 
01245         importToolset( context )
01246 
01247         self.assertEqual( len( site.objectIds() ), 2 )
01248         self.failUnless( 'setup_tool' in site.objectIds() )
01249         self.failUnless( 'saved' in site.objectIds() )
01250 
01251     def test_required_tools_missing( self ):
01252 
01253         from Products.GenericSetup.tool import TOOLSET_XML
01254         from Products.GenericSetup.tool import importToolset
01255 
01256         site = self._initSite()
01257         self.assertEqual( len( site.objectIds() ), 1 )
01258 
01259         context = DummyImportContext( site, tool=site.setup_tool )
01260         context._files[ TOOLSET_XML ] = _REQUIRED_TOOLSET_XML
01261 
01262         importToolset( context )
01263 
01264         self.assertEqual( len( site.objectIds() ), 3 )
01265         self.failUnless( isinstance( aq_base( site._getOb( 'mandatory' ) )
01266                                    , DummyTool ) )
01267         self.failUnless( isinstance( aq_base( site._getOb( 'obligatory' ) )
01268                                    , DummyTool ) )
01269 
01270     def test_required_tools_no_replacement( self ):
01271 
01272         from Products.GenericSetup.tool import TOOLSET_XML
01273         from Products.GenericSetup.tool import importToolset
01274 
01275         site = self._initSite()
01276 
01277         mandatory = DummyTool()
01278         mandatory._setId( 'mandatory' )
01279         site._setObject( 'mandatory', mandatory )
01280 
01281         obligatory = DummyTool()
01282         obligatory._setId( 'obligatory' )
01283         site._setObject( 'obligatory', obligatory )
01284 
01285         self.assertEqual( len( site.objectIds() ), 3 )
01286 
01287         context = DummyImportContext( site, tool=site.setup_tool )
01288         context._files[ TOOLSET_XML ] = _REQUIRED_TOOLSET_XML
01289 
01290         importToolset( context )
01291 
01292         self.assertEqual( len( site.objectIds() ), 3 )
01293         self.failUnless( aq_base( site._getOb( 'mandatory' ) ) is mandatory )
01294         self.failUnless( aq_base( site._getOb( 'obligatory' ) ) is obligatory )
01295 
01296     def test_required_tools_with_replacement( self ):
01297 
01298         from Products.GenericSetup.tool import TOOLSET_XML
01299         from Products.GenericSetup.tool import importToolset
01300 
01301         site = self._initSite()
01302 
01303         mandatory = AnotherDummyTool()
01304         mandatory._setId( 'mandatory' )
01305         site._setObject( 'mandatory', mandatory )
01306 
01307         obligatory = AnotherDummyTool()
01308         obligatory._setId( 'obligatory' )
01309         site._setObject( 'obligatory', obligatory )
01310 
01311         self.assertEqual( len( site.objectIds() ), 3 )
01312 
01313         context = DummyImportContext( site, tool=site.setup_tool )
01314         context._files[ TOOLSET_XML ] = _REQUIRED_TOOLSET_XML
01315 
01316         importToolset( context )
01317 
01318         self.assertEqual( len( site.objectIds() ), 3 )
01319 
01320         self.failIf( aq_base( site._getOb( 'mandatory' ) ) is mandatory )
01321         self.failUnless( isinstance( aq_base( site._getOb( 'mandatory' ) )
01322                                    , DummyTool ) )
01323 
01324         self.failIf( aq_base( site._getOb( 'obligatory' ) ) is obligatory )
01325         self.failUnless( isinstance( aq_base( site._getOb( 'obligatory' ) )
01326                                    , DummyTool ) )
01327 
01328     def test_required_tools_missing_acquired_nofail( self ):
01329 
01330         from Products.GenericSetup.tool import TOOLSET_XML
01331         from Products.GenericSetup.tool import importToolset
01332 
01333         site = self._initSite()
01334         parent_site = Folder()
01335 
01336         mandatory = AnotherDummyTool()
01337         mandatory._setId( 'mandatory' )
01338         parent_site._setObject( 'mandatory', mandatory )
01339 
01340         obligatory = AnotherDummyTool()
01341         obligatory._setId( 'obligatory' )
01342         parent_site._setObject( 'obligatory', obligatory )
01343 
01344         site = site.__of__(parent_site)
01345 
01346         # acquiring subobjects of a different class during import
01347         # should not prevent new objects from being created if they
01348         # don't exist in the site
01349 
01350         context = DummyImportContext( site, tool=site.setup_tool )
01351         context._files[ TOOLSET_XML ] = _REQUIRED_TOOLSET_XML
01352 
01353         importToolset( context )
01354 
01355         self.failIf( aq_base( site._getOb( 'mandatory' ) ) is mandatory )
01356         self.failUnless( isinstance( aq_base( site._getOb( 'mandatory' ) )
01357                                    , DummyTool ) )
01358 
01359         self.failIf( aq_base( site._getOb( 'obligatory' ) ) is obligatory )
01360         self.failUnless( isinstance( aq_base( site._getOb( 'obligatory' ) )
01361                                    , DummyTool ) )
01362 
01363 
01364 class DummyTool( Folder ):
01365 
01366     pass
01367 
01368 class AnotherDummyTool( Folder ):
01369 
01370     pass
01371 
01372 class DummyToolRequiresId( Folder ):
01373 
01374     def __init__(self, id):
01375         Folder.__init__(self)
01376         self._setId(id)
01377 
01378 
01379 _EMPTY_TOOLSET_XML = """\
01380 <?xml version="1.0"?>
01381 <tool-setup>
01382 </tool-setup>
01383 """
01384 
01385 _NORMAL_TOOLSET_XML = """\
01386 <?xml version="1.0" ?>
01387 <tool-setup>
01388 <forbidden tool_id="doomed"/>
01389 <required class="path.to.one" tool_id="mandatory"/>
01390 <required class="path.to.another" tool_id="obligatory"/>
01391 </tool-setup>
01392 """
01393 
01394 _FORBIDDEN_TOOLSET_XML = """\
01395 <?xml version="1.0"?>
01396 <tool-setup>
01397  <forbidden tool_id="doomed" />
01398  <forbidden tool_id="damned" />
01399  <forbidden tool_id="blasted" />
01400 </tool-setup>
01401 """
01402 
01403 _REQUIRED_TOOLSET_XML = """\
01404 <?xml version="1.0"?>
01405 <tool-setup>
01406  <required
01407     tool_id="mandatory"
01408     class="Products.GenericSetup.tests.test_tool.DummyTool" />
01409  <required
01410     tool_id="obligatory"
01411     class="Products.GenericSetup.tests.test_tool.DummyTool" />
01412 </tool-setup>
01413 """
01414 
01415 _WITH_ID_TOOLSET_XML = """\
01416 <?xml version="1.0"?>
01417 <tool-setup>
01418   <required
01419     tool_id="mandatory"
01420     class="Products.GenericSetup.tests.test_tool.DummyTool" />
01421   <required
01422     tool_id="requires_id"
01423     class="Products.GenericSetup.tests.test_tool.DummyToolRequiresId" />
01424 </tool-setup>
01425 """
01426 
01427 def test_suite():
01428     # reimport to make sure tests are run from Products
01429     from Products.GenericSetup.tests.test_tool import SetupToolTests
01430     from Products.GenericSetup.tests.test_tool import Test_exportToolset
01431     from Products.GenericSetup.tests.test_tool import Test_importToolset
01432 
01433     return unittest.TestSuite((
01434         unittest.makeSuite( SetupToolTests ),
01435         unittest.makeSuite( Test_exportToolset ),
01436         unittest.makeSuite( Test_importToolset ),
01437         ))
01438 
01439 if __name__ == '__main__':
01440     from Products.GenericSetup.testing import run
01441     run(test_suite())