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

29 30
import unittest
import os
31 32
import popen2
import urllib
33 34 35 36

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.CMFCore.utils import getToolByName
from AccessControl.SecurityManagement import newSecurityManager
37 38 39
from zLOG import LOG
from xml.dom import minidom

40
from glob import glob
41 42 43 44 45 46 47 48 49 50

try:
  from transaction import get as get_transaction
except ImportError:
  pass

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

55 56 57 58 59 60 61 62 63
# dependency order
target_business_templates = (
  'erp5_base',
  'erp5_trade',

  'erp5_pdf_editor',
  'erp5_pdf_style',
  'erp5_pdm',
  'erp5_accounting',
64
  'erp5_invoicing',
65 66 67 68 69 70 71 72 73

  'erp5_apparel',

##   'erp5_banking_core',
##   'erp5_banking_cash',
##   'erp5_banking_check',
##   'erp5_banking_inventory',

  'erp5_budget',
74
  'erp5_public_accounting_budget',
75 76 77 78 79

  'erp5_commerce',

  'erp5_consulting',

80 81
  'erp5_ingestion',
  'erp5_ingestion_mysql_innodb_catalog',
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  'erp5_crm',

  'erp5_web',
  'erp5_dms',

  'erp5_forge',

  'erp5_immobilisation',

  'erp5_item',

  'erp5_mrp',

  'erp5_payroll',

  'erp5_project',
98 99

  'erp5_calendar',
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
)


class TestXHTML(ERP5TypeTestCase):

  run_all_test = 1

  def getTitle(self):
    return "XHTML Test"

  def getBusinessTemplateList(self):
    """  """
    return target_business_templates

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

119
  def login(self):
120 121 122 123 124 125 126 127 128 129 130 131
    uf = self.getPortal().acl_users
    uf._doAddUser('seb', '', ['Manager'], [])
    uf._doAddUser('ERP5TypeTestCase', '', ['Manager'], [])
    user = uf.getUserById('seb').__of__(uf)
    newSecurityManager(None, user)

  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')

132 133 134 135 136 137
  def test_deadProxyFields(self):
    # check that all proxy fields defined in business templates have a valid
    # target
    skins_tool = self.portal.portal_skins
    for field_path, field in skins_tool.ZopeFind(
              skins_tool, obj_metatypes=['ProxyField'], search_sub=1):
