Back to index

plone3  3.1.7
test_widgets.py
Go to the documentation of this file.
00001 ################################################################################
00002 #
00003 # Copyright (c) 2002-2005, Benjamin Saller <bcsaller@ideasuite.com>, and
00004 #                              the respective authors. All rights reserved.
00005 # For a list of Archetypes contributors see docs/CREDITS.txt.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions are met:
00009 #
00010 # * Redistributions of source code must retain the above copyright notice, this
00011 #   list of conditions and the following disclaimer.
00012 # * Redistributions in binary form must reproduce the above copyright notice,
00013 #   this list of conditions and the following disclaimer in the documentation
00014 #   and/or other materials provided with the distribution.
00015 # * Neither the name of the author nor the names of its contributors may be used
00016 #   to endorse or promote products derived from this software without specific
00017 #   prior written permission.
00018 #
00019 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00020 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00022 # FOR A PARTICULAR PURPOSE.
00023 #
00024 ################################################################################
00025 """
00026 """
00027 
00028 import os
00029 
00030 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00031 from Products.Archetypes.tests.utils import PACKAGE_HOME
00032 from Products.Archetypes.tests.utils import makeContent
00033 from Products.Archetypes.tests.test_fields import FakeRequest
00034 from Products.Archetypes.atapi import *
00035 from DateTime import DateTime
00036 
00037 stub_text_file = None
00038 stub_text_content = ''
00039 stub_bin_file = None
00040 stub_bin_content = ''
00041 
00042 class WidgetTests(ATSiteTestCase):
00043 
00044     def afterSetUp(self):
00045         # XXX messing up with global vars is bad!
00046         global stub_text_file, stub_text_content, \
00047                stub_bin_file, stub_bin_content
00048         stub_text_file = open(os.path.join(PACKAGE_HOME, 'input', 'rest1.rst'))
00049         stub_text_content = stub_text_file.read()
00050         stub_text_file.seek(0)
00051         stub_bin_file = open(os.path.join(PACKAGE_HOME, 'input', 'word.doc'))
00052         stub_bin_content = stub_bin_file.read()
00053         stub_bin_file.seek(0)
00054         # Make SESSION var available
00055         self.app.REQUEST['SESSION'] = {}
00056 
00057     def beforeTearDown(self):
00058         global stub_text_file, stub_bin_file
00059         stub_text_file.close()
00060         stub_bin_file.close()
00061 
00062     def test_subject_keyword_widget(self):
00063         doc = makeContent(self.folder, portal_type='ComplexType', id='demodoc')
00064         field = doc.Schema()['subject']
00065         widget = field.widget
00066         form = {'subject_keywords':['bla','ble'],
00067                 'subject_existing_keywords':['bli']
00068                 }
00069         expected = ['bla', 'ble', 'bli']
00070         result = widget.process_form(doc, field, form)
00071         result[0].sort()
00072         self.assertEqual(expected, result[0])
00073         form = {'subject_keywords':['bla'],
00074                 'subject_existing_keywords':['ble','bli']
00075                 }
00076         result = widget.process_form(doc, field, form)
00077         result[0].sort()
00078         self.assertEqual(expected, result[0])
00079         form = {'subject_keywords':[],
00080                 'subject_existing_keywords':['bla', 'ble','bli']
00081                 }
00082         result = widget.process_form(doc, field, form)
00083         result[0].sort()
00084         self.assertEqual(expected, result[0])
00085         form = {'subject_keywords':['bla', 'ble','bli'],
00086                 'subject_existing_keywords':['bla', 'ble','bli']
00087                 }
00088         result = widget.process_form(doc, field, form)
00089         result[0].sort()
00090         self.assertEqual(expected, result[0])
00091         form = {'subject_keywords':['bla', 'ble','bli'],
00092                 'subject_existing_keywords':[]
00093                 }
00094         result = widget.process_form(doc, field, form)
00095         result[0].sort()
00096         self.assertEqual(expected, result[0])
00097 
00098     def test_subject_keyword_widget2(self):
00099         doc = makeContent(self.folder, portal_type='ComplexType', id='demodoc')
00100         field = doc.Schema()['selectionlinesfield3']
00101         widget = field.widget
00102         form = {'selectionlinesfield3':['bla','ble']}
00103         expected = ['bla', 'ble']
00104         result = widget.process_form(doc, field, form)
00105         result[0].sort()
00106         self.assertEqual(expected, result[0])
00107         form = {'selectionlinesfield3':'bla\nble'}
00108         result = widget.process_form(doc, field, form)
00109         result[0].sort()
00110         self.assertEqual(expected, result[0])
00111 
00112     def _test_widgets(self):
00113         doc = makeContent(self.folder, portal_type='ComplexType', id='demodoc')
00114 
00115         #Now render this doc in view and edit modes. If this works
00116         #then we have pretty decent assurance that things are working
00117         view = doc.base_view()
00118         edit = doc.base_edit()
00119 
00120         #No exceptions are good, parse the results more if you need to
00121         #I feel fine...
00122 
00123     def test_appendtextarea_widget(self):
00124         request = FakeRequest()
00125         mystring = str('<<<<this is a test string>>>>')
00126         
00127         doc = makeContent(self.folder, portal_type='ComplexType', id='demodoc')
00128         field = doc.Schema()['textarea_appendonly']
00129         widget = field.widget
00130         
00131         form = {'textarea_appendonly':''}
00132         result = widget.process_form(doc, field, form)
00133         expected = '', {}
00134         self.assertEqual(expected, result)
00135         
00136         form = {'textarea_appendonly': mystring}
00137         expected = mystring, {}
00138         result = widget.process_form(doc, field, form)
00139         self.assertEqual(expected, result)
00140 
00141         doc.Schema()[field.getName()].set(doc, mystring)
00142         form = {'textarea_appendonly': mystring}
00143         expected = mystring + widget.divider + mystring, {}
00144         result = widget.process_form(doc, field, form)
00145         self.assertEqual(expected, result)
00146 
00147     def test_appendtextarea_timestamp_widget(self):
00148         """ This is a test I can write """
00149         request = FakeRequest()
00150         mystring = str('<<<<this is a test string>>>>')
00151         
00152         doc = makeContent(self.folder, portal_type='ComplexType', id='demodoc')
00153         field = doc.Schema()['textarea_appendonly_timestamp']
00154         widget = field.widget
00155         
00156         form = {'textarea_appendonly_timestamp':''}
00157         result = widget.process_form(doc, field, form)
00158         expected = '', {}
00159         self.assertEqual(expected, result)
00160         
00161         form = {'textarea_appendonly_timestamp': mystring}
00162         expected = mystring, {}
00163         result = widget.process_form(doc, field, form)
00164         self.assertEqual(expected, result)
00165 
00166         doc.Schema()[field.getName()].set(doc, mystring)
00167         form = {'textarea_appendonly_timestamp': mystring}
00168         expectation = mystring + '\n\n' + str(DateTime()) + widget.divider + mystring, {}
00169         results = widget.process_form(doc, field, form)
00170         
00171         # some magic (nightmares?) here for rectifying DateTime delay        
00172         result = results[0].split('\n\n')
00173         expected = expectation[0].split('\n\n')
00174         
00175         result[1] = result[1].split(' ')
00176         expected[1] = expected[1].split(' ')
00177         
00178         result[1][1] = expected[1][1][:-3]
00179         expected[1][1] = expected[1][1][:-3]
00180         
00181         self.assertEqual(expected, result)
00182 
00183     def test_maxlength_textarea_widget(self):
00184         """ Show me HOW to write this test and I will ~Spanky """
00185         
00186         request = FakeRequest()
00187         mystring = str('The little black dog jumped over the sleeping Moose')
00188         
00189         doc = makeContent(self.folder, portal_type='ComplexType', id='demodoc')
00190         field = doc.Schema()['textarea_maxlength']
00191         widget = field.widget
00192         
00193         form = {'textarea_maxlength':''}
00194         result = widget.process_form(doc, field, form)
00195         expected = '', {}
00196         self.assertEqual(expected, result)
00197         
00198         form = {'textarea_maxlength': mystring}
00199         expected = mystring, {}
00200         result = widget.process_form(doc, field, form)
00201         self.assertEqual(expected, result)
00202 
00203         doc.Schema()[field.getName()].set(doc, mystring)
00204         form = {'textarea_maxlength': mystring}
00205         expected = 'The little black dog', {}
00206         result = widget.process_form(doc, field, form)
00207         #self.assertEqual(expected, result)
00208 
00209 
00210     def test_rich_text_widget(self):
00211         request = FakeRequest()
00212         doc = makeContent(self.folder, portal_type='ComplexType', id='demodoc')
00213         field = doc.Schema()['richtextfield']
00214         widget = field.widget
00215         form = {'richtextfield_text_format':'text/x-rst',
00216                 'richtextfield_file':stub_bin_file,
00217                 'richtextfield':'',
00218                 }
00219         expected = stub_bin_file, {}
00220         result = widget.process_form(doc, field, form)
00221         self.assertEqual(expected, result)
00222 
00223         form = {'richtextfield_text_format':'text/x-rst',
00224                 'richtextfield_file':stub_bin_file,
00225                 'richtextfield':stub_text_file,
00226                 }
00227         expected = stub_bin_file, {}
00228         result = widget.process_form(doc, field, form)
00229         self.assertEqual(expected, result)
00230 
00231         form = {'richtextfield_text_format':'text/x-rst',
00232                 'richtextfield_file':stub_text_file,
00233                 'richtextfield':'',
00234                 }
00235         expected = stub_text_file, {}
00236         result = widget.process_form(doc, field, form)
00237         self.assertEqual(expected, result)
00238 
00239         form = {'richtextfield_text_format':'text/x-rst',
00240                 'richtextfield_file':stub_text_file,
00241                 'richtextfield':stub_text_content,
00242                 }
00243         expected = stub_text_file, {}
00244         result = widget.process_form(doc, field, form)
00245         self.assertEqual(expected, result)
00246 
00247 
00248         form = {'richtextfield_text_format':'text/x-rst',
00249                 'richtextfield_file':'',
00250                 'richtextfield':stub_text_content,
00251                 }
00252         expected = stub_text_content, {'mimetype':'text/x-rst'}
00253         result = widget.process_form(doc, field, form)
00254         self.assertEqual(expected, result)
00255 
00256         form = {'richtextfield_file':stub_text_file}
00257         request.form.update(form)
00258         doc.processForm(REQUEST=request)
00259         self.assertEqual(field.getContentType(doc), 'text/x-rst')
00260 
00261         # XXX: This makes wv-1.0.3 spin.
00262         #form = {'richtextfield_file':stub_bin_file}
00263         #request.form.update(form)
00264         #doc.processForm(REQUEST=request)
00265         #self.assertEqual(field.getContentType(doc), 'application/msword')
00266         #self.assertEqual(str(doc[field.getName()]), stub_bin_content)
00267 
00268         form = {'richtextfield_text_format':'text/x-rst',
00269                 'richtextfield_file':'',
00270                 'richtextfield':stub_text_content,
00271                 }
00272         expected = stub_text_content, {'mimetype':'text/x-rst'}
00273         result = widget.process_form(doc, field, form)
00274         self.assertEqual(expected, result)
00275 
00276         form = {'richtextfield_text_format':'',
00277                 'richtextfield_file':'',
00278                 'richtextfield':stub_text_content,
00279                 }
00280         expected = stub_text_content, {}
00281         result = widget.process_form(doc, field, form)
00282         self.assertEqual(expected, result)
00283 
00284         form = {'richtextfield_text_format':'text/plain',
00285                 'richtextfield_file':stub_text_file,
00286                 }
00287         request.form.update(form)
00288         doc.processForm(REQUEST=request)
00289         self.assertEqual(field.getContentType(doc), 'text/x-rst')
00290         self.assertEqual(str(doc[field.getName()]), stub_text_content)
00291 
00292     def test_getBestIcon(self):
00293         doc = makeContent(self.folder, 'SimpleType', id='doc')
00294         self.assertEqual(doc.getBestIcon(), 'txt.png')
00295 
00296 
00297 def test_suite():
00298     from unittest import TestSuite, makeSuite
00299     suite = TestSuite()
00300     suite.addTest(makeSuite(WidgetTests))
00301     return suite