testScribusUtils.py 18.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
##############################################################################
#
# Copyright (c) 2007 Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability 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
# garantees 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
import os

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import FileUpload
from AccessControl.SecurityManagement import newSecurityManager
34
from DateTime import DateTime
35

36
class TestScribusUtils(ERP5TypeTestCase):
37 38 39
  '''Unit test for SribusUtils API'''

  run_all_test = True
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  username = 'rc'

  def getTitle(self):
    return "PDF Editor"

  def getBusinessTemplateList(self):
    return ('erp5_base', 'erp5_pdf_editor')

  def login(self):
    uf = self.getPortal().acl_users
    uf._doAddUser(self.username, '', ['Manager'], [])
    user = uf.getUserById(self.username).__of__(uf)
    newSecurityManager(None, user)

  def afterSetUp(self):
    self.login()

  def makeFileUpload(self, filename):
    return FileUpload(
            os.path.join(os.path.dirname(__file__),
60
            'data', filename))
61

62
  def test_01_SimpleModuleCreation(self):
63
    '''Just create a module using scribus file and pdf file with minimal
64
    option'''
65

66 67 68 69 70 71 72 73 74 75 76 77 78
    self.portal.ERP5Site_createModuleScribus(
                  module_portal_type="Dummy Module",
                  portal_skins_folder="erp5_test",
                  object_portal_type="Dummy",
                  object_title="Dummy",
                  module_id="dummy_module",
                  module_title="Dummy Module Title",
                  import_pdf_file=self.makeFileUpload('test_1.pdf'),
                  import_scribus_file=self.makeFileUpload('test_1.sla'),)

    self.assertNotEqual(self.portal._getOb('dummy_module', None), None)
    self.assertNotEqual(
        self.portal.portal_skins._getOb("erp5_test", None), None)
79
    self.assertEqual("Dummy Module Title",
80 81 82 83 84 85
                      self.portal.dummy_module.getTitle())
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Dummy Module"),
                        None)
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Dummy"), None)


86
  def test_02_ModuleCreationWithBackground(self):
87 88 89
    '''Create a module with the option_html. That mean, a background will be
    generated (using convert), and a css file created'''

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    self.portal.ERP5Site_createModuleScribus(
              option_html=1,
              desired_width=800,
              desired_height=1132,
              module_portal_type="Dummy Module",
              portal_skins_folder="erp5_test",
              object_portal_type="Dummy",
              object_title="Dummy",
              module_id="dummy_module",
              module_title="Dummy Module Title",
              import_pdf_file=self.makeFileUpload('test_background.pdf'),
              import_scribus_file=self.makeFileUpload('test_background.sla'),)

    self.assertNotEqual(self.portal._getOb('dummy_module', None), None)
    self.assertNotEqual(
        self.portal.portal_skins._getOb("erp5_test", None), None)
106
    self.assertEqual("Dummy Module Title",
107 108 109 110 111 112 113 114 115 116 117
                      self.portal.dummy_module.getTitle())
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Dummy Module"),
                        None)
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Dummy"), None)

    # test the background existense
    skin_folder = self.portal.portal_skins._getOb("erp5_test", None)
    background_object = getattr(skin_folder,'Dummy_background_0', None)
    self.assertNotEquals(background_object, None)


118
  def test_03_ModuleListBox(self):
119 120
    '''Check the module listBox could be rendered without errors'''

121 122 123 124 125 126 127 128 129 130 131 132 133
    self.portal.ERP5Site_createModuleScribus(
                  module_portal_type="Dummy Module",
                  portal_skins_folder="erp5_test",
                  object_portal_type="Dummy",
                  object_title="Dummy",
                  module_id="dummy_module",
                  module_title="Dummy Module Title",
                  import_pdf_file=self.makeFileUpload('test_1.pdf'),
                  import_scribus_file=self.makeFileUpload('test_1.sla'),)
    # a form is created for the module
    form = self.portal.portal_skins.erp5_test._getOb(
                                      'DummyModule_viewDummyList', None)
    self.assertNotEquals(form, None)
134
    self.assertEqual(form.pt, 'form_list')
135 136
    self.assertTrue(hasattr(form, 'listbox'))
    # listbox is in bottom group
137
    self.assertTrue('listbox' in [field.getId() for field in
138 139 140 141 142 143 144 145
                                  form.get_fields_in_group('bottom')])
    # the listbox managment screen can be accessed without error
    form.listbox.manage_main()
    # After we call changeSkin() so that portal_skins realize we have a new
    # skin folder, the listbox can be used to render the module without error
    self.portal.changeSkin(None)
    self.portal.dummy_module.DummyModule_viewDummyList()

