testXHTML.py 22.4 KB
Newer Older
1
# -*- coding: utf-8 -*-
2 3 4
##############################################################################
#
# Copyright (c) 2007 Nexedi SARL and Contributors. All Rights Reserved.
5 6
#               Fabien Morin <fabien@nexedi.com
#               Jacek Medrzycki <jacek@erp5.pl>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# 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.
#
##############################################################################

30 31
import unittest
import os
32 33
import popen2
import urllib
34

35
from Testing import ZopeTestCase
36
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
37
from Products.ERP5Type.tests.backportUnittest import expectedFailure
38
from Products.ERP5 import __file__ as ERP5PackagePath
39 40
from Products.CMFCore.utils import getToolByName
from AccessControl.SecurityManagement import newSecurityManager
41 42 43
from zLOG import LOG
from xml.dom import minidom

44
from glob import glob
45 46 47 48 49

#
# Test Setting
#
INSTANCE_HOME = os.environ['INSTANCE_HOME']
50 51
bt5_base_path = os.environ.get('erp5_tests_bt5_path',
                               os.path.join(INSTANCE_HOME, 'bt5'))
52 53
bootstrap_base_path = os.path.join(os.path.dirname(ERP5PackagePath),
                                   'bootstrap')
54

55

56
class TestXHTML(ERP5TypeTestCase):
57

58
  run_all_test = 1
59

60 61
  def getTitle(self):
    return "XHTML Test"
62

63 64 65 66 67 68
  @staticmethod
  def getBusinessTemplateList():
    """  """
    return ( # dependency order
      'erp5_base',
      'erp5_trade',
69

70 71 72 73 74
      'erp5_pdf_editor',
      'erp5_pdf_style',
      'erp5_pdm',
      'erp5_accounting',
      'erp5_invoicing',
75

76
      'erp5_apparel',
77

78 79 80 81
##    'erp5_banking_core',
##    'erp5_banking_cash',
##    'erp5_banking_check',
##    'erp5_banking_inventory',
82

83 84
      'erp5_budget',
      'erp5_public_accounting_budget',
85

86
      'erp5_consulting',
87

88 89 90
      'erp5_ingestion',
      'erp5_ingestion_mysql_innodb_catalog',
      'erp5_crm',
91

92 93
      'erp5_web',
      'erp5_dms',
94

95 96
      'erp5_commerce',

97
      'erp5_forge',
98

99
      'erp5_immobilisation',
100

101
      'erp5_item',
102

103
      'erp5_mrp',
104

105
      'erp5_payroll',
106

107
      'erp5_project',
108

109
      'erp5_calendar',
110 111 112 113 114 115 116

      'erp5_advanced_invoicing',

      'erp5_odt_style',
      'erp5_documentation',

      'erp5_administration',
117 118

      'erp5_knowledge_pad',
119
    )
120 121 122 123 124 125

  def afterSetUp(self):
    self.portal = self.getPortal()

    uf = self.getPortal().acl_users
    uf._doAddUser('seb', '', ['Manager'], [])
126 127 128

    self.login('seb')
    self.enableDefaultSitePreference()
129 130 131 132 133 134 135

  def enableDefaultSitePreference(self):
    portal_preferences = getToolByName(self.portal, 'portal_preferences')
    portal_workflow = getToolByName(self.portal, 'portal_workflow')
    default_site_preference = portal_preferences.default_site_preference
    portal_workflow.doActionFor(default_site_preference, 'enable_action')

136 137 138 139 140 141 142 143
  def changeSkin(self, skin_name):
    """
      Change current Skin
    """
    request = self.app.REQUEST
    self.getPortal().portal_skins.changeSkin(skin_name)
    request.set('portal_skin', skin_name)