138
      self.assertNotEqual(None, field.getTemplateField(),
139 140 141 142
          '%s\nform_id:%s\nfield_id:%s\n' % (field_path,
                                             field.get_value('form_id'),
                                             field.get_value('field_id')))

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
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
    """
    error_list=[]
    warning_list=[]
    xml_doc = minidom.parseString(result)
    for error in xml_doc.getElementsByTagName('m:error'):
      error_line = error.getElementsByTagName('m:line')[0].firstChild.nodeValue
      error_col = error.getElementsByTagName('m:col')[0].firstChild.nodeValue
      error_message = error.getElementsByTagName('m:message')[0].firstChild.nodeValue
      error_list.append((error_line,error_col,error_message))
    for warning in xml_doc.getElementsByTagName('m:warning'):
      warning_line = warning.getElementsByTagName('m:line')[0].firstChild.nodeValue
      warning_col = warning.getElementsByTagName('m:col')[0].firstChild.nodeValue
      warning_message = warning.getElementsByTagName('m:message')[0].firstChild.nodeValue
      warning_list.append((warning_line, warning_col, warning_message))
    return error_list, warning_list

  def getErrorAndWarningList(self, page_source):
    '''
      retrun two list : a list of errors and an other for warnings
    '''
    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
188
  def __init__(self, validator_path, show_warnings):
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    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
  message = []
241 242 243
  message.append('Using %s validator to parse the view "%s" (from %s bt) with warning %sdisplayed :' %\
      (validator.name, view_name, bt_name, 
        validator.show_warnings and ' ' or 'NOT '))
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

  error_list, warning_list = validator.getErrorAndWarningList(source)

  if error_list:
    # build error message
    for error in error_list:
      message.append('Error: line %s column %s : %s' % error)

  if warning_list and validator.show_warnings:
    # build error message
    for warning in warning_list:
      message.append('Warning: line %s column %s : %s' % warning)

  message = '\n'.join(message)
  if validator.show_warnings:
    return ((not (len(error_list) or len(warning_list))), message)
  return ((not len(error_list)), message)


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
266
    if not portal_type_list:
267
      return content
Jérome Perrin's avatar
Jérome Perrin committed
268 269 270 271 272
    if portal_type_list == [content.getPortalType()]:
      return content
    return createSubContent(
               content.newContent(portal_type=portal_type_list[0]),
               portal_type_list[1:])
273

274 275
  def testMethod(self):
    module = getattr(self.portal, module_id)
276 277 278 279 280 281 282 283
    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))
284 285
  return testMethod

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
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
316
        if (action_information['category'] in ('object_view', 'object_list') and
317 318 319
            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
320
          view_name = action_information['text'].split('/')[-1].split('?')[0]
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
          method = makeTestMethod(validator,
                                  module_id, 
                                  portal_path,
                                  view_name, 
                                  business_template_info.title)
          method_name = 'test.%s.%s.%s' % (business_template_info.title, portal_type, view_name)
          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)


def addTestMethodDynamically(validator):
363 364
  from Products.ERP5.tests.utils import BusinessTemplateInfoTar
  from Products.ERP5.tests.utils import BusinessTemplateInfoDir
365 366 367 368 369
  business_template_info_list = []

  # add erp5_core to the list here but not in the target_business_templates 
  # list to not return it on getBusinessTemplateList call 
  for i in ('erp5_core',) + target_business_templates:
370 371
    business_template = os.path.join(bt5_base_path, i)

372
    # Look for business templates, they can be:
373 374 375
    #  .bt5 files in $INSTANCE_HOME/bt5/
    #  directories in $INSTANCE_HOME/
    #  directories in $INSTANCE_HOME/bt5/*/
376
    #  directories in $INSTANCE_HOME/Products/ERP5/bootstrap/
377 378 379
    if not ( os.path.exists(business_template) or
        os.path.exists('%s.bt5' % business_template)):
      # try in $INSTANCE_HOME/bt5/*/
380
      business_template_glob_list = glob('%s/*/%s' % (bt5_base_path, i))
381 382
      if business_template_glob_list:
        business_template = business_template_glob_list[0]
383 384 385
      else:
        # try in $INSTANCE_HOME/Products/ERP5/bootstrap
        business_template = os.path.join(bootstrap_base_path,i) 
386

387 388 389 390 391
    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:
392
      raise KeyError, "Can't find the business template: %s" % i
393
    business_template_info_list.append(business_template_info)
394

395 396
  tested_portal_type_list = []
  for business_template_info in business_template_info_list:
397
    for module_id, module_portal_type in business_template_info.modules.items():
Jérome Perrin's avatar
Jérome Perrin committed
398 399
      portal_type_list = [module_portal_type, ] + \
            business_template_info.allowed_content_types.get(module_portal_type, ())
400
      portal_type_path_dict = {}
Jérome Perrin's avatar
Jérome Perrin committed
401
      portal_type_path_dict = dict(map(None,portal_type_list,portal_type_list))
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 427 428
      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':
  validator_path = '/usr/share/w3c-markup-validator/cgi-bin/check'
  if not os.path.exists(validator_path):
    print 'w3c validator is not installed at %s' % validator_path
  else:
    validator = W3Validator(validator_path, show_warnings)

Jérome Perrin's avatar
Jérome Perrin committed
429
elif validator_to_use == 'tidy':
430 431 432 433 434 435 436 437 438 439 440
  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)

# add the tests
if validator is not None:
  addTestMethodDynamically(validator)
441

442 443 444 445
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestXHTML))
  return suite