146
  def test_04_SimpleModuleUpdate(self):
147
    ''' Update a module created with a scribus file and pdf file.
148
        Change a field name in the new scribus file, and check that after
149 150
        update, the ERP5 StringField have the new name.'''

151 152 153 154 155 156 157 158 159 160 161 162 163 164
    # first module creation:
    self.portal.ERP5Site_createModuleScribus(
                  module_id="dummy_module",
                  module_portal_type="Dummy Module",
                  module_title="Dummy Module Title",
                  import_pdf_file=self.makeFileUpload('test_1.pdf'),
                  import_scribus_file=self.makeFileUpload('test_1.sla'),
                  portal_skins_folder="erp5_test",
                  object_title="Dummy",
                  object_portal_type="Dummy")

    self.assertNotEqual(self.portal._getOb('dummy_module', None), None)
    self.assertNotEqual(
        self.portal.portal_skins._getOb("erp5_test", None), None)
165
    self.assertEqual("Dummy Module Title",
166 167 168 169 170
                      self.portal.dummy_module.getTitle())
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Dummy Module"),
                        None)
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Dummy"), None)

171
    # check that a field with title text_1 (present in the sla file)
172
    # has been created in the form
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    self.assertNotEquals(getattr(self.portal.portal_skins.erp5_test.Dummy_view,
      'text_1', None), None)


    # Update the ERP5Form, scribus, PDFForm, css and background picture
    self.portal.ERP5Site_updateModuleScribus(
                  import_pdf_file=self.makeFileUpload('test_1.pdf'),
                  import_scribus_file=self.makeFileUpload('test_2.sla'),
                  object_portal_type="Dummy")

    # check that the modified field with title text_couscous (present in the
    # new sla file) has been created in the form
    self.assertNotEquals(getattr(self.portal.portal_skins.erp5_test.Dummy_view,
      'text_couscous', None), None)
    # the old field text_1 must have been removed
188
    self.assertEqual(getattr(self.portal.portal_skins.erp5_test.Dummy_view,
189 190
      'text_1', None), None)

Fabien Morin's avatar
Fabien Morin committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  def test_05_requiredFields(self):
    ''' Set required property on a scribus field. After creating module with
    scribus, the ERP5 field should have the required property set on.'''

    # module creation using scribus file containing fields with requied
    # property set:
    self.portal.ERP5Site_createModuleScribus(
          module_id="dummy_module",
          module_portal_type="Dummy Module",
          module_title="Dummy Module Title",
          import_pdf_file=self.makeFileUpload('test_field_properties.pdf'),
          import_scribus_file=self.makeFileUpload('test_field_properties.sla'),
          portal_skins_folder="erp5_test",
          object_title="Dummy",
          object_portal_type="Dummy")

    self.assertNotEqual(self.portal._getOb('dummy_module', None), None)
    self.assertNotEqual(
        self.portal.portal_skins._getOb("erp5_test", None), None)
210
    self.assertEqual("Dummy Module Title",
Fabien Morin's avatar
Fabien Morin committed
211 212 213 214 215 216 217 218
                      self.portal.dummy_module.getTitle())
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Dummy Module"),
                        None)
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Dummy"), None)

    # define some field lists
    required_field_name_list = ['my_required_string_field',
                                'my_required_date']
219
    not_required_field_name_list = [ 'my_simple_string_field',
Fabien Morin's avatar
Fabien Morin committed
220 221 222 223 224 225 226 227 228 229 230 231 232
                                     'my_simple_date']
    all_field_name_list = required_field_name_list + \
                                        not_required_field_name_list

    # check the that fields prensent in the sla file have been created in the
    # erp5 form
    form = self.portal.portal_skins.erp5_test.Dummy_view
    for field_name in all_field_name_list:
      self.assertNotEquals(getattr(form, field_name, None), None)

    # check the required fields are required :
    for field_name in required_field_name_list:
      field = getattr(form, field_name, None)
233
      self.assertEqual(field.values['required'], 1)
Fabien Morin's avatar
Fabien Morin committed
234 235 236 237 238 239 240

    # check fields not required are not :
    for field_name in not_required_field_name_list:
      field = getattr(form, field_name, None)
      self.assertNotEquals(field.values['required'], 1)

  def test_06_readOnlyFields(self):
Fabien Morin's avatar
typo  
Fabien Morin committed
241
    '''check the read_only fields are really read_only'''