144 145 146 147 148 149 150 151 152 153 154 155
  def getFieldList(self, form, form_path):
    try:
      for field in form.get_fields(include_disabled=1):
        if field.getTemplateField() is not None:
          try:
            if field.get_value('enabled'):
              yield field
          except Exception:
            yield field
    except AttributeError, e:
      ZopeTestCase._print("%s is broken: %s" % (form_path, e))

156 157 158 159
  def test_deadProxyFields(self):
    # check that all proxy fields defined in business templates have a valid
    # target
    skins_tool = self.portal.portal_skins
160
    error_list = []
161 162 163 164 165 166 167 168 169 170 171

    for skin_name, skin_folder_string in skins_tool.getSkinPaths():
      skin_folder_id_list = skin_folder_string.split(',')
      self.changeSkin(skin_name)

      for skin_folder_id in skin_folder_id_list:
        for field_path, field in skins_tool[skin_folder_id].ZopeFind(
                  skins_tool[skin_folder_id], 
                  obj_metatypes=['ProxyField'], search_sub=1):
          template_field = field.getTemplateField(cache=False)
          if template_field is None:
Jérome Perrin's avatar
Jérome Perrin committed
172
            # Base_viewRelatedObjectList (used for proxy listbox ids on
173 174
            # relation fields) is an exception, the proxy field has no target
            # by default.
Jérome Perrin's avatar
Jérome Perrin committed
175
            if field_path != 'Base_viewRelatedObjectList/listbox':
176 177
              error_list.append((skin_name, field_path, field.get_value('form_id'),
                                 field.get_value('field_id')))
178 179

    if error_list:
180 181
      message = '\nDead proxy field list%s\n' \
                    % '\n\t'.join(str(e) for e in error_list)
182
      self.fail(message)
183

184
  @expectedFailure
185 186 187 188 189 190 191 192 193
  def test_configurationOfFieldLibrary(self):
    error_list = []
    for business_template in self.portal.portal_templates.searchFolder():
      # XXX Impossible to filter by installation state, as it is not catalogued
      business_template = business_template.getObject()
      for modifiable_field in business_template.BusinessTemplate_getModifiableFieldList():
        error_list.append((modifiable_field.object_id,
                          modifiable_field.choice_item_list[0][0]))
    if error_list:
194 195 196 197
      message = '%s fields to modify' % len(error_list)
      #message += '\n\t' + '\n\t'.join(fieldname + ": " + message
      #                                 for fieldname, message in error_list)
      self.fail(message) # uncomment above for details on each field
198

199 200 201 202 203 204 205 206
  def test_portalTypesDomainTranslation(self):
    # according to bt5-Module.Creation.Guidelines document, module
    # portal_types should be translated using erp5_ui, and normal ones, using
    # erp5_content
    error_list = []
    portal_types_module = self.portal.portal_types
    for portal_type in portal_types_module.contentValues(portal_type=\
        'Base Type'):
207
      if portal_type.getId().endswith('Module'):
208 209 210 211 212
        for k, v in portal_type.getPropertyTranslationDomainDict().items():
          if v.getDomainName() != 'erp5_ui':
            error_list.append('"%s" should use erp5_ui' % \
                portal_type.getId())
    if error_list:
213 214
      message = '\nBad portal_type domain translation list%s\n' \
                    % '\n\t'.join(error_list)
215 216
      self.fail(message)

217 218 219 220 221 222
  def test_emptySelectionNameInListbox(self):
    # check all empty selection name in listboxes
    skins_tool = self.portal.portal_skins
    error_list = []
    for form_path, form in skins_tool.ZopeFind(
              skins_tool, obj_metatypes=['ERP5 Form'], search_sub=1):
223
      for field in self.getFieldList(form, form_path):
Fabien Morin's avatar
Fabien Morin committed
224
        if field.getRecursiveTemplateField().meta_type == 'ListBox':
225 226 227 228
          selection_name = field.get_value("selection_name")
          if selection_name in ("",None):
            error_list.append(form_path)
    self.assertEquals(error_list, [])
