Back to index

plone3  3.1.7
test_view.py
Go to the documentation of this file.
00001 import unittest
00002 from plone.app.redirector.tests.base import RedirectorTestCase
00003 
00004 from zope.component import getUtility, getMultiAdapter
00005 from plone.app.redirector.interfaces import IRedirectionStorage
00006 
00007 class TestRedirectorView(RedirectorTestCase):
00008     """Ensure that the redirector view behaves as expected.
00009     """
00010 
00011     @property
00012     def storage(self):
00013         return getUtility(IRedirectionStorage)
00014 
00015     def view(self, context, actual_url):
00016         request = self.app.REQUEST
00017         request['ACTUAL_URL'] = actual_url
00018         return getMultiAdapter((context, request), name='plone_redirector_view')
00019 
00020     def test_attempt_redirect_with_known_url(self):
00021         fp = '/'.join(self.folder.getPhysicalPath())
00022         fu = self.folder.absolute_url()
00023         self.storage.add(fp + '/foo', fp + '/bar')
00024         view = self.view(self.portal, fu + '/foo')
00025         self.assertEquals(True, view.attempt_redirect())
00026         self.assertEquals(301, self.app.REQUEST.response.getStatus())
00027         self.assertEquals(fu + '/bar', self.app.REQUEST.response.getHeader('location'))
00028 
00029     def test_attempt_redirect_with_known_url_and_template(self):
00030         fp = '/'.join(self.folder.getPhysicalPath())
00031         fu = self.folder.absolute_url()
00032         self.storage.add(fp + '/foo', fp + '/bar')
00033         view = self.view(self.portal, fu + '/foo/view')
00034         self.assertEquals(True, view.attempt_redirect())
00035         self.assertEquals(301, self.app.REQUEST.response.getStatus())
00036         self.assertEquals(fu + '/bar/view', self.app.REQUEST.response.getHeader('location'))
00037 
00038     def test_attempt_redirect_with_unknown_url(self):
00039         fp = '/'.join(self.folder.getPhysicalPath())
00040         fu = self.folder.absolute_url()
00041         view = self.view(self.portal, fu + '/foo')
00042         self.assertEquals(False, view.attempt_redirect())
00043         self.assertNotEquals(301, self.app.REQUEST.response.getStatus())
00044 
00045     def test_find_first_parent_found_leaf(self):
00046         self.folder.invokeFactory('Folder', 'f1')
00047         fu = self.folder.absolute_url()
00048         view = self.view(self.portal, fu + '/f1/p1')
00049         obj = view.find_first_parent()
00050         self.assertEquals(fu + '/f1', obj.absolute_url())
00051 
00052     def test_find_first_parent_found_node(self):
00053         self.folder.invokeFactory('Folder', 'f1')
00054         fu = self.folder.absolute_url()
00055         view = self.view(self.portal, fu + '/f1/p1/p2')
00056         obj = view.find_first_parent()
00057         self.assertEquals(fu + '/f1', obj.absolute_url())
00058 
00059     def test_find_first_parent_not_found(self):
00060         view = self.view(self.portal, '/foo/f1/p1/p2')
00061         self.assertEquals(None, view.find_first_parent())
00062 
00063     def test_search_leaf(self):
00064         self.folder.invokeFactory('Folder', 'f1')
00065         self.folder.invokeFactory('Folder', 'f2')
00066         self.folder.f1.invokeFactory('Document', 'p1')
00067         self.folder.f1.invokeFactory('Document', 'p2')
00068         fu = self.folder.absolute_url()
00069         view = self.view(self.portal, fu + '/f2/p1')
00070         urls = sorted([b.getURL() for b in view.search_for_similar()])
00071         self.assertEquals(1, len(urls))
00072         self.assertEquals(fu + '/f1/p1', urls[0])
00073 
00074     def test_search_ignore_ids(self):
00075         self.folder.invokeFactory('Folder', 'f1')
00076         self.folder.invokeFactory('Folder', 'f2')
00077         self.folder.f1.invokeFactory('Document', 'p1')
00078         self.folder.f1.invokeFactory('Document', 'p2')
00079         self.folder.f1.invokeFactory('Document', 'p3', title='view')
00080         fu = self.folder.absolute_url()
00081         view = self.view(self.portal, fu + '/f2/p1/view')
00082         urls = sorted([b.getURL() for b in view.search_for_similar()])
00083         self.assertEquals(1, len(urls))
00084         self.assertEquals(fu + '/f1/p1', urls[0])
00085 
00086     def test_search_node(self):
00087         self.folder.invokeFactory('Folder', 'f1')
00088         self.folder.invokeFactory('Folder', 'f2')
00089         self.folder.f1.invokeFactory('Document', 'p1')
00090         self.folder.f1.invokeFactory('Document', 'p2')
00091         fu = self.folder.absolute_url()
00092         view = self.view(self.portal, fu + '/f2/p1/f3')
00093         urls = sorted([b.getURL() for b in view.search_for_similar()])
00094         self.assertEquals(1, len(urls))
00095         self.assertEquals(fu + '/f1/p1', urls[0])
00096 
00097     def test_search_blacklisted(self):
00098         self.folder.invokeFactory('Folder', 'f1')
00099         self.folder.invokeFactory('Folder', 'f2')
00100         self.folder.f1.invokeFactory('Document', 'p1')
00101         self.folder.f1.invokeFactory('Document', 'p2')
00102         fu = self.folder.absolute_url()
00103         self.portal.portal_properties.site_properties.types_not_searched = ['Document']
00104         view = self.view(self.portal, fu + '/f2/p1')
00105         urls = sorted([b.getURL() for b in view.search_for_similar()])
00106         self.assertEquals(1, len(urls))
00107         self.assertEquals(fu + '/f2', urls[0])
00108 
00109 def test_suite():
00110     suite = unittest.TestSuite()
00111     suite.addTest(unittest.makeSuite(TestRedirectorView))
00112     return suite