Index: main/kofacustom.iuokada/trunk/src/kofacustom/iuokada/applicants/tests/test_browser.py
===================================================================
--- main/kofacustom.iuokada/trunk/src/kofacustom/iuokada/applicants/tests/test_browser.py	(revision 15563)
+++ main/kofacustom.iuokada/trunk/src/kofacustom/iuokada/applicants/tests/test_browser.py	(revision 15878)
@@ -19,13 +19,23 @@
 Test the applicant-related UI components.
 """
+import grok
+import datetime
+import pytz
 import os
-import datetime
+from StringIO import StringIO
+from zope.event import notify
 from zope.component import createObject, getUtility
 from zope.catalog.interfaces import ICatalog
 from zope.intid.interfaces import IIntIds
 from hurry.workflow.interfaces import IWorkflowState
+from zope.component import createObject, getUtility
+from waeup.kofa.configuration import SessionConfiguration
+from waeup.kofa.interfaces import (
+    IUserAccount, IExtFileStore, IFileStoreNameChooser)
 from kofacustom.iuokada.testing import FunctionalLayer
+from waeup.kofa.applicants.container import ApplicantsContainer
 from waeup.kofa.browser.tests.test_pdf import samples_dir
-from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
+from waeup.kofa.applicants.tests.test_browser import (ApplicantsFullSetup,
+    ApplicantsFullSetup, container_name_1, session_1)
 from waeup.kofa.applicants.tests.test_batching import ApplicantImportExportSetup
 from kofacustom.iuokada.applicants.export import CustomApplicantExporter
@@ -91,2 +101,75 @@
         open(path, 'wb').write(self.browser.contents)
         print "Sample application_slip.pdf written to %s" % path
+
+    def test_upload_stateresult_by_manager(self):
+        # Add trans applicants container
+        self.transcontainer = ApplicantsContainer()
+        self.transcontainer.mode = 'create'
+        self.transcontainer.code = u'ug%s' % session_1
+        self.transcontainer.prefix = u'ug'
+        self.transcontainer.application_category = u'no'
+        self.transcontainer.year = session_1
+        self.transcontainer.application_fee = 300.0
+        self.transcontainer.title = u'This is the ug%s container' % session_1
+        self.app['applicants'][self.transcontainer.code] = self.transcontainer
+        delta = datetime.timedelta(days=10)
+        self.transcontainer.startdate = datetime.datetime.now(pytz.utc) - delta
+        self.transcontainer.enddate = datetime.datetime.now(pytz.utc) + delta
+        # Add applicant
+        transapplicant = createObject(u'waeup.Applicant')
+        transapplicant.firstname = u'Anna'
+        transapplicant.lastname = u'Post'
+        transapplicant.subtype = 'transfer'
+        self.app['applicants'][self.transcontainer.code].addApplicant(transapplicant)
+        self.transapplicant = self.app['applicants'][self.transcontainer.code][
+            transapplicant.application_number]
+        self.transapplicant_view_path = ('http://localhost/app/applicants/ug%s/%s'
+            % (session_1, transapplicant.application_number))
+        self.transapplicant_manage_path = ('http://localhost/app/applicants/ug%s/%s/manage'
+            % (session_1, transapplicant.application_number))
+        # Login
+        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
+        self.browser.open(self.transapplicant_manage_path)
+        # Create a pseudo file with acceptable size
+        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
+        pseudo_pdf = StringIO(pdf_content)
+        ctrl = self.browser.getControl(name='form.stateresult')
+        file_ctrl = ctrl.mech_control
+        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
+        self.browser.getControl("Save").click() # submit form
+        # Even though the form could not be saved ...
+        self.assertTrue('Required input is missing' in self.browser.contents)
+        # ... the file has been successfully uploaded
+        pdf_url = self.transapplicant_manage_path.replace('manage', 'stateresult.pdf')
+        self.browser.open(pdf_url)
+        self.assertEqual(
+            self.browser.headers['content-type'], 'application/pdf')
+        self.assertEqual(len(self.browser.contents), 307200)
+        # There is really a file stored for the applicant
+        storage = getUtility(IExtFileStore)
+        file_id = IFileStoreNameChooser(self.transapplicant).chooseName(
+            attr='stateresult.pdf')
+        # The stored file can be fetched
+        fd = storage.getFile(file_id)
+        file_len = len(fd.read())
+        self.assertEqual(file_len, 307200)
+        # A file link is displayed on the edit view ...
+        self.browser.open(self.transapplicant_manage_path)
+        self.assertTrue('<a href="stateresult.pdf">' in self.browser.contents)
+        # ... and on the dislay view
+        self.browser.open(self.transapplicant_view_path)
+        self.assertTrue('stateresult.pdf">Statement of Result</a>'
+            in self.browser.contents)
+        # Adding file is properly logged
+        logfile = os.path.join(
+            self.app['datacenter'].storage, 'logs', 'applicants.log')
+        logcontent = open(logfile).read()
+        self.assertTrue(
+            'zope.mgr - kofacustom.iuokada.applicants.browser.CustomApplicantManageFormPage'
+            ' - %s - saved: stateresult'
+            % (self.transapplicant.applicant_id)
+            in logcontent)
+        # When an applicant is removed, also the pdf files are gone.
+        del self.app['applicants'][self.transcontainer.code][self.transapplicant.application_number]
+        fd = storage.getFile(file_id)
+        self.assertTrue(fd is None)