229

Nicolas Delaby's avatar
Nicolas Delaby committed
230
  def test_callableListMethodInListbox(self):
231 232 233 234 235
    # check all list_method in listboxes
    skins_tool = self.portal.portal_skins
    error_list = []
    for form_path, form in skins_tool.ZopeFind(
              skins_tool, obj_metatypes=['ERP5 Form'], search_sub=1):
236
      for field in self.getFieldList(form, form_path):
Fabien Morin's avatar
Fabien Morin committed
237
        if field.getRecursiveTemplateField().meta_type == 'ListBox':
238 239 240
          list_method = field.get_value("list_method")
          if list_method:
            if isinstance(list_method, str):
241
              method = getattr(self.portal, list_method, None)
242 243 244
            else:
              method = list_method
            if not callable(method):
245
              error_list.append((form_path, list_method))
246
    self.assertEquals(error_list, [])
247

248 249 250 251 252 253 254
  def test_listActionInListbox(self):
    # check all list_action in listboxes
    skins_tool = self.portal.portal_skins
    error_list = []
    for form_path, form in skins_tool.ZopeFind(
              skins_tool, obj_metatypes=['ERP5 Form'], search_sub=1):
      for field in self.getFieldList(form, form_path):
Fabien Morin's avatar
Fabien Morin committed
255
        if field.getRecursiveTemplateField().meta_type == 'ListBox':
256 257 258 259 260 261 262 263 264 265 266
          list_action = field.get_value("list_action")
          if list_action and list_action != 'list': # We assume that 'list'
                                                    # list_action exists
            if isinstance(list_action, str):
              method = getattr(self.portal, list_action.split('?')[0], None)
            else:
              method = list_action
            if not callable(method):
              error_list.append(('%s/%s' % (form_path, field.id), list_action))
    self.assertEquals(error_list, [])

267 268
  def test_moduleListMethod(self):
    """Make sure that module's list method works."""
269
    error_list = []
270 271
    for document in self.portal.contentValues():
      if document.portal_type.endswith(' Module'):
272 273 274
        if document.title not in document.list(reset=1):
          error_list.append(document.id)
    self.assertEqual([], error_list)
275

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  def test_preferenceViewDuplication(self):
    """Make sure that preference view is not duplicated."""
    preference_view_id_dict = {}
    def addPreferenceView(folder_id, view_id):
      if not view_id in preference_view_id_dict:
        preference_view_id_dict[view_id] = []
      preference_view_id_dict[view_id].append('%s.%s' % (folder_id, view_id))
    error_list = []
    for object_ in self.portal.portal_skins.objectValues():
      if object_.isPrincipiaFolderish:
        for id_ in object_.objectIds():
          if id_.startswith('Preference_view'):
            addPreferenceView(object_.id, id_)
    for view_id, location_list in preference_view_id_dict.items():
      if len(location_list)>1:
        error_list.extend(location_list)
    self.assertEqual(error_list, [])

294 295 296 297 298 299 300 301 302 303 304 305
class W3Validator(object):

  def __init__(self, validator_path, show_warnings):
    self.validator_path = validator_path
    self.show_warnings = show_warnings
    self.name = 'w3c'

  def _parse_validation_results(self, result):
    """
    parses the validation results, returns a list of tuples:
    line_number, col_number, error description
    """
306
    result_list_list = []
307
    xml_doc = minidom.parseString(result)
308 309 310 311 312 313 314 315 316 317 318 319 320
    for severity in 'm:error', 'm:warning':
      result_list = []
      for error in xml_doc.getElementsByTagName(severity):
        result = []
        for name in 'm:line', 'm:col', 'm:message':
          element_list = error.getElementsByTagName(name)
          if element_list:
            result.append(element_list[0].firstChild.nodeValue)
          else:
            result.append(None)
        result_list.append(tuple(result))
      result_list_list.append(result_list)
    return result_list_list
