testPDFForm.py 7 KB
Newer Older
Jérome Perrin's avatar
Jérome Perrin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
##############################################################################
#
# Copyright (c) 2007 Nexedi SARL and Contributors. All Rights Reserved.
#          Jerome Perrin <jerome@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# 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.
#
##############################################################################

import unittest
import os

32 33 34 35 36 37 38
try:
    from zope.app.testing.placelesssetup import PlacelessSetup
except ImportError:
    # BACK: Zope 2.8. Remove when we no longer support it
    from zope.component.tests.placelesssetup import PlacelessSetup


Jérome Perrin's avatar
Jérome Perrin committed
39 40 41 42
from Products.ERP5Form.PDFForm import PDFForm
from Products.ERP5.Document.Document import Document


43
class TestPDFForm(PlacelessSetup, unittest.TestCase):
Jérome Perrin's avatar
Jérome Perrin committed
44 45
  """Tests PDF Form
  """
Romain Courteaud's avatar
Romain Courteaud committed
46 47 48 49

  def getTitle(self):
    return "PDF Form"

Jérome Perrin's avatar
Jérome Perrin committed
50 51 52
  def setUp(self):
    """Creates a PDFForm, and a document on which the PDF form is rendered.
    """
53 54 55 56 57 58
    super(TestPDFForm, self).setUp()
    try:
        from Products.CMFCore.tests.base.utils import _setUpDefaultTraversable
        _setUpDefaultTraversable()
    except ImportError:
        pass # On Zope 2.8, remove when we no longer support it
59
    self.document = Document('doc_id')
Jérome Perrin's avatar
Jérome Perrin committed
60
    pdf_file = open(os.path.join(os.path.dirname(__file__),
61
                                      'data', 'test_1.pdf'), 'rb')
Jérome Perrin's avatar
Jérome Perrin committed
62 63
    self.pdf_form = PDFForm('test_pdf_form').__of__(self.document)
    self.pdf_form.manage_upload(pdf_file)
64 65 66

  # if tearDown is ever added, don't forget to call PlacelessSetup.tearDown()

Jérome Perrin's avatar
Jérome Perrin committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  def test_getCellNames(self):
    self.assertEquals(['text_1', 'text_2', 'text_3'],
                      self.pdf_form.getCellNames())

  def test_SimpleGeneratePDF(self):
    self.pdf_form.setCellTALES('text_1', 'string:Something simple')
    self.failUnless(self.pdf_form.generatePDF())
    # aliases
    self.failUnless(self.pdf_form.index_html())
    self.failUnless(self.pdf_form())
  
  def test_EmptyGeneratePdf(self):
    self.failUnless(self.pdf_form.generatePDF())
    # aliases
    self.failUnless(self.pdf_form.index_html())
    self.failUnless(self.pdf_form())
  
  def test_showCellName(self):
    self.failUnless(self.pdf_form.showCellNames())
  
  def test_CellTALES(self):
    self.pdf_form.setCellTALES('text_1', 'here/getId')
    self.assertEquals('here/getId', self.pdf_form.getCellTALES('text_1'))

  def test_setInvalidTALES(self):
92 93
    from Products.PageTemplates.Expressions import getEngine
    CompilerError = getEngine().getCompilerError()
Jérome Perrin's avatar
Jérome Perrin committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    self.pdf_form.setCellTALES('text_1', 'python:(inv.alid "= ')
    # maybe should raise when setting the TALES, not when getting ?
    self.assertRaises(CompilerError, self.pdf_form.evaluateCell, 'text_1')
  
  def test_EditCells(self):
    self.pdf_form.doEditCells(REQUEST=dict(text_1='here/getId',
                                           text_2='string:'))
    self.assertEquals('here/getId', self.pdf_form.getCellTALES('text_1'))
    self.assertEquals('string:', self.pdf_form.getCellTALES('text_2'))
  
  def test_EvaluateCell(self):
    self.pdf_form.setCellTALES('text_1', 'here/getId')
    self.assertEquals('doc_id', self.pdf_form.evaluateCell('text_1'))
  
  def test_EvaluateNonExistCell(self):
    self.assertRaises(KeyError, self.pdf_form.evaluateCell,
                      'this_cell_does_not_exist')
  
  def test_CalculateCellValues(self):
    self.pdf_form.setCellTALES('text_1', 'here/getId')
    self.pdf_form.setCellTALES('text_2', 'string:static')
    calculated_values = self.pdf_form.calculateCellValues()
    self.assertEquals('doc_id', calculated_values['text_1'])
    self.assertEquals('static', calculated_values['text_2'])
  
  def test_CalculateCellValuesWithCellKey(self):
    self.pdf_form.setCellTALES('text_1', 'here/getId')
    self.pdf_form.setCellTALES('text_2', 'cell/text_1')
    calculated_values = self.pdf_form.calculateCellValues()
    self.assertEquals('doc_id', calculated_values['text_1'])
    self.assertEquals('doc_id', calculated_values['text_2'])
  
  def test_CalculateCellValuesTotal(self):
    # The original use case of `cell`
    self.pdf_form.setCellTALES('text_1', 'python:3')
    self.pdf_form.setCellTALES('text_2', 'python:2')
    self.pdf_form.setCellTALES('text_3',
                               'python:cell["text_1"] + cell["text_2"]')
    self.assertEquals(3 + 2, self.pdf_form.calculateCellValues()['text_3'])
  
  def test_CalculateCellValuesCircularRefs(self):
    self.pdf_form.setCellTALES('text_1', 'cell/text2')
    self.pdf_form.setCellTALES('text_2', 'cell/text_1')
    from Products.ERP5Form.PDFForm import CircularReferencyError
    self.assertRaises(CircularReferencyError,
                      self.pdf_form.calculateCellValues)
  
  def test_CalculateCellValuesParms(self):
    self.pdf_form.setCellTALES('text_1', 'a_parameter')
    calculated_values = self.pdf_form.calculateCellValues(a_parameter='Value')
    self.assertEquals('Value', calculated_values['text_1'])
  

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
class TestPDFFormButtons(unittest.TestCase):
  """Tests PDF Form with buttons
  """
  def setUp(self):
    """Creates a PDFForm with buttons, and a document on which the PDF form is
    rendered.
    """
    self.document = Document('doc_id')
    pdf_file = open(os.path.join(os.path.dirname(__file__),
                        'data', 'test_button.pdf'), 'rb')
    self.pdf_form = PDFForm('test_pdf_form').__of__(self.document)
    self.pdf_form.manage_upload(pdf_file)
    
  def test_getCellNames(self):
    self.assertEquals(['check_box',],
                      self.pdf_form.getCellNames())

  def test_SimpleGeneratePDF(self):
    self.pdf_form.setCellTALES('check_box', 'python: 1')
    self.failUnless(self.pdf_form.generatePDF())
    # aliases
    self.failUnless(self.pdf_form.index_html())
    self.failUnless(self.pdf_form())

    # XXX for debugging:
    # file('/tmp/out.pdf', 'w').write(self.pdf_form())
    # os.system('xpdf /tmp/out.pdf')
174 175
    # os.system('kpdf /tmp/out.pdf')
    # os.system('acroread /tmp/out.pdf')
176 177


Jérome Perrin's avatar
Jérome Perrin committed
178 179 180
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestPDFForm))
181
  suite.addTest(unittest.makeSuite(TestPDFFormButtons))
Jérome Perrin's avatar
Jérome Perrin committed
182
  return suite