testFormPrintoutAsODT.py 53.2 KB
Newer Older
1
# -*- coding: utf-8 -*-
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 32 33 34
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#                    Fabien Morin <fabien@nexedi.com>
#                    Tatuya Kamada <tatuya@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility 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
# guarantees 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
# USA.
#
##############################################################################

import unittest
import transaction
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
35
from Products.ERP5Type.tests.backportUnittest import skip
36
from Products.ERP5OOo.tests.TestFormPrintoutMixin import TestFormPrintoutMixin
37 38 39 40 41
from Products.ERP5Type.tests.utils import createZODBPythonScript
from Products.MimetypesRegistry.mime_types.magic import guessMime
from Products.ERP5OOo.OOoUtils import OOoBuilder
from Products.ERP5OOo.tests.utils import Validator
from Products.ERP5Type.tests.utils import FileUpload
42
from DateTime import DateTime
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
from lxml import etree
import os

class TestFormPrintoutAsODT(TestFormPrintoutMixin):

  def getTitle(self):
    """
      Return the title of the current test set.
    """
    return "FormPrintout"

  def afterSetUp(self):
    self.login()
    self.setSystemPreference()
    # XML validator
    v12schema_url = os.path.join(os.path.dirname(__file__),
                                 'OpenDocument-schema-v1.2-draft9.rng')
    self.validator = Validator(schema_url=v12schema_url)
Fabien Morin's avatar
Fabien Morin committed
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    foo_file_path = os.path.join(os.path.dirname(__file__),
                                'test_document',
                                'Foo_001.odt')
    foo2_file_path = os.path.join(os.path.dirname(__file__),
                                  'test_document',
                                  'Foo_002.odt')
    foo3_file_path = os.path.join(os.path.dirname(__file__),
                                  'test_document',
                                  'Foo_003.odt')
    foo4_file_path = os.path.join(os.path.dirname(__file__),
                                  'test_document',
                                  'Foo_004.odt')
    foo5_file_path = os.path.join(os.path.dirname(__file__),
                                  'test_document',
                                  'Foo_005.odt')
77 78 79
    variable_file_path = os.path.join(os.path.dirname(__file__),
                                  'test_document',
                                  'Foo_001_with_variable.odt')
80 81 82 83 84
    foo_file = open(foo_file_path, 'rb')
    foo2_file = open(foo2_file_path, 'rb')
    foo3_file = open(foo3_file_path, 'rb')
    foo4_file = open(foo4_file_path, 'rb')
    foo5_file = open(foo5_file_path, 'rb')
85
    variable_file_object = open(variable_file_path, 'rb')
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    custom = self.portal.portal_skins.custom
    addStyleSheet = custom.manage_addProduct['OFSP'].manage_addFile
    if custom._getOb('Foo_getODTStyleSheet', None) is None:
      addStyleSheet(id='Foo_getODTStyleSheet', file=foo_file, title='',
                    precondition='', content_type = 'application/vnd.oasis.opendocument.text')
    if custom._getOb('Foo2_getODTStyleSheet', None) is None:
      addStyleSheet(id='Foo2_getODTStyleSheet', file=foo2_file, title='',
                    precondition='', content_type = 'application/vnd.oasis.opendocument.text')
    if custom._getOb('Foo3_getODTStyleSheet', None) is None:
      addStyleSheet(id='Foo3_getODTStyleSheet', file=foo3_file, title='',
                    precondition='', content_type = 'application/vnd.oasis.opendocument.text')
    if custom._getOb('Foo4_getODTStyleSheet', None) is None:
      addStyleSheet(id='Foo4_getODTStyleSheet', file=foo4_file, title='',
                    precondition='', content_type = 'application/vnd.oasis.opendocument.text')
    if custom._getOb('Foo5_getODTStyleSheet', None) is None:
      addStyleSheet(id='Foo5_getODTStyleSheet', file=foo5_file, title='',
                    precondition='', content_type = 'application/vnd.oasis.opendocument.text')
103 104 105 106 107
    if custom._getOb('Foo_getVariableODTStyleSheet', None) is None:
      addStyleSheet(id='Foo_getVariableODTStyleSheet',
                    file=variable_file_object, title='',
                    precondition='',
                    content_type='application/vnd.oasis.opendocument.text')
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
    erp5OOo = custom.manage_addProduct['ERP5OOo']
    addOOoTemplate = erp5OOo.addOOoTemplate
    if custom._getOb('Foo_viewAsOdt', None) is None:
      addOOoTemplate(id='Foo_viewAsOdt', title='')
    request = self.app.REQUEST
    Foo_viewAsOdt = custom.Foo_viewAsOdt
    Foo_viewAsOdt.doSettings(request, title='', xml_file_id='content.xml',
                             ooo_stylesheet='Foo_getODTStyleSheet')
    #Foo_viewAsOdt.pt_upload(request, file=foo_file)
    #render_result = Foo_viewAsOdt(REQUEST=request)
    builder = OOoBuilder(foo_file)
    content = builder.extract('content.xml')
    Foo_viewAsOdt.pt_edit(content, content_type='application/vnd.oasis.opendocument.text')
    if custom._getOb('Foo_viewAsPrintout', None) is None:
      erp5OOo.addFormPrintout(id='Foo_viewAsPrintout', title='',
                              form_name='Foo_view', template='Foo_getODTStyleSheet')
    if custom._getOb('FooReport_viewAsPrintout', None) is None:
      erp5OOo.addFormPrintout(id='FooReport_viewAsPrintout',
                              title='')
    if custom._getOb('Foo5_viewAsPrintout', None) is None:
      erp5OOo.addFormPrintout(id='Foo5_viewAsPrintout', title='',
                              form_name='Foo_view', template='Foo5_getODTStyleSheet')

    ## append 'test1' data to a listbox
    foo_module = self.portal.foo_module
    if foo_module._getOb('test1', None) is None:
      foo_module.newContent(id='test1', portal_type='Foo')
    test1 =  foo_module.test1
    if test1._getOb("foo_1", None) is None:
      test1.newContent("foo_1", portal_type='Foo Line')
    if test1._getOb("foo_2", None) is None:
      test1.newContent("foo_2", portal_type='Foo Line')
    transaction.commit()
    self.tic()

Fabien Morin's avatar
Fabien Morin committed
143
  def test_01_Paragraph(self):
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    """
    mapping a field to a paragraph
    """
    portal = self.getPortal()
    foo_module = self.portal.foo_module
    if foo_module._getOb('test1', None) is None:
      foo_module.newContent(id='test1', portal_type='Foo')
    test1 =  foo_module.test1
    test1.setTitle('Foo title!')
    transaction.commit()
    self.tic()

    # test target
    foo_printout = portal.foo_module.test1.Foo_viewAsPrintout
    self._validate(self.getODFDocumentFromPrintout(foo_printout))

    request = self.app.REQUEST
    # 1. Normal case: "my_title" field to the "my_title" reference in the ODF document
    self.portal.changeSkin('ODT')
    odf_document = foo_printout.index_html(REQUEST=request)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("Foo title!") > 0)
    self.assertEqual(request.RESPONSE.getHeader('content-type'),
169
                     'application/vnd.oasis.opendocument.text')
