def test_activating_external_editing(self):
        mock_context = self.mocker.mock()
        manager = self.mocker.mock()
        self.expect(manager.checkout(mock_context)).result(None)
        self.expect(
            mock_context.restrictedTraverse('checkout_documents')).result(
            manager)

        self.expect(mock_context.absolute_url()).result('http://foo.com')
        mock_request = self.mocker.mock(count=False)

        redirector = self.mocker.mock()
        self.expect(redirector(mock_request)).result(redirector)
        self.expect(redirector.redirect(
            'http://foo.com/external_edit',
            target="_self",
            timeout=1000)).result(None)

        self.mock_adapter(redirector, IRedirector, [Interface, ])

        self.replay()

        view = TemplateDocumentFormView(mock_context, mock_request)
        view.activate_external_editing(mock_context)
    def test_create_document_method(self):

        # we need to register any plone.directives.form magic components
        # from the module manually (they are not grokky):
        for factory, name in document.__form_value_adapters__:
            provideAdapter(factory, name=name)

        class MockContext(object):
            def __init__(self, fti, template):
                self.fti = fti
                self.template = template
            def _setObject(self, id, obj):
                self.obj = obj
            def _getOb(self, id):
                return self.obj
            def restrictedTraverse(self, testpath):
                return self.template
            def getTypeInfo(self):
                return self.fti

        # 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)

        # fti fake
        fti = DexterityFTI(u'opengever.document.document')
        fti.schema = 'opengever.document.document.IDocumentSchema'
        fti.model_source = None
        fti.model_file = None
        fti.addable_types = ['opengever.document.document']
        fti.isConstructionAllowed = lambda x: True
        fti.allowType = lambda x: True
        register(fti)
        site_manager_mock = self.mocker.proxy(
            PersistentComponents(bases=(getGlobalSiteManager(),)))
        getSiteManager_mock = self.mocker.replace(
            'zope.app.component.hooks.getSiteManager')
        self.expect(
            getSiteManager_mock(
                dummy_site)).result(site_manager_mock).count(0 , None)

        alsoProvides(IDocumentSchema.get('file'), IPrimaryField)

        # 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,))

        # template
        namedfile = self.stub()
        template_doc = self.stub()
        self.expect(template_doc.file).result(namedfile)
        self.expect(template_doc.portal_type).result(
            'opengever.document.document')
        self.expect(namedfile.data).result('data data data')
        self.expect(namedfile.filename).result(u'test_filename.doc')

        # context and request
        context = MockContext(fti, template_doc)
        request = self.stub_request()
        testpath = 'testpath'

        # registry
        registry_mock = self.stub()
        self.expect(registry_mock.forInterface(IDocumentSettings)).result(registry_mock)
        self.expect(registry_mock.preserved_as_paper_default).result(False)
        self.mock_utility(registry_mock, IRegistry)

        self.replay()
        view = TemplateDocumentFormView(context, request)
        view.title = u'Test Title'

        view.create_document(testpath)

        self.assertEqual(context.obj.portal_type, u'opengever.document.document')
        self.assertFalse(context.obj.file == namedfile)
        self.assertEquals(context.obj.file.data, namedfile.data)
        self.assertEqual(context.obj.document_date, datetime.now().date())
        self.assertEqual(context.obj.preserved_as_paper, False)