testFormPrintoutAsODG.py 20.1 KB
Newer Older
Fabien Morin's avatar
Fabien Morin committed
1
# -*- coding: utf-8 -*-
2
##############################################################################
Fabien Morin's avatar
Fabien Morin committed
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
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#                    Fabien Morin <fabien@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
33
from Products.ERP5OOo.tests.TestFormPrintoutMixin import TestFormPrintoutMixin
Fabien Morin's avatar
Fabien Morin committed
34 35
from Products.ERP5OOo.OOoUtils import OOoBuilder
from Products.ERP5OOo.tests.utils import Validator
Fabien Morin's avatar
Fabien Morin committed
36
from Products.ERP5Type.tests.utils import FileUpload
37
from DateTime.DateTime import DateTime
Fabien Morin's avatar
Fabien Morin committed
38 39 40
from lxml import etree
import os

41
class TestFormPrintoutAsODG(TestFormPrintoutMixin):
Fabien Morin's avatar
Fabien Morin committed
42 43 44 45 46 47 48 49 50 51
  run_all_test = 1

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

  def afterSetUp(self):
    self.login()
52
    self.setSystemPreference()
Fabien Morin's avatar
Fabien Morin committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    # XML validator
    v12schema_url = os.path.join(os.path.dirname(__file__),
                                 'OpenDocument-schema-v1.2-draft9.rng')
    self.validator = Validator(schema_url=v12schema_url)

    foo_file_path = os.path.join(os.path.dirname(__file__),
                                'test_document',
                                'Foo_001.odg')
    foo_file = open(foo_file_path, 'rb')
    self._validate(foo_file.read())
    custom = self.portal.portal_skins.custom
    addStyleSheet = custom.manage_addProduct['OFSP'].manage_addFile
    if custom._getOb('Foo_getODGStyleSheet', None) is None:
      addStyleSheet(id='Foo_getODGStyleSheet', file=foo_file, title='',
                    precondition='',
                    content_type='application/vnd.oasis.opendocument.graphics')
    erp5OOo = custom.manage_addProduct['ERP5OOo']
    addOOoTemplate = erp5OOo.addOOoTemplate
    if custom._getOb('Foo_viewAsOdg', None) is None:
      addOOoTemplate(id='Foo_viewAsOdg', title='')
    request = self.app.REQUEST
    Foo_viewAsOdg = custom.Foo_viewAsOdg
    Foo_viewAsOdg.doSettings(request, title='', xml_file_id='content.xml',
                             ooo_stylesheet='Foo_getODGStyleSheet')
    builder = OOoBuilder(foo_file)
    content = builder.extract('content.xml')
    Foo_viewAsOdg.pt_edit(content,
        content_type='application/vnd.oasis.opendocument.graphics')
    if custom._getOb('Foo_viewAsODGPrintout', None) is None:
      erp5OOo.addFormPrintout(id='Foo_viewAsODGPrintout', title='',
                              form_name='Foo_view', template='Foo_getODGStyleSheet')
84 85 86
    if custom._getOb('Foo_viewProxyFieldAsODGPrintout', None) is None:
      erp5OOo.addFormPrintout(id='Foo_viewProxyFieldAsODGPrintout', title='',
                              form_name='Foo_viewProxyField', template='Foo_getODGStyleSheet')
Fabien Morin's avatar
Fabien Morin committed
87 88 89 90 91 92 93 94 95 96
    if custom._getOb('FooReport_viewAsODGPrintout', None) is None:
      erp5OOo.addFormPrintout(id='FooReport_viewAsODGPrintout',
                              title='')

    ## 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:
97
      test1.newContent("foo_1", title='Foo Line 1', portal_type='Foo Line')
Fabien Morin's avatar
Fabien Morin committed
98
    if test1._getOb("foo_2", None) is None:
99
      test1.newContent("foo_2", title='Foo Line 2', portal_type='Foo Line')