170 171 172
    self.assertEqual(request.RESPONSE.getHeader('content-disposition'),
                     'inline;filename="Foo_viewAsPrintout.odt"')
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
173

174 175 176 177 178 179 180 181 182
    # 2. Normal case: change the field value and check again the ODF document
    test1.setTitle("Changed Title!")
    #foo_form.my_title.set_value('default', "Changed Title!")
    odf_document = foo_printout.index_html(REQUEST=request)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("Changed Title!") > 0)
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
183 184

    # 3. False case: change the field name
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    test1.setTitle("you cannot find")
    # rename id 'my_title' to 'xxx_title', then does not match in the ODF document
    foo_form = portal.foo_module.test1.Foo_view
    foo_form.manage_renameObject('my_title', 'xxx_title', REQUEST=request)
    odf_document = foo_printout.index_html(REQUEST=request)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertFalse(content_xml.find("you cannot find") > 0)
    self._validate(odf_document)
    # put back
    foo_form.manage_renameObject('xxx_title', 'my_title', REQUEST=request)

    ## 4. False case: does not set a ODF template
    self.assertTrue(foo_printout.template == 'Foo_getODTStyleSheet')
Fabien Morin's avatar
Fabien Morin committed
200
    tmp_template = foo_printout.template
201
    foo_printout.template = None
202
    self.assertRaises(ValueError, foo_printout.index_html, request)
203 204
    # put back
    foo_printout.template = tmp_template
Fabien Morin's avatar
Fabien Morin committed
205

206 207 208
    # 5. Normal case: just call a FormPrintout object
    request.RESPONSE.setHeader('Content-Type', 'text/html')
    test1.setTitle("call!")
209
    odf_document = foo_printout(request, batch_mode=True) # call
210 211 212 213
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("call!") > 0)
214
    self.assertEqual(request.RESPONSE.getHeader('content-type'), 'text/html')
215
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
216

217 218
    # 5. Normal case: utf-8 string
    test1.setTitle("Français")
219
    odf_document = foo_printout(self.portal.REQUEST)
220 221 222 223 224
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("Français") > 0)
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
225

226 227
    # 6. Normal case: unicode string
    test1.setTitle(u'Français test2')
228
    odf_document = foo_printout(self.portal.REQUEST)
229 230 231 232 233 234
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("Français test2") > 0)
    self._validate(odf_document)

235 236 237 238 239 240 241 242 243
    # 7. Change Filename of downloadable file
    reference = 'My Reference'
    test1.setReference(reference)
    foo_printout.filename = 'here/getReference'
    odf_document = foo_printout(self.portal.REQUEST)
    self.assertEqual(request.RESPONSE.getHeader('content-disposition'),
                     'inline;filename="%s.odt"' % reference)
    test1.setReference(None)

Fabien Morin's avatar
Fabien Morin committed
244
  def test_01_Paragraph_07_LinesField(self):
245 246 247 248 249 250 251
    """test LinesField into multi line"""
    foo_printout = self.portal.foo_module.test1.Foo_viewAsPrintout
    foo_form = self.portal.foo_module.test1.Foo_view
    if foo_form._getOb("week", None) is None:
      foo_form.manage_addField('week', 'week', 'LinesField')
    week = foo_form.week
    week.values['default'] = ['line1', 'line2']
Fabien Morin's avatar
Fabien Morin committed
252

253
    odf_document = foo_printout(self.portal.REQUEST)
254 255 256 257 258 259 260 261
    self.assertTrue(odf_document is not None)
    #test_output = open("/tmp/test_01_Paragraph_07_LinesField.odf", "w")
    #test_output.write(odf_document)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    content_tree = etree.XML(content_xml)
    span_list = content_tree.xpath('//text:reference-mark-start[@text:name="week"]/following-sibling::text:span',
                                   namespaces=content_tree.nsmap)
262 263 264 265 266 267 268 269 270 271
    if span_list:
      self.assertEquals(1, len(span_list))
      span = span_list[0]
      self.assertEquals('line1', span.text)
      self.assertEquals('line2', span[0].tail)
    else:
      reference_mark_node = content_tree.xpath('//text:reference-mark-start[@text:name="week"][1]',
                                               namespaces=content_tree.nsmap)[0]
      self.assertEquals('line1', reference_mark_node.tail)
      self.assertEquals('line2', reference_mark_node.getnext().tail)
272 273
    self._validate(odf_document)

Fabien Morin's avatar
Fabien Morin committed
274
  def test_01_Paragraph_08_Field_Format(self):
275 276 277 278 279 280 281
    """test a field with format"""
    foo_printout = self.portal.foo_module.test1.Foo_viewAsPrintout
    foo_form = self.portal.foo_module.test1.Foo_view
    if foo_form._getOb("number", None) is None:
      foo_form.manage_addField('number', 'number', 'FloatField')
    number = foo_form.number
    number.values['default'] = '543210'
Fabien Morin's avatar
Fabien Morin committed
282
    # set a float field format
283
    number.values['input_style'] = '-1 234.5'
284
    odf_document = foo_printout(self.portal.REQUEST)
285 286 287 288 289 290 291 292
    self.assertTrue(odf_document is not None)
    #test_output = open("/tmp/test_01_Paragraph_08_Filed_Format.odf", "w")
    #test_output.write(odf_document)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    content = etree.XML(content_xml)
    self.assertTrue(content.xpath('//text:p[text() = "543 210.0"]', namespaces=content.nsmap))
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
293

294 295
    # change format
    number.values['input_style'] = '-1234.5'
296
    odf_document = foo_printout(self.portal.REQUEST)
297 298 299 300 301
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    content = etree.XML(content_xml)
    self.assertTrue(content.xpath('//text:p = "543210.0"', namespaces=content.nsmap))
Fabien Morin's avatar
Fabien Morin committed
302

303 304
    self._validate(odf_document)

Fabien Morin's avatar
Fabien Morin committed
305
  def test_01_Paragraph_09_RangeReferenceWithSpan(self):
306 307 308 309 310 311 312
    """test range reference and span setting"""
    foo_printout = self.portal.foo_module.test1.Foo_viewAsPrintout
    foo_form = self.portal.foo_module.test1.Foo_view
    if foo_form._getOb("my_test_title", None) is None:
      foo_form.manage_addField('my_test_title', 'test title', 'StringField')
    test_title = foo_form.my_test_title
    test_title.values['default'] = 'ZZZ test here ZZZ'
Fabien Morin's avatar
Fabien Morin committed
313