321 322 323 324 325

  def getErrorAndWarningList(self, page_source):
    '''
      retrun two list : a list of errors and an other for warnings
    '''
326 327 328
    if isinstance(page_source, unicode):
      # Zope 2.12 renders page templates as unicode
      page_source = page_source.encode('utf-8')
329 330 331 332 333 334 335 336 337 338 339 340 341 342
    source = 'fragment=%s&output=soap12' % urllib.quote_plus(page_source)
    os.environ['CONTENT_LENGTH'] = str(len(source))
    os.environ['REQUEST_METHOD'] = 'POST'
    stdout, stdin, stderr = popen2.popen3(self.validator_path)
    stdin.write(source)
    stdin.close()
    while stdout.readline() != '\n':
      pass
    result = stdout.read()
    return self._parse_validation_results(result)


class TidyValidator(object):

Jérome Perrin's avatar
Jérome Perrin committed
343
  def __init__(self, validator_path, show_warnings):
344 345 346 347 348 349 350 351 352 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 388 389 390 391 392 393
    self.validator_path = validator_path
    self.show_warnings = show_warnings
    self.name = 'tidy'

  def _parse_validation_results(self, result):
    """
    parses the validation results, returns a list of tuples:
    line_number, col_number, error description
    """
    error_list=[]
    warning_list=[]

    for i in result:
      data = i.split(' - ')
      if len(data) >= 2:
        data[1] = data[1].replace('\n','')
        if data[1].startswith('Error: '):
          location_list = data[0].split(' ')
          line = location_list[1]
          column = location_list[3]
          error = True
          message = data[1].split(': ')[1]
          error_list.append((line, column, message))
        elif data[1].startswith('Warning: '):
          location_list = data[0].split(' ')
          line = location_list[1]
          column = location_list[3]
          warning = True
          message = data[1].split(': ')[1]
          warning_list.append((line, column, message))
    return (error_list, warning_list)

  def getErrorAndWarningList(self, page_source):
    '''
      retrun two list : a list of errors and an other for warnings
    '''
    stdout, stdin, stderr = popen2.popen3('%s -e -q -utf8' % self.validator_path)
    stdin.write(page_source)
    stdin.close()
    return self._parse_validation_results(stderr)


def validate_xhtml(validator, source, view_name, bt_name):
  '''
    validate_xhtml return True if there is no error on the page, False else.
    Now it's possible to show warnings, so, if the option is set to True on the
    validator object, and there is some warning on the page, the function 
    return False, even if there is no error.
  '''
  # display some information when test faild to facilitate debugging
394
  message = ['Using %s validator to parse the view "%s" (from %s bt)'
Julien Muchembled's avatar
typo  
Julien Muchembled committed
395
             ' with warnings%sdisplayed :'
396
             % (validator.name, view_name, bt_name,
Julien Muchembled's avatar
typo  
Julien Muchembled committed
397
                validator.show_warnings and ' ' or ' NOT ')]
398

399
  result_list_list = validator.getErrorAndWarningList(source)
400

401 402 403
  severity_list = ['Error']
  if validator.show_warnings:
    severity_list.append('Warning')
404

405 406 407 408 409 410 411
  for i, severity in enumerate(severity_list):
    for line, column, msg in result_list_list[i]:
      if line is None and column is None:
        message.append('%s: %s' % (severity, msg))
      else:
        message.append('%s: line %s column %s : %s' %
                       (severity, line, column, msg))
412

413
  return len(message) == 1, '\n'.join(message)
414 415 416 417 418


def makeTestMethod(validator, module_id, portal_type, view_name, bt_name):

  def createSubContent(content, portal_type_list):
Jérome Perrin's avatar
Jérome Perrin committed
419
    if not portal_type_list:
420
      return content