Fabien Morin's avatar
Fabien Morin committed
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
    transaction.commit()
    self.tic()

  def getStyleDictFromFieldName(self, content_xml, field_id):
    '''parse content_xml string and return a dict with node node.tag
    as key and style dict as value
    '''
    element_tree = etree.XML(content_xml)
    text_xpath = '//draw:frame[@draw:name="%s"]/*' % field_id
    node_list = element_tree.xpath(text_xpath, namespaces=element_tree.nsmap)
    style_dict = {}
    for target_node in node_list:
      style_dict = {}
      for descendant in target_node.iterdescendants():
        style_dict.setdefault(descendant.tag, {}).update(descendant.attrib)
    return style_dict

  def test_01_TextField(self):
    """
    mapping a field to textbox
    """
    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()

    style_dict = {'{urn:oasis:names:tc:opendocument:xmlns:text:1.0}span':
                    {'{urn:oasis:names:tc:opendocument:xmlns:text:1.0}style-name': 'T2'},
132
                  '{urn:oasis:names:tc:opendocument:xmlns:text:1.0}p': {}
Fabien Morin's avatar
Fabien Morin committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
                 }

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

    # extract content.xml from original odg document
    original_doc_builder = OOoBuilder(original_file_content)
    original_content_xml = original_doc_builder.extract("content.xml")
    # get style of the title in the orignal test document
    original_document_style_dict = self.getStyleDictFromFieldName(original_content_xml,
        'my_title')

    # check the style is good before the odg generation
    self.assertEqual(original_document_style_dict, style_dict)

    request = self.app.REQUEST
    # 1. Normal case: "my_title" field to the "my_title" reference in the ODF document
152
    odf_document = foo_printout.index_html(request)
Fabien Morin's avatar
Fabien Morin committed
153 154 155 156 157 158 159 160 161 162 163
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    final_document_style_dict = self.getStyleDictFromFieldName(content_xml,
        'my_title')

    # check the style is keept after the odg generation
    self.assertEqual(final_document_style_dict, style_dict)

    self.assertTrue(content_xml.find("Foo title!") > 0)
    self.assertEqual(request.RESPONSE.getHeader('content-type'),
164
                     'application/vnd.oasis.opendocument.graphics')
Fabien Morin's avatar
Fabien Morin committed
165 166 167 168 169 170 171
    self.assertEqual(request.RESPONSE.getHeader('content-disposition'),
                     'inline;filename="Foo_viewAsODGPrintout.odg"')
    self._validate(odf_document)

    # 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!")
172
    odf_document = foo_printout.index_html(request)
Fabien Morin's avatar
Fabien Morin committed
173 174 175 176 177 178 179 180 181 182 183
    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)

    # 3. False case: change the field name
    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)
184
    odf_document = foo_printout.index_html(request)
Fabien Morin's avatar
Fabien Morin committed
185 186 187 188 189 190 191 192 193 194 195 196
    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_getODGStyleSheet')
    tmp_template = foo_printout.template
    foo_printout.template = None
197
    self.assertRaises(ValueError, foo_printout.index_html, request)
Fabien Morin's avatar
Fabien Morin committed
198 199 200 201 202 203 204

    # put back
    foo_printout.template = tmp_template

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

    # 5. Normal case: utf-8 string
    test1.setTitle("Français")
216
    odf_document = foo_printout(request)
Fabien Morin's avatar
Fabien Morin committed
217 218 219 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)

    # 6. Normal case: unicode string
    test1.setTitle(u'Français test2')
225
    odf_document = foo_printout(request)
