testFormPrintout.py 27.4 KB
Newer Older
1
##############################################################################
Tatuya Kamada's avatar
Tatuya Kamada committed
2
# -*- coding: utf-8 -*-
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
# Copyright (c) 2009 Nexedi KK and Contributors. All Rights Reserved.
#                    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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

import unittest
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
31
from Products.ERP5Type.tests.utils import createZODBPythonScript
32 33 34
from AccessControl.SecurityManagement import newSecurityManager
from Products.ERP5OOo.OOoUtils import OOoBuilder
from zLOG import LOG , INFO
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
35
from lxml import etree
36 37 38
import os

class TestFormPrintout(ERP5TypeTestCase):
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
39
  run_all_test = 0
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

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

  def getBusinessTemplateList(self):
    return ('erp5_ui_test', 'erp5_odt_style')

  def afterSetUp(self):
    self.login()
    foo_file_path = os.path.join(os.path.dirname(__file__),
                                'test_document',
                                'Foo_001.odt')
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
55 56 57
    foo2_file_path = os.path.join(os.path.dirname(__file__),
                                  'test_document',
                                  'Foo_002.odt')   
58
    foo_file = open(foo_file_path, 'rb')
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
59
    foo2_file = open(foo2_file_path, 'rb')
60 61
    custom = self.portal.portal_skins.custom
    addStyleSheet = custom.manage_addProduct['OFSP'].manage_addFile
62 63 64
    if custom._getOb('Foo_getODTStyleSheet', None) is None:
      addStyleSheet(id='Foo_getODTStyleSheet', file=foo_file, title='',
                    precondition='', content_type = 'application/vnd.oasis.opendocument.text')
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
65 66 67
    if custom._getOb('Foo2_getODTStyleSheet', None) is None:
      addStyleSheet(id='Foo2_getODTStyleSheet', file=foo2_file, title='',
                    precondition='', content_type = 'application/vnd.oasis.opendocument.text')   
68 69
    erp5OOo = custom.manage_addProduct['ERP5OOo']
    addOOoTemplate = erp5OOo.addOOoTemplate
70 71
    if custom._getOb('Foo_viewAsOdt', None) is None:
      addOOoTemplate(id='Foo_viewAsOdt', title='')
72 73 74 75 76 77 78 79 80
    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')
81 82 83
    if custom._getOb('Foo_viewAsPrintout', None) is None:
      erp5OOo.addFormPrintout(id='Foo_viewAsPrintout', title='',
                              form_name='Foo_view', template='Foo_getODTStyleSheet')
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
84 85 86
    if custom._getOb('FooReport_viewAsPrintout', None) is None:
      erp5OOo.addFormPrintout(id='FooReport_viewAsPrintout',
                              title='')
87

Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
88 89 90 91 92 93 94 95 96 97 98 99
    ## 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')
    get_transaction().commit()
    self.tic()
    
100 101 102 103 104 105
  def login(self):
    uf = self.getPortal().acl_users
    uf._doAddUser('tatuya', '', ['Manager'], [])
    user = uf.getUserById('tatuya').__of__(uf)
    newSecurityManager(None, user)
                          
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
106
  def test_01_Paragraph(self, run=run_all_test):
107 108 109
    """
    mapping a field to a paragraph
    """
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
110 111
    if not run: return
    
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    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!')
    get_transaction().commit()
    self.tic()

    # test target
    foo_printout = portal.foo_module.test1.Foo_viewAsPrintout

    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)
Tatuya Kamada's avatar
Tatuya Kamada committed
132 133 134 135 136
    self.assertEqual(request.RESPONSE.getHeader('content-type'),
                     'application/vnd.oasis.opendocument.text; charset=utf-8')
    self.assertEqual(request.RESPONSE.getHeader('content-disposition'),
                     'inline;filename="Foo_viewAsPrintout.odt"')
    
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    # 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)

    # 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)
    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)
    # 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')
161
    tmp_template = foo_printout.template 
162 163 164 165 166 167 168 169
    foo_printout.template = None
    # template == None, causes a ValueError 
    try: 
      foo_printout.index_html(REQUEST=request)
    except ValueError, e:
      # e -> 'Can not create a ODF Document without a odf_template'
      self.assertTrue(True)

170 171 172 173
    # put back
    foo_printout.template = tmp_template
    
    # 5. Normal case: just call a FormPrintout object
Tatuya Kamada's avatar
Tatuya Kamada committed
174
    request.RESPONSE.setHeader('Content-Type', 'text/html')