314
    odf_document = foo_printout(self.portal.REQUEST)
315 316 317 318 319 320 321 322 323
    self.assertTrue(odf_document is not None)
    #test_output = open("/tmp/test_01_Paragraph_09_RangeReferenceWithSpan.odf", "w")
    #test_output.write(odf_document)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("ZZZ test here ZZZ") > 0)
    self.assertTrue(content_xml.find("test title") < 0)
    self._validate(odf_document)

Fabien Morin's avatar
Fabien Morin committed
324
  def test_02_Table_01_Normal(self):
325
    """To test listbox and ODF table mapping
Fabien Morin's avatar
Fabien Morin committed
326

327
     * Test Data Format
Fabien Morin's avatar
Fabien Morin committed
328

329 330 331 332 333 334 335 336 337 338 339 340 341 342
     ODF table named 'listbox':
     +------------------------------+
     |  ID | Title | Quantity |Date |
     |-----+-------+----------+-----|
     |     |       |          |     |
     |-----+-------+----------+-----|
     |   Total     |          |     |
     +------------------------------+
    """
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
Fabien Morin's avatar
Fabien Morin committed
343
    request = self.app.REQUEST
344
    request['here'] = test1
Fabien Morin's avatar
Fabien Morin committed
345

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    # 1. Normal Case: ODF table last row is stat line
    test1.foo_1.setTitle('foo_title_1')
    message = listbox.ListBox_setPropertyList(
      field_list_method = 'objectValues',
      field_portal_types = 'Foo Line | Foo Line',
      field_stat_method = 'portal_catalog',
      field_stat_columns = 'quantity | Foo_statQuantity',
      field_columns = 'id|ID\ntitle|Title\nquantity|Quantity\nstart_date|Date',)
    self.failUnless('Set Successfully' in message)
    listboxline_list = listbox.get_value('default', render_format = 'list',
                                         REQUEST = request)
    self.assertEqual(len(listboxline_list), 4)
    self.assertTrue(listboxline_list[0].isTitleLine())
    self.assertTrue(listboxline_list[1].isDataLine())
    self.assertTrue(listboxline_list[2].isDataLine())
    self.assertTrue(listboxline_list[3].isStatLine())
    column_list = listboxline_list[0].getColumnPropertyList()
    self.assertEqual(len(column_list), 4)
    self.assertTrue(listboxline_list[1].getColumnProperty('id') == "foo_1")
    self.assertTrue(listboxline_list[1].getColumnProperty('title') == "foo_title_1")
Fabien Morin's avatar
Fabien Morin committed
366

367 368 369 370 371 372 373 374
    odf_document = foo_printout.index_html(REQUEST=request)
    #test_output = open("/tmp/test_02_01_Table.odf", "w")
    #test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("foo_title_1") > 0)
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
375 376

  def test_02_Table_02_SmallerThanListboxColumns(self):
377 378 379 380 381 382
    """2. Irregular case: listbox columns count smaller than table columns count"""
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
Fabien Morin's avatar
Fabien Morin committed
383
    request = self.app.REQUEST
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    request['here'] = test1

    test1.foo_1.setTitle('foo_title_2')
    message = listbox.ListBox_setPropertyList(
      field_list_method = 'objectValues',
      field_portal_types = 'Foo Line | Foo Line',
      field_stat_method = 'portal_catalog',
      field_stat_columns = 'quantity | Foo_statQuantity',
      field_columns = 'id|ID\ntitle|Title\nquantity|Quantity',)
    self.failUnless('Set Successfully' in message)
    self.assertEqual(listbox.get_value('columns'),
                     [('id', 'ID'), ('title', 'Title'), ('quantity', 'Quantity')])
    listboxline_list = listbox.get_value('default', render_format = 'list',
                                         REQUEST = request)
    self.assertEqual(len(listboxline_list), 4)
    self.assertTrue(listboxline_list[0].isTitleLine())
    self.assertTrue(listboxline_list[1].isDataLine())
    self.assertTrue(listboxline_list[2].isDataLine())
    self.assertTrue(listboxline_list[3].isStatLine())
    self.assertTrue(listboxline_list[1].getColumnProperty('title') == "foo_title_2")
Fabien Morin's avatar
Fabien Morin committed
404

405 406 407 408 409 410 411 412 413 414 415 416 417
    column_list = listboxline_list[0].getColumnPropertyList()
    self.assertEqual(len(column_list), 3)

    odf_document = foo_printout.index_html(REQUEST=request)
    #test_output = open("/tmp/test_02_02_Table.odf", "w")
    #test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertFalse(content_xml.find("foo_title_1") > 0)
    self.assertTrue(content_xml.find("foo_title_2") > 0)
    self._validate(odf_document)

Fabien Morin's avatar
Fabien Morin committed
418
  def test_02_Table_03_ListboxColumnsLargerThanTable(self):
419 420 421 422 423 424
    """3. Irregular case: listbox columns count larger than table columns count"""
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
Fabien Morin's avatar
Fabien Morin committed
425
    request = self.app.REQUEST
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    request['here'] = test1

    test1.foo_1.setTitle('foo_title_3')
    message = listbox.ListBox_setPropertyList(
      field_list_method = 'objectValues',
      field_portal_types = 'Foo Line | Foo Line',
      field_stat_method = 'portal_catalog',
      field_stat_columns = 'quantity | Foo_statQuantity',
      field_columns = 'id|ID\ntitle|Title\nquantity|Quantity\n'
                      'start_date|Date\nstatus|Status',)
    self.failUnless('Set Successfully' in message)
    listboxline_list = listbox.get_value('default', render_format = 'list',
                                         REQUEST = request)
    self.assertEqual(len(listboxline_list), 4)
    self.assertTrue(listboxline_list[1].getColumnProperty('title') == "foo_title_3")
Fabien Morin's avatar
Fabien Morin committed
441

442 443 444 445 446 447 448 449 450 451 452
    column_list = listboxline_list[0].getColumnPropertyList()
    self.assertEqual(len(column_list), 5)
    odf_document = foo_printout.index_html(REQUEST=request)
    #test_output = open("/tmp/test_02_03_Table.odf", "w")
    #test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertFalse(content_xml.find("foo_title_2") > 0)
    self.assertTrue(content_xml.find("foo_title_3") > 0)
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
453 454

  def test_02_Table_04_ListboxHasNotStat(self):