Fabien Morin's avatar
Fabien Morin committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    # module creation using scribus file containing fields with read only
    # property set:
    self.portal.ERP5Site_createModuleScribus(
          module_id="dummy_module",
          module_portal_type="Dummy Module",
          module_title="Dummy Module Title",
          import_pdf_file=self.makeFileUpload('test_field_properties.pdf'),
          import_scribus_file=self.makeFileUpload('test_field_properties.sla'),
          portal_skins_folder="erp5_test",
          object_title="Dummy",
          object_portal_type="Dummy")

    self.assertNotEqual(self.portal._getOb('dummy_module', None), None)
    self.assertNotEqual(
        self.portal.portal_skins._getOb("erp5_test", None), None)
257
    self.assertEqual("Dummy Module Title",
Fabien Morin's avatar
Fabien Morin committed
258 259 260 261 262 263 264 265
                      self.portal.dummy_module.getTitle())
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Dummy Module"),
                        None)
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Dummy"), None)

    # define some field lists
    read_only_field_name_list = ['my_read_only_string_field',
                                'my_read_only_date']
266
    not_read_only_field_name_list = [ 'my_simple_string_field',
Fabien Morin's avatar
Fabien Morin committed
267 268 269 270 271 272 273 274 275 276 277 278 279
                                     'my_simple_date']
    all_field_name_list = read_only_field_name_list + \
                                        not_read_only_field_name_list

    # check the that fields prensent in the sla file have been created in the
    # erp5 form
    form = self.portal.portal_skins.erp5_test.Dummy_view
    for field_name in all_field_name_list:
      self.assertNotEquals(getattr(form, field_name, None), None)

    # check the read_only fields are read_only :
    for field_name in read_only_field_name_list:
      field = getattr(form, field_name, None)
280
      self.assertEqual(field.values['editable'], 0)
Fabien Morin's avatar
Fabien Morin committed
281 282 283 284

    # check fields not read_only are not :
    for field_name in not_read_only_field_name_list:
      field = getattr(form, field_name, None)
285
      self.assertEqual(field.values['editable'], 1)
Fabien Morin's avatar
Fabien Morin committed
286

287
  def test_07_DateTimeFieldWithModuleCreation(self):
Fabien Morin's avatar
typo  
Fabien Morin committed
288
    '''test DateTime format'''
289 290 291 292 293 294 295 296 297 298 299

    self.portal.ERP5Site_createModuleScribus(
                  option_html=1,
                  desired_width=800,
                  desired_height=600,
                  module_portal_type="Authorisation Module",
                  portal_skins_folder="erp5_authorisation",
                  object_portal_type="Authorisation",
                  object_title="Authorisation",
                  module_id="authorisation_module",
                  module_title="Authorisation Module Title",
300 301 302 303
                import_pdf_file=self.makeFileUpload('Authorisation.pdf'),
                import_scribus_file=self.makeFileUpload('Authorisation.sla'),)
    portal = self.getPortal()
    portal_types = self.portal.portal_types
304 305 306
    self.assertNotEqual(self.portal._getOb('authorisation_module', None), None)
    self.assertNotEqual(
        self.portal.portal_skins._getOb("erp5_authorisation", None), None)
307
    self.assertEqual("Authorisation Module Title",
308
                      self.portal.authorisation_module.getTitle())
309 310 311
    module_portal_type = portal_types.getTypeInfo("Authorisation Module")
    self.assertNotEqual(module_portal_type, None)
    self.assertNotEqual(portal_types.getTypeInfo("Authorisation"), None)
312
    # Create an Authorisation
313
    # add property sheet Task in portal type Authorisation
Julien Muchembled's avatar
Julien Muchembled committed
314
    self.portal.portal_types.Authorisation._setTypePropertySheet('Task')
315
    authorisation_module = self.portal.authorisation_module
316 317
    start_date = DateTime('2000/01/01')
    stop_date = DateTime('2001/01/01 12:00 GMT')
318 319 320 321 322 323 324
    authorisation = authorisation_module.newContent(\
          portal_type='Authorisation',
          title = 'Mum Dad',
          start_date = start_date,
          stop_date = stop_date
        )

