testOOoChart.py 14.1 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
##############################################################################
Thibaut Deheunynck's avatar
Thibaut Deheunynck committed
3 4
# Copyright (c) 2008 Nexedi SA and Contributors. All Rights Reserved.
#          Deheunynck Thibaut <thibaut@nexedi.com>
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
#
# 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
30
import transaction
31 32 33 34 35 36 37 38
from Products.ERP5Form.Form import ERP5Form
from DocumentTemplate import String

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Form.Selection import Selection
from Testing import ZopeTestCase
from Products.ERP5OOo.tests.utils import Validator
from Acquisition import aq_base
39
from Products.ERP5Type.Globals import get_request
40
from Products.ERP5.Document.Document import VALID_IMAGE_FORMAT_LIST
41
from Products.ERP5Type.Core.Folder import Folder
42
from lxml import etree
43 44 45 46 47 48 49

HTTP_OK = 200
debug = 0

class TestOOoChart(ERP5TypeTestCase, ZopeTestCase.Functional):
    """Tests OOoChart a and this render for ERP5."""
    form_id = 'TestOOochart_viewForm'
50
    ooo_chart_id = 'my_ooochart'
51 52 53 54 55 56 57
    nb_persons = 10
    content_type = 'application/vnd.oasis.opendocument.graphics'

    def getTitle(self):
      return 'Test OOoChart'

    def getBusinessTemplateList(self):
58 59
      return ('erp5_core_proxy_field_legacy',
              'erp5_base',
60 61 62 63 64 65
              'erp5_ingestion',
              'erp5_web',
              'erp5_dms',
              'erp5_ui_test', 
              'erp5_odt_style',
              'erp5_ods_style')
66 67 68 69 70

    def afterSetUp(self):
      self.auth = 'ERP5TypeTestCase:'
      portal = self.getPortal()
      container = portal.portal_skins.custom
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      if self.form_id not in container.objectIds():
        container._setObject(self.form_id, ERP5Form(self.form_id, 'View'))
        form = getattr(container, self.form_id)

        # create some persons in person_module
        self.createPersons()
        # add a ListBox field
        form.manage_addField('listbox', 'listbox', 'ListBox')
        form.listbox.ListBox_setPropertyList(field_list_method='zCountDocumentPerOwner',
                                field_count_method='',
                                field_columns=['owner | Owner',
                                              'owner_count | Owner Count',
                                              'number_count | Reference Count'],
                                )

        # create a Field OOoChart
        form.manage_addField(self.ooo_chart_id, self.ooo_chart_id, 'OOoChart')
        
        # create a Field OOoChart
        form.manage_addField('your_ooochart', 'your_ooochart', 'OOoChart')



        # create a ZSQL Method
        sql = """SELECT owner, count(uid) AS owner_count,
                count(reference) AS number_count
                FROM catalog
                WHERE portal_type = 'Person'
                GROUP BY owner ORDER BY owner_count DESC"""

        template = String(source_string=sql)
        container.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod('zCountDocumentPerOwner', 'zCountDocumentPerOwner', 'erp5_sql_connection', '', template)
        # enable preference
        preference = self.getPortal().portal_preferences.default_site_preference
        preference.setPriority(1)
        if preference.getPreferenceState() == 'disabled':
          self.getWorkflowTool().doActionFor(ob=preference,
                                            action='enable_action',
                                            wf_id='preference_workflow')

111
      self.validator = Validator()
112
      transaction.commit()
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
      self.tic()

    def createPersons(self):
      """ Create 10 persons in person_module """
      module = self.getPersonModule()
      if len(module.objectIds()) == 0:
        for i in range(self.nb_persons):
          module.newContent(portal_type='Person', id='person%s' % i)

    def _validate(self, odf_file_data):
      error_list = self.validator.validate(odf_file_data)
      if error_list:
        self.fail(''.join(error_list))

    def test_ooo_chart(self):
      portal = self.getPortal()
      # Does the form exist ?
      self.assertTrue(self.form_id in portal.portal_skins.custom.objectIds())
      getattr(aq_base(portal.portal_skins.custom), self.form_id)
      form = getattr(portal.portal_skins.custom, self.form_id)
133 134 135
      #listbox = form.listbox
      listbox = getattr(form, 'listbox')
      self.assertEquals(listbox.meta_type, 'ListBox')
136 137 138 139 140 141 142 143 144 145
      request = get_request()
      request['here'] = portal.portal_skins.custom
      line_list = [l for l in listbox.get_value('default',
                              render_format='list',
                              REQUEST=request) ]

      # listbox is empty?
      self.assertEquals(2, len(line_list))

      # Does the field OOoChart exist ?