Fabien Morin's avatar
Fabien Morin committed
226 227 228 229 230 231
    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)

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  def test_02_TextFieldWithMultiLines(self):
    """
    mapping a field containing many lines ('\n') to a textbox
    """
    portal = self.getPortal()

    # add a description field in the form
    foo_form = self.portal.foo_module.test1.Foo_view
    if foo_form._getOb("my_description", None) is None:
      foo_form.manage_addField('my_description', 'Description', 'TextAreaField')
    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.setDescription('A text a bit more longer\n\nWith a newline !')
    transaction.commit()
    self.tic()

    style_dict = {'{urn:oasis:names:tc:opendocument:xmlns:text:1.0}line-break': {},
                  '{urn:oasis:names:tc:opendocument:xmlns:text:1.0}p': {},
                  '{urn:oasis:names:tc:opendocument:xmlns:text:1.0}span':
                    {'{urn:oasis:names:tc:opendocument:xmlns:text:1.0}style-name': 'T4'}
                 }

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

    # extract content.xml from original odg document
    original_doc_builder = OOoBuilder(original_file_content)
    original_content_xml = original_doc_builder.extract("content.xml")
    # get style of the title in the orignal test document
    original_document_style_dict = self.getStyleDictFromFieldName(original_content_xml,
        'my_description')

    # check the style is good before the odg generation
    self.assertEqual(original_document_style_dict, style_dict)

    request = self.app.REQUEST
    # 1. Normal case: "my_title" field to the "my_title" reference in the ODF document
273
    odf_document = foo_printout.index_html(request)
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    self.assertTrue(odf_document is not None)
    # validate the generated document
    self._validate(odf_document)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    content = etree.XML(content_xml)
    final_document_style_dict = self.getStyleDictFromFieldName(content_xml,
        'my_description')

    # check the style is keept after the odg generation
    self.assertEqual(final_document_style_dict, style_dict)

    # check the two lines are prensent in the generated document
    self.assertTrue(content_xml.find('A text a bit more longer') > 0)
    self.assertTrue(content_xml.find('With a newline !') > 0)

    # check there is two line-break in the element my_description
    text_xpath = '//draw:frame[@draw:name="my_description"]//text:line-break'
    node_list = content.xpath(text_xpath, namespaces=content.nsmap)
    self.assertEqual(len(node_list), 2)

Fabien Morin's avatar
Fabien Morin committed
295 296 297 298 299 300 301
  def test_03_Image(self):
    """
    Mapping an ImageField to odg document.
    Check it's possible to use an odg document to map an image with a
    form.ImageField
    """
    # create a new person
302
    request = self.portal.REQUEST
Fabien Morin's avatar
Fabien Morin committed
303 304 305 306 307 308 309 310 311 312
    person_module = self.portal.getDefaultModule('Person')
    if person_module._getOb('person1', None) is None:
      person_module.newContent(id='person1', portal_type='Person')
    person1 =  person_module.person1

    # add an image to this person
    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')
313
    image = person1.newContent(portal_type='Embedded File')
Fabien Morin's avatar
Fabien Morin committed
314 315 316 317 318 319 320 321 322 323 324 325
    image.edit(file=file_data)

    foo_printout = image.Foo_viewAsODGPrintout
    foo_form = image.Foo_view
    # add an image_field to Foo_view if there is not
    if foo_form._getOb("image_view", None) is None:
      foo_form.manage_addField('image_view', 'logo', 'ImageField')
    image_view_field = foo_form.image_view
    # set the image on the field
    image_view_field.values['default'] = image.absolute_url_path()

    # 01 - Normal image mapping
326
    odf_document = foo_printout(request)
Fabien Morin's avatar
Fabien Morin committed
327 328 329 330
    self.assertTrue(odf_document is not None)
    self._validate(odf_document)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
331
    self.assertTrue(content_xml.find("Pictures/0.png") > 0)
Fabien Morin's avatar
Fabien Morin committed
332 333 334

    # check the image is in the odg file
    try:
335
      builder.extract("Pictures/0.png")
Fabien Morin's avatar
Fabien Morin committed
336
    except KeyError:
337
      self.fail('image "Pictures/0.png" not found in odg document')