Jérome Perrin's avatar
Jérome Perrin committed
421 422 423 424 425
    if portal_type_list == [content.getPortalType()]:
      return content
    return createSubContent(
               content.newContent(portal_type=portal_type_list[0]),
               portal_type_list[1:])
426

427 428
  def testMethod(self):
    module = getattr(self.portal, module_id)
429 430 431 432 433 434 435 436
    portal_type_list = portal_type.split('/')

    object = createSubContent(module, portal_type_list)
    view = getattr(object, view_name)
    self.assert_(*validate_xhtml( validator=validator, 
                                  source=view(), 
                                  view_name=view_name, 
                                  bt_name=bt_name))
437 438
  return testMethod

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
def testPortalTypeViewRecursivly(validator, module_id, business_template_info, 
    business_template_info_list, portal_type_list, portal_type_path_dict, 
    base_path, tested_portal_type_list):
  '''
  This function go on all portal_type recursivly if the portal_type could 
  contain other portal_types and make a test for all view that have action
  '''
  # iteration over all allowed portal_types inside the module/portal_type
  for portal_type in portal_type_list:
    portal_path = portal_type_path_dict[portal_type]
    if portal_type not in tested_portal_type_list:
      # this portal type haven't been tested yet

      backuped_module_id = module_id 
      backuped_business_template_info = business_template_info

      if not business_template_info.actions.has_key(portal_type):
        # search in other bt :
        business_template_info = None
        for bt_info in business_template_info_list:
          if bt_info.actions.has_key(portal_type):
            business_template_info = bt_info
            break
        if not business_template_info:
          LOG("Can't find the action :", 0, portal_type)
          break
        # create the object in portal_trash module
        module_id = 'portal_trash'

      for action_information in business_template_info.actions[portal_type]:
Jérome Perrin's avatar
Jérome Perrin committed
469
        if (action_information['category'] in ('object_view', 'object_list') and
470 471 472
            action_information['visible']==1 and
            action_information['text'].startswith('string:${object_url}/') and
            len(action_information['text'].split('/'))==2):
Jérome Perrin's avatar
Jérome Perrin committed
473
          view_name = action_information['text'].split('/')[-1].split('?')[0]
474 475 476 477 478
          method = makeTestMethod(validator,
                                  module_id, 
                                  portal_path,
                                  view_name, 
                                  business_template_info.title)
479 480 481 482 483
          method_name = ('test_%s_%s_%s' % 
                         (business_template_info.title, 
                          str(portal_type).replace(' ','_'), # can be unicode
                          view_name))
          method.__name__ = method_name
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
          setattr(TestXHTML, method_name, method)
          module_id = backuped_module_id
          business_template_info = backuped_business_template_info

      # add the portal_type to the tested portal_types. This avoid to test many
      # times a Portal Type wich is many bt.
      tested_portal_type_list.append(portal_type)

      new_portal_type_list = business_template_info.allowed_content_types.get(portal_type, ())
      new_portal_type_path_dict = {}

      if base_path != '':
        next_base_path = '%s/%s' % (base_path, portal_type)
      # Module portal_type not to have been added to the path because
      # this portal type object already existing
      elif 'Module' not in portal_type:
        next_base_path = portal_type
      else:
        next_base_path = ''

      for pt in new_portal_type_list:
        if next_base_path != '' and 'Module' not in pt:
          new_portal_type_path_dict[pt] = '%s/%s' % (next_base_path, pt)
        else:
          new_portal_type_path_dict[pt] = pt 
      testPortalTypeViewRecursivly(validator=validator,
                       module_id=module_id, 
                       business_template_info=backuped_business_template_info, 
                       business_template_info_list=business_template_info_list,
                       portal_type_list=new_portal_type_list, 
                       portal_type_path_dict=new_portal_type_path_dict,
                       base_path=next_base_path,
                       tested_portal_type_list=tested_portal_type_list)


