[Zope3-checkins] SVN: Zope3/trunk/src/zope/testbrowser/ resolve issue 495:

Benji York benji at zope.com
Mon Nov 28 22:52:35 EST 2005


Log message for revision 40399:
  resolve issue 495:
  
      zope.testbrowser file control lacks support for setting file name and
      content type
  

Changed:
  U   Zope3/trunk/src/zope/testbrowser/README.txt
  U   Zope3/trunk/src/zope/testbrowser/browser.py

-=-
Modified: Zope3/trunk/src/zope/testbrowser/README.txt
===================================================================
--- Zope3/trunk/src/zope/testbrowser/README.txt	2005-11-29 03:30:53 UTC (rev 40398)
+++ Zope3/trunk/src/zope/testbrowser/README.txt	2005-11-29 03:52:33 UTC (rev 40399)
@@ -608,34 +608,32 @@
 
   - File Control
 
-    The following is basically unit testing, since most web servers
-    require uploaded files to have a filename, and building a control
-    in the following way means the uploaded file has no name or
-    content type:
+    The minimum setup required for file controls is to assign a file-like
+    object to the control's ``value`` attribute:
 
     >>> ctrl = browser.getControl('File Control')
     >>> ctrl
     <Control name='file-value' type='file'>
     >>> verifyObject(interfaces.IControl, ctrl)
     True
-    >>> ctrl.value
+    >>> ctrl.value is None
+    True
     >>> import cStringIO
     >>> ctrl.value = cStringIO.StringIO('File contents')
+
+    The file control's content type and file name can also be set:
+
+    >>> ctrl.filename = 'test.txt'
+    >>> ctrl.content_type = 'text/plain'
+
+    The file control (like the other controls) also knows if it is disabled
+    or if it can have multiple values.
+
     >>> ctrl.disabled
     False
     >>> ctrl.multiple
     False
 
-    Unfortunately, testbrowser doesn't really support what you need,
-    so you have to use the underlying mechanize control:
-    XXX this missing feature must be fixed before 3.2 is released (issue 495)
-
-    >>> ctrl.mech_control.add_file(
-    ...     cStringIO.StringIO('File contents'),
-    ...     content_type='text/plain',
-    ...     filename='test.txt',
-    ...     )
-
   - Selection Control (Single-Valued)
 
     >>> ctrl = browser.getControl('Single Select Control')

Modified: Zope3/trunk/src/zope/testbrowser/browser.py
===================================================================
--- Zope3/trunk/src/zope/testbrowser/browser.py	2005-11-29 03:30:53 UTC (rev 40398)
+++ Zope3/trunk/src/zope/testbrowser/browser.py	2005-11-29 03:52:33 UTC (rev 40399)
@@ -392,13 +392,19 @@
         self.mech_form = form
         self.browser = browser
         self._browser_counter = self.browser._counter
-        self._enable_setattr_errors = True
 
+        if self.mech_control.type == 'file':
+            self.filename = None
+            self.content_type = None
+
         # for some reason ClientForm thinks we shouldn't be able to modify
         # hidden fields, but while testing it is sometimes very important
         if self.mech_control.type == 'hidden':
             self.mech_control.readonly = False
 
+        # disable addition of further attributes
+        self._enable_setattr_errors = True
+
     @property
     def disabled(self):
         return bool(getattr(self.mech_control, 'disabled', False))
@@ -430,7 +436,9 @@
             if self._browser_counter != self.browser._counter:
                 raise interfaces.ExpiredError
             if self.mech_control.type == 'file':
-                self.mech_control.add_file(value)
+                self.mech_control.add_file(value,
+                                           content_type=self.content_type,
+                                           filename=self.filename)
             elif self.type == 'checkbox' and len(self.mech_control.items) == 1:
                 self.mech_control.items[0].selected = bool(value)
             else:



More information about the Zope3-Checkins mailing list