testFormPrintoutAsODG.py 20.2 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
# 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
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
32
from Products.ERP5OOo.tests.TestFormPrintoutMixin import TestFormPrintoutMixin
33
from Products.ERP5Type.tests.backportUnittest import expectedFailure
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):
42 43 44 45 46 47 48 49 50
  """
    XXX-Tatuya:
    Currently following _validate() methods are failing because LibreOffice 3.4
    itself outputs 'xmlns:graphics' element which is incosistent with the scheme.
    Thus I comment out these validation for now so that we run the other parts
    of the tests. This could be better than to mark them expectedFailure,
    in the aspect of these tests's importance of existence.
  """

Fabien Morin's avatar
Fabien Morin committed
51 52 53 54 55 56 57 58 59 60
  run_all_test = 1

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

  def afterSetUp(self):
    self.login()
61
    self.setSystemPreference()
Fabien Morin's avatar
Fabien Morin committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    # 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']
79

Fabien Morin's avatar
Fabien Morin committed
80 81 82
    if custom._getOb('Foo_viewAsODGPrintout', None) is None:
      erp5OOo.addFormPrintout(id='Foo_viewAsODGPrintout', title='',
                              form_name='Foo_view', template='Foo_getODGStyleSheet')
83 84 85
    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
86 87 88 89 90 91 92 93 94 95
    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:
96
      test1.newContent("foo_1", title='Foo Line 1', portal_type='Foo Line')
Fabien Morin's avatar
Fabien Morin committed
97
    if test1._getOb("foo_2", None) is None:
98
      test1.newContent("foo_2", title='Foo Line 2', portal_type='Foo Line')
Fabien Morin's avatar
Fabien Morin committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    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

115
  # see comment at top
Fabien Morin's avatar
Fabien Morin committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129
  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!')
    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'},
130
                  '{urn:oasis:names:tc:opendocument:xmlns:text:1.0}p': {}
Fabien Morin's avatar
Fabien Morin committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
                 }

    # 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
150
    odf_document = foo_printout.index_html(request)
Fabien Morin's avatar
Fabien Morin committed
151 152 153 154 155 156 157 158 159 160 161
    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'),
162
                     'application/vnd.oasis.opendocument.graphics')
Fabien Morin's avatar
Fabien Morin committed
163 164
    self.assertEqual(request.RESPONSE.getHeader('content-disposition'),
                     'inline;filename="Foo_viewAsODGPrintout.odg"')
165
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
166 167 168 169

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

    # 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)
182
    odf_document = foo_printout.index_html(request)
Fabien Morin's avatar
Fabien Morin committed
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)
187
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
188 189 190 191 192 193 194
    # 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
195
    self.assertRaises(ValueError, foo_printout.index_html, request)
Fabien Morin's avatar
Fabien Morin committed
196 197 198 199 200 201 202

    # 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!")
203
    odf_document = foo_printout(request, batch_mode=True) # call
Fabien Morin's avatar
Fabien Morin committed
204 205 206 207 208 209
    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')
210
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
211 212 213

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

    # 6. Normal case: unicode string
    test1.setTitle(u'Français test2')
223
    odf_document = foo_printout(request)
Fabien Morin's avatar
Fabien Morin committed
224 225 226 227
    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)
228
    # leave _validate() here not to forget the validation failure
Fabien Morin's avatar
Fabien Morin committed
229 230
    self._validate(odf_document)

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

    # check the image is in the odg file
    try:
333
      image_data = builder.extract("Pictures/0.png")
Fabien Morin's avatar
Fabien Morin committed
334
    except KeyError:
335
      self.fail('image "Pictures/0.png" not found in odg document')
336 337
    self.assertEquals(image.getData(), image_data,
                      '%s != %s' % (len(image.getData()), len(image_data)))
Fabien Morin's avatar
Fabien Morin committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    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'] = ''
354
    odf_document = foo_printout(request)
Fabien Morin's avatar
Fabien Morin committed
355 356 357 358
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    # confirming the image was removed
359
    self.assertFalse(content_xml.find("Pictures/0.png") > 0)
360
    self._validate(odf_document)
Fabien Morin's avatar
Fabien Morin committed
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 388 389 390 391 392 393 394 395
  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!')
    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
396
    odf_document = foo_printout.index_html(request)
397 398 399 400 401 402 403 404 405 406 407
    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'),
408
                     'application/vnd.oasis.opendocument.graphics')
409 410
    self.assertEqual(request.RESPONSE.getHeader('content-disposition'),
                     'inline;filename="Foo_viewProxyFieldAsODGPrintout.odg"')
411
    self._validate(odf_document)
412 413 414 415

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

    # 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)
428
    odf_document = foo_printout.index_html(request)
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)
433
    self._validate(odf_document)
434 435 436 437 438 439 440
    # 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
441
    self.assertRaises(ValueError, foo_printout.index_html, request)
442 443 444 445 446 447
    # 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!")
448
    odf_document = foo_printout(request) # call
449 450 451 452
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("call!") > 0)
453 454
    self.assertEqual(request.RESPONSE.getHeader('content-type'),
                     'application/vnd.oasis.opendocument.graphics')
455
    self._validate(odf_document)
456 457 458

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

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

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