175 176 177 178 179 180
    test1.setTitle("call!")
    odf_document = foo_printout() # call
    self.assertTrue(odf_document is not None)
    builder = OOoBuilder(odf_document)
    content_xml = builder.extract("content.xml")
    self.assertTrue(content_xml.find("call!") > 0)
Tatuya Kamada's avatar
Tatuya Kamada committed
181 182
    # when just call FormPrintout, it does not change content-type
    self.assertEqual(request.RESPONSE.getHeader('content-type'), 'text/html')
Tatuya Kamada's avatar
Tatuya Kamada committed
183 184 185 186 187 188 189 190

    # 5. Normal case: utf-8 string
    test1.setTitle("Français")
    odf_document = foo_printout() 
    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)
Tatuya Kamada's avatar
Tatuya Kamada committed
191 192 193 194 195 196 197 198

    # 6. Normal case: unicode string
    test1.setTitle(u'Français test2')
    odf_document = foo_printout() 
    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)
199
    
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
200 201 202 203 204 205 206 207 208 209 210 211 212
  def test_02_Table_01_Normal(self, run=run_all_test):
    """To test listbox and ODF table mapping
    
     * Test Data Format
    
     ODF table named 'listbox':
     +------------------------------+
     |  ID | Title | Quantity |Date |
     |-----+-------+----------+-----|
     |     |       |          |     |
     |-----+-------+----------+-----|
     |   Total     |          |     |
     +------------------------------+
213 214
    """
    # test target
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
215 216
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
217 218 219 220
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
    request = self.app.REQUEST 
    request['here'] = test1
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
221 222 223 224 225 226 227 228 229 230
    
    # 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)
231 232
    listboxline_list = listbox.get_value('default', render_format = 'list',
                                         REQUEST = request)
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
233
    self.assertEqual(len(listboxline_list), 4)
234 235
    self.assertTrue(listboxline_list[0].isTitleLine())
    self.assertTrue(listboxline_list[1].isDataLine())
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
236 237
    self.assertTrue(listboxline_list[2].isDataLine())
    self.assertTrue(listboxline_list[3].isStatLine())
238 239 240 241 242 243 244 245 246 247 248 249 250
    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")
    
    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)

Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
251 252 253 254 255 256 257 258 259 260 261
  def test_02_Table_02_SmallerThanListboxColumns(self, run=run_all_test):
    """2. Irregular case: listbox columns count smaller than table columns count"""
    if not run: return 
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
    request = self.app.REQUEST 
    request['here'] = test1

262 263 264 265 266 267 268 269 270 271 272 273
    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)
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
274
    self.assertEqual(len(listboxline_list), 4)
275 276
    self.assertTrue(listboxline_list[0].isTitleLine())
    self.assertTrue(listboxline_list[1].isDataLine())
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
277 278
    self.assertTrue(listboxline_list[2].isDataLine())
    self.assertTrue(listboxline_list[3].isStatLine())
279 280 281 282 283 284 285 286 287 288 289 290 291 292
    self.assertTrue(listboxline_list[1].getColumnProperty('title') == "foo_title_2")
    
    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)

Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
293 294 295 296 297 298 299 300 301 302 303
  def test_02_Table_03_ListboxColumnsLargerThanTable(self, run=run_all_test):
    """3. Irregular case: listbox columns count larger than table columns count"""
    if not run: return 
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
    request = self.app.REQUEST 
    request['here'] = test1

304 305 306 307 308 309 310 311 312 313 314
    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)
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
315
    self.assertEqual(len(listboxline_list), 4)
316 317 318 319 320 321 322 323 324 325 326 327 328
    self.assertTrue(listboxline_list[1].getColumnProperty('title') == "foo_title_3")
    
    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)

Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
329 330 331 332 333 334 335 336 337 338
  def test_02_Table_04_ListboxHasNotStat(self, run=run_all_test):
    """4. Irregular case: listbox has not a stat line, but table has a stat line"""
    if not run: return 
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
    request = self.app.REQUEST 
    request['here'] = test1