455 456 457 458 459 460
    """4. Irregular case: listbox has not a stat line, but table has a stat line"""
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
Fabien Morin's avatar
Fabien Morin committed
461
    request = self.app.REQUEST
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    request['here'] = test1

    test1.foo_1.setTitle('foo_title_4')
    test1.foo_1.setStartDate('2009-01-01')
    message = listbox.ListBox_setPropertyList(
      field_list_method = 'objectValues',
      field_portal_types = 'Foo Line | Foo Line',
      field_stat_method = '',
      field_stat_columns = 'quantity | Foo_statQuantity',
      field_columns = 'id|ID\ntitle|Title\nquantity|Quantity\nstart_date|Date',)
    self.failUnless('Set Successfully' in message)
    listboxline_list = listbox.get_value('default', render_format = 'list',
                                         REQUEST = request)
    for line in listboxline_list:
      self.assertEqual(line.isStatLine(), False)
    self.assertEqual(len(listboxline_list), 3)
    self.assertTrue(listboxline_list[1].getColumnProperty('title') == "foo_title_4")

    odf_document = foo_printout.index_html(REQUEST=request)
    #test_output = open("/tmp/test_02_04_Table.odf", "w")
    #test_output.write(odf_document)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(odf_document is not None)
    self.assertFalse(content_xml.find("foo_title_3") > 0)
    self.assertTrue(content_xml.find("foo_title_4") > 0)

    content = etree.XML(content_xml)
    table_row_xpath = '//table:table[@table:name="listbox"]/table:table-row'
    odf_table_rows = content.xpath(table_row_xpath, namespaces=content.nsmap)
    self.assertEqual(len(odf_table_rows), 2)
Fabien Morin's avatar
Fabien Morin committed
493
    # to test copying ODF table cell styles
494 495 496 497 498 499 500 501
    first_row = odf_table_rows[0]
    first_row_columns = first_row.getchildren()
    last_row = odf_table_rows[-1]
    last_row_columns = last_row.getchildren()
    span_attribute = "{%s}number-columns-spanned" % content.nsmap['table']
    self.assertFalse(first_row_columns[0].attrib.has_key(span_attribute))
    self.assertEqual(int(last_row_columns[0].attrib[span_attribute]), 2)
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
502 503

  def test_02_Table_05_NormalSameLayout(self):
504 505 506
    """5. Normal case: the listobx and the ODF table are same layout

    * Test Data Format:
Fabien Morin's avatar
Fabien Morin committed
507

508 509 510 511 512 513 514 515 516 517 518 519
     ODF table named 'listbox2'
     +-------------------------------+
     |  A    |   B   |   C   |   D   |
     |-------+-------+-------+-------|
     |       |       |       |       |
     +-------+-------+-------+-------+
    """
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
Fabien Morin's avatar
Fabien Morin committed
520
    request = self.app.REQUEST
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    request['here'] = test1

    foo_form.manage_renameObject('listbox', 'listbox2', REQUEST=request)
    listbox2 = foo_form.listbox2
    test1.foo_1.setTitle('foo_title_5')
    message = listbox2.ListBox_setPropertyList(
      field_list_method = 'objectValues',
      field_portal_types = 'Foo Line | Foo Line',
      field_stat_method = 'portal_catalog',
      field_stat_columns = 'quantity | Foo_statQuantity',
      field_columns = 'id|ID\ntitle|Title\nquantity|Quantity\nstart_date|Date',)
    self.failUnless('Set Successfully' in message)
    listboxline_list = listbox2.get_value('default', render_format = 'list',
                                         REQUEST = request)
    self.assertEqual(len(listboxline_list), 4)
    self.assertTrue(listboxline_list[1].getColumnProperty('title') == "foo_title_5")

    odf_document = foo_printout.index_html(REQUEST=request)
    #test_output = open("/tmp/test_02_05_Table.odf", "w")
    #test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    content_tree = etree.XML(content_xml)
    #Check that foo_1 is inside table, with the same style
    xpath_style_name_expression = '//table:table[@table:name="listbox2"]/table:table-row/table:table-cell/text:p[@text:style-name="P4"]/text()'
    self.assertEquals(['foo_1', 'foo_2'], content_tree.xpath(xpath_style_name_expression, namespaces=content_tree.nsmap))
    #Check that each listbox values are inside ODT table cells
    xpath_result_expression = '//table:table[@table:name="listbox2"]/table:table-row/table:table-cell/text:p/text()'
    self.assertEquals(['foo_1', 'foo_title_5', 'foo_2', 'foo_2', '1234.5'], content_tree.xpath(xpath_result_expression, namespaces=content_tree.nsmap))
    self.assertFalse(content_xml.find("foo_title_4") > 0)
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
553

554 555 556
    # put back the field name
    foo_form.manage_renameObject('listbox2', 'listbox', REQUEST=request)

Fabien Morin's avatar
Fabien Morin committed
557
  def test_02_Table_06_TableDoesNotHaveAHeader(self):
558 559
    """6. Normal case: ODF table does not have a header
     * Test Data format:
Fabien Morin's avatar
Fabien Morin committed
560

561 562 563 564 565 566 567 568 569 570 571 572 573 574
     ODF table named 'listbox3'
     the table listbox3 has not table header.
     first row is a table content, too.
     +-------------------------------+
     |  1    |   2   |   3   |   4   |
     |-------+-------+-------+-------|
     |       |       |       |       |
     +-------+-------+-------+-------+
    """
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
Fabien Morin's avatar
Fabien Morin committed
575
    request = self.app.REQUEST
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    request['here'] = test1

    foo_form.manage_renameObject('listbox', 'listbox3', REQUEST=request)
    listbox3 = foo_form.listbox3
    test1.foo_1.setTitle('foo_title_6')
    message = listbox3.ListBox_setPropertyList(
      field_list_method = 'objectValues',
      field_portal_types = 'Foo Line | Foo Line',
      field_stat_method = 'portal_catalog',
      field_stat_columns = 'quantity | Foo_statQuantity',
      field_columns = 'id|ID\ntitle|Title\nquantity|Quantity\nstart_date|Date',)
    self.failUnless('Set Successfully' in message)
    listboxline_list = listbox3.get_value('default', render_format = 'list',
                                         REQUEST = request)
    self.assertEqual(len(listboxline_list), 4)
    self.assertTrue(listboxline_list[1].getColumnProperty('title') == "foo_title_6")
Fabien Morin's avatar
Fabien Morin committed
592

593 594 595 596 597 598 599 600 601
    odf_document = foo_printout.index_html(REQUEST=request)
    #test_output = open("/tmp/test_02_06_Table.odf", "w")
    #test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertFalse(content_xml.find("foo_title_5") > 0)
    self.assertTrue(content_xml.find("foo_title_6") > 0)
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
602

603 604 605
    # put back the field name
    foo_form.manage_renameObject('listbox3', 'listbox', REQUEST=request)

Fabien Morin's avatar
Fabien Morin committed
606
  def test_02_Table_07_CellFormat(self):
607
    """7. Normal case: make sure that it is enable to use ODF cell format setting in FormPrintout"""
608 609 610 611 612
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
Fabien Morin's avatar
Fabien Morin committed
613
    request = self.app.REQUEST
614 615 616
    request['here'] = test1

    test1.foo_1.setTitle('foo_title_7')
617
    test1.foo_1.setStartDate(DateTime(2009,4,20))
