testProxify.py 6.85 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
##############################################################################
#
# Copyright (c) 2007 Nexedi SARL 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

# Make it possible to use Globals.get_request
class DummyRequest(dict):
  def set(self, k, v):
    self[k] = v

global request
request = DummyRequest()

def get_request():
  global request
  return request

# apply patch (before it's imported by other modules)
import Globals
Globals.get_request = get_request

from Products.Formulator.TALESField import TALESMethod
from Products.ERP5Type.Core.Folder import Folder
from Products.ERP5Form.Form import ERP5Form
49
from Products.ERP5Form.ProxyField import purgeFieldValueCache
50

51 52 53
# install ERP5Form and load patches
from Testing import ZopeTestCase
ZopeTestCase.installProduct('ERP5Form')
54

55 56

class TestProxify(unittest.TestCase):
57

Romain Courteaud's avatar
Romain Courteaud committed
58 59 60
  def getTitle(self):
    return "Proxify"

61 62
  def setUp(self):
    # base field library
63
    self.container = Folder('container').__of__(Folder('root'))
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    self.container._setObject('Base_view',
                               ERP5Form('Base_view', 'Base'))
    base_view = self.base_view = self.container.Base_view
    base_view.manage_addField('my_string_field', 'String Field', 'StringField')
    base_view.manage_addField('my_list_field', 'List Field', 'ListField')
    base_view.my_string_field.values['display_width'] = 30
    base_view.my_list_field.values['size'] = 1

    # address view
    self.container._setObject('Address_view',
                              ERP5Form('Address_view', 'Address'))
    address_view = self.address_view = self.container.Address_view
    address_view.manage_addField('my_region', 'Country', 'StringField')
    address_view.my_region.values['size'] = 1
    address_view.my_region.tales['items'] = TALESMethod('here/portal_categories/region/getCategoryChildTitleItemList')

    # person view
    self.container._setObject('Person_view',
                               ERP5Form('Person_view', 'Person'))
    person_view = self.person_view = self.container.Person_view
    person_view.manage_addField('my_name', 'Name', 'StringField')
    person_view.manage_addField('my_default_region', 'Country', 'ListField')
    person_view.my_name.values['size'] = 20
    person_view.my_default_region.values['size'] = 1
    person_view.my_default_region.tales['items'] = TALESMethod('here/portal_categories/region/getCategoryChildTranslatedLogicalPathItemList')
89
    person_view.my_default_region.values['scrap_variable'] = 'obsolete'
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    global request
    request = DummyRequest()

  def test_single_level_proxify(self):
    self.person_view.proxifyField({'my_name':'Base_view.my_string_field'})

    field = self.person_view.my_name
    self.assertEqual(field.meta_type, 'ProxyField')
    self.assertEqual(field.get_value('form_id'), 'Base_view')
    self.assertEqual(field.get_value('field_id'), 'my_string_field')
    self.assertEqual(field.is_delegated('title'), False)
    self.assertEqual(field.get_value('title'), 'Name')
    self.assertEqual(field.is_delegated('size'), False)
    self.assertEqual(field.get_value('size'), 20)
    self.assertEqual(field.is_delegated('enabled'), True)
    self.assertEqual(field.get_value('enabled'), 1)
    self.assertEqual(field.is_delegated('description'), True)
    self.assertEqual(field.get_value('description'), '')

110
    purgeFieldValueCache() # must purge cache before changing internal field value.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    template_field = self.base_view.my_string_field
    template_field.values['description'] = 'Description'
    self.assertEqual(field.get_value('description'), 'Description')

  def test_multi_level_proxify(self):
    self.address_view.proxifyField({'my_region':'Base_view.my_list_field'})
    self.person_view.proxifyField({'my_default_region':'Address_view.my_region'})

    field = self.person_view.my_default_region
    self.assertEqual(field.meta_type, 'ProxyField')
    self.assertEqual(field.get_value('form_id'), 'Address_view')
    self.assertEqual(field.get_value('field_id'), 'my_region')
    self.assertEqual(field.getTemplateField().getId(), 'my_region')
    self.assertEqual(field.getRecursiveTemplateField().getId(), 'my_list_field')
    self.assertEqual(field.is_delegated('title'), True)
    self.assertEqual(field.get_value('title'), 'Country')
    self.assertEqual(field.is_delegated('size'), True)
    self.assertEqual(field.get_value('size'), 1)
    self.assertEqual(field.is_delegated('items'), False)
    self.assertEqual(field.get_tales('items')._text,
                     'here/portal_categories/region/getCategoryChildTranslatedLogicalPathItemList')
    self.assertEqual(field.is_delegated('enabled'), True)
    self.assertEqual(field.get_value('enabled'), 1)
    self.assertEqual(field.is_delegated('description'), True)
    self.assertEqual(field.get_value('description'), '')

137 138
    self.assertEqual(field.has_value('scrap_variable'), 0)

139
    purgeFieldValueCache() # must purge cache before changing internal field value.
140 141 142 143
    template_field = self.address_view.my_region
    template_field.values['title'] = 'Region'
    self.assertEqual(field.get_value('title'), 'Region')

144 145 146 147 148 149 150 151 152 153 154 155 156
  def test_unproxify(self):
    #Proxify First
    self.address_view.proxifyField({'my_region':'Base_view.my_list_field'})
    self.person_view.proxifyField({'my_default_region':'Address_view.my_region'})
    purgeFieldValueCache()
    #UnProxify
    self.person_view.unProxifyField({'my_default_region':'on'})
    field = self.person_view.my_default_region
    self.assertEqual(field.meta_type, 'ListField')
    self.assertEqual(field.get_value('title'), 'Country')
    self.assertEqual(field.get_tales('items')._text,
                     'here/portal_categories/region/getCategoryChildTranslatedLogicalPathItemList')

157 158 159 160
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestProxify))
  return suite