146 147 148 149 150 151 152 153 154 155 156 157 158
      ooochart = getattr(form, self.ooo_chart_id)
      self.assertEquals(ooochart.meta_type, 'OOoChart')

      response = self.publish(
                    '/%s/%s/%s?render_format=&display=medium'
                    % (self.portal.getId(), self.form_id, self.ooo_chart_id), self.auth )
      # test render raw
      self.assertEquals(HTTP_OK, response.getStatus())
      content_type = response.getHeader('content-type')

      # test content type : application/vnd.oasis.opendocument.graphics
      self.assertTrue(content_type.startswith(self.content_type), content_type)
      content_disposition = response.getHeader('content-disposition')
159
      self.assertEquals('attachment', content_disposition.split(';')[0])
160 161 162 163 164 165 166 167 168
      # Test ODG (zip)
      body = response.getBody()
      # Test Validation Relax NG
      self._validate(body)

      from Products.ERP5OOo.OOoUtils import OOoParser
      parser = OOoParser()
      parser.openFromString(body)
      content_xml_view = parser.oo_files['content.xml']
169 170

      doc_view = etree.fromstring(content_xml_view)
171
      xpath = '//@*[name() = "xlink:href"]'
172
      num_object = doc_view.xpath(xpath)[0][2:]
173 174

      content_xml_build = parser.oo_files['%s/content.xml' % num_object]
175
      doc_build = etree.fromstring(content_xml_build)
176
      xpath = '//@*[name() = "office:value"]'
177
      value_list = doc_build.xpath(xpath)
178
      # Test the data presence in the file XML
179
      self.assertNotEquals(0, len(value_list))
180 181
      # 2 values because there are - 10 document created by a owner 
      #                            - 0 Reference count
182
      self.assertEquals(2, len(value_list))
183 184 185

      # Test the differents render
      # render image
Nicolas Delaby's avatar
Nicolas Delaby committed
186
      for image_format in VALID_IMAGE_FORMAT_LIST:
187 188 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 241
        response = self.publish(
                      '/%s/%s/%s?render_format=%s&display=medium'
                      % (self.portal.getId(), self.form_id, self.ooo_chart_id, image_format), self.auth )
        self.assertEquals(HTTP_OK, response.getStatus(), '%s rendering failed: %s' % (image_format, response.getStatus()))

      # render pdf
      response = self.publish(
                    '/%s/%s/%s?render_format=pdf&display=medium'
                    % (self.portal.getId(), self.form_id, self.ooo_chart_id), self.auth )
      self.assertEquals(HTTP_OK, response.getStatus())


      # Change some params  and restart (circle, bar, ...)
      # chart type : circle
      form.my_ooochart.manage_edit_xmlrpc(dict(chart_type='chart:circle'))
      response = self.publish(
                    '/%s/%s/%s?render_format=&display=medium'
                    % (self.portal.getId(), self.form_id, self.ooo_chart_id), self.auth )
      # Test ODG (zip) with other params
      body = response.getBody()
      # Test Validation Relax NG
      self._validate(body)

      # chart type : line
      form.my_ooochart.manage_edit_xmlrpc(dict(chart_type='chart:line'))
      response = self.publish(
                    '/%s/%s/%s?render_format=&display=medium'
                    % (self.portal.getId(), self.form_id, self.ooo_chart_id), self.auth )
      # Test ODG (zip) with other params
      body = response.getBody()
      # Test Validation Relax NG
      self._validate(body)

      #chart type : scatter
      form.my_ooochart.manage_edit_xmlrpc(dict(chart_type='chart:scatter'))
      response = self.publish(
                    '/%s/%s/%s?render_format=&display=medium'
                    % (self.portal.getId(), self.form_id, self.ooo_chart_id), self.auth )
      # Test ODG (zip) with other params
      body = response.getBody()
      # Test Validation Relax NG
      self._validate(body)

    def test_proxy_ooo_chart(self):
      portal = self.getPortal()
      # Does the form exist ?
      self.assertTrue(self.form_id in portal.portal_skins.custom.objectIds())
      getattr(aq_base(portal.portal_skins.custom), self.form_id)
      form = getattr(portal.portal_skins.custom, self.form_id)

      #Proxify the Field my_ooochart
      form.proxifyField({self.ooo_chart_id:'TestOOochart_viewForm.your_ooochart'})
      # Does the field OOoChart exist ?
      ooochart = getattr(form, self.ooo_chart_id)
      self.assertEquals(ooochart.meta_type, 'ProxyField')