618 619 620 621 622 623 624 625 626 627 628
    message = listbox.ListBox_setPropertyList(
      field_list_method = 'objectValues',
      field_portal_types = 'Foo Line | Foo Line',
      field_stat_method = 'portal_catalog',
      field_stat_columns = 'quantity | Foo_statQuantity',
      field_columns = 'id|ID\ntitle|Title\nquantity|Quantity\nstart_date|Date',)
    self.failUnless('Set Successfully' in message)
    listboxline_list = listbox.get_value('default', render_format = 'list',
                                         REQUEST = request)
    self.assertEqual(len(listboxline_list), 4)
    self.assertTrue(listboxline_list[1].getColumnProperty('title') == "foo_title_7")
Fabien Morin's avatar
Fabien Morin committed
629

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
    odf_document = foo_printout.index_html(REQUEST=request)
    #test_output = open("/tmp/test_02_07_Table.odf", "w")
    #test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertFalse(content_xml.find("foo_title_6") > 0)
    self.assertTrue(content_xml.find("foo_title_7") > 0)

    content = etree.XML(content_xml)
    table_row_xpath = '//table:table[@table:name="listbox"]/table:table-row'
    odf_table_rows = content.xpath(table_row_xpath, namespaces=content.nsmap)
    self.assertEqual(len(odf_table_rows), 3)
    # to test ODF table cell number format
    first_row = odf_table_rows[0]
    first_row_columns = first_row.getchildren()
    date_column = first_row_columns[3]
    date_value_attrib = "{%s}date-value" % content.nsmap['office']
    self.assertTrue(date_column.attrib.has_key(date_value_attrib))
    self.assertEqual(date_column.attrib[date_value_attrib], '2009-04-20')
    self._validate(odf_document)

Fabien Morin's avatar
Fabien Morin committed
652
  def test_02_Table_08_Nodata(self):
653 654 655 656 657 658
    """8. Normal case: list box has no data"""
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
Fabien Morin's avatar
Fabien Morin committed
659
    request = self.app.REQUEST
660 661 662 663 664 665 666 667 668 669
    request['here'] = test1

    test1.foo_1.setTitle('foo_title_8')
    message = listbox.ListBox_setPropertyList(
      field_columns = 'id|ID\ntitle|Title\nquantity|Quantity\nstart_date|Date',)
    self.failUnless('Set Successfully' in message)
    listboxline_list = listbox.get_value('default', render_format = 'list',
                                         REQUEST = request)
    # title line only
    self.assertEqual(len(listboxline_list), 1)
Fabien Morin's avatar
Fabien Morin committed
670

671 672 673 674 675 676 677 678 679 680 681 682 683 684
    odf_document = foo_printout.index_html(REQUEST=request)
    #test_output = open("/tmp/test_02_08_Table.odf", "w")
    #test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")

    content = etree.XML(content_xml)
    table_row_xpath = '//table:table[@table:name="listbox"]/table:table-row'
    odf_table_rows = content.xpath(table_row_xpath, namespaces=content.nsmap)
    # no rows
    self.assertEqual(len(odf_table_rows), 0)
    self._validate(odf_document)

Fabien Morin's avatar
Fabien Morin committed
685
  def test_02_Table_09_StyleSetting(self):
686
    """ 9. Normal case: setting the style of the row.
Fabien Morin's avatar
Fabien Morin committed
687

688 689 690 691 692 693 694 695
     * Test Data format:
     The table listbox4 has six rows which contains the reference of the row.
    """
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
Fabien Morin's avatar
Fabien Morin committed
696
    request = self.app.REQUEST
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
    request['here'] = test1

    for i in xrange(3, 7):
      foo_id = "foo_%s" % i
      if test1._getOb(foo_id, None) is None:
        test1.newContent(foo_id, portal_type='Foo Line')

    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'Foo_getRowCssList',
      '*args,**kw',
r"""
line_index = kw['list_index']
line_number = line_index + 1
for n in xrange(6, 0, -1):
  if line_number % n is 0:
    return "line" + str(n)
"""
      )

    foo_form.manage_renameObject('listbox', 'listbox4', REQUEST=request)
    listbox4 = foo_form.listbox4
    test1.foo_1.setTitle('foo_title_9')
    message = listbox4.ListBox_setPropertyList(
      field_list_method = 'objectValues',
      field_portal_types = 'Foo Line | Foo Line',
      field_row_css_method = 'Foo_getRowCssList',
      field_columns = 'id|ID\ntitle|Title\nquantity|Quantity',)
    self.failUnless('Set Successfully' in message)
    listboxline_list = listbox4.get_value('default', render_format = 'list',
                                          REQUEST = request)
    self.assertEqual(len(listboxline_list), 7)
    self.assertTrue(listboxline_list[1].getColumnProperty('title') == "foo_title_9")

    ## test
    odf_document = foo_printout.index_html(REQUEST=request)
    #test_output = open("/tmp/test_02_Table_09_StyleSetting.odf", "w")
    #test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("foo_title_9") > 0)
Fabien Morin's avatar
Fabien Morin committed
739

740 741 742 743
    content = etree.XML(content_xml)
    table_row_xpath = '//table:table[@table:name="listbox4"]/table:table-row'
    table_row_list = content.xpath(table_row_xpath, namespaces=content.nsmap)
    self.assertEqual(len(table_row_list), 6)
Fabien Morin's avatar
Fabien Morin committed
744

745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
    line2 = table_row_list[1]
    line2_cell_list = line2.xpath('table:table-cell', namespaces=content.nsmap)
    self.assertEqual(len(line2_cell_list), 2)
    line2_cell2 = line2_cell_list[1]
    span_attribute_name = '{%s}number-columns-spanned' % content.nsmap['table']
    line2_cell2_span = line2_cell2.attrib[span_attribute_name]
    self.assertEqual(line2_cell2_span, "2")

    line3 = table_row_list[2]
    line3_cell_list = line3.xpath('table:table-cell', namespaces=content.nsmap)
    self.assertEqual(len(line3_cell_list), 1)
    line3_cell1 = line3_cell_list[0]
    line3_cell1_span = line3_cell1.attrib[span_attribute_name]
    self.assertEqual(line3_cell1_span, "3")

    line5 = table_row_list[4]
    line5_cell_list = line5.xpath('table:table-cell', namespaces=content.nsmap)
    self.assertEqual(len(line5_cell_list), 2)
    line5_cell1 = line5_cell_list[0]
    line5_cell1_span = line5_cell1.attrib[span_attribute_name]
    self.assertEqual(line5_cell1_span, "2")
Fabien Morin's avatar
Fabien Morin committed
766

767
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
768

769 770 771 772 773
    # put back the field name
    foo_form.manage_renameObject('listbox4', 'listbox', REQUEST=request)
    # delete the test objects
    test1.manage_delObjects(['foo_3','foo_4','foo_5','foo_6'])