Fabien Morin's avatar
Fabien Morin committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

    content = etree.XML(content_xml)
    image_frame_xpath = '//draw:frame[@draw:name="image_view"]'
    image_frame_list = content.xpath(image_frame_xpath, namespaces=content.nsmap)
    self.assertTrue(len(image_frame_list) > 0)
    image_frame = image_frame_list[0]
    # Check the image size.
    # as the test image (form_printout_icon.png) is a square, proportions
    # should be keept, so heigh and width should be same and equal to the
    # height of the original image in the original odf test document.
    self.assertEqual(image_frame.attrib['{%s}height' % content.nsmap['svg']],
                     '1.206cm')
    self.assertEqual(image_frame.attrib['{%s}width' % content.nsmap['svg']],
                     '1.206cm')

    # 02: No image defined
    image_view_field.values['default'] = ''
355
    odf_document = foo_printout(request)
Fabien Morin's avatar
Fabien Morin committed
356 357 358 359
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    # confirming the image was removed
360
    self.assertFalse(content_xml.find("Pictures/0.png") > 0)
Fabien Morin's avatar
Fabien Morin committed
361 362
    self._validate(odf_document)

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  def test_04_ProxyField(self):
    """
    Check it's possible to use an odg document to map proxyfields
    """
    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()

    style_dict = {'{urn:oasis:names:tc:opendocument:xmlns:text:1.0}span':
                    {'{urn:oasis:names:tc:opendocument:xmlns:text:1.0}style-name': 'T2'},
                  '{urn:oasis:names:tc:opendocument:xmlns:text:1.0}p': {}
                 }

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

    # extract content.xml from original odg document
    original_doc_builder = OOoBuilder(original_file_content)
    original_content_xml = original_doc_builder.extract("content.xml")
    # get style of the title in the orignal test document
    original_document_style_dict = self.getStyleDictFromFieldName(original_content_xml,
        'my_title')

    # check the style is good before the odg generation
    self.assertEqual(original_document_style_dict, style_dict)

    request = self.app.REQUEST
    # 1. Normal case: "my_title" field to the "my_title" reference in the ODF document
398
    odf_document = foo_printout.index_html(request)
399 400 401 402 403 404 405 406 407 408 409
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    final_document_style_dict = self.getStyleDictFromFieldName(content_xml,
        'my_title')

    # check the style is keept after the odg generation
    self.assertEqual(final_document_style_dict, style_dict)

    self.assertTrue(content_xml.find("Foo title!") > 0)
    self.assertEqual(request.RESPONSE.getHeader('content-type'),
410
                     'application/vnd.oasis.opendocument.graphics')
411 412 413 414 415 416 417
    self.assertEqual(request.RESPONSE.getHeader('content-disposition'),
                     'inline;filename="Foo_viewProxyFieldAsODGPrintout.odg"')
    self._validate(odf_document)

    # 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!")
418
    odf_document = foo_printout.index_html(request)
419 420 421 422 423 424 425 426 427 428 429
    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)

    # 3. False case: change the field name
    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_viewProxyField
    foo_form.manage_renameObject('my_title', 'xxx_title', REQUEST=request)
430
    odf_document = foo_printout.index_html(request)
431 432 433 434 435 436 437 438 439 440 441 442
    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_getODGStyleSheet')
    tmp_template = foo_printout.template
    foo_printout.template = None
443
    self.assertRaises(ValueError, foo_printout.index_html, request)
444 445 446 447 448 449
    # put back
    foo_printout.template = tmp_template

    # 5. Normal case: just call a FormPrintout object
    request.RESPONSE.setHeader('Content-Type', 'text/html')
    test1.setTitle("call!")
450
    odf_document = foo_printout(request) # call
451 452 453 454
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("call!") > 0)
455 456
    self.assertEqual(request.RESPONSE.getHeader('content-type'),
                     'application/vnd.oasis.opendocument.graphics')
457 458 459 460
    self._validate(odf_document)

    # 5. Normal case: utf-8 string
    test1.setTitle("Français")
461
    odf_document = foo_printout(request)
462 463 464 465 466 467 468 469
    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)

    # 6. Normal case: unicode string
    test1.setTitle(u'Français test2')
470
    odf_document = foo_printout(request)
471 472 473 474 475 476
    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)

Fabien Morin's avatar
Fabien Morin committed
477 478 479 480
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestFormPrintoutAsODG))
  return suite