Back to index

plone3  3.1.7
testNavTree.py
Go to the documentation of this file.
00001 #
00002 # Tests the PloneTool
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 
00007 from Products.CMFCore.utils import getToolByName
00008 
00009 from plone.app.layout.navigation.interfaces import INavigationRoot
00010 
00011 from plone.app.layout.navigation.navtree import NavtreeStrategyBase
00012 from plone.app.layout.navigation.navtree import buildFolderTree
00013 from plone.app.layout.navigation.root import getNavigationRoot
00014 
00015 from Products.CMFPlone.browser.navtree import NavtreeQueryBuilder
00016 from Products.CMFPlone.browser.navtree import SitemapQueryBuilder
00017 from Products.CMFPlone.browser.navtree import SitemapNavtreeStrategy
00018 from Products.CMFPlone.browser.navtree import DefaultNavtreeStrategy
00019 
00020 from zope.interface import directlyProvides
00021 from zope.interface import implements
00022 
00023 default_user = PloneTestCase.default_user
00024 
00025 from Products.CMFPlone.PloneFolder import PloneFolder
00026 from Products.CMFPlone.interfaces import INonStructuralFolder
00027 
00028 class DummyNonStructuralFolder(PloneFolder):
00029     implements(INonStructuralFolder)
00030 
00031 
00032 class TestFolderTree(PloneTestCase.PloneTestCase):
00033     '''Test the basic buildFolderTree method'''
00034 
00035     def afterSetUp(self):
00036         self.populateSite()
00037         self.setupAuthenticator()
00038 
00039     def populateSite(self):
00040         """
00041             Portal
00042             +-doc1
00043             +-doc2
00044             +-doc3
00045             +-folder1
00046               +-doc11
00047               +-doc12
00048               +-doc13
00049             +-link1
00050             +-folder2
00051               +-doc21
00052               +-doc22
00053               +-doc23
00054               +-file21
00055               +-folder21
00056                 +-doc211
00057                 +-doc212
00058         """
00059         self.setRoles(['Manager'])
00060 
00061         for item in self.portal.getFolderContents():
00062             self.portal._delObject(item.getId)
00063 
00064         self.portal.invokeFactory('Document', 'doc1')
00065         self.portal.invokeFactory('Document', 'doc2')
00066         self.portal.invokeFactory('Document', 'doc3')
00067         self.portal.invokeFactory('Folder', 'folder1')
00068         self.portal.invokeFactory('Link', 'link1')
00069         self.portal.link1.setRemoteUrl('http://plone.org')
00070         self.portal.link1.reindexObject()
00071         folder1 = getattr(self.portal, 'folder1')
00072         folder1.invokeFactory('Document', 'doc11')
00073         folder1.invokeFactory('Document', 'doc12')
00074         folder1.invokeFactory('Document', 'doc13')
00075         self.portal.invokeFactory('Folder', 'folder2')
00076         folder2 = getattr(self.portal, 'folder2')
00077         folder2.invokeFactory('Document', 'doc21')
00078         folder2.invokeFactory('Document', 'doc22')
00079         folder2.invokeFactory('Document', 'doc23')
00080         folder2.invokeFactory('File', 'file21')
00081         folder2.invokeFactory('Folder', 'folder21')
00082         folder21 = getattr(folder2, 'folder21')
00083         folder21.invokeFactory('Document', 'doc211')
00084         folder21.invokeFactory('Document', 'doc212')
00085         self.setRoles(['Member'])
00086 
00087     # Get from the root, filters
00088 
00089     def testGetFromRoot(self):
00090         tree = buildFolderTree(self.portal)['children']
00091         rootPath = '/'.join(self.portal.getPhysicalPath())
00092         self.assertEqual(len(tree), 6)
00093         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/doc1')
00094         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/doc2')
00095         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/doc3')
00096         self.assertEqual(tree[3]['item'].getPath(), rootPath + '/folder1')
00097         self.assertEqual(len(tree[3]['children']), 3)
00098         self.assertEqual(tree[3]['children'][0]['item'].getPath(), rootPath + '/folder1/doc11')
00099         self.assertEqual(tree[3]['children'][1]['item'].getPath(), rootPath + '/folder1/doc12')
00100         self.assertEqual(tree[3]['children'][2]['item'].getPath(), rootPath + '/folder1/doc13')
00101         self.assertEqual(tree[4]['item'].getPath(), rootPath + '/link1')
00102         self.assertEqual(tree[5]['item'].getPath(), rootPath + '/folder2')
00103         self.assertEqual(len(tree[5]['children']), 5)
00104         self.assertEqual(tree[5]['children'][0]['item'].getPath(), rootPath + '/folder2/doc21')
00105         self.assertEqual(tree[5]['children'][1]['item'].getPath(), rootPath + '/folder2/doc22')
00106         self.assertEqual(tree[5]['children'][2]['item'].getPath(), rootPath + '/folder2/doc23')
00107         self.assertEqual(tree[5]['children'][3]['item'].getPath(), rootPath + '/folder2/file21')
00108         self.assertEqual(tree[5]['children'][4]['item'].getPath(), rootPath + '/folder2/folder21')
00109         self.assertEqual(len(tree[5]['children'][4]['children']), 2)
00110         self.assertEqual(tree[5]['children'][4]['children'][0]['item'].getPath(), rootPath + '/folder2/folder21/doc211')
00111         self.assertEqual(tree[5]['children'][4]['children'][1]['item'].getPath(), rootPath + '/folder2/folder21/doc212')
00112 
00113     def testGetFromRootWithSpecifiedRoot(self):
00114         rootPath = '/'.join(self.portal.getPhysicalPath())
00115         strategy = NavtreeStrategyBase()
00116         strategy.rootPath = rootPath + '/folder1'
00117         tree = buildFolderTree(self.portal, strategy=strategy)['children']
00118         self.assertEqual(len(tree), 3)
00119         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/folder1/doc11')
00120         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/folder1/doc12')
00121         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/folder1/doc13')
00122 
00123     def testGetFromRootWithNodeFilter(self):
00124         class Strategy(NavtreeStrategyBase):
00125             def nodeFilter(self, node):
00126                 return ('doc' not in node['item'].getId)
00127         tree = buildFolderTree(self.portal, strategy=Strategy())['children']
00128         rootPath = '/'.join(self.portal.getPhysicalPath())
00129         self.assertEqual(len(tree), 3)
00130         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/folder1')
00131         self.assertEqual(len(tree[0]['children']), 0)
00132         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/link1')
00133         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/folder2')
00134         self.assertEqual(len(tree[2]['children']), 2)
00135         self.assertEqual(tree[2]['children'][0]['item'].getPath(), rootPath + '/folder2/file21')
00136         self.assertEqual(tree[2]['children'][1]['item'].getPath(), rootPath + '/folder2/folder21')
00137         self.assertEqual(len(tree[2]['children'][1]['children']), 0)
00138 
00139     def testGetFromRootWithNodeFilterOnFolder(self):
00140         class Strategy(NavtreeStrategyBase):
00141             def nodeFilter(self, node):
00142                 return ('folder' not in node['item'].getId)
00143         tree = buildFolderTree(self.portal, strategy=Strategy())['children']
00144         rootPath = '/'.join(self.portal.getPhysicalPath())
00145         self.assertEqual(len(tree), 4)
00146         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/doc1')
00147         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/doc2')
00148         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/doc3')
00149         self.assertEqual(tree[3]['item'].getPath(), rootPath + '/link1')
00150 
00151     def testGetFromRootWithSubtreeFilter(self):
00152         class Strategy(NavtreeStrategyBase):
00153             def subtreeFilter(self, node):
00154                 return ('folder2' != node['item'].getId)
00155         tree = buildFolderTree(self.portal, strategy=Strategy())['children']
00156         rootPath = '/'.join(self.portal.getPhysicalPath())
00157         self.assertEqual(len(tree), 6)
00158         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/doc1')
00159         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/doc2')
00160         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/doc3')
00161         self.assertEqual(tree[3]['item'].getPath(), rootPath + '/folder1')
00162         self.assertEqual(len(tree[3]['children']), 3)
00163         self.assertEqual(tree[3]['children'][0]['item'].getPath(), rootPath + '/folder1/doc11')
00164         self.assertEqual(tree[3]['children'][1]['item'].getPath(), rootPath + '/folder1/doc12')
00165         self.assertEqual(tree[3]['children'][2]['item'].getPath(), rootPath + '/folder1/doc13')
00166         self.assertEqual(tree[4]['item'].getPath(), rootPath + '/link1')
00167         self.assertEqual(tree[5]['item'].getPath(), rootPath + '/folder2')
00168         self.assertEqual(len(tree[5]['children']), 0)
00169 
00170     def testNonFolderishObjectNotExpanded(self):
00171         self.setRoles(['Manager'])
00172         f = DummyNonStructuralFolder('ns_folder')
00173         self.portal._setObject('ns_folder', f)
00174         ns_folder = self.portal.ns_folder
00175         self.portal.portal_catalog.indexObject(self.portal.ns_folder)
00176         ns_folder.invokeFactory('Document', 'doc')
00177         self.setRoles(['Member'])
00178         tree = buildFolderTree(self.portal, self.portal.ns_folder)['children']
00179         rootPath = '/'.join(self.portal.getPhysicalPath())
00180         self.assertEqual(tree[-1]['item'].getPath(), rootPath + '/ns_folder')
00181         self.assertEqual(len(tree[-1]['children']), 0)
00182 
00183     def testShowAllParentsOverridesNonFolderishObjectNotExpanded(self):
00184         strategy = NavtreeStrategyBase()
00185         strategy.showAllParents = True
00186         self.setRoles(['Manager'])
00187         f = DummyNonStructuralFolder('ns_folder')
00188         self.portal._setObject('ns_folder', f)
00189         ns_folder = self.portal.ns_folder
00190         self.portal.portal_catalog.indexObject(self.portal.ns_folder)
00191         ns_folder.invokeFactory('Document', 'doc')
00192         self.setRoles(['Member'])
00193         tree = buildFolderTree(self.portal, self.portal.ns_folder.doc, strategy=strategy)['children']
00194         rootPath = '/'.join(self.portal.getPhysicalPath())
00195         self.assertEqual(tree[-1]['item'].getPath(), rootPath + '/ns_folder')
00196         self.assertEqual(len(tree[-1]['children']), 1)
00197         self.assertEqual(tree[-1]['children'][0]['item'].getPath(), rootPath + '/ns_folder/doc')
00198 
00199     def testGetWithRootContext(self):
00200         tree = buildFolderTree(self.portal, obj=self.portal)['children']
00201         rootPath = '/'.join(self.portal.getPhysicalPath())
00202         self.assertEqual(len(tree), 6)
00203         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/doc1')
00204         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/doc2')
00205         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/doc3')
00206         self.assertEqual(tree[3]['item'].getPath(), rootPath + '/folder1')
00207         self.assertEqual(len(tree[3]['children']), 3)
00208         self.assertEqual(tree[3]['children'][0]['item'].getPath(), rootPath + '/folder1/doc11')
00209         self.assertEqual(tree[3]['children'][1]['item'].getPath(), rootPath + '/folder1/doc12')
00210         self.assertEqual(tree[3]['children'][2]['item'].getPath(), rootPath + '/folder1/doc13')
00211         self.assertEqual(tree[4]['item'].getPath(), rootPath + '/link1')
00212         self.assertEqual(tree[5]['item'].getPath(), rootPath + '/folder2')
00213         self.assertEqual(len(tree[5]['children']), 5)
00214         self.assertEqual(tree[5]['children'][0]['item'].getPath(), rootPath + '/folder2/doc21')
00215         self.assertEqual(tree[5]['children'][1]['item'].getPath(), rootPath + '/folder2/doc22')
00216         self.assertEqual(tree[5]['children'][2]['item'].getPath(), rootPath + '/folder2/doc23')
00217         self.assertEqual(tree[5]['children'][3]['item'].getPath(), rootPath + '/folder2/file21')
00218         self.assertEqual(tree[5]['children'][4]['item'].getPath(), rootPath + '/folder2/folder21')
00219         self.assertEqual(len(tree[5]['children'][4]['children']), 2)
00220         self.assertEqual(tree[5]['children'][4]['children'][0]['item'].getPath(), rootPath + '/folder2/folder21/doc211')
00221         self.assertEqual(tree[5]['children'][4]['children'][1]['item'].getPath(), rootPath + '/folder2/folder21/doc212')
00222 
00223     def testGetFromFixed(self):
00224         rootPath = '/'.join(self.portal.getPhysicalPath())
00225         query = {'path' : rootPath + '/folder1'}
00226         tree = buildFolderTree(self.portal, query=query)['children']
00227         self.assertEqual(len(tree), 3)
00228         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/folder1/doc11')
00229         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/folder1/doc12')
00230         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/folder1/doc13')
00231 
00232     def testGetFromFixedAndDepth(self):
00233         rootPath = '/'.join(self.portal.getPhysicalPath())
00234         query = {'path' : rootPath + '/folder2', 'depth' : 1}
00235         tree = buildFolderTree(self.portal, query=query)['children']
00236         self.assertEqual(len(tree), 5)
00237         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/folder2/doc21')
00238         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/folder2/doc22')
00239         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/folder2/doc23')
00240         self.assertEqual(tree[3]['item'].getPath(), rootPath + '/folder2/file21')
00241         self.assertEqual(tree[4]['item'].getPath(), rootPath + '/folder2/folder21')
00242 
00243     def testGetFromRootWithCurrent(self):
00244         context = self.portal.folder2.doc21
00245         tree = buildFolderTree(self.portal, obj=context)['children']
00246         self.assertEqual(len(tree), 6)
00247         for t in tree:
00248             if t['item'].getId == 'folder2':
00249                 self.assertEqual(t['currentItem'], False)
00250                 self.assertEqual(t['currentParent'], True)
00251                 for c in t['children']:
00252                     if c['item'].getId == 'doc21':
00253                         self.assertEqual(c['currentItem'], True)
00254                         self.assertEqual(c['currentParent'], False)
00255                     else:
00256                         self.assertEqual(c['currentItem'], False)
00257                         self.assertEqual(c['currentParent'], False)
00258             else:
00259                 self.assertEqual(t['currentItem'], False)
00260                 self.assertEqual(t['currentParent'], False)
00261 
00262     def testGetFromRootIgnoresDefaultPages(self):
00263         self.portal.folder1.setDefaultPage('doc12')
00264         self.portal.folder2.setDefaultPage('doc21')
00265         tree = buildFolderTree(self.portal)['children']
00266         rootPath = '/'.join(self.portal.getPhysicalPath())
00267         self.assertEqual(len(tree), 6)
00268         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/doc1')
00269         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/doc2')
00270         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/doc3')
00271         self.assertEqual(tree[3]['item'].getPath(), rootPath + '/folder1')
00272         self.assertEqual(len(tree[3]['children']), 2)
00273         self.assertEqual(tree[3]['children'][0]['item'].getPath(), rootPath + '/folder1/doc11')
00274         self.assertEqual(tree[3]['children'][1]['item'].getPath(), rootPath + '/folder1/doc13')
00275         self.assertEqual(tree[4]['item'].getPath(), rootPath + '/link1')
00276         self.assertEqual(tree[5]['item'].getPath(), rootPath + '/folder2')
00277         self.assertEqual(len(tree[5]['children']), 4)
00278         self.assertEqual(tree[5]['children'][0]['item'].getPath(), rootPath + '/folder2/doc22')
00279         self.assertEqual(tree[5]['children'][1]['item'].getPath(), rootPath + '/folder2/doc23')
00280         self.assertEqual(tree[5]['children'][2]['item'].getPath(), rootPath + '/folder2/file21')
00281         self.assertEqual(tree[5]['children'][3]['item'].getPath(), rootPath + '/folder2/folder21')
00282         self.assertEqual(len(tree[5]['children'][3]['children']), 2)
00283         self.assertEqual(tree[5]['children'][3]['children'][0]['item'].getPath(), rootPath + '/folder2/folder21/doc211')
00284         self.assertEqual(tree[5]['children'][3]['children'][1]['item'].getPath(), rootPath + '/folder2/folder21/doc212')
00285 
00286     def testGetFromRootWithCurrentIsDefaultPage(self):
00287         self.portal.folder2.setDefaultPage('doc21')
00288         context = self.portal.folder2.doc21
00289         tree = buildFolderTree(self.portal, obj=context)['children']
00290         for t in tree:
00291             if t['item'].getId == 'folder2':
00292                 self.assertEqual(t['currentItem'], True)
00293                 self.assertEqual(t['currentParent'], False)
00294                 for c in t['children']:
00295                     self.assertEqual(c['currentItem'], False)
00296                     self.assertEqual(c['currentParent'], False)
00297             else:
00298                 self.assertEqual(t['currentItem'], False)
00299                 self.assertEqual(t['currentParent'], False)
00300 
00301     def testGetFromRootWithCustomQuery(self):
00302         query = {'portal_type' : 'Document'}
00303         tree = buildFolderTree(self.portal, query=query)['children']
00304         rootPath = '/'.join(self.portal.getPhysicalPath())
00305         self.assertEqual(len(tree), 3)
00306         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/doc1')
00307         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/doc2')
00308         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/doc3')
00309 
00310     def testGetFromRootWithDecoratorFactory(self):
00311         class Strategy(NavtreeStrategyBase):
00312             def decoratorFactory(self, node):
00313                 node['foo'] = True
00314                 return node
00315         tree = buildFolderTree(self.portal, strategy=Strategy())['children']
00316         self.assertEqual(tree[0]['foo'], True)
00317 
00318     def testShowAllParents(self):
00319         strategy = NavtreeStrategyBase()
00320         strategy.showAllParents = True
00321         query = {'portal_type' : 'Folder'}
00322         context = self.portal.folder1.doc11
00323         tree = buildFolderTree(self.portal, query=query, obj=context, strategy=strategy)['children']
00324         rootPath = '/'.join(self.portal.getPhysicalPath())
00325         self.assertEqual(len(tree), 2)
00326         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/folder1')
00327         self.assertEqual(len(tree[0]['children']), 1)
00328         self.assertEqual(tree[0]['children'][0]['item'].getPath(), rootPath + '/folder1/doc11')
00329         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/folder2')
00330         self.assertEqual(len(tree[1]['children']), 1)
00331         self.assertEqual(tree[1]['children'][0]['item'].getPath(), rootPath + '/folder2/folder21')
00332 
00333     def testShowAllParentsWithRestrictedParent(self):
00334         strategy = NavtreeStrategyBase()
00335         strategy.showAllParents = True
00336         wftool = getToolByName(self.portal, 'portal_workflow')
00337         wftool.doActionFor(self.portal.folder1, 'hide')
00338         self.portal.folder1.reindexObject()
00339         query = {'portal_type' : 'Folder'}
00340         context = self.portal.folder1.doc11
00341         tree = buildFolderTree(self.portal, query=query, obj=context, strategy=strategy)['children']
00342         rootPath = '/'.join(self.portal.getPhysicalPath())
00343         self.assertEqual(len(tree), 2)
00344         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/folder1')
00345         self.assertEqual(len(tree[0]['children']), 1)
00346         self.assertEqual(tree[0]['children'][0]['item'].getPath(), rootPath + '/folder1/doc11')
00347         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/folder2')
00348         self.assertEqual(len(tree[1]['children']), 1)
00349         self.assertEqual(tree[1]['children'][0]['item'].getPath(), rootPath + '/folder2/folder21')
00350 
00351     def testShowAllParentsWithParentNotInCatalog(self):
00352         strategy = NavtreeStrategyBase()
00353         strategy.showAllParents = True
00354         wftool = getToolByName(self.portal, 'portal_workflow')
00355         self.portal.folder1.unindexObject()
00356         query = {'portal_type' : 'Folder'}
00357         context = self.portal.folder1.doc11
00358         tree = buildFolderTree(self.portal, query=query, obj=context, strategy=strategy)['children']
00359         rootPath = '/'.join(self.portal.getPhysicalPath())
00360         # XXX: Ideally, this shouldn't happen, we should get a dummy node, but
00361         # there's no way to do that with the catalog
00362         self.assertEqual(len(tree), 1)
00363         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/folder2')
00364 
00365     def testDontShowAllParents(self):
00366         strategy = NavtreeStrategyBase()
00367         strategy.showAllParents = False
00368         query = {'portal_type' : 'Folder'}
00369         context = self.portal.folder1.doc11
00370         tree = buildFolderTree(self.portal, query=query, obj=context, strategy=strategy)['children']
00371         rootPath = '/'.join(self.portal.getPhysicalPath())
00372         self.assertEqual(len(tree), 2)
00373         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/folder1')
00374         self.assertEqual(len(tree[0]['children']), 0)
00375         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/folder2')
00376         self.assertEqual(len(tree[1]['children']), 1)
00377         self.assertEqual(tree[1]['children'][0]['item'].getPath(), rootPath + '/folder2/folder21')
00378 
00379     def testGetFromRootWithCurrentNavtree(self):
00380         context = self.portal.folder1.doc11
00381         query = {'path' : {'query' : '/'.join(context.getPhysicalPath()),
00382                            'navtree' : 1}}
00383         tree = buildFolderTree(self.portal, query=query)['children']
00384         rootPath = '/'.join(self.portal.getPhysicalPath())
00385         self.assertEqual(len(tree), 6)
00386         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/doc1')
00387         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/doc2')
00388         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/doc3')
00389         self.assertEqual(tree[3]['item'].getPath(), rootPath + '/folder1')
00390         self.assertEqual(len(tree[3]['children']), 3)
00391         self.assertEqual(tree[3]['children'][0]['item'].getPath(), rootPath + '/folder1/doc11')
00392         self.assertEqual(tree[3]['children'][1]['item'].getPath(), rootPath + '/folder1/doc12')
00393         self.assertEqual(tree[3]['children'][2]['item'].getPath(), rootPath + '/folder1/doc13')
00394         self.assertEqual(tree[4]['item'].getPath(), rootPath + '/link1')
00395         self.assertEqual(tree[5]['item'].getPath(), rootPath + '/folder2')
00396         self.assertEqual(len(tree[5]['children']), 0)
00397 
00398     def testGetFromRootWithCurrentNavtreeAndStartLevel(self):
00399         context = self.portal.folder1.doc11
00400         query = {'path' : {'query' : '/'.join(context.getPhysicalPath()),
00401                            'navtree' : 1,
00402                            'navtree_start' : 2}}
00403         rootPath = '/'.join(self.portal.getPhysicalPath())
00404         tree = buildFolderTree(self.portal, query=query)['children']
00405         self.assertEqual(len(tree), 3)
00406         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/folder1/doc11')
00407         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/folder1/doc12')
00408         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/folder1/doc13')
00409 
00410     def testGetFromRootWithCurrentNavtreePruned(self):
00411         context = self.portal.folder1.doc11
00412         class Strategy(NavtreeStrategyBase):
00413             def subtreeFilter(self, node):
00414                 return (node['item'].getId != 'folder1')
00415             showAllParents = True
00416 
00417         query = {'path' : {'query' : '/'.join(context.getPhysicalPath()),
00418                            'navtree' : 1}}
00419         rootPath = '/'.join(self.portal.getPhysicalPath())
00420         tree = buildFolderTree(self.portal, query=query, obj=context, strategy=Strategy())['children']
00421         self.assertEqual(len(tree), 6)
00422         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/doc1')
00423         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/doc2')
00424         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/doc3')
00425         self.assertEqual(tree[3]['item'].getPath(), rootPath + '/folder1')
00426         self.assertEqual(len(tree[3]['children']), 1)
00427         self.assertEqual(tree[3]['children'][0]['item'].getPath(), rootPath + '/folder1/doc11')
00428         self.assertEqual(tree[4]['item'].getPath(), rootPath + '/link1')
00429         self.assertEqual(tree[5]['item'].getPath(), rootPath + '/folder2')
00430         self.assertEqual(len(tree[5]['children']), 0)
00431 
00432     def testGetFromRootWithCurrentFolderishNavtreePruned(self):
00433         context = self.portal.folder2.folder21
00434         class Strategy(NavtreeStrategyBase):
00435             def subtreeFilter(self, node):
00436                 return (node['item'].getId != 'folder2')
00437             showAllParents=True
00438 
00439         query = {'path' : {'query' : '/'.join(context.getPhysicalPath()),
00440                            'navtree' : 1}}
00441         rootPath = '/'.join(self.portal.getPhysicalPath())
00442         tree = buildFolderTree(self.portal, query=query, obj=context, strategy=Strategy())['children']
00443         self.assertEqual(len(tree), 6)
00444         self.assertEqual(tree[0]['item'].getPath(), rootPath + '/doc1')
00445         self.assertEqual(tree[1]['item'].getPath(), rootPath + '/doc2')
00446         self.assertEqual(tree[2]['item'].getPath(), rootPath + '/doc3')
00447         self.assertEqual(tree[3]['item'].getPath(), rootPath + '/folder1')
00448         self.assertEqual(len(tree[3]['children']), 0)
00449         self.assertEqual(tree[4]['item'].getPath(), rootPath + '/link1')
00450         self.assertEqual(tree[5]['item'].getPath(), rootPath + '/folder2')
00451         self.assertEqual(len(tree[5]['children']), 1)
00452         self.assertEqual(tree[5]['children'][0]['item'].getPath(), rootPath + '/folder2/folder21')
00453         self.assertEqual(len(tree[5]['children'][0]['children']), 2)
00454         self.assertEqual(tree[5]['children'][0]['children'][0]['item'].getPath(), rootPath + '/folder2/folder21/doc211')
00455         self.assertEqual(tree[5]['children'][0]['children'][1]['item'].getPath(), rootPath + '/folder2/folder21/doc212')
00456 
00457     def testCurrentParent(self):
00458         self.loginAsPortalOwner()
00459         self.portal.invokeFactory('Document', 'doc')
00460         context = self.portal.doc1
00461         tree = buildFolderTree(self.portal, obj=context)['children']
00462         for t in tree:
00463             if t['item'].getId == 'doc':
00464                 self.assertEqual(t['currentParent'], False)
00465 
00466 class TestNavigationRoot(PloneTestCase.PloneTestCase):
00467 
00468     def testGetNavigationRootPropertyNotSet(self):
00469         self.portal.portal_properties.navtree_properties._delProperty('root')
00470         root = getNavigationRoot(self.portal)
00471         self.assertEqual(root, '/'.join(self.portal.getPhysicalPath()))
00472 
00473     def testGetNavigationRootPropertyEmptyNoVirtualHost(self):
00474         self.portal.portal_properties.navtree_properties.manage_changeProperties(root='')
00475         root = getNavigationRoot(self.portal)
00476         self.assertEqual(root, '/'.join(self.portal.getPhysicalPath()))
00477 
00478     # XXX: If we re-enable this in PloneTool, we should fix up this test
00479     # def testGetNavigationRootPropertyEmptyWithVirtualHost(self):
00480     #    self.fail('Test missing (see Sprout, which this code was stolen from)')
00481 
00482     def testGetNavigationRootPropertyIsRoot(self):
00483         self.portal.portal_properties.navtree_properties.manage_changeProperties(root='/')
00484         root = getNavigationRoot(self.portal)
00485         self.assertEqual(root, '/'.join(self.portal.getPhysicalPath()))
00486 
00487     def testGetNavigationRootPropertyIsFolder(self):
00488         folderPath = '/'.join(self.folder.getPhysicalPath())
00489         portalPath = '/'.join(self.portal.getPhysicalPath())
00490         relativePath = folderPath[len(portalPath):]
00491         self.portal.portal_properties.navtree_properties.manage_changeProperties(root=relativePath)
00492         root = getNavigationRoot(self.portal)
00493         self.assertEqual(root, folderPath)
00494         
00495     def testGetNavigationRootWithINavigationRoot(self):
00496         folderPath = '/'.join(self.folder.getPhysicalPath())
00497         self.folder.invokeFactory('Folder', 'folder1')
00498         self.folder.folder1.invokeFactory('Document', 'doc1')
00499         directlyProvides(self.folder, INavigationRoot)
00500         root = getNavigationRoot(self.folder.folder1.doc1)
00501         self.assertEqual(root, folderPath)
00502         
00503 
00504 def test_suite():
00505     from unittest import TestSuite, makeSuite
00506     suite = TestSuite()
00507     suite.addTest(makeSuite(TestFolderTree))
00508     suite.addTest(makeSuite(TestNavigationRoot))
00509     return suite