Fabien Morin's avatar
Fabien Morin committed
774
  def _test_03_Frame(self):
775 776 777 778 779
    """
    Frame not tested yet
    """
    pass

Fabien Morin's avatar
Fabien Morin committed
780
  def test_04_Iteration(self):
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
    """
    Iteration using ERP5Report ReportSection test
    """
    # create test target
    custom = self.portal.portal_skins.custom
    erp5form = custom.manage_addProduct['ERP5Form']
    erp5form.addERP5Report(id='FooReport_view', title='Foo Report')
    foo_report_view = custom.FooReport_view
    foo_report_view.report_method = 'FooReport_getReportSectionList'

    erp5form.addERP5Form(id='Foo2_view', title='Foo2')
    foo2_view = custom.Foo2_view
    foo2_view.manage_addField('listbox_report', 'listbox report', 'ListBox')
    listbox = foo2_view.listbox_report

    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'FooReport_getFooList',
      'title,**kw',
r"""
foo_list = context.objectValues(portal_type='Foo Line')
for foo in foo_list:
  foo.setTitle(title)
return foo_list
"""
      )
    message = listbox.ListBox_setPropertyList(
      field_list_method = 'FooReport_getFooList',
      field_selection_name = 'listbox_report_selection',
      field_portal_types = 'Foo Line | Foo Line',
      field_columns = 'id|ID\ntitle|Title\nquantity|Quantity\nstart_date|Date',)
    self.failUnless('Set Successfully' in message)
    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'FooReport_getReportSectionList',
      '',
r"""
from Products.ERP5Form.Report import ReportSection

r1 = ReportSection(path=context.getPhysicalPath(),
                   form_id='Foo2_view',
                   selection_name='listbox_report_selection',
                   selection_params={'title':'foo_04_Iteration_1'})
r2 = ReportSection(path=context.getPhysicalPath(),
                   form_id='Foo2_view',
                   selection_name='listbox_report_selection',
                   selection_params={'title':'foo_04_Iteration_2'})
report_section_list = [r1, r2]
return report_section_list
"""
      )
Fabien Morin's avatar
Fabien Morin committed
832

833 834 835 836 837 838 839 840
    # 01. normal case using Frame
    test1 = self.portal.foo_module.test1
    foo_report_printout = test1.FooReport_viewAsPrintout
    foo_report_printout.doSettings(REQUEST=None,
                                   title='',
                                   form_name='FooReport_view',
                                   template='Foo2_getODTStyleSheet')
    self._validate(self.getODFDocumentFromPrintout(foo_report_printout))
841
    odf_document = foo_report_printout(self.portal.REQUEST)
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857

    #test_output = open("/tmp/test_04_Iteratoin.odf", "w")
    #test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("foo_04_Iteration_1") > 0)
    content = etree.XML(content_xml)
    frame_xpath = '//draw:frame[@draw:name="FooReport_getReportSectionList"]'
    frame_list = content.xpath(frame_xpath, namespaces=content.nsmap)
    self.assertEqual(len(frame_list), 1)
    frame1_xpath = '//draw:frame[@draw:name="FooReport_getReportSectionList_1"]'
    frame1_list = content.xpath(frame1_xpath, namespaces=content.nsmap)
    self.assertEqual(len(frame1_list), 1)

    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
858

859 860 861 862 863 864 865 866 867 868
    # 02. no report section using frame
    custom.manage_delObjects(['FooReport_getReportSectionList'])
    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'FooReport_getReportSectionList',
      '',
r"""
return []
"""
      )
869
    odf_document = foo_report_printout(self.portal.REQUEST)
870 871 872 873 874 875 876 877 878 879 880 881 882 883
    #test_output = open("/tmp/test_04_02_Iteratoin.odf", "w")
    #test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertFalse(content_xml.find("foo_04_Iteration") > 0)
    content = etree.XML(content_xml)
    frame_xpath = '//draw:frame[@draw:name="FooReport_getReportSectionList"]'
    frame_list = content.xpath(frame_xpath, namespaces=content.nsmap)
    # the frame was removed
    self.assertEqual(len(frame_list), 0)
    self._validate(odf_document)


Fabien Morin's avatar
Fabien Morin committed
884
  def test_04_Iteration_02_Section(self):
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
    """
    Iteration using ERP5Report ReportSection and ODF Section test
    """
    # create test target
    custom = self.portal.portal_skins.custom
    erp5form = custom.manage_addProduct['ERP5Form']
    erp5form.addERP5Report(id='FooReport_view', title='Foo Report')
    foo_report_view = custom.FooReport_view
    foo_report_view.report_method = 'FooReport_getReportSectionList'

    erp5form.addERP5Form(id='Foo2_view', title='Foo2')
    foo2_view = custom.Foo2_view
    foo2_view.manage_addField('listbox_report', 'listbox report', 'ListBox')
    listbox = foo2_view.listbox_report

    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'FooReport_getFooList',
      'title,**kw',
r"""
foo_list = context.objectValues(portal_type='Foo Line')
for foo in foo_list:
  foo.setTitle(title)
return foo_list
"""
      )
    message = listbox.ListBox_setPropertyList(
      field_list_method = 'FooReport_getFooList',
      field_selection_name = 'listbox_report_selection',
      field_portal_types = 'Foo Line | Foo Line',
      field_columns = 'id|ID\ntitle|Title\nquantity|Quantity\nstart_date|Date',)
    self.failUnless('Set Successfully' in message)
    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'FooReport_getReportSectionList',
      '',
r"""
from Products.ERP5Form.Report import ReportSection

r1 = ReportSection(path=context.getPhysicalPath(),
                   form_id='Foo2_view',
                   selection_name='listbox_report_selection',
                   selection_params={'title':'foo_04_Iteration_1'})
r2 = ReportSection(path=context.getPhysicalPath(),
                   form_id='Foo2_view',
                   selection_name='listbox_report_selection',
                   selection_params={'title':'foo_04_Iteration_2'})
report_section_list = [r1, r2]
return report_section_list
"""
       )

    # 01. normal case using ODF Section
    test1 = self.portal.foo_module.test1
    foo_report_printout = test1.FooReport_viewAsPrintout
    foo_report_printout.doSettings(REQUEST=None,
                                   title='',
                                   form_name='FooReport_view',
                                   template='Foo3_getODTStyleSheet')
    self._validate(self.getODFDocumentFromPrintout(foo_report_printout))
945
    odf_document = foo_report_printout(self.portal.REQUEST)
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961

    #test_output = open("/tmp/test_04_Iteratoin_02_Section_01.odf", "w")
    #test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("foo_04_Iteration_1") > 0)
    content = etree.XML(content_xml)
    section_xpath = '//text:section[@text:name="FooReport_getReportSectionList"]'
    section_list = content.xpath(section_xpath, namespaces=content.nsmap)
    self.assertEqual(len(section_list), 1)
    section1_xpath = '//text:section[@text:name="FooReport_getReportSectionList_1"]'
    section1_list = content.xpath(section1_xpath, namespaces=content.nsmap)
    self.assertEqual(len(section1_list), 1)

    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
