Back to index

plone3  3.1.7
test_Link.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Unit tests for Link module.
00014 
00015 $Id: test_Link.py 68344 2006-05-29 14:19:25Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from re import compile
00022 
00023 from Products.CMFCore.testing import ConformsToContent
00024 from Products.CMFCore.tests.base.content import BASIC_RFC822
00025 from Products.CMFCore.tests.base.content import RFC822_W_CONTINUATION
00026 
00027 
00028 class LinkTests(ConformsToContent, unittest.TestCase):
00029 
00030     def _getTargetClass(self):
00031         from Products.CMFDefault.Link import Link
00032 
00033         return Link
00034 
00035     def _makeOne(self, *args, **kw):
00036         return self._getTargetClass()(*args, **kw)
00037 
00038     def canonTest(self, table):
00039         for orig, wanted in table.items():
00040             # test with constructor
00041             d = self._makeOne('foo', remote_url=orig)
00042             self.assertEqual(d.getRemoteUrl(), wanted)
00043             # test with edit method too
00044             d = self._makeOne('bar')
00045             d.edit(orig)
00046             self.assertEqual(d.getRemoteUrl(), wanted)
00047 
00048     def test_z3interfaces(self):
00049         from zope.interface.verify import verifyClass
00050         from Products.CMFDefault.interfaces import ILink
00051         from Products.CMFDefault.interfaces import IMutableLink
00052 
00053         verifyClass(ILink, self._getTargetClass())
00054         verifyClass(IMutableLink, self._getTargetClass())
00055 
00056     def test_Empty( self ):
00057         d = self._makeOne('foo')
00058         self.assertEqual( d.Title(), '' )
00059         self.assertEqual( d.Description(), '' )
00060         self.assertEqual( d.getRemoteUrl(), '' )
00061         self.assertEqual( d.format, 'text/url' )
00062         self.assertEqual( d.URL_FORMAT, 'text/url')
00063 
00064         d = self._makeOne('foo', remote_url='bar')
00065         d.edit('')
00066         self.assertEqual(d.getRemoteUrl(), '')
00067 
00068         d = self._makeOne('foo', remote_url='http://')
00069         self.assertEqual(d.getRemoteUrl(), '')
00070 
00071         d = self._makeOne('foo', remote_url='http:')
00072         self.assertEqual(d.getRemoteUrl(), '')
00073 
00074     def test_RFC822(self):
00075         d = self._makeOne('foo')
00076         d._writeFromPUT( body=BASIC_RFC822 )
00077 
00078         self.assertEqual( d.Title(), 'Zope Community' )
00079         self.assertEqual( d.Description()
00080                         , 'Link to the Zope Community website.' )
00081         self.assertEqual( len(d.Subject()), 3 )
00082         self.assertEqual( d.getRemoteUrl(), 'http://www.zope.org' )
00083 
00084     def test_RFC822_w_Continuation(self):
00085         d = self._makeOne('foo')
00086         d._writeFromPUT( body=RFC822_W_CONTINUATION )
00087         rnlinesplit = compile( r'\r?\n?' )
00088         desc_lines = rnlinesplit.split( d.Description() )
00089 
00090         self.assertEqual( d.Title(), 'Zope Community' )
00091         self.assertEqual( desc_lines[0]
00092                         , 'Link to the Zope Community website,' )
00093         self.assertEqual( desc_lines[1]
00094                         , ' including hundreds of contributed Zope products.' )
00095         self.assertEqual( len(d.Subject()), 3 )
00096         self.assertEqual( d.getRemoteUrl(), 'http://www.zope.org' )
00097 
00098     def test_PutWithoutMetadata(self):
00099         d = self._makeOne('foo')
00100         d._writeFromPUT( body='' )
00101         self.assertEqual( d.Title(), '' )
00102         self.assertEqual( d.Format(), 'text/url' )
00103         self.assertEqual( d.Description(), '' )
00104         self.assertEqual( d.Subject(), () )
00105         self.assertEqual( d.Contributors(), () )
00106         self.assertEqual( d.EffectiveDate(), 'None' )
00107         self.assertEqual( d.ExpirationDate(), 'None' )
00108         self.assertEqual( d.Language(), '' )
00109         self.assertEqual( d.Rights(), '' )
00110 
00111     def test_fixupMissingScheme(self):
00112         table = {
00113             'http://foo.com':      'http://foo.com',
00114             '//bar.com':           'http://bar.com',
00115             }
00116         self.canonTest(table)
00117 
00118     def test_keepRelativeUrl(self):
00119         table = {
00120             'baz.com':             'baz.com',
00121             'baz2.com/index.html': 'baz2.com/index.html',
00122             '/huh/zoinx.html':     '/huh/zoinx.html',
00123             'hmmm.com/lol.txt':    'hmmm.com/lol.txt',
00124             }
00125         self.canonTest(table)
00126 
00127     def test_trailingSlash(self):
00128         table = {
00129             'http://foo.com/bar/': 'http://foo.com/bar/',
00130             'baz.com/':            'baz.com/',
00131             '/baz.org/zoinx/':     '/baz.org/zoinx/',
00132             }
00133         self.canonTest(table)
00134 
00135     def test_otherScheme(self):
00136         table = {
00137             'mailto:user@foo.com':      'mailto:user@foo.com',
00138             'https://bank.com/account': 'https://bank.com/account',
00139             }
00140         self.canonTest(table)
00141 
00142 
00143 def test_suite():
00144     return unittest.TestSuite((
00145         unittest.makeSuite(LinkTests),
00146         ))
00147 
00148 if __name__ == '__main__':
00149     unittest.main(defaultTest='test_suite')