519
def addTestMethodDynamically(validator, target_business_templates):
520 521
  from Products.ERP5.tests.utils import BusinessTemplateInfoTar
  from Products.ERP5.tests.utils import BusinessTemplateInfoDir
522 523
  business_template_info_list = []

524
  for i in target_business_templates:
525 526
    business_template = os.path.join(bt5_base_path, i)

527
    # Look for business templates, they can be:
528 529 530
    #  .bt5 files in $INSTANCE_HOME/bt5/
    #  directories in $INSTANCE_HOME/
    #  directories in $INSTANCE_HOME/bt5/*/
531
    #  directories in $INSTANCE_HOME/Products/ERP5/bootstrap/
532 533 534
    if not ( os.path.exists(business_template) or
        os.path.exists('%s.bt5' % business_template)):
      # try in $INSTANCE_HOME/bt5/*/
535
      business_template_glob_list = glob('%s/*/%s' % (bt5_base_path, i))
536 537
      if business_template_glob_list:
        business_template = business_template_glob_list[0]
538 539 540
      else:
        # try in $INSTANCE_HOME/Products/ERP5/bootstrap
        business_template = os.path.join(bootstrap_base_path,i) 
541

542 543 544 545 546
    if os.path.isdir(business_template):
      business_template_info = BusinessTemplateInfoDir(business_template)
    elif os.path.isfile(business_template+'.bt5'):
      business_template_info = BusinessTemplateInfoTar(business_template+'.bt5')
    else:
547
      raise KeyError, "Can't find the business template: %s" % i
548
    business_template_info_list.append(business_template_info)
549

550 551
  tested_portal_type_list = []
  for business_template_info in business_template_info_list:
552
    for module_id, module_portal_type in business_template_info.modules.items():
Jérome Perrin's avatar
Jérome Perrin committed
553
      portal_type_list = [module_portal_type, ] + \
554
            business_template_info.allowed_content_types.get(module_portal_type, [])
555
      portal_type_path_dict = {}
Jérome Perrin's avatar
Jérome Perrin committed
556
      portal_type_path_dict = dict(map(None,portal_type_list,portal_type_list))
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
      testPortalTypeViewRecursivly(validator=validator,
                       module_id=module_id, 
                       business_template_info=business_template_info, 
                       business_template_info_list=business_template_info_list,
                       portal_type_list=portal_type_list, 
                       portal_type_path_dict=portal_type_path_dict,
                       base_path = '',
                       tested_portal_type_list=tested_portal_type_list)


# Two validators are available : tidy and the w3c validator
# It's hightly recommanded to use the w3c validator because tidy dont show
# all errors and show more warnings that there is.
validator_to_use = 'w3c'
show_warnings = True

validator = None

# tidy or w3c may not be installed in livecd. Then we will skip xhtml validation tests.
# create the validator object
if validator_to_use == 'w3c':
578 579 580 581 582 583
  validator_paths = ['/usr/share/w3c-markup-validator/cgi-bin/check',
                     '/usr/lib/cgi-bin/check']
  for validator_path in validator_paths:
    if os.path.exists(validator_path):
      validator = W3Validator(validator_path, show_warnings)
      break
584
  else:
585
    print 'No w3c validator found at', validator_paths
586

Jérome Perrin's avatar
Jérome Perrin committed
587
elif validator_to_use == 'tidy':
588 589 590 591 592 593 594 595
  error = False
  warning = False
  validator_path = '/usr/bin/tidy'
  if not os.path.exists(validator_path):
    print 'tidy is not installed at %s' % validator_path
  else:
    validator = TidyValidator(validator_path, show_warnings)

596
def test_suite():
597
  # add the tests
598 599 600 601 602
  if validator is not None:
    # add erp5_core to the list here to not return it
    # on getBusinessTemplateList call
    addTestMethodDynamically(validator,
      ('erp5_core',) + TestXHTML.getBusinessTemplateList())
603 604 605
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestXHTML))
  return suite