242
      response = self.publish(
243 244
                    '/%s/%s/%s?render_format=&display=medium'
                    % (self.portal.getId(), self.form_id, self.ooo_chart_id), self.auth )
245 246 247 248 249 250 251
      # test render raw
      self.assertEquals(HTTP_OK, response.getStatus())
      content_type = response.getHeader('content-type')

      # test content type : application/vnd.oasis.opendocument.graphics
      self.assertTrue(content_type.startswith(self.content_type), content_type)
      content_disposition = response.getHeader('content-disposition')
252
      self.assertEquals('attachment', content_disposition.split(';')[0])
253 254 255 256 257 258 259 260 261
      # Test ODG (zip)
      body = response.getBody()
      # Test Validation Relax NG
      self._validate(body)

      from Products.ERP5OOo.OOoUtils import OOoParser
      parser = OOoParser()
      parser.openFromString(body)
      content_xml_view = parser.oo_files['content.xml']
262 263

      doc_view = etree.fromstring(content_xml_view)
264
      xpath = '//@*[name() = "xlink:href"]'
265
      num_object = doc_view.xpath(xpath)[0][2:]
266 267

      content_xml_build = parser.oo_files['%s/content.xml' % num_object]
268
      doc_build = etree.fromstring(content_xml_build)
269
      xpath = '//@*[name() = "office:value"]'
270
      value_list = doc_build.xpath(xpath)
271
      # Test the data presence in the file XML
272
      self.assertNotEquals(0, len(value_list))
273 274
      # 2 values because there are - 10 document created by a owner 
      #                            - 0 Reference count
275
      self.assertEquals(2, len(value_list))
276 277 278

      # Test the differents render
      # render image
279
      for image_format in VALID_IMAGE_FORMAT_LIST:
280
        response = self.publish(
281 282
                      '/%s/%s/%s?render_format=%s&display=medium'
                      % (self.portal.getId(), self.form_id, self.ooo_chart_id, image_format), self.auth )
283
        self.assertEquals(HTTP_OK, response.getStatus(), '%s rendering failed: %s' % (image_format, response.getStatus()))
284 285 286

      # render pdf
      response = self.publish(
287 288
                    '/%s/%s/%s?render_format=pdf&display=medium'
                    % (self.portal.getId(), self.form_id, self.ooo_chart_id), self.auth )
289 290 291 292 293
      self.assertEquals(HTTP_OK, response.getStatus())


      # Change some params  and restart (circle, bar, ...)
      # chart type : circle
294
      form.my_ooochart.manage_edit_xmlrpc(dict(chart_type='chart:circle'))
295
      response = self.publish(
296 297
                    '/%s/%s/%s?render_format=&display=medium'
                    % (self.portal.getId(), self.form_id, self.ooo_chart_id), self.auth )
298 299 300 301 302 303
      # Test ODG (zip) with other params
      body = response.getBody()
      # Test Validation Relax NG
      self._validate(body)

      # chart type : line
304
      form.my_ooochart.manage_edit_xmlrpc(dict(chart_type='chart:line'))
305
      response = self.publish(
306 307
                    '/%s/%s/%s?render_format=&display=medium'
                    % (self.portal.getId(), self.form_id, self.ooo_chart_id), self.auth )
308 309 310 311 312 313
      # Test ODG (zip) with other params
      body = response.getBody()
      # Test Validation Relax NG
      self._validate(body)

      #chart type : scatter
314
      form.my_ooochart.manage_edit_xmlrpc(dict(chart_type='chart:scatter'))
315
      response = self.publish(
316 317
                    '/%s/%s/%s?render_format=&display=medium'
                    % (self.portal.getId(), self.form_id, self.ooo_chart_id), self.auth )
318 319 320 321 322
      # Test ODG (zip) with other params
      body = response.getBody()
      # Test Validation Relax NG
      self._validate(body)

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    def test_ods_style(self):
      # simple rendering of a chart in ods style
      self.portal.changeSkin('ODS')
      response = self.publish(
          '/%s/%s' % (self.portal.getId(), self.form_id),
          self.auth,
          handle_errors=False )
      self.assertEquals(HTTP_OK, response.getStatus())
      body = response.getBody()
      self._validate(body)

    def test_odt_style(self):
      # simple rendering of a chart in odt style
      self.portal.changeSkin('ODT')
      response = self.publish(
          '/%s/%s' % (self.portal.getId(), self.form_id),
          self.auth,
          handle_errors=False )
      self.assertEquals(HTTP_OK, response.getStatus())
      body = response.getBody()
      self._validate(body)

345 346 347 348 349 350 351

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