325
    form = self.portal.portal_skins.erp5_authorisation.Authorisation_view
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    # test property input_order on all DateTimeField
    input_order_other_date = form.my_other_date.get_value('input_order')
    input_order_start_date = form.my_start_date.get_value('input_order')
    input_order_stop_date = form.my_stop_date.get_value('input_order')
    self.assertEqual(input_order_other_date,'ymd')
    self.assertEqual(input_order_start_date,'dmy')
    self.assertEqual(input_order_stop_date,'ymd')
    # test result of expression TALES with DateTimeField
    form = self.portal.portal_skins.erp5_authorisation
    pdf = form.Authorisation_viewAuthorisationAsPdf
    cell_name_other_date = pdf.getCellNames()[0]
    cell_name_start_date = pdf.getCellNames()[1]
    cell_name_stop_date = pdf.getCellNames()[2]
    tales_expr_other_date = pdf.getCellTALES(cell_name_other_date)
    tales_expr_start_date = pdf.getCellTALES(cell_name_start_date)
    tales_expr_stop_date = pdf.getCellTALES(cell_name_stop_date)
    from Products.CMFCore.Expression import Expression
    from Products.CMFCore.Expression import getExprContext
    expr_other_date = Expression(tales_expr_other_date)
    expr_start_date = Expression(tales_expr_start_date)
    expr_stop_date = Expression(tales_expr_stop_date)
347
    result_other_date = expr_other_date(getExprContext(authorisation,
Fabien Morin's avatar
typo  
Fabien Morin committed
348
                                                       authorisation))
349
    result_start_date = expr_start_date(getExprContext(authorisation,
Fabien Morin's avatar
typo  
Fabien Morin committed
350
                                                       authorisation))
351
    result_stop_date = expr_stop_date(getExprContext(authorisation,
Fabien Morin's avatar
typo  
Fabien Morin committed
352
                                                     authorisation))
353 354 355
    self.assertEqual(result_other_date, '')
    self.assertEqual(result_start_date, start_date.strftime('%d/%m/%Y'))
    self.assertEqual(result_stop_date, stop_date.strftime('%Y/%m/%d %H:%M'))
356

357 358 359 360 361 362 363 364 365 366 367 368 369 370
  def test_08_ModuleCreationWithoutField(self):
    '''check it's possible to create a module using an empty scribus file without errors'''
    self.portal.ERP5Site_createModuleScribus(
            module_portal_type="Empty Module",
            portal_skins_folder="erp5_empty",
            object_portal_type="Empty",
            object_title="Empty",
            module_id="empty_module",
            module_title="Empty Module Title",
            import_pdf_file=self.makeFileUpload('test_without_field.pdf'),
            import_scribus_file=self.makeFileUpload('test_without_field.sla'),)
    self.assertNotEqual(self.portal._getOb('empty_module', None), None)
    self.assertNotEqual(
        self.portal.portal_skins._getOb("erp5_empty", None), None)
371
    self.assertEqual("Empty Module Title",
372 373 374 375 376
                      self.portal.empty_module.getTitle())
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Empty Module"),
                        None)
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Empty"), None)
    form = self.portal.portal_skins.erp5_empty.Empty_view
377
    self.assertEqual(0, len(form.objectValues()))
378

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  def test_09_creationRadioField(self):
    '''check it's possible to put a field radioField in ERP5 Form created with
    scribus.
       Create a RadioField if possible only when you use ERP5 Form rendering
       and not graphic rendering
       RadioField with 3 items : young, adult and senior
    '''
    self.portal.ERP5Site_createModuleScribus(
            module_portal_type="Radio Module",
            portal_skins_folder="erp5_radio",
            object_portal_type="Radio",
            object_title="Radio",
            module_id="radio_module",
            module_title="Radio Module Title",
            import_pdf_file=self.makeFileUpload('test_RadioField.pdf'),
            import_scribus_file=self.makeFileUpload('test_RadioField.sla'),)
    self.assertNotEqual(self.portal._getOb('radio_module', None), None)
    self.assertNotEqual(
        self.portal.portal_skins._getOb("erp5_radio", None), None)
398
    self.assertEqual("Radio Module Title",
399 400 401 402 403 404
                      self.portal.radio_module.getTitle())
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Radio Module"),
                        None)
    self.assertNotEqual(self.portal.portal_types.getTypeInfo("Radio"), None)
    form = self.portal.portal_skins.erp5_radio.Radio_view
    field_radio = form.my_radio
405
    self.assertEqual(3,
406 407
                      len(field_radio.get_value('items')))
    items_list = [('young', 'Young'), ('adult', 'Adult'), ('senior', 'Senior')]
408
    self.assertEqual(items_list, field_radio.get_value('items'))
409

410 411
def test_suite():
  suite = unittest.TestSuite()
412
  suite.addTest(unittest.makeSuite(TestScribusUtils))
413
  return suite