962

963 964 965 966 967 968 969 970 971 972
    # 02. no report section and using ODF Section
    custom.manage_delObjects(['FooReport_getReportSectionList'])
    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'FooReport_getReportSectionList',
      '',
r"""
return []
"""
      )
973
    odf_document = foo_report_printout(self.portal.REQUEST)
974 975 976 977 978 979 980 981 982 983 984 985 986
    #test_output = open("/tmp/test_04_Iteratoin_02_Section_02.odf", "w")
    #test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertFalse(content_xml.find("foo_04_Iteration") > 0)
    content = etree.XML(content_xml)
    section_xpath = '//text:section[@text:name="FooReport_getReportSectionList"]'
    section_list = content.xpath(section_xpath, namespaces=content.nsmap)
    # the section was removed
    self.assertEqual(len(section_list), 0)
    self._validate(odf_document)

Fabien Morin's avatar
Fabien Morin committed
987 988

  def test_04_Iteration_03_ReportBox_and_Section(self):
989 990 991 992 993 994
    """
    Iteration using ReportBox and ODF Section test
    """
    # create test target
    custom = self.portal.portal_skins.custom
    erp5form = custom.manage_addProduct['ERP5Form']
Fabien Morin's avatar
Fabien Morin committed
995

996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
    erp5form.addERP5Form(id='Foo_Box_view', title='Foo Box')
    foo_box_view = custom.Foo_Box_view
    foo_box_view.manage_addField('listbox_report', 'listbox report', 'ListBox')
    listbox = foo_box_view.listbox_report

    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'FooReport_getFooList',
      'title,**kw',
r"""
foo_list = context.objectValues(portal_type='Foo Line')
for foo in foo_list:
  foo.setTitle(title)
return foo_list
"""
      )

    message = listbox.ListBox_setPropertyList(
      field_list_method = 'FooReport_getFooList',
      field_selection_name = 'listbox_report_selection',
      field_portal_types = 'Foo Line | Foo Line',
      field_columns = 'id|ID\ntitle|Title\nquantity|Quantity\nstart_date|Date',)
    self.failUnless('Set Successfully' in message)

    # report box
    foo2_view = erp5form.addERP5Form(id='Foo2_view', title='Foo2 View')
    foo2_view = custom.Foo2_view
    foo2_view.manage_addField('your_report_box1', 'Your Report Box', 'ReportBox')
    your_report_box1 = foo2_view.your_report_box1
    your_report_box1._edit({'report_method':'FooReport_getReportSectionList'})

    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'FooReport_getReportSectionList',
      '',
r"""
from Products.ERP5Form.Report import ReportSection

r1 = ReportSection(path=context.getPhysicalPath(),
                   form_id='Foo_Box_view',
                   selection_name='listbox_report_selection',
                   selection_params={'title':'foo_04_Iteration_1'})
r2 = ReportSection(path=context.getPhysicalPath(),
                   form_id='Foo_Box_view',
                   selection_name='listbox_report_selection',
                   selection_params={'title':'foo_04_Iteration_2'})
report_section_list = [r1, r2]
return report_section_list
"""
       )

    # 01. normal case using ODF Section
    test1 = self.portal.foo_module.test1
    request = self.app.REQUEST
    request['here'] = test1
    foo_report_printout = test1.FooReport_viewAsPrintout
    foo_report_printout.doSettings(REQUEST=request,
                                   title='',
                                   form_name='Foo2_view',
                                   template='Foo4_getODTStyleSheet')
    self._validate(self.getODFDocumentFromPrintout(foo_report_printout))
1057
    odf_document = foo_report_printout(self.portal.REQUEST)
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073

    # test_output = open("/tmp/test_04_Iteratoin_03_Section_01.odf", "w")
    # test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("foo_04_Iteration_1") > 0)
    content = etree.XML(content_xml)
    section_xpath = '//text:section[@text:name="your_report_box1"]'
    section_list = content.xpath(section_xpath, namespaces=content.nsmap)
    self.assertEqual(len(section_list), 1)
    section1_xpath = '//text:section[@text:name="your_report_box1_1"]'
    section1_list = content.xpath(section1_xpath, namespaces=content.nsmap)
    self.assertEqual(len(section1_list), 1)

    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
1074

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
    # 02. no report section and using ODF Section
    custom.manage_delObjects(['FooReport_getReportSectionList'])
    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'FooReport_getReportSectionList',
      '',
r"""
return []
"""
      )
1085
    odf_document = foo_report_printout(self.portal.REQUEST)
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
    #test_output = open("/tmp/test_04_Iteratoin_02_Section_02.odf", "w")
    #test_output.write(odf_document)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertFalse(content_xml.find("foo_04_Iteration") > 0)
    content = etree.XML(content_xml)
    section_xpath = '//text:section[@text:name="your_report_box1"]'
    section_list = content.xpath(section_xpath, namespaces=content.nsmap)
    # the section was removed
    self.assertEqual(len(section_list), 0)
    self._validate(odf_document)

Fabien Morin's avatar
Fabien Morin committed
1099
  def _test_05_Styles(self):
1100 1101 1102 1103 1104
    """
    styles.xml not tested yet
    """
    pass

Fabien Morin's avatar
Fabien Morin committed
1105
  def _test_06_Meta(self):
1106 1107 1108 1109 1110
    """
    meta.xml not supported yet
    """
    pass

Fabien Morin's avatar
Fabien Morin committed
1111
  def test_07_Image(self):
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
    """
    Image mapping not tested yet
    """
    current_dir = os.path.dirname(__file__)
    parent_dir = os.path.dirname(current_dir)
    image_path = os.path.join(parent_dir,
                              'www',
                              'form_printout_icon.png')
    file_data = FileUpload(image_path, 'rb')
    image = self.portal.newContent(portal_type='Image', id='test_image')
    image.edit(file=file_data)

    foo_printout = self.portal.foo_module.test1.Foo_viewAsPrintout
    foo_form = self.portal.foo_module.test1.Foo_view
    if foo_form._getOb("my_default_image_absolute_url", None) is None:
      foo_form.manage_addField('my_default_image_absolute_url', 'logo', 'ImageField')
    my_default_image_absolute_url = foo_form.my_default_image_absolute_url
    my_default_image_absolute_url.values['default'] = image.absolute_url_path()

    # 01: Normal
1132
    odf_document = foo_printout(self.portal.REQUEST)
