def test_update_checks_allowed_types(self): # Context and request context_mock = self.create_dummy(portal_type=u'testtype', allowedContentTypes=lambda: []) 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)) # Form self.mocker.replay() view = DefaultAddForm(context_mock, request_mock) view.portal_type = fti_mock.getId() self.assertRaises(ValueError, view.update)
def test_schema_lookup(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.expect(fti_mock.behaviors).result( (IBehaviorOne.__identifier__, IBehaviorTwo.__identifier__, IBehaviorThree.__identifier__)) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") self.expect(fti_mock.behaviors).result([]) # Form self.replay() view = DefaultAddForm(context_mock, request_mock) view.portal_type = u"testtype" self.assertEqual(ISchema, view.schema) self.assertEqual([IBehaviorOne, IBehaviorTwo], list(view.additionalSchemata, )) # When we register our own IBehaviorAssignable we can # influence what goes into the additionalSchemata: provideAdapter(NoBehaviorAssignable) self.assertEqual([], list(view.additionalSchemata, ))
def test_form_create(self): # Context and request context = Container(u"container") request = TestRequest() # FTI - returns dummy factory name fti_mock = DexterityFTI(u"testtype") fti_mock.factory = u'testfactory' self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # The form we're testing form = DefaultAddForm(context, request) form.portal_type = u"testtype" class ISchema(Interface): foo = schema.TextLine() form.fields = Fields(ISchema) # createObject and applyChanges obj_dummy = Item(id="dummy") alsoProvides(obj_dummy, ISchema) data_dummy = {u"foo": u"bar"} from zope.component import createObject self.patch_global(createObject, return_value=obj_dummy) provideAdapter(AttributeField) self.assertEqual(obj_dummy, form.create(data_dummy)) self.assertEqual("testtype", obj_dummy.portal_type)
def test_form_create(self): # Context and request context = Container(u"container") request = TestRequest() # FTI - returns dummy factory name fti_mock = DexterityFTI(u"testtype") fti_mock.factory = u'testfactory' self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # The form we're testing form = DefaultAddForm(context, request) form.portal_type = u"testtype" class ISchema(Interface): foo = schema.TextLine() form.fields = Fields(ISchema) # createObject and applyChanges obj_dummy = Item(id="dummy") alsoProvides(obj_dummy, ISchema) data_dummy = {u"foo": u"bar"} from zope.component import createObject self.patch_global(createObject, return_value=obj_dummy) provideAdapter(AttributeField) self.assertEqual(obj_dummy, form.create(data_dummy)) self.assertEqual("testtype", obj_dummy.portal_type)
def test_form_create(self): # Context and request context = Container(u"container") request = TestRequest() # FTI - returns dummy factory name fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.factory).result(u"testfactory") self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # The form we're testing form = DefaultAddForm(context, request) form.portal_type = u"testtype" # createObject and applyChanges obj_dummy = Item(id="dummy") data_dummy = {u"foo": u"bar"} createObject_mock = self.mocker.replace('zope.component.createObject') self.expect(createObject_mock(u"testfactory")).result(obj_dummy) applyChanges_mock = self.mocker.replace('z3c.form.form.applyChanges') self.expect(applyChanges_mock(form, obj_dummy, data_dummy)) self.replay() self.assertEqual(obj_dummy, form.create(data_dummy)) self.assertEqual("testtype", obj_dummy.portal_type)
def test_form_create(self): # Context and request context = Container(u"container") request = TestRequest() # FTI - returns dummy factory name fti_mock = DexterityFTI(u"testtype") fti_mock.factory = u'testfactory' self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # The form we're testing form = DefaultAddForm(context, request) form.portal_type = u"testtype" # createObject and applyChanges obj_dummy = Item(id="dummy") data_dummy = {u"foo": u"bar"} from zope.component import createObject self.patch_global(createObject, return_value=obj_dummy) from z3c.form.form import applyChanges self.patch_global(applyChanges) self.assertEqual(obj_dummy, form.create(data_dummy)) self.assertEqual("testtype", obj_dummy.portal_type)
def test_label(self): # Add view should take its label from the FTI title # Context and request context_mock = Mock() request_mock = TestRequest() request_mock.form['disable_border'] = True # FTI fti_mock = DexterityFTI(u"testtype") fti_mock.Title = Mock(return_value=u'Test title') self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Form addform = DefaultAddForm(context_mock, request_mock) addform.portal_type = u"testtype" label = addform.label self.assertEqual(u"Add ${name}", six.text_type(label)) self.assertEqual(u"Test title", label.mapping['name'])
def test_add_raises_value_error_if_type_not_addable(self): # Container, new object, and request container = Mock() obj = Mock() request = TestRequest() obj.portal_type = 'testtype' # New object's FTI fti_mock = DexterityFTI(u"testtype") fti_mock.isConstructionAllowed = Mock(return_value=True) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Container FTI container_fti_mock = DexterityFTI(u"containertype") container_fti_mock.allowType = Mock(return_value=False) self.mock_utility(container_fti_mock, IDexterityFTI, name=u"containertype") container.getTypeInfo = Mock(return_value=container_fti_mock) form = DefaultAddForm(container, request) form.portal_type = u"testtype" self.assertRaises(ValueError, form.add, obj)
def test_label(self): # Add view should take its label from the FTI title # Context and request context_mock = self.mocker.mock() request_mock = self.mocker.proxy(TestRequest()) request_mock['disable_border'] = True # FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.Title()).result(u"Test title") self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Form self.replay() addform = DefaultAddForm(context_mock, request_mock) addform.portal_type = u"testtype" label = addform.label self.assertEqual(u"Add ${name}", unicode(label)) self.assertEqual(u"Test title", label.mapping['name'])
def test_form_create(self): # Context and request context = Container(u"container") request = TestRequest() # FTI - returns dummy factory name fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.factory).result(u"testfactory") self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # The form we're testing form = DefaultAddForm(context, request) form.portal_type = u"testtype" # createObject and applyChanges obj_dummy = Item(id="dummy") data_dummy = {u"foo": u"bar"} createObject_mock = self.mocker.replace('zope.component.createObject') self.expect(createObject_mock(u"testfactory")).result(obj_dummy) applyChanges_mock = self.mocker.replace('z3c.form.form.applyChanges') self.expect(applyChanges_mock(form, obj_dummy, data_dummy)) self.replay() self.assertEquals(obj_dummy, form.create(data_dummy)) self.assertEquals("testtype", obj_dummy.portal_type)
def test_schema_lookup(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.expect(fti_mock.behaviors).result((IBehaviorOne.__identifier__, IBehaviorTwo.__identifier__, IBehaviorThree.__identifier__)) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") self.expect(fti_mock.behaviors).result([]) # Form self.replay() view = DefaultAddForm(context_mock, request_mock) view.portal_type = u"testtype" self.assertEquals(ISchema, view.schema) self.assertEquals([IBehaviorOne, IBehaviorTwo], list(view.additionalSchemata,)) # When we register our own IBehaviorAssignable we can # influence what goes into the additionalSchemata: provideAdapter(NoBehaviorAssignable) self.assertEquals([], list(view.additionalSchemata,))
def test_fires_add_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: IAddBegunEvent.providedBy(x) ))) # Form self.replay() view = DefaultAddForm(context_mock, request_mock) view.portal_type = fti_mock.getId() view.update()
def test_add_raises_value_error_if_type_not_addable(self): # Container, new object, and request container = self.mocker.mock() obj = self.mocker.mock() request = TestRequest() self.expect(obj.portal_type).result("testtype").count(0,None) # New object's FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.isConstructionAllowed(container)).result(True) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Container FTI container_fti_mock = self.mocker.proxy(DexterityFTI(u"containertype")) self.expect(container_fti_mock.allowType(u"testtype")).result(False) self.mock_utility(container_fti_mock, IDexterityFTI, name=u"containertype") self.expect(container.getTypeInfo()).result(container_fti_mock) self.replay() form = DefaultAddForm(container, request) form.portal_type = u"testtype" self.assertRaises(ValueError, form.add, obj)
def test_label(self): # Add view should take its label from the FTI title # Context and request context_mock = self.mocker.mock() request_mock = self.mocker.proxy(TestRequest()) request_mock['disable_border'] = True # FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.Title()).result(u"Test title") self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Form self.replay() addform = DefaultAddForm(context_mock, request_mock) addform.portal_type = u"testtype" label = addform.label self.assertEquals(u"Add ${name}", unicode(label)) self.assertEquals(u"Test title", label.mapping['name'])
def test_fires_add_begun_event(self): # Context and request context_mock = self.create_dummy( portal_type=u'testtype', allowedContentTypes=lambda: [self.create_dummy(getId=lambda: 'testtype')]) request_mock = TestRequest() # FTI fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=ISchema) 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 from zope.event import notify notify_mock = self.patch_global(notify) # Form view = DefaultAddForm(context_mock, request_mock) view.portal_type = fti_mock.getId() view.update() self.assertTrue(notify_mock.called) self.assertTrue(IAddBegunEvent.providedBy(notify_mock.call_args[0][0]))
def test_label(self): # Add view should take its label from the FTI title # Context and request context_mock = Mock() request_mock = TestRequest() request_mock.form['disable_border'] = True # FTI fti_mock = DexterityFTI(u"testtype") fti_mock.Title = Mock(return_value=u'Test title') self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Form addform = DefaultAddForm(context_mock, request_mock) addform.portal_type = u"testtype" label = addform.label self.assertEqual(u"Add ${name}", six.text_type(label)) self.assertEqual(u"Test title", label.mapping['name'])
def test_update_checks_allowed_types(self): # Context and request context_mock = self.create_dummy( portal_type=u'testtype', allowedContentTypes=lambda: []) 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)) # Form self.mocker.replay() view = DefaultAddForm(context_mock, request_mock) view.portal_type = fti_mock.getId() self.assertRaises(ValueError, view.update)
def test_fires_add_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: IAddBegunEvent.providedBy(x)))) # Form self.replay() view = DefaultAddForm(context_mock, request_mock) view.portal_type = fti_mock.getId() view.update()
def test_add_raises_value_error_if_type_not_addable(self): # Container, new object, and request container = self.mocker.mock() obj = self.mocker.mock() request = TestRequest() self.expect(obj.portal_type).result("testtype").count(0, None) # New object's FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.isConstructionAllowed(container)).result(True) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Container FTI container_fti_mock = self.mocker.proxy(DexterityFTI(u"containertype")) self.expect(container_fti_mock.allowType(u"testtype")).result(False) self.mock_utility(container_fti_mock, IDexterityFTI, name=u"containertype") self.expect(container.getTypeInfo()).result(container_fti_mock) self.replay() form = DefaultAddForm(container, request) form.portal_type = u"testtype" self.assertRaises(ValueError, form.add, obj)
def test_add_raises_value_error_if_type_not_addable(self): # Container, new object, and request container = Mock() obj = Mock() request = TestRequest() obj.portal_type = 'testtype' # New object's FTI fti_mock = DexterityFTI(u"testtype") fti_mock.isConstructionAllowed = Mock(return_value=True) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Container FTI container_fti_mock = DexterityFTI(u"containertype") container_fti_mock.allowType = Mock(return_value=False) self.mock_utility( container_fti_mock, IDexterityFTI, name=u"containertype" ) container.getTypeInfo = Mock(return_value=container_fti_mock) form = DefaultAddForm(container, request) form.portal_type = u"testtype" self.assertRaises(ValueError, form.add, obj)
def test_update_checks_allowed_types(self): # Context and request context_mock = self.create_dummy( portal_type=u'testtype', allowedContentTypes=lambda: []) request_mock = TestRequest() # FTI fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=ISchema) 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)) # Form view = DefaultAddForm(context_mock, request_mock) view.portal_type = fti_mock.getId() self.assertRaises(ValueError, view.update)
def test_update_ignores_type_check_if_security_check_deferred(self): # Context and request context_mock = self.create_dummy(portal_type=u'testtype', allowedContentTypes=lambda: []) request_mock = TestRequest() alsoProvides(request_mock, IDeferSecurityCheck) # FTI fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=ISchema) 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)) # Form view = DefaultAddForm(context_mock, request_mock) view.portal_type = fti_mock.getId() try: view.update() except ValueError: self.fail("Update raised Unauthorized with security checks " "deferred")
def test_update_ignores_type_check_if_security_check_deferred(self): # Context and request context_mock = self.create_dummy( portal_type=u'testtype', allowedContentTypes=lambda: []) request_mock = TestRequest() alsoProvides(request_mock, IDeferSecurityCheck) # FTI fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=ISchema) 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)) # Form view = DefaultAddForm(context_mock, request_mock) view.portal_type = fti_mock.getId() try: view.update() except ValueError: self.fail("Update raised Unauthorized with security checks " "deferred")
def test_fires_add_begun_event(self): # Context and request context_mock = self.create_dummy( portal_type=u'testtype', allowedContentTypes=lambda: [self.create_dummy(getId=lambda: 'testtype')]) request_mock = TestRequest() # FTI fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=ISchema) 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 from zope.event import notify notify_mock = self.patch_global(notify) # Form view = DefaultAddForm(context_mock, request_mock) view.portal_type = fti_mock.getId() view.update() self.assertTrue(notify_mock.called) self.assertTrue(IAddBegunEvent.providedBy(notify_mock.call_args[0][0]))
def test_schema_lookup_add(self): # Context and request context_mock = self.create_dummy(portal_type=u'testtype') request_mock = TestRequest() # FTI fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=ISchema) fti_mock.behaviors = (IBehaviorOne.__identifier__, IBehaviorTwo.__identifier__, IBehaviorThree.__identifier__) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") from plone.behavior.interfaces import IBehavior from plone.behavior.registration import BehaviorRegistration registration = BehaviorRegistration( title=u"Test Behavior 1", description=u"Provides test behavior", interface=IBehaviorOne, marker=None, factory=None) self.mock_utility(registration, IBehavior, IBehaviorOne.__identifier__) registration = BehaviorRegistration( title=u"Test Behavior 2", description=u"Provides test behavior", interface=IBehaviorTwo, marker=None, factory=None) self.mock_utility(registration, IBehavior, IBehaviorTwo.__identifier__) registration = BehaviorRegistration( title=u"Test Behavior 3", description=u"Provides test behavior", interface=IBehaviorThree, marker=None, factory=None) self.mock_utility(registration, IBehavior, IBehaviorThree.__identifier__) # Form view = DefaultAddForm(context_mock, request_mock) view.portal_type = u"testtype" self.assertEqual(ISchema, view.schema) # we expect here only formfieldprovider! self.assertEqual((IBehaviorOne, IBehaviorTwo), tuple(view.additionalSchemata)) # When we register our own IBehaviorAssignable we can # influence what goes into the additionalSchemata. But in an Addform # this never grips, since its an adapter on context, and contextless # there is always the FTI the only valid source self.mock_adapter(NoBehaviorAssignable, IBehaviorAssignable, [Interface]) self.assertEqual((IBehaviorOne, IBehaviorTwo), tuple(view.additionalSchemata))
def test_vocabulary_field_allowed_from_add_form(self): add_form = DefaultAddForm(self.portal, self.request) add_form.portal_type = 'dx_mock' view = VocabularyView(add_form, self.request) self.request.form.update({ 'name': 'plone.app.vocabularies.PortalTypes', 'field': 'allowed_field', }) data = json.loads(view()) self.assertEquals(len(data['results']), len(self.portal.portal_types.objectIds()))
def test_vocabulary_field_allowed_from_add_form(self): add_form = DefaultAddForm(self.portal, self.request) add_form.portal_type = 'dx_mock' view = VocabularyView(add_form, self.request) self.request.form.update({ 'name': 'plone.app.vocabularies.PortalTypes', 'field': 'allowed_field', }) data = json.loads(view()) self.assertEquals(len(data['results']), len(self.portal.portal_types.objectIds()))
def test_add(self): # Container, new object, and request container = self.mocker.mock() obj = self.mocker.mock() request = TestRequest() self.expect(container._setObject(u"newid", obj)).result(u"newid") self.expect(container._getOb(u"newid")).result(obj) self.expect( container.absolute_url() ).result("http://nohost/plone/container") obj.id = u"newid" self.expect(obj.id).result(u"newid") self.expect(obj.id).result(u"newid") self.expect(obj.portal_type).result("testtype").count(0, None) # New object's FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.isConstructionAllowed(container)).result(True) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Container FTI container_fti_mock = self.mocker.proxy(DexterityFTI(u"containertype")) self.expect(container_fti_mock.allowType(u"testtype")).result(True) self.mock_utility( container_fti_mock, IDexterityFTI, name=u"containertype" ) self.expect(container.getTypeInfo()).result(container_fti_mock) # Name chooser class NameChooser(object): implements(INameChooser) def __init__(self, context): pass def chooseName(self, name, object): return u"newid" self.mock_adapter(NameChooser, INameChooser, (Interface,)) self.replay() form = DefaultAddForm(container, request) form.portal_type = u"testtype" form.add(obj)
def test_add(self): # Container, new object, and request container = self.mocker.mock() obj = self.mocker.mock() request = TestRequest() self.expect(container._setObject(u"newid", obj)).result(u"newid") self.expect(container._getOb(u"newid")).result(obj) self.expect( container.absolute_url()).result("http://nohost/plone/container") obj.id = u"newid" self.expect(obj.id).result(u"newid") self.expect(obj.id).result(u"newid") self.expect(obj.portal_type).result("testtype").count(0, None) # New object's FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.isConstructionAllowed(container)).result(True) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Container FTI container_fti_mock = self.mocker.proxy(DexterityFTI(u"containertype")) self.expect(container_fti_mock.allowType(u"testtype")).result(True) self.mock_utility(container_fti_mock, IDexterityFTI, name=u"containertype") self.expect(container.getTypeInfo()).result(container_fti_mock) # Name chooser class NameChooser(object): implements(INameChooser) def __init__(self, context): pass def chooseName(self, name, object): return u"newid" self.mock_adapter(NameChooser, INameChooser, (Interface, )) self.replay() form = DefaultAddForm(container, request) form.portal_type = u"testtype" form.add(obj)
def test_add(self): # Container, new object, and request container = Mock() obj = Mock() request = TestRequest() container._setObject = Mock(return_value=u'newid') container._getOb = Mock(return_value=obj) container.absolute_url = Mock( return_value="http://nohost/plone/container") obj.id = u"newid" obj.portal_type = 'testtype' # New object's FTI fti_mock = DexterityFTI(u"testtype") fti_mock.isConstructionAllowed = Mock(return_value=True) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Container FTI container_fti_mock = DexterityFTI(u"containertype") container_fti_mock.allowType = Mock(return_value=True) self.mock_utility( container_fti_mock, IDexterityFTI, name=u"containertype" ) container.getTypeInfo = Mock(return_value=container_fti_mock) # Name chooser @implementer(INameChooser) class NameChooser(object): def __init__(self, context): pass def chooseName(self, name, object): return u"newid" self.mock_adapter(NameChooser, INameChooser, (Interface,)) form = DefaultAddForm(container, request) form.portal_type = u"testtype" form.add(obj)
def test_add(self): # Container, new object, and request container = Mock() obj = Mock() request = TestRequest() container._setObject = Mock(return_value=u'newid') container._getOb = Mock(return_value=obj) container.absolute_url = Mock( return_value="http://nohost/plone/container") obj.id = u"newid" obj.portal_type = 'testtype' # New object's FTI fti_mock = DexterityFTI(u"testtype") fti_mock.isConstructionAllowed = Mock(return_value=True) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Container FTI container_fti_mock = DexterityFTI(u"containertype") container_fti_mock.allowType = Mock(return_value=True) self.mock_utility(container_fti_mock, IDexterityFTI, name=u"containertype") container.getTypeInfo = Mock(return_value=container_fti_mock) # Name chooser @implementer(INameChooser) class NameChooser(object): def __init__(self, context): pass def chooseName(self, name, object): return u"newid" self.mock_adapter(NameChooser, INameChooser, (Interface, )) form = DefaultAddForm(container, request) form.portal_type = u"testtype" form.add(obj)
def test_update_checks_allowed_types(self): # Context and request context_mock = self.create_dummy(portal_type=u'testtype', allowedContentTypes=lambda: []) request_mock = TestRequest() # FTI fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=ISchema) 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)) # Form view = DefaultAddForm(context_mock, request_mock) view.portal_type = fti_mock.getId() self.assertRaises(ValueError, view.update)
def test_add_raises_unauthorized_if_construction_not_allowed(self): # Container, new object, and request container = Mock() obj = Mock() request = TestRequest() # New object's FTI fti_mock = DexterityFTI(u"testtype") fti_mock.isConstructionAllowed = Mock(return_value=False) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Container FTI container_fti_mock = DexterityFTI(u"containertype") self.mock_utility(container_fti_mock, IDexterityFTI, name=u"containertype") container.getTypeInfo = Mock(return_value=container_fti_mock) obj.portal_type = 'testtype' form = DefaultAddForm(container, request) form.portal_type = u"testtype" self.assertRaises(Unauthorized, form.add, obj)
def test_add_raises_unauthorized_if_construction_not_allowed(self): # Container, new object, and request container = Mock() obj = Mock() request = TestRequest() # New object's FTI fti_mock = DexterityFTI(u"testtype") fti_mock.isConstructionAllowed = Mock(return_value=False) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") # Container FTI container_fti_mock = DexterityFTI(u"containertype") self.mock_utility( container_fti_mock, IDexterityFTI, name=u"containertype" ) container.getTypeInfo = Mock(return_value=container_fti_mock) obj.portal_type = 'testtype' form = DefaultAddForm(container, request) form.portal_type = u"testtype" self.assertRaises(Unauthorized, form.add, obj)
def test_schema_lookup_add(self): # Context and request context_mock = self.create_dummy(portal_type=u'testtype') request_mock = TestRequest() # FTI fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=ISchema) fti_mock.behaviors = ( IBehaviorOne.__identifier__, IBehaviorTwo.__identifier__, IBehaviorThree.__identifier__ ) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") from plone.behavior.interfaces import IBehavior from plone.behavior.registration import BehaviorRegistration registration = BehaviorRegistration( title=u"Test Behavior 1", description=u"Provides test behavior", interface=IBehaviorOne, marker=None, factory=None ) self.mock_utility( registration, IBehavior, IBehaviorOne.__identifier__ ) registration = BehaviorRegistration( title=u"Test Behavior 2", description=u"Provides test behavior", interface=IBehaviorTwo, marker=None, factory=None ) self.mock_utility( registration, IBehavior, IBehaviorTwo.__identifier__ ) registration = BehaviorRegistration( title=u"Test Behavior 3", description=u"Provides test behavior", interface=IBehaviorThree, marker=None, factory=None ) self.mock_utility( registration, IBehavior, IBehaviorThree.__identifier__ ) # Form view = DefaultAddForm(context_mock, request_mock) view.portal_type = u"testtype" self.assertEqual(ISchema, view.schema) # we expect here only formfieldprovider! self.assertEqual( (IBehaviorOne, IBehaviorTwo), tuple(view.additionalSchemata) ) # When we register our own IBehaviorAssignable we can # influence what goes into the additionalSchemata. But in an Addform # this never grips, since its an adapter on context, and contextless # there is always the FTI the only valid source self.mock_adapter( NoBehaviorAssignable, IBehaviorAssignable, [Interface] ) self.assertEqual( (IBehaviorOne, IBehaviorTwo), tuple(view.additionalSchemata) )