Beispiel #1
0
    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 = 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 = DefaultEditForm(context_mock, request_mock)
        view.update()

        self.assertTrue(notify_mock.called)
        self.assertTrue(
            IEditBegunEvent.providedBy(notify_mock.call_args[0][0]))
Beispiel #2
0
    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
        @implementer(IStatusMessage)
        class StatusMessage(object):

            def __init__(self, request):
                pass

            def addStatusMessage(self, msg, type=''):
                pass
        self.mock_adapter(StatusMessage, IStatusMessage, (Interface,))

        # mock notify
        from zope.event import notify
        notify_mock = self.patch_global(notify)

        # Form
        view = DefaultEditForm(context_mock, request_mock)
        view.widgets = Mock()
        view.widgets.extract = Mock(return_value=({'title': u'foo'}, []))
        view.applyChanges = Mock()
        view.handleApply(view, {})

        self.assertTrue(notify_mock.called)
        self.assertTrue(
            IEditFinishedEvent.providedBy(notify_mock.call_args[0][0]))
Beispiel #3
0
    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()
Beispiel #4
0
    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, {})
Beispiel #5
0
    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")
      
        # Form
        
        self.replay()
        
        view = DefaultEditForm(context_mock, request_mock)
        
        # emulate update()
        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,))
Beispiel #6
0
 def test_label(self):
     
     # Edit view should take its label from the FTI title
     
     # 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.Title()).result(u"Test title")
     self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")
   
     # Form
     
     self.replay()
     
     editview = DefaultEditForm(context_mock, request_mock)
     
     # emulate update()
     editview.portal_type = u"testtype"
     
     label = editview.label
     self.assertEquals(u"Edit ${name}", unicode(label))
     self.assertEquals(u"Test title", label.mapping['name'])
Beispiel #7
0
def patched_compute_fields_order(self, obj):
    form = DefaultEditForm(obj, getRequest())
    form.portal_type = obj.portal_type
    form.updateFields()
    all_fields = list()
    all_fields += [(form.fields[name].field, name) for name in form.fields]
    if form.groups:
        for group in form.groups:
            all_fields += [(group.fields[name].field, name) for name in group.fields]

    return all_fields
    def _compute_fields_order(self, obj):
        """
        Given a content, compute the field ordering the way the edit form does.

        Return: a list of tuples (field, field name) in order.
        """
        form = DefaultEditForm(obj, getRequest())
        form.portal_type = obj.portal_type
        form.updateFields()
        all_fields = list()
        all_fields += [(form.fields[name].field, name) for name in form.fields]
        if form.groups:
            for group in form.groups:
                all_fields += [(group.fields[name].field, name) for name in group.fields]

        return all_fields
Beispiel #9
0
    def test_label(self):

        # Edit view should take its label from the FTI title

        # Context and request

        context_mock = self.create_dummy(portal_type=u'testtype')
        request_mock = TestRequest()

        # 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
        editview = DefaultEditForm(context_mock, request_mock)

        # emulate update()
        editview.portal_type = u"testtype"

        label = editview.label
        self.assertEqual(u"Edit ${name}", six.text_type(label))
        self.assertEqual(u"Test title", label.mapping['name'])
Beispiel #10
0
 def update(self):
     DefaultEditForm.update(self)
Beispiel #11
0
    def test_schema_lookup_edit(self):

        # Context and request
        class IMarker(IDexterityContent):
            pass

        context_mock = self.create_dummy(portal_type=u'testtype')
        alsoProvides(context_mock, IMarker)
        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 = DefaultEditForm(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.
        self.mock_adapter(
            NoBehaviorAssignable,
            IBehaviorAssignable,
            [IMarker]
        )
        additionalSchemata = tuple(view.additionalSchemata)
        self.assertEqual(tuple(), tuple(additionalSchemata))
    def test_widgets_render_missing_values(self):
        """Test that gets run for each of the portal type specific subclasses,
        and asserts that a rendered z3c.form widget correctly returns the
        missing value if that's what's currently persisted on the object.
        """
        if self.portal_type is None:
            # Don't attempt to run this test for the abstract base class
            return

        self.login(self.manager)

        obj = self.get_obj_of_own_type()
        form = DefaultEditForm(obj, self.request)

        # Populate the form with fields according to the object's portal type
        with fake_interaction():
            # We need a fake IInteraction context because otherwise
            # z3c.formwidget.query.widget fails with its checkPermission()
            form.update()

        for widget in self.iter_widgets(form):
            field = widget.field

            if field.required:
                # Required fields shouldn't have missing values
                return

            if field.readonly:
                return

            # Determine what this field's missing value would be
            missing_value = field.missing_value

            # Manipulate fixture obj to have missing value for this field
            field.set(field.interface(obj), missing_value)

            # Update the widget to reflect that changed value on the obj
            with fake_interaction():
                widget.update()

            # Use the widget to retrieve the value - but turn it into a
            # field value using the field's DataConverter, in order to
            # compare it to missing value.
            dc = IDataConverter(widget)
            field_value_from_widget = dc.toFieldValue(widget.value)

            if isinstance(widget, SingleCheckBoxWidget):
                # Boolean fields handled by SingleCheckBoxWidgets are funny:
                # Their fields' missing value is None, which ends up
                # as a widget.value of empty list [], which
                # IDataConverter.toFieldValue() then turns into False.
                #
                # In other words, there isn't really a concept of missing
                # values for booleans - MV will always end up being
                # considered the same as False.
                if field_value_from_widget is False:
                    field_value_from_widget = None

            if isinstance(field, List) and isinstance(widget, CheckBoxWidget):
                # zope.schema.List is weird too - it gets rendered using
                # a CheckBoxWidget.
                missing_value = []

            self.assertEqual(
                missing_value, field_value_from_widget,
                'Unexpectedly got %r instead of missing value %r '
                'from widget %r (for an %r object) ' % (
                    field_value_from_widget, missing_value,
                    widget, obj.portal_type))