## $Id: test_browser.py 12482 2015-01-16 06:52:21Z henrik $
##
## Copyright (C) 2013 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
"""
Test the applicant-related UI components.
"""
import os
import datetime
from zope.component import createObject, getUtility
from zope.catalog.interfaces import ICatalog
from zope.intid.interfaces import IIntIds
from kofacustom.sampleuni.testing import FunctionalLayer
from waeup.kofa.schoolgrades import ResultEntry
from waeup.kofa.utils.utils import KofaUtils
from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
from waeup.kofa.applicants.tests.test_batching import ApplicantImportExportSetup
from kofacustom.sampleuni.applicants.export import CustomApplicantExporter
from kofacustom.sampleuni.applicants.batching import CustomApplicantProcessor

class CustomApplicantUITests(ApplicantsFullSetup):
    # Tests for uploading/browsing the passport image of appplicants

    layer = FunctionalLayer

class ApplicantExporterTest(ApplicantImportExportSetup):

    layer = FunctionalLayer

    def setUp(self):
        super(ApplicantExporterTest, self).setUp()
        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
        self.cat = getUtility(ICatalog, name='applicants_catalog')
        self.intids = getUtility(IIntIds)
        return

    def setup_applicant(self, applicant):
        # set predictable values for `applicant`
        applicant.reg_number = u'123456'
        applicant.applicant_id = u'dp2011_654321'
        applicant.firstname = u'Anna'
        applicant.lastname = u'Tester'
        applicant.middlename = u'M.'
        applicant.date_of_birth = datetime.date(1981, 2, 4)
        applicant.sex = 'f'
        applicant.email = 'anna@sample.com'
        applicant.phone = u'+234-123-12345'
        applicant.course1 = self.certificate
        applicant.course2 = self.certificate
        applicant.course_admitted = self.certificate
        applicant.notice = u'Some notice\nin lines.'
        applicant.password = 'any password'
        result_entry = ResultEntry(
            KofaUtils.EXAM_SUBJECTS_DICT.keys()[0],
            KofaUtils.EXAM_GRADES[0][0]
            )
        applicant.school_grades = [
            result_entry]
        return applicant

    def test_export_reimport_all(self):
        # we can export all applicants in a portal
        # set values we can expect in export file
        self.applicant = self.setup_applicant(self.applicant)
        exporter = CustomApplicantExporter()
        exporter.export_all(self.app, self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertTrue(
            'applicant_id,application_date,application_number,course1,course2,'
            'course_admitted,date_of_birth,display_fullname,email,firstname,'
            'history,lastname,locked,middlename,notice,password,phone,'
            'reg_number,sex,special_application,state,'
            'student_id,suspended,container_code\r\n'
            'dp2011_654321,,654321,CERT1,CERT1,CERT1,1981-02-04#,'
            'Anna M. Tester,anna@sample.com,Anna,'
            in result)
        self.assertTrue(
            'Application initialized by system\'],'
            'Tester,0,M.,"Some notice\nin lines.",any password,'
            '+234-123-12345#,123456,f,,initialized,,0,dp2011\r\n'
            in result)
        # We can import the same file if we ignore some columns.
        # Since the applicants_catalog hasn't been notified, the same
        # record with same reg_number can be imported twice.
        processor = CustomApplicantProcessor()
        result = processor.doImport(
            self.outfile,
            ['ignore_applicant_id','application_date',
            'ignore_application_number','course1','course2',
            'course_admitted','date_of_birth',
            'ignore_display_fullname','email','firstname',
            'ignore_history','lastname','locked','middlename',
            'notice','password','phone',
            'reg_number','sex','special_application','state',
            'student_id','suspended','container_code'],
            mode='create')
        num_succ, num_fail, finished_path, failed_path = result
        #content = open(failed_path).read()
        self.assertEqual(num_succ,1)
        self.assertEqual(num_fail,0)
        return
