testFormPrintoutAsODG.py 19.6 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
    # 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']
70

Fabien Morin's avatar
Fabien Morin committed
71 72 73
    if custom._getOb('Foo_viewAsODGPrintout', None) is None:
      erp5OOo.addFormPrintout(id='Foo_viewAsODGPrintout', title='',
                              form_name='Foo_view', template='Foo_getODGStyleSheet')
74 75 76
    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
77 78 79 80 81 82 83 84 85 86
    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:
87
      test1.newContent("foo_1", title='Foo Line 1', portal_type='Foo Line')
Fabien Morin's avatar
Fabien Morin committed
88
    if test1._getOb("foo_2", None) is None:
89
      test1.newContent("foo_2", title='Foo Line 2', portal_type='Foo Line')
Fabien Morin's avatar
Fabien Morin committed
90 91 92 93 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
    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'},
122
                  '{urn:oasis:names:tc:opendocument:xmlns:text:1.0}p': {}
Fabien Morin's avatar
Fabien Morin committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
                 }

    # 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
142
    odf_document = foo_printout.index_html(request)
Fabien Morin's avatar
Fabien Morin committed
143 144 145 146 147 148 149 150 151 152 153
    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'),
154
                     'application/vnd.oasis.opendocument.graphics')
Fabien Morin's avatar
Fabien Morin committed
155 156 157 158 159 160 161
    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!")
162
    odf_document = foo_printout.index_html(request)
Fabien Morin's avatar
Fabien Morin committed
163 164 165 166 167 168 169 170 171 172 173
    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)
174
    odf_document = foo_printout.index_html(request)
Fabien Morin's avatar
Fabien Morin committed
175 176 177 178 179 180 181 182 183 184 185 186
    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
187
    self.assertRaises(ValueError, foo_printout.index_html, request)
Fabien Morin's avatar
Fabien Morin committed
188 189 190 191 192 193 194

    # 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!")
195
    odf_document = foo_printout(request, batch_mode=True) # call
Fabien Morin's avatar
Fabien Morin committed
196 197 198 199 200 201 202 203 204 205
    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")
206
    odf_document = foo_printout(request)
Fabien Morin's avatar
Fabien Morin committed
207 208 209 210 211 212 213 214
    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')
215
    odf_document = foo_printout(request)
Fabien Morin's avatar
Fabien Morin committed
216 217 218 219 220 221
    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)

222 223 224 225 226 227 228 229 230 231 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
  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
263
    odf_document = foo_printout.index_html(request)
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    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
285 286 287 288 289 290 291
  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
292
    request = self.portal.REQUEST
Fabien Morin's avatar
Fabien Morin committed
293 294 295 296 297 298 299 300 301 302
    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')
303
    image = person1.newContent(portal_type='Embedded File')
Fabien Morin's avatar
Fabien Morin committed
304 305 306 307 308 309 310 311 312 313 314 315
    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
316
    odf_document = foo_printout(request)
Fabien Morin's avatar
Fabien Morin committed
317 318 319 320
    self.assertTrue(odf_document is not None)
    self._validate(odf_document)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
321
    self.assertTrue(content_xml.find("Pictures/0.png") > 0)
Fabien Morin's avatar
Fabien Morin committed
322 323 324

    # check the image is in the odg file
    try:
325
      builder.extract("Pictures/0.png")
Fabien Morin's avatar
Fabien Morin committed
326
    except KeyError:
327
      self.fail('image "Pictures/0.png" not found in odg document')
Fabien Morin's avatar
Fabien Morin committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

    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'] = ''
345
    odf_document = foo_printout(request)
Fabien Morin's avatar
Fabien Morin committed
346 347 348 349
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    # confirming the image was removed
350
    self.assertFalse(content_xml.find("Pictures/0.png") > 0)
Fabien Morin's avatar
Fabien Morin committed
351 352
    self._validate(odf_document)

353 354 355 356 357 358 359 360 361 362 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
  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
388
    odf_document = foo_printout.index_html(request)
389 390 391 392 393 394 395 396 397 398 399
    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'),
400
                     'application/vnd.oasis.opendocument.graphics')
401 402 403 404 405 406 407
    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!")
408
    odf_document = foo_printout.index_html(request)
409 410 411 412 413 414 415 416 417 418 419
    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)
420
    odf_document = foo_printout.index_html(request)
421 422 423 424 425 426 427 428 429 430 431 432
    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
433
    self.assertRaises(ValueError, foo_printout.index_html, request)
434 435 436 437 438 439
    # 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!")
440
    odf_document = foo_printout(request) # call
441 442 443 444
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("call!") > 0)
445 446
    self.assertEqual(request.RESPONSE.getHeader('content-type'),
                     'application/vnd.oasis.opendocument.graphics')
447 448 449 450
    self._validate(odf_document)

    # 5. Normal case: utf-8 string
    test1.setTitle("Français")
451
    odf_document = foo_printout(request)
452 453 454 455 456 457 458 459
    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')
460
    odf_document = foo_printout(request)
461 462 463 464 465 466
    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
467 468 469 470
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestFormPrintoutAsODG))
  return suite