339 340

    test1.foo_1.setTitle('foo_title_4')
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
341
    test1.foo_1.setStartDate('2009-01-01')
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    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)

Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
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 398 399
    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)
    # to test copying ODF table cell styles 
    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)

  def test_02_Table_05_NormalSameLayout(self, run=run_all_test):
    """5. Normal case: the listobx and the ODF table are same layout

    * Test Data Format:
    
     ODF table named 'listbox2'
     +-------------------------------+
     |  A    |   B   |   C   |   D   |
     |-------+-------+-------+-------|
     |       |       |       |       |
     +-------+-------+-------+-------+
    """
    if not run: return

    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
    request = self.app.REQUEST 
    request['here'] = test1

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    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")
    self.assertFalse(content_xml.find("foo_title_4") > 0)
    self.assertTrue(content_xml.find("foo_title_5") > 0)
    
    # put back the field name
    foo_form.manage_renameObject('listbox2', 'listbox', REQUEST=request)

Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    
  def test_02_Table_06_TableDoesNotHaveAHeader(self, run=run_all_test):
    """6. Normal case: ODF table does not have a header
     * Test Data format:
    
     ODF table named 'listbox3'
     the table listbox3 has not table header.
     first row is a table content, too.
     +-------------------------------+
     |  1    |   2   |   3   |   4   |
     |-------+-------+-------+-------|
     |       |       |       |       |
     +-------+-------+-------+-------+
    """
    if not run: return 
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
    request = self.app.REQUEST 
    request['here'] = test1

450 451 452
    foo_form.manage_renameObject('listbox', 'listbox3', REQUEST=request)
    listbox3 = foo_form.listbox3
    test1.foo_1.setTitle('foo_title_6')
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
453 454 455 456 457 458 459
    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)
460 461 462 463
    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")
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
464
    
465 466 467 468 469 470 471 472 473 474 475 476
    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)

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

Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
  def test_02_Table_07_CellFormat(self, run=run_all_test):
    """7. Normal case: cell format cetting"""
    if not run: return 
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
    request = self.app.REQUEST 
    request['here'] = test1

    test1.foo_1.setTitle('foo_title_7')
    test1.foo_1.setStartDate('2009-04-20')
    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")
      
    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')
Tatuya Kamada's avatar
Tatuya Kamada committed
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 553 554 555 556


  def test_02_Table_08_Nodata(self, run=run_all_test):
    """7. Normal case: list box has no data"""
    if not run: return 
    # test target
    test1 = self.portal.foo_module.test1
    foo_printout = test1.Foo_viewAsPrintout
    foo_form = test1.Foo_view
    listbox = foo_form.listbox
    request = self.app.REQUEST 
    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)
      
    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)

Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
557
  def _test_03_Frame(self, run=run_all_test):
558 559 560 561 562
    """
    Frame not supported yet
    """
    pass

Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
563
  def test_04_Iteration(self, run=run_all_test):
564
    """
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
565
    Iteration using ERP5Report ReportSection test
566
    """
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
567 568 569 570 571 572 573 574
    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
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
    foo2_view.manage_addField('listbox', 'listbox', 'ListBox')
    listbox = foo2_view.listbox

    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',
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
591 592 593 594
      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(
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
595 596 597
      self.portal.portal_skins.custom,
      'FooReport_getReportSectionList',
      '',
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
598 599 600 601 602
r"""
from Products.ERP5Form.Report import ReportSection

r1 = ReportSection(path=context.getPhysicalPath(),
                   form_id='Foo2_view',
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
603
                   selection_params={'title':'foo_04_Iteration_1'})
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
604 605
r2 = ReportSection(path=context.getPhysicalPath(),
                   form_id='Foo2_view',
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
606
                   selection_params={'title':'foo_04_Iteration_2'})
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
607 608 609 610 611 612 613 614 615
report_section_list = [r1, r2]
return report_section_list
"""
        )
    test1 = self.portal.foo_module.test1
    foo_report_printout = test1.FooReport_viewAsPrintout
    foo_report_printout.doSettings(REQUEST=None,
                                   title='',
                                   form_name='FooReport_view',
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
616
                                   template='Foo2_getODTStyleSheet')
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
617 618 619 620 621 622 623
    odf_document = foo_report_printout()

    #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")
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
624
    self.assertTrue(content_xml.find("foo_04_Iteration_1") > 0)
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
    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)

    # 02. no report section
    custom.manage_delObjects(['FooReport_getReportSectionList'])
    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'FooReport_getReportSectionList',
      '',
r"""
return []
"""
      )
    odf_document = foo_report_printout()
    #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)
     
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
656
  def _test_05_Styles(self, run=run_all_test):
657
    """
Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
658
    styles.xml not tested yet
659 660 661
    """
    pass

Tatuya Kamada's avatar
* Fix  
Tatuya Kamada committed
662
  def _test_06_Meta(self, run=run_all_test):
663 664 665 666
    """
    meta.xml not supported yet
    """
    pass
667 668 669 670 671

def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestFormPrintout))
  return suite