1133 1134 1135 1136 1137 1138 1139 1140
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    content = etree.XML(content_xml)
    image_frame_xpath = '//draw:frame[@draw:name="my_default_image_absolute_url"]'
    image_frame_list = content.xpath(image_frame_xpath, namespaces=content.nsmap)
    self.assertTrue(len(image_frame_list) > 0)
    image_frame = image_frame_list[0]
1141 1142 1143 1144
    height = image_frame.attrib['{%s}height' % content.nsmap['svg']]
    self.assertTrue(height in ('0.838cm', '0.3299in'))
    width = image_frame.attrib['{%s}width' % content.nsmap['svg']]
    self.assertTrue(width in ('0.838cm', '0.3299in'))
1145 1146 1147 1148
    self._validate(odf_document)

    # 02: no image data
    my_default_image_absolute_url.values['default'] = ''
1149
    odf_document = foo_printout(self.portal.REQUEST)
1150 1151 1152
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
Fabien Morin's avatar
Fabien Morin committed
1153
    # confirming the image was removed
1154 1155
    self.assertTrue(content_xml.find('<draw:image xlink:href') < 0)
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
1156 1157

  def test_08_OOoConversion(self):
1158 1159 1160 1161 1162 1163 1164
    """test ooo conversion"""
    foo_printout = self.portal.foo_module.test1.Foo_viewAsPrintout
    foo_form = self.portal.foo_module.test1.Foo_view
    if foo_form._getOb("my_test_title", None) is None:
      foo_form.manage_addField('my_test_title', 'test title', 'StringField')
    test_title = foo_form.my_test_title
    test_title.values['default'] = 'ZZZ test here ZZZ'
Fabien Morin's avatar
Fabien Morin committed
1165

1166
    self.portal.REQUEST.set('format', 'pdf')
Fabien Morin's avatar
Fabien Morin committed
1167
    printout = foo_printout(REQUEST=self.portal.REQUEST)
1168 1169 1170 1171 1172
    #test_output = open("/tmp/test_99_OOoConversion.pdf", "w")
    #test_output.write(printout.data)
    self.assertEqual('application/pdf', guessMime(printout.data))

    self.portal.REQUEST.set('format', 'doc')
Fabien Morin's avatar
Fabien Morin committed
1173
    printout = foo_printout(REQUEST=self.portal.REQUEST)
1174 1175 1176 1177
    #test_output = open("/tmp/test_99_OOoConversion.doc", "w")
    #test_output.write(printout.data)
    self.assertEqual('application/msword', guessMime(printout.data))

1178
  def test_09_FieldReplacement(self, validate=False):
1179 1180
    """test field in ODF Documents"""
    foo_printout = self.portal.foo_module.test1.Foo5_viewAsPrintout
1181 1182
    if validate:
      self._validate(self.getODFDocumentFromPrintout(foo_printout))
1183 1184 1185 1186 1187 1188 1189
    foo_form = self.portal.foo_module.test1.Foo_view
    field_name = 'your_checkbox'
    if foo_form._getOb(field_name, None) is None:
      foo_form.manage_addField(field_name, 'CheckBox', 'CheckBoxField')
    checkbox = getattr(foo_form, field_name)

    checkbox.values['default'] = 1
1190
    odf_document = foo_printout(self.portal.REQUEST)
1191 1192
    if validate:
      self._validate(odf_document)
1193 1194 1195 1196 1197 1198 1199
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    document_tree = etree.XML(content_xml)
    node = document_tree.xpath('//form:checkbox[@form:name = "%s"]' % field_name, namespaces=document_tree.nsmap)[0]
    self.assertTrue(node.get('{%s}current-state' % document_tree.nsmap['form']))

    checkbox.values['default'] = 0
1200
    odf_document = foo_printout(self.portal.REQUEST)
1201 1202
    if validate:
      self._validate(odf_document)
1203 1204 1205 1206 1207 1208
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    document_tree = etree.XML(content_xml)
    node = document_tree.xpath('//form:checkbox[@form:name = "%s"]' % field_name, namespaces=document_tree.nsmap)[0]
    self.assertFalse(node.get('{%s}current-state' % document_tree.nsmap['form']))

1209 1210 1211 1212 1213 1214 1215
  @skip('Disable validation because OOo does not produce compliant'\
        ' xml, and RelaxNG status is still draft')
  def test_09_FieldReplacementWithValidation(self):
    """
    """
    return self.test_09_FieldReplacement(validate=True)

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
  def test_field_replacement_with_variable(self):
    """test variables replacement in ODT documents.
    """
    document = self.portal.foo_module.test1
    document.setTitle(None)
    foo_form = document.Foo_view
    field_configuration_list = (
      ('my_string', 'StringField', 'ZZZ test here ZZZ'),
      ('my_figure', 'IntegerField', 221),
      ('my_float', 'FloatField', 23.43535),
      ('my_date', 'DateTimeField', DateTime('2010-12-6 23:24:15.234 GMT+6')),
1227
      ('my_boolean', 'CheckBoxField', False),
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
      )
    for field_configuration in field_configuration_list:
      field_id, klass, value = field_configuration
      if foo_form._getOb(field_id, None) is not None:
        foo_form._delObject(field_id)
      foo_form.manage_addField(field_id, field_id, klass)
      field = foo_form[field_id]
      field.values['default'] = value
    foo_printout = self.portal.foo_module.test1.Foo_viewAsPrintout
    # Use template with variable defines
    foo_printout.template = 'Foo_getVariableODTStyleSheet'
    odf_document = foo_printout(self.portal.REQUEST)
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    content_tree = etree.fromstring(content_xml)
    nsmap = content_tree.nsmap
    for field_configuration in field_configuration_list:
      field_id, klass, value = field_configuration
      xpath = '//text:variable-set[@text:name = "%s"]' % field_id
      node_list = content_tree.xpath(xpath, namespaces=nsmap)
      self.assertEquals(1, len(node_list))
      node = node_list[0]
      if klass == 'StringField':
        self.assertEquals(node.get('{%s}value-type' % nsmap['office']),
                          'string')
        self.assertEquals(node.text, value)
      elif klass in ('IntegerField', 'FloatField'):
        self.assertEquals(node.get('{%s}value-type' % nsmap['office']),
                          'float')
        self.assertEquals(node.get('{%s}value' % nsmap['office']), str(value))
      elif klass == 'DateTimeField':
        self.assertEquals(node.get('{%s}value-type' % nsmap['office']), 'date')
        self.assertEquals(node.text, value.strftime('%d/%m/%Y %H:%M:%S'))
1262 1263 1264 1265 1266 1267
      elif klass == 'CheckBoxField':
        self.assertEquals(node.get('{%s}value-type' % nsmap['office']),
                          'boolean')
        self.assertEquals(node.get('{%s}boolean-value' % nsmap['office']),
                          'false')
        self.assertEquals(node.text, 'FALSE')
1268 1269 1270
      else:
        raise NotImplementedError

1271 1272 1273 1274
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestFormPrintoutAsODT))
  return suite