def test_fires_modified_event_on_change_props_per_changed_property(self): fti = DexterityFTI('testtype') fti.title = 'Old title' fti.allow_discussion = False fti.global_allow = True notify_mock = self.mocker.replace('zope.event.notify') self.expect( notify_mock( mocker.MATCH( lambda x: IObjectModifiedEvent.providedBy(x) and len( x.descriptions) == 1 and x.descriptions[0].attribute == 'title' and x.descriptions[0].oldValue == 'Old title'))) self.expect( notify_mock( mocker.MATCH( lambda x: IObjectModifiedEvent.providedBy(x) and len( x.descriptions) == 1 and x.descriptions[0].attribute == 'global_allow' and x.descriptions[0].oldValue is True))) self.replay() fti.manage_changeProperties(title='New title', allow_discussion=False, global_allow=False)
def test_fires_edit_finished_event(self): # Context and request context_mock = self.create_dummy(portal_type=u'testtype', title=u'foo') context_mock.absolute_url = \ lambda *a, **kw: 'http://127.0.0.1/plone/item' request_mock = TestRequest() # mock status message class StatusMessage(object): implements(IStatusMessage) def __init__(self, request): pass def addStatusMessage(self, msg, type=''): pass self.mock_adapter(StatusMessage, IStatusMessage, (Interface, )) # mock notify notify_mock = self.mocker.replace('zope.event.notify') self.expect( notify_mock( mocker.MATCH(lambda x: IEditFinishedEvent.providedBy(x)))) # Form view = DefaultEditForm(context_mock, request_mock) view.widgets = self.create_dummy() view.widgets.extract = lambda *a, **kw: ({'title': u'foo'}, []) self.replay() view.handleApply(view, {})
def test_fires_edit_begun_event(self): # Context and request context_mock = self.create_dummy(portal_type=u'testtype') request_mock = TestRequest() # FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ISchema) self.mocker.count(0, 100) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") self.mock_adapter(FieldWidgets, IWidgets, (Interface, Interface, Interface)) self.mock_adapter(Actions, IActions, (Interface, Interface, Interface)) # mock notify notify_mock = self.mocker.replace('zope.event.notify') self.expect( notify_mock(mocker.MATCH(lambda x: IEditBegunEvent.providedBy(x)))) # Form view = DefaultEditForm(context_mock, request_mock) self.replay() view.update()
def test_fires_add_cancelled_event(self): # Context and request context_mock = self.create_dummy(portal_type=u'testtype') context_mock.absolute_url = \ lambda *a, **kw: 'http://127.0.0.1/plone/item' request_mock = TestRequest() # mock status message class StatusMessage(object): implements(IStatusMessage) def __init__(self, request): pass def addStatusMessage(self, msg, type=''): pass self.mock_adapter(StatusMessage, IStatusMessage, (Interface, )) # mock notify notify_mock = self.mocker.replace('zope.event.notify') self.expect( notify_mock( mocker.MATCH(lambda x: IAddCancelledEvent.providedBy(x)))) # Form self.replay() view = DefaultAddForm(context_mock, request_mock) view.handleCancel(view, {})
def test_components_reregistered_on_rename(self): portal_type = u"testtype" fti = DexterityFTI(portal_type) container_dummy = self.create_dummy() # Mock the lookup of the site and the site manager at the site root dummy_site = self.create_dummy() self.mock_utility(dummy_site, ISiteRoot) site_manager_mock = self.mocker.proxy( PersistentComponents(bases=(getGlobalSiteManager(), ))) getSiteManager_mock = self.mocker.replace( 'zope.component.hooks.getSiteManager') self.expect( getSiteManager_mock(dummy_site)).result(site_manager_mock).count( 1, None) # First look for unregistration of all local components self.expect( site_manager_mock.unregisterUtility( provided=IDexterityFTI, name=portal_type)).passthrough() # Then look for re-registration of global components self.expect( site_manager_mock.registerUtility( fti, IDexterityFTI, portal_type, info='plone.dexterity.dynamic')).passthrough() self.expect( site_manager_mock.registerUtility( mocker.MATCH(lambda x: isinstance(x, DexterityFactory) and x. portal_type == portal_type), IFactory, portal_type, info='plone.dexterity.dynamic')).passthrough() self.assertEqual('string:${folder_url}/++add++testtype', fti.add_view_expr) self.replay() ftiRenamed( fti, ObjectMovedEvent(fti, container_dummy, fti.getId(), container_dummy, u"newtype")) site_dummy = self.create_dummy( getSiteManager=lambda: site_manager_mock) setSite(site_dummy) setHooks() self.assertNotEquals(None, queryUtility(IDexterityFTI, name=portal_type)) self.assertNotEquals(None, queryUtility(IFactory, name=portal_type))
def test_renamed_factory_not_unregistered_if_not_unique(self): portal_type = 'testtype' fti = DexterityFTI(portal_type, factory='common-factory') portal_type2 = 'testtype2' fti2 = DexterityFTI(portal_type2, factory='common-factory') # Mock the lookup of the site and the site manager at the site root dummy_site = self.create_dummy() self.mock_utility(dummy_site, Interface) site_manager_mock = self.mocker.proxy( PersistentComponents(bases=(getGlobalSiteManager(), ))) getSiteManager_mock = self.mocker.replace( 'zope.component.hooks.getSiteManager') self.expect( getSiteManager_mock(dummy_site)).result(site_manager_mock).count( 1, None) # Pretend two FTIs are registered, both using common-factory self.expect(site_manager_mock.registeredUtilities()).result([ self.create_dummy(provided=IFactory, name='common-factory', info='plone.dexterity.dynamic'), self.create_dummy(component=fti, provided=IDexterityFTI, name='testtype', info='plone.dexterity.dynamic'), self.create_dummy(component=fti2, provided=IDexterityFTI, name='testtype2', info='plone.dexterity.dynamic'), ]) # We shouldn't remove this since fti2 still uses it self.expect( site_manager_mock.unregisterUtility( provided=IFactory, name='common-factory')).count(0) # And a new one to be created with the new factory name self.expect( site_manager_mock.registerUtility( mocker.MATCH(lambda x: isinstance(x, DexterityFactory) and x. portal_type == portal_type), IFactory, 'new-factory', info='plone.dexterity.dynamic')).passthrough() self.replay() fti.factory = 'new-factory' ftiModified( fti, ObjectModifiedEvent( fti, DexterityFTIModificationDescription('factory', 'common-factory')))
def test_fires_modified_event_on_update_property_if_changed(self): fti = DexterityFTI(u"testtype") fti.title = u"Old title" fti.global_allow = False notify_mock = self.mocker.replace('zope.event.notify') self.expect(notify_mock(mocker.MATCH(lambda x: IObjectModifiedEvent.providedBy(x) \ and len(x.descriptions) == 1 \ and x.descriptions[0].attribute == 'title' \ and x.descriptions[0].oldValue == "Old title"))) self.replay() fti._updateProperty('title', "New title") # fires event caught above fti._updateProperty('allow_discussion', False) # does not fire
def test_old_factory_not_unregistered_if_not_created_by_dexterity(self): portal_type = 'testtype' fti = DexterityFTI(portal_type) # Mock the lookup of the site and the site manager at the site root dummy_site = self.create_dummy() self.mock_utility(dummy_site, Interface) site_manager_mock = self.mocker.proxy( PersistentComponents(bases=(getGlobalSiteManager(), ))) getSiteManager_mock = self.mocker.replace( 'zope.component.hooks.getSiteManager') self.expect( getSiteManager_mock(dummy_site)).result(site_manager_mock).count( 1, None) # Pretend like we have a utility registered reg1 = self.create_dummy() reg1.provided = IFactory reg1.name = 'old-factory' reg1.info = None self.expect(site_manager_mock.registeredUtilities()).result([reg1]) # This should not be removed, since we didn't create it self.expect( site_manager_mock.unregisterUtility(provided=IFactory, name='old-factory')).count(0) # A new one may still be created, however self.expect( site_manager_mock.registerUtility( mocker.MATCH(lambda x: isinstance(x, DexterityFactory) and x. portal_type == portal_type), IFactory, 'new-factory', info='plone.dexterity.dynamic')).passthrough() self.replay() fti.factory = 'new-factory' ftiModified( fti, ObjectModifiedEvent( fti, DexterityFTIModificationDescription('factory', 'old-factory')))
def test_old_factory_unregistered_after_name_changed_if_dynamic(self): portal_type = u"testtype" fti = DexterityFTI(portal_type) # Mock the lookup of the site and the site manager at the site root dummy_site = self.create_dummy() self.mock_utility(dummy_site, ISiteRoot) site_manager_mock = self.mocker.proxy( PersistentComponents(bases=(getGlobalSiteManager(), ))) getSiteManager_mock = self.mocker.replace( 'zope.component.hooks.getSiteManager') self.expect( getSiteManager_mock(dummy_site)).result(site_manager_mock).count( 1, None) # Pretend like we have a utility registered reg1 = self.create_dummy() reg1.provided = IFactory reg1.name = 'old-factory' reg1.info = 'plone.dexterity.dynamic' self.expect(site_manager_mock.registeredUtilities()).result([reg1]) # Expect this to get removed self.expect( site_manager_mock.unregisterUtility(provided=IFactory, name='old-factory')) # And a new one to be created with the new factory name self.expect( site_manager_mock.registerUtility( mocker.MATCH(lambda x: isinstance(x, DexterityFactory) and x. portal_type == portal_type), IFactory, 'new-factory', info='plone.dexterity.dynamic')).passthrough() self.replay() fti.factory = 'new-factory' ftiModified( fti, ObjectModifiedEvent( fti, DexterityFTIModificationDescription('factory', 'old-factory')))
def test_import(self): """ Testing with pygments not installed. """ import_stub = self.mocker.replace('__builtin__.__import__') self.expect(import_stub(mocker.MATCH(lambda x: x.count('pygments')), mocker.ANY, mocker.ANY, mocker.ANY)).\ count(0, None) def import_errror_raiser(name, globals, locals, fromlist): raise ImportError self.mocker.call(import_errror_raiser) self.mocker.replay() import codeexample.code_example_processor reload(codeexample.code_example_processor) from codeexample.code_example_processor import CodeExample self.assertEqual(CodeExample.is_have_pygments(), False)
def test_components_registered_on_add(self): portal_type = 'testtype' fti = DexterityFTI(portal_type) container_dummy = self.create_dummy() # Mock the lookup of the site and the site manager at the site root dummy_site = self.create_dummy() self.mock_utility(dummy_site, Interface) site_manager_mock = self.mocker.proxy( PersistentComponents(bases=(getGlobalSiteManager(), ))) getSiteManager_mock = self.mocker.replace( 'zope.component.hooks.getSiteManager') self.expect(getSiteManager_mock(dummy_site)).result(site_manager_mock) # We expect that no components are registered , so look for all # registrations self.expect( site_manager_mock.registerUtility( fti, IDexterityFTI, portal_type, info='plone.dexterity.dynamic')).passthrough() self.expect( site_manager_mock.registerUtility( mocker.MATCH(lambda x: isinstance(x, DexterityFactory) and x. portal_type == portal_type), IFactory, portal_type, info='plone.dexterity.dynamic')).passthrough() self.replay() ftiAdded(fti, ObjectAddedEvent(fti, container_dummy, fti.getId())) site_dummy = self.create_dummy( getSiteManager=lambda: site_manager_mock) setSite(site_dummy) setHooks() self.assertNotEquals(None, queryUtility(IDexterityFTI, name=portal_type)) self.assertNotEquals(None, queryUtility(IFactory, name=portal_type))
def test_new_factory_not_registered_after_name_changed_if_exists(self): portal_type = 'testtype' fti = DexterityFTI(portal_type) # Mock the lookup of the site and the site manager at the site root dummy_site = self.create_dummy() self.mock_utility(dummy_site, Interface) site_manager_mock = self.mocker.proxy( PersistentComponents(bases=(getGlobalSiteManager(), ))) getSiteManager_mock = self.mocker.replace( 'zope.component.hooks.getSiteManager') self.expect( getSiteManager_mock(dummy_site)).result(site_manager_mock).count( 1, None) # Create a global default for the new name self.mock_utility(DexterityFactory(portal_type), IFactory, name='new-factory') # Factory should not be registered again self.expect( site_manager_mock.registerUtility( mocker.MATCH(lambda x: isinstance(x, DexterityFactory) and x. portal_type == portal_type), IFactory, 'new-factory', info='plone.dexterity.dynamic')).passthrough().count(0) self.replay() fti.factory = 'new-factory' ftiModified( fti, ObjectModifiedEvent( fti, DexterityFTIModificationDescription('factory', 'old-factory')))
def test_components_not_registered_on_add_if_exist(self): portal_type = 'testtype' fti = DexterityFTI(portal_type) container_dummy = self.create_dummy() # Mock the lookup of the site and the site manager at the site root dummy_site = self.create_dummy() self.mock_utility(dummy_site, Interface) site_manager_mock = self.mocker.proxy( PersistentComponents(bases=(getGlobalSiteManager(), ))) getSiteManager_mock = self.mocker.replace( 'zope.component.hooks.getSiteManager') self.expect(getSiteManager_mock(dummy_site)).result(site_manager_mock) # Register FTI utility and factory utility self.mock_utility(fti, IDexterityFTI, name=portal_type) self.mock_utility(DexterityFactory(portal_type), IFactory, name=portal_type) # We expect that all components are registered, so do not expect any # registrations self.expect( site_manager_mock.registerUtility( fti, IDexterityFTI, portal_type)).passthrough().count(0) self.expect( site_manager_mock.registerUtility( mocker.MATCH(lambda x: isinstance(x, DexterityFactory) and x. portal_type == portal_type), IFactory, portal_type)).passthrough().count(0) self.replay() ftiAdded(fti, ObjectAddedEvent(fti, container_dummy, fti.getId()))
def match_addview(): return mocker.MATCH(lambda x: issubclass(x, add.DefaultAddView))