Commit 94776f7e authored by Jérome Perrin's avatar Jérome Perrin

Add some unit tests for ProxyFields.

Make sure that 'field' in the TALES context is the proxy field and not the proxified field.



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@15398 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent a971dfe9
...@@ -67,7 +67,16 @@ def get_value(self, id, **kw): ...@@ -67,7 +67,16 @@ def get_value(self, id, **kw):
container = obj.aq_inner.aq_parent container = obj.aq_inner.aq_parent
else: else:
container = None container = None
if REQUEST is not None:
# Proxyfield stores the "real" field in the request. Look if the
# corresponding field exists in request, and use it as field in the
# TALES context
field = REQUEST.get('field__proxyfield_%s_%s' % (self.id, id), self)
kw['field'] = field
else:
kw['field'] = self kw['field'] = self
kw['form'] = form kw['form'] = form
kw['request'] = REQUEST kw['request'] = REQUEST
kw['here'] = obj kw['here'] = obj
...@@ -118,7 +127,8 @@ def get_value(self, id, **kw): ...@@ -118,7 +127,8 @@ def get_value(self, id, **kw):
# stored in the context, only if the field is prefixed with my_. # stored in the context, only if the field is prefixed with my_.
REQUEST = get_request() REQUEST = get_request()
if REQUEST is not None: if REQUEST is not None:
field_id = REQUEST.get('%s_%s_id' % (self.id, id), self.id) field_id = REQUEST.get('field__proxyfield_%s_%s' % (self.id, id),
self).id
else: else:
field_id = self.id field_id = self.id
......
...@@ -477,8 +477,8 @@ class ProxyField(ZMIField): ...@@ -477,8 +477,8 @@ class ProxyField(ZMIField):
proxy_field = self.getTemplateField() proxy_field = self.getTemplateField()
if proxy_field is not None: if proxy_field is not None:
REQUEST = get_request() REQUEST = get_request()
REQUEST.set('%s_%s_id' % (proxy_field.id, id), REQUEST.set('field__proxyfield_%s_%s' % (proxy_field.id, id),
REQUEST.get('%s_%s_id' % (self.id, id), self.id)) REQUEST.get('field__proxyfield_%s_%s' % (self.id, id), self))
result = proxy_field.get_value(id, **kw) result = proxy_field.get_value(id, **kw)
return result return result
......
...@@ -28,13 +28,32 @@ ...@@ -28,13 +28,32 @@
import unittest import unittest
# Load monkey patches # 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
# Initialize ERP5Form Product to load monkey patches
from Testing import ZopeTestCase from Testing import ZopeTestCase
ZopeTestCase.installProduct('ERP5Form') ZopeTestCase.installProduct('ERP5Form')
from Products.Formulator.StandardFields import FloatField from Products.Formulator.StandardFields import FloatField
from Products.Formulator.StandardFields import StringField from Products.Formulator.StandardFields import StringField
from Products.Formulator.TALESField import TALESMethod
from Products.ERP5Type.Core.Folder import Folder
from Products.ERP5Form.Form import ERP5Form
class TestFloatField(unittest.TestCase): class TestFloatField(unittest.TestCase):
...@@ -92,9 +111,79 @@ class TestStringField(unittest.TestCase): ...@@ -92,9 +111,79 @@ class TestStringField(unittest.TestCase):
self.assertEquals('&lt;script&gt;', self.field.render("<script>")) self.assertEquals('&lt;script&gt;', self.field.render("<script>"))
class TestProxyField(unittest.TestCase):
def setUp(self):
self.container = Folder('container').__of__(Folder('root'))
self.container._setObject('Base_viewProxyFieldLibrary',
ERP5Form('Base_viewProxyFieldLibrary', 'Proxys'))
self.container._setObject('Base_view',
ERP5Form('Base_view', 'View'))
global request
request = DummyRequest()
def test_get_template_field(self):
self.container.Base_viewProxyFieldLibrary.manage_addField(
'my_title', 'Title', 'StringField')
original_field = self.container.Base_viewProxyFieldLibrary.my_title
self.container.Base_view.manage_addField(
'my_title', 'Not Title', 'ProxyField')
proxy_field = self.container.Base_view.my_title
proxy_field.manage_edit_xmlrpc(dict(form_id='Base_viewProxyFieldLibrary',
field_id='my_title',))
self.assertEquals(original_field, proxy_field.getTemplateField())
def test_simple_surcharge(self):
self.container.Base_viewProxyFieldLibrary.manage_addField(
'my_title', 'Title', 'StringField')
original_field = self.container.Base_viewProxyFieldLibrary.my_title
self.assertEquals('Title', original_field.get_value('title'))
self.container.Base_view.manage_addField(
'my_title', 'Not Title', 'ProxyField')
proxy_field = self.container.Base_view.my_title
proxy_field.manage_edit_xmlrpc(dict(form_id='Base_viewProxyFieldLibrary',
field_id='my_title',))
self.assert_(proxy_field.is_delegated('title'))
self.assertEquals('Title', proxy_field.get_value('title'))
def test_get_value_default(self):
# If the proxy field is named 'my_id', it will get 'id'
# property on the context, regardless of the id of the proxified field
self.container.Base_viewProxyFieldLibrary.manage_addField(
'my_title', 'Title', 'StringField')
original_field = self.container.Base_viewProxyFieldLibrary.my_title
self.container.Base_view.manage_addField(
'my_id', 'ID', 'ProxyField')
proxy_field = self.container.Base_view.my_id
proxy_field.manage_edit_xmlrpc(dict(form_id='Base_viewProxyFieldLibrary',
field_id='my_title',))
self.assertEquals('container', self.container.getId())
self.assertEquals('container', proxy_field.get_value('default'))
def test_tales_context(self):
# in the TALES context, "field" will be the proxyfield, not the original
# field.
self.container.Base_viewProxyFieldLibrary.manage_addField(
'my_title', 'Title', 'StringField')
original_field = self.container.Base_viewProxyFieldLibrary.my_title
original_field.manage_tales_xmlrpc(dict(title='field/getId'))
self.assertEquals('my_title', original_field.get_value('title'))
self.container.Base_view.manage_addField(
'my_reference', 'Not Title', 'ProxyField')
proxy_field = self.container.Base_view.my_reference
proxy_field.manage_edit_xmlrpc(dict(form_id='Base_viewProxyFieldLibrary',
field_id='my_title',))
# 'my_reference' is the ID of the proxy field
self.assertEquals('my_reference', proxy_field.get_value('title'))
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestFloatField)) suite.addTest(unittest.makeSuite(TestFloatField))
suite.addTest(unittest.makeSuite(TestStringField)) suite.addTest(unittest.makeSuite(TestStringField))
suite.addTest(unittest.makeSuite(TestProxyField))
return suite return suite
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment