Back to index

plone3  3.1.7
testKSSRegistry.py
Go to the documentation of this file.
00001 #
00002 # CSSRegistry Tests
00003 #
00004 from zope.component import getMultiAdapter
00005 from zope.contentprovider.interfaces import IContentProvider
00006 
00007 from App.Common import rfc1123_date
00008 from DateTime import DateTime
00009 from AccessControl import Unauthorized
00010 from zope.interface.verify import verifyObject
00011 
00012 from Products.CMFCore.utils import getToolByName
00013 
00014 from Products.PloneTestCase.PloneTestCase import PLONE21, portal_owner, default_password
00015 
00016 from Products.ResourceRegistries.config import KSSTOOLNAME
00017 from Products.ResourceRegistries.interfaces import IKSSRegistry
00018 from Products.ResourceRegistries.tests.RegistryTestCase import RegistryTestCase
00019 from Products.ResourceRegistries.tests.RegistryTestCase import FunctionalRegistryTestCase
00020 
00021 
00022 class TestImplementation(RegistryTestCase):
00023 
00024     def test_interfaces(self):
00025         tool = getattr(self.portal, KSSTOOLNAME)
00026         self.failUnless(IKSSRegistry.providedBy(tool))
00027         self.failUnless(verifyObject(IKSSRegistry, tool))
00028 
00029 class TestTool(RegistryTestCase):
00030 
00031     def testToolExists(self):
00032         self.failUnless(KSSTOOLNAME in self.portal.objectIds())
00033 
00034     def testZMIForm(self):
00035         tool = getattr(self.portal, KSSTOOLNAME)
00036         self.setRoles(['Manager'])
00037         self.failUnless(tool.manage_kssForm())
00038         self.failUnless(tool.manage_kssComposition())
00039 
00040 
00041 class testZMIMethods(RegistryTestCase):
00042 
00043     def afterSetUp(self):
00044         self.tool = getattr(self.portal, KSSTOOLNAME)
00045         self.tool.clearResources()
00046 
00047     def testAdd(self):
00048         self.tool.manage_addKineticStylesheet(id='joe')
00049         self.assertEqual(len(self.tool.getResources()), 1)
00050         self.failUnless(self.tool.getResources())
00051 
00052 
00053 class TestKineticStylesheetRegistration(RegistryTestCase):
00054 
00055     def afterSetUp(self):
00056         self.tool = getattr(self.portal, KSSTOOLNAME)
00057         self.tool.clearResources()
00058 
00059     def testStoringKineticStylesheet(self):
00060         self.tool.registerKineticStylesheet('foo')
00061         self.assertEqual(len(self.tool.getResources()), 1)
00062         self.assertEqual(self.tool.getResources()[0].getId(), 'foo')
00063 
00064     def testDefaultKineticStylesheetAttributes(self):
00065         self.tool.registerKineticStylesheet('foodefault')
00066         self.assertEqual(self.tool.getResources()[0].getId(), 'foodefault')
00067         self.assertEqual(self.tool.getResources()[0].getExpression(), '')
00068         self.failUnless(self.tool.getResources()[0].getEnabled())
00069 
00070     def testKineticStylesheetAttributes(self):
00071         self.tool.registerKineticStylesheet('foo', expression='python:1',
00072                                             enabled=0)
00073         self.assertEqual(self.tool.getResources()[0].getId(), 'foo')
00074         self.assertEqual(self.tool.getResources()[0].getExpression(), 'python:1')
00075         self.failIf(self.tool.getResources()[0].getEnabled())
00076 
00077     def testDisallowingDuplicateIds(self):
00078         self.tool.registerKineticStylesheet('foo')
00079         self.assertRaises(ValueError, self.tool.registerKineticStylesheet, 'foo')
00080 
00081     def testUnregisterKineticStylesheet(self):
00082         self.tool.registerKineticStylesheet('foo')
00083         self.assertEqual(len(self.tool.getResources()), 1)
00084         self.assertEqual(self.tool.getResources()[0].getId(), 'foo')
00085         self.tool.unregisterResource('foo')
00086         self.assertEqual(len(self.tool.getResources()), 0)
00087 
00088     def testKineticStylesheetsDict(self):
00089         self.tool.registerKineticStylesheet('spam')
00090         self.tool.registerKineticStylesheet('ham')
00091         keys = self.tool.getResourcesDict().keys()
00092         keys.sort()
00093         res = ['ham', 'spam']
00094         res.sort()
00095         self.assertEqual(res, keys)
00096         self.assertEqual(self.tool.getResourcesDict()['ham'].getId(), 'ham')
00097 
00098 
00099 class TestKineticStylesheetRenaming(RegistryTestCase):
00100 
00101     def afterSetUp(self):
00102         self.tool = getattr(self.portal, KSSTOOLNAME)
00103         self.tool.clearResources()
00104 
00105     def testRenaming(self):
00106         self.tool.registerKineticStylesheet('ham')
00107         self.tool.registerKineticStylesheet('spam')
00108         self.tool.registerKineticStylesheet('eggs')
00109         self.assertEqual(self.tool.concatenatedresources[
00110                          self.tool.cookedresources[0].getId()],
00111                          ['ham', 'spam', 'eggs'])
00112         self.tool.renameResource('spam', 'bacon')
00113         self.assertEqual(self.tool.concatenatedresources[
00114                          self.tool.cookedresources[0].getId()],
00115                          ['ham', 'bacon', 'eggs'])
00116 
00117     def testRenamingIdClash(self):
00118         self.tool.registerKineticStylesheet('ham')
00119         self.tool.registerKineticStylesheet('spam')
00120         self.tool.registerKineticStylesheet('eggs')
00121         self.assertRaises(ValueError, self.tool.renameResource, 'spam', 'eggs')
00122 
00123     def testDoubleRenaming(self):
00124         self.tool.registerKineticStylesheet('ham')
00125         self.tool.registerKineticStylesheet('spam')
00126         self.tool.registerKineticStylesheet('eggs')
00127         self.tool.renameResource('spam', 'bacon')
00128         self.assertRaises(ValueError, self.tool.renameResource, 'spam', 'bacon')
00129 
00130 
00131 class TestToolSecurity(RegistryTestCase):
00132 
00133     def afterSetUp(self):
00134         self.tool = getattr(self.portal, KSSTOOLNAME)
00135         self.tool.clearResources()
00136 
00137     def testRegistrationSecurity(self):
00138         self.assertRaises(Unauthorized, self.tool.restrictedTraverse,
00139                           'registerKineticStylesheet')
00140         self.assertRaises(Unauthorized, self.tool.restrictedTraverse,
00141                           'unregisterResource')
00142         self.setRoles(['Manager'])
00143         try:
00144             self.tool.restrictedTraverse('registerKineticStylesheet')
00145             self.tool.restrictedTraverse('unregisterResource')
00146         except Unauthorized:
00147             self.fail()
00148 
00149 
00150 class TestToolExpression(RegistryTestCase):
00151 
00152     def afterSetUp(self):
00153         self.tool = getattr(self.portal, KSSTOOLNAME)
00154         self.tool.clearResources()
00155 
00156     def testSimplestExpression(self):
00157         context = self.portal
00158         self.failUnless(self.tool.evaluateExpression('python:1', context))
00159         self.failIf(self.tool.evaluateExpression('python:0', context))
00160         self.failUnless(self.tool.evaluateExpression('python:0+1', context))
00161 
00162     def testNormalExpression(self):
00163         context = self.portal
00164         self.failUnless(self.tool.evaluateExpression('object/absolute_url',
00165                                                      context))
00166 
00167     def testExpressionInFolder(self):
00168         self.folder.invokeFactory('Document', 'eggs')
00169         context = self.folder
00170         self.failUnless(self.tool.evaluateExpression(
00171                         'python:"eggs" in object.objectIds()', context))
00172 
00173 
00174 class TestKineticStylesheetCooking(RegistryTestCase):
00175 
00176     def afterSetUp(self):
00177         self.tool = getattr(self.portal, KSSTOOLNAME)
00178         self.tool.clearResources()
00179 
00180     def testKineticStylesheetCooking(self):
00181         self.tool.registerKineticStylesheet('ham')
00182         self.tool.registerKineticStylesheet('spam')
00183         self.tool.registerKineticStylesheet('eggs')
00184         self.assertEqual(len(self.tool.getResources()), 3)
00185         self.assertEqual(len(self.tool.cookedresources), 1)
00186         self.assertEqual(len(self.tool.concatenatedresources.keys()), 4)
00187         self.tool.setAutoGroupingMode(True)
00188         self.assertEqual(len(self.tool.getResources()), 3)
00189         self.assertEqual(len(self.tool.cookedresources), 1)
00190         self.assertEqual(len(self.tool.concatenatedresources.keys()), 4)
00191 
00192     def testKineticStylesheetCookingValues(self):
00193         self.tool.registerKineticStylesheet('ham')
00194         self.tool.registerKineticStylesheet('spam')
00195         self.tool.registerKineticStylesheet('eggs')
00196         self.assertEqual(self.tool.concatenatedresources[
00197                          self.tool.cookedresources[0].getId()],
00198                          ['ham', 'spam', 'eggs'])
00199 
00200     def testGetEvaluatedKineticStylesheetsCollapsing(self):
00201         self.tool.registerKineticStylesheet('ham')
00202         self.tool.registerKineticStylesheet('spam')
00203         self.tool.registerKineticStylesheet('eggs')
00204         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 1)
00205 
00206     def testMoreComplexKineticStylesheetsCollapsing(self):
00207         self.tool.registerKineticStylesheet('ham')
00208         self.tool.registerKineticStylesheet('spam', expression='string:spam')
00209         self.tool.registerKineticStylesheet('spam spam', expression='string:spam')
00210         self.tool.registerKineticStylesheet('spam spam spam', expression='string:spam')
00211         self.tool.registerKineticStylesheet('eggs')
00212         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 3)
00213         magic_ids = [item.getId() for item in self.tool.getEvaluatedResources(self.folder)]
00214         self.failUnless('ham' in self.tool.concatenatedresources[magic_ids[0]])
00215         self.failUnless('eggs' in self.tool.concatenatedresources[magic_ids[2]])
00216         self.failUnless('spam' in self.tool.concatenatedresources[magic_ids[1]])
00217         self.failUnless('spam spam' in self.tool.concatenatedresources[magic_ids[1]])
00218         self.failUnless('spam spam spam' in self.tool.concatenatedresources[magic_ids[1]])
00219 
00220         self.tool.setAutoGroupingMode(True)
00221         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 2)
00222         magic_ids = [item.getId() for item in self.tool.getEvaluatedResources(self.folder)]
00223         self.failUnless('ham' in self.tool.concatenatedresources[magic_ids[0]])
00224         self.failUnless('eggs' in self.tool.concatenatedresources[magic_ids[0]])
00225         self.failUnless('spam' in self.tool.concatenatedresources[magic_ids[1]])
00226         self.failUnless('spam spam' in self.tool.concatenatedresources[magic_ids[1]])
00227         self.failUnless('spam spam spam' in self.tool.concatenatedresources[magic_ids[1]])
00228 
00229     def testGetEvaluatedKineticStylesheetsWithExpression(self):
00230         self.tool.registerKineticStylesheet('ham')
00231         self.tool.registerKineticStylesheet('spam', expression='python:1')
00232         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 2)
00233         self.tool.setAutoGroupingMode(True)
00234         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 2)
00235 
00236     def testGetEvaluatedKineticStylesheetsWithFailingExpression(self):
00237         self.tool.registerKineticStylesheet('ham')
00238         self.tool.registerKineticStylesheet('spam', expression='python:0')
00239         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 1)
00240         self.tool.setAutoGroupingMode(True)
00241         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 1)
00242 
00243     def testGetEvaluatedKineticStylesheetsWithContextualExpression(self):
00244         self.folder.invokeFactory('Document', 'eggs')
00245         self.tool.registerKineticStylesheet('spam', expression='python:"eggs" in object.objectIds()')
00246         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 1)
00247         self.tool.setAutoGroupingMode(True)
00248         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 1)
00249 
00250     def testCollapsingKineticStylesheetsLookup(self):
00251         self.tool.registerKineticStylesheet('ham')
00252         self.tool.registerKineticStylesheet('spam', expression='string:ham')
00253         self.tool.registerKineticStylesheet('spam spam', expression='string:ham')
00254         evaluated = self.tool.getEvaluatedResources(self.folder)
00255         self.assertEqual(len(evaluated), 2)
00256         self.tool.setAutoGroupingMode(True)
00257         evaluated = self.tool.getEvaluatedResources(self.folder)
00258         self.assertEqual(len(evaluated), 2)
00259 
00260     def testRenderingIsInTheRightOrder(self):
00261         self.tool.registerKineticStylesheet('ham', expression='string:ham')
00262         self.tool.registerKineticStylesheet('spam', expression='string:spam')
00263         evaluated = self.tool.getEvaluatedResources(self.folder)
00264         magic_ids = [item.getId() for item in evaluated]
00265         ids = []
00266         for magic_id in magic_ids:
00267             self.assertEqual(len(self.tool.concatenatedresources[magic_id]), 1)
00268             ids.append(self.tool.concatenatedresources[magic_id][0])
00269         self.failUnless(ids[0] == 'ham')
00270         self.failUnless(ids[1] == 'spam')
00271 
00272     def testConcatenatedSheetsAreInTheRightOrderToo(self):
00273         self.tool.registerKineticStylesheet('ham')
00274         self.tool.registerKineticStylesheet('spam')
00275         self.tool.registerKineticStylesheet('eggs')
00276         evaluated = self.tool.getEvaluatedResources(self.folder)
00277         results = self.tool.concatenatedresources[evaluated[0].getId()]
00278         self.failUnless(results[0] == 'ham')
00279         self.failUnless(results[1] == 'spam')
00280         self.failUnless(results[2] == 'eggs')
00281         self.tool.setAutoGroupingMode(True)
00282         evaluated = self.tool.getEvaluatedResources(self.folder)
00283         results = self.tool.concatenatedresources[evaluated[0].getId()]
00284         self.failUnless(results[0] == 'ham')
00285         self.failUnless(results[1] == 'spam')
00286         self.failUnless(results[2] == 'eggs')
00287 
00288 
00289 class TestKineticStylesheetMoving(RegistryTestCase):
00290 
00291     def afterSetUp(self):
00292         self.tool = getattr(self.portal, KSSTOOLNAME)
00293         self.tool.clearResources()
00294 
00295     def testKineticStylesheetMoveDown(self):
00296         self.tool.registerKineticStylesheet('ham')
00297         self.tool.registerKineticStylesheet('spam')
00298         self.tool.registerKineticStylesheet('eggs')
00299         self.assertEqual(self.tool.getResourceIds(),
00300                          ('ham', 'spam', 'eggs'))
00301         self.tool.moveResourceDown('spam')
00302         self.assertEqual(self.tool.getResourceIds(),
00303                          ('ham', 'eggs', 'spam'))
00304 
00305     def testKineticStylesheetMoveDownAtEnd(self):
00306         self.tool.registerKineticStylesheet('ham')
00307         self.tool.registerKineticStylesheet('spam')
00308         self.tool.registerKineticStylesheet('eggs')
00309         self.assertEqual(self.tool.getResourceIds(),
00310                          ('ham', 'spam', 'eggs'))
00311         self.tool.moveResourceDown('eggs')
00312         self.assertEqual(self.tool.getResourceIds(),
00313                          ('ham', 'spam', 'eggs'))
00314 
00315     def testKineticStylesheetMoveUp(self):
00316         self.tool.registerKineticStylesheet('ham')
00317         self.tool.registerKineticStylesheet('spam')
00318         self.tool.registerKineticStylesheet('eggs')
00319         self.assertEqual(self.tool.getResourceIds(),
00320                          ('ham', 'spam', 'eggs'))
00321         self.tool.moveResourceUp('spam')
00322         self.assertEqual(self.tool.getResourceIds(),
00323                          ('spam', 'ham', 'eggs'))
00324 
00325     def testKineticStylesheetMoveUpAtStart(self):
00326         self.tool.registerKineticStylesheet('ham')
00327         self.tool.registerKineticStylesheet('spam')
00328         self.tool.registerKineticStylesheet('eggs')
00329         self.assertEqual(self.tool.getResourceIds(),
00330                          ('ham', 'spam', 'eggs'))
00331         self.tool.moveResourceUp('ham')
00332         self.assertEqual(self.tool.getResourceIds(),
00333                          ('ham', 'spam', 'eggs'))
00334 
00335     def testKineticStylesheetMoveIllegalId(self):
00336         self.tool.registerKineticStylesheet('ham')
00337         self.tool.registerKineticStylesheet('spam')
00338         self.tool.registerKineticStylesheet('eggs')
00339         self.assertEqual(self.tool.getResourceIds(),
00340                          ('ham', 'spam', 'eggs'))
00341         self.assertRaises(ValueError, self.tool.moveResourceUp, 'foo')
00342 
00343     def testKineticStylesheetMoveToBottom(self):
00344         self.tool.registerKineticStylesheet('ham')
00345         self.tool.registerKineticStylesheet('spam')
00346         self.tool.registerKineticStylesheet('eggs')
00347         self.assertEqual(self.tool.getResourceIds(),
00348                          ('ham', 'spam', 'eggs'))
00349         self.tool.moveResourceToBottom('ham')
00350         self.assertEqual(self.tool.getResourceIds(),
00351                          ('spam', 'eggs', 'ham'))
00352 
00353     def testKineticStylesheetMoveToTop(self):
00354         self.tool.registerKineticStylesheet('ham')
00355         self.tool.registerKineticStylesheet('spam')
00356         self.tool.registerKineticStylesheet('eggs')
00357         self.assertEqual(self.tool.getResourceIds(),
00358                          ('ham', 'spam', 'eggs'))
00359         self.tool.moveResourceToTop('eggs')
00360         self.assertEqual(self.tool.getResourceIds(),
00361                          ('eggs', 'ham', 'spam'))
00362 
00363     def testKineticStylesheetMoveBefore(self):
00364         self.tool.registerKineticStylesheet('ham')
00365         self.tool.registerKineticStylesheet('spam')
00366         self.tool.registerKineticStylesheet('eggs')
00367         self.tool.registerKineticStylesheet('bacon')
00368         self.assertEqual(self.tool.getResourceIds(),
00369                          ('ham', 'spam', 'eggs', 'bacon'))
00370         self.tool.moveResourceBefore('bacon', 'ham')
00371         self.assertEqual(self.tool.getResourceIds(),
00372                          ('bacon', 'ham', 'spam', 'eggs'))
00373         self.tool.moveResourceBefore('bacon', 'eggs')
00374         self.assertEqual(self.tool.getResourceIds(),
00375                          ('ham', 'spam', 'bacon', 'eggs'))
00376 
00377     def testKineticStylesheetMoveAfter(self):
00378         self.tool.registerKineticStylesheet('ham')
00379         self.tool.registerKineticStylesheet('spam')
00380         self.tool.registerKineticStylesheet('eggs')
00381         self.tool.registerKineticStylesheet('bacon')
00382         self.assertEqual(self.tool.getResourceIds(),
00383                          ('ham', 'spam', 'eggs', 'bacon'))
00384         self.tool.moveResourceAfter('bacon', 'ham')
00385         self.assertEqual(self.tool.getResourceIds(),
00386                          ('ham', 'bacon', 'spam', 'eggs'))
00387         self.tool.moveResourceAfter('bacon', 'spam')
00388         self.assertEqual(self.tool.getResourceIds(),
00389                          ('ham', 'spam', 'bacon', 'eggs'))
00390 
00391     def testKineticStylesheetMove(self):
00392         self.tool.registerKineticStylesheet('ham')
00393         self.tool.registerKineticStylesheet('spam')
00394         self.tool.registerKineticStylesheet('eggs')
00395         self.tool.registerKineticStylesheet('bacon')
00396         self.assertEqual(self.tool.getResourceIds(),
00397                          ('ham', 'spam', 'eggs', 'bacon'))
00398         self.tool.moveResource('ham', 2)
00399         self.assertEqual(self.tool.getResourceIds(),
00400                          ('spam', 'eggs', 'ham', 'bacon'))
00401         self.tool.moveResource('bacon', 0)
00402         self.assertEqual(self.tool.getResourceIds(),
00403                          ('bacon', 'spam', 'eggs', 'ham'))
00404 
00405 
00406 class TestTraversal(RegistryTestCase):
00407 
00408     def afterSetUp(self):
00409         self.tool = getattr(self.portal, KSSTOOLNAME)
00410         self.tool.clearResources()
00411         self.tool.registerKineticStylesheet('test_rr_1.kss')
00412 
00413     def testGetItemTraversal(self):
00414         self.failUnless(self.tool['test_rr_1.kss'])
00415 
00416     def testGetItemTraversalContent(self):
00417         self.failUnless('background-color' in str(self.tool['test_rr_1.kss']))
00418 
00419     def testRestrictedTraverseContent(self):
00420         self.failUnless('background-color' in str(
00421                         self.portal.restrictedTraverse('portal_kss/test_rr_1.kss')))
00422 
00423     def testRestrictedTraverseComposition(self):
00424         self.tool.registerKineticStylesheet('test_rr_2.kss')
00425         styles = self.tool.getEvaluatedResources(self.portal)
00426         self.assertEqual(len(styles), 1)
00427         magicId = styles[0].getId()
00428         content = str(self.portal.restrictedTraverse('portal_kss/%s' % magicId))
00429         self.failUnless('background-color' in content)
00430         self.failUnless('blue' in content)
00431 
00432     def testCompositesWithBrokedId(self):
00433         self.tool.registerKineticStylesheet('nonexistant.kss')
00434         stylesheets = self.tool.getEvaluatedResources(self.portal)
00435         self.assertEqual(len(stylesheets), 1)
00436         magicId = stylesheets[0].getId()
00437         content = str(self.portal.restrictedTraverse('portal_kss/%s' % magicId))
00438 
00439 
00440 class TestZODBTraversal(RegistryTestCase):
00441 
00442     def afterSetUp(self):
00443         self.tool = getattr(self.portal, KSSTOOLNAME)
00444         self.tool.clearResources()
00445         self.setRoles(['Manager'])
00446         self.portal.invokeFactory('File',
00447                                    id='testroot.kss',
00448                                    format='text/css',
00449                                    content_type='text/css',
00450                                    file='body { background-color : red }')
00451         self.portal.invokeFactory('Folder', 'subfolder')
00452         self.portal.subfolder.invokeFactory('File',
00453                                    id='testsubfolder.kss',
00454                                    format='text/css',
00455                                    content_type='text/css',
00456                                    file='body { background-color : blue }')
00457 
00458         self.tool.registerKineticStylesheet('testroot.kss')
00459         self.tool.registerKineticStylesheet('subfolder/testsubfolder.kss')
00460         self.setRoles(['Member'])
00461 
00462     def testGetItemTraversal(self):
00463         self.failUnless(self.tool['testroot.kss'])
00464         self.failUnless(self.tool['subfolder/testsubfolder.kss'])
00465 
00466     def testGetItemTraversalContent(self):
00467         self.failUnless('red' in str(self.tool['testroot.kss']))
00468         self.failUnless('blue' in str(self.tool['subfolder/testsubfolder.kss']))
00469         self.failIf('blue' in str(self.tool['testroot.kss']))
00470         self.failIf('red' in str(self.tool['subfolder/testsubfolder.kss']))
00471 
00472 
00473     def testRestrictedTraverseContent(self):
00474         self.failUnless('red' in str(
00475                         self.portal.restrictedTraverse('portal_kss/testroot.kss')))
00476         self.failUnless('blue' in str(
00477                         self.portal.restrictedTraverse('portal_kss/subfolder/testsubfolder.kss')))
00478         self.failIf('blue' in str(
00479                         self.portal.restrictedTraverse('portal_kss/testroot.kss')))
00480         self.failIf('red' in str(
00481                         self.portal.restrictedTraverse('portal_kss/subfolder/testsubfolder.kss')))
00482 
00483     def testRestrictedTraverseComposition(self):
00484         styles = self.tool.getEvaluatedResources(self.portal)
00485         self.assertEqual(len(styles), 1)
00486         magicId = styles[0].getId()
00487         content = str(self.portal.restrictedTraverse('portal_kss/%s' % magicId))
00488         self.failUnless('background-color' in content)
00489         self.failUnless('red' in content)
00490         self.failUnless('blue' in content)
00491 
00492 
00493 class TestMergingDisabled(RegistryTestCase):
00494 
00495     def afterSetUp(self):
00496         self.tool = getattr(self.portal, KSSTOOLNAME)
00497         self.tool.clearResources()
00498         self.tool.registerKineticStylesheet('testroot.kss')
00499         self.tool.registerKineticStylesheet('test_rr_1.kss')
00500         self.tool.registerKineticStylesheet('test_rr_2.kss', cookable=False)
00501         self.setRoles(['Manager'])
00502         self.portal.invokeFactory('File',
00503                                    id='testroot.kss',
00504                                    format='text/css',
00505                                    content_type='text/css',
00506                                    file='body { background-color : green }')
00507         self.setRoles(['Member'])
00508 
00509     def testDefaultKineticStylesheetCookableAttribute(self):
00510         self.failUnless(self.tool.getResources()[self.tool.getResourcePosition('test_rr_1.kss')].getCookable())
00511         self.failUnless(self.tool.getResources()[self.tool.getResourcePosition('testroot.kss')].getCookable())
00512 
00513     def testKineticStylesheetCookableAttribute(self):
00514         self.failIf(self.tool.getResources()[self.tool.getResourcePosition('test_rr_2.kss')].getCookable())
00515 
00516     def testNumberOfResources(self):
00517         self.assertEqual(len(self.tool.getResources()), 3)
00518         self.assertEqual(len(self.tool.cookedresources), 2)
00519         self.assertEqual(len(self.tool.concatenatedresources), 5)
00520         styles = self.tool.getEvaluatedResources(self.portal)
00521         self.assertEqual(len(styles), 2)
00522 
00523     def testCompositionWithLastUncooked(self):
00524         self.tool.moveResourceToBottom('test_rr_2.kss')
00525         self.assertEqual(len(self.tool.getResources()), 3)
00526         self.assertEqual(len(self.tool.cookedresources), 2)
00527         self.assertEqual(len(self.tool.concatenatedresources), 5)
00528         styles = self.tool.getEvaluatedResources(self.portal)
00529         self.assertEqual(len(styles), 2)
00530         magicIds = []
00531         for style in styles:
00532             id = style.getId()
00533             if '-cachekey' in id:
00534                 magicIds.append(id)
00535         self.failUnless(magicIds[-1].startswith('test_rr_2'))
00536         content = str(self.portal.restrictedTraverse('portal_kss/%s' % magicIds[-2]))
00537         self.failUnless('red' in content)
00538         self.failUnless('green' in content)
00539         self.failIf('blue' in content)
00540         content = str(self.portal.restrictedTraverse('portal_kss/%s' % magicIds[-1]))
00541         self.failIf('red' in content)
00542         self.failIf('green' in content)
00543         self.failUnless('blue' in content)
00544         content = str(self.portal.restrictedTraverse('portal_kss/test_rr_2.kss'))
00545         self.failUnless('blue' in content)
00546 
00547     def testCompositionWithFirstUncooked(self):
00548         self.tool.moveResourceToTop('test_rr_2.kss')
00549         self.assertEqual(len(self.tool.getResources()), 3)
00550         self.assertEqual(len(self.tool.cookedresources), 2)
00551         self.assertEqual(len(self.tool.concatenatedresources), 5)
00552         styles = self.tool.getEvaluatedResources(self.portal)
00553         self.assertEqual(len(styles), 2)
00554         magicId = None
00555         for style in styles:
00556             id = style.getId()
00557             if '-cachekey' in id:
00558                 magicId = id
00559         self.failUnless(magicId)
00560         content = str(self.portal.restrictedTraverse('portal_kss/%s' % magicId))
00561         self.failUnless('red' in content)
00562         self.failUnless('green' in content)
00563         self.failIf('blue' in content)
00564         content = str(self.portal.restrictedTraverse('portal_kss/test_rr_2.kss'))
00565         self.failUnless('blue' in content)
00566 
00567     def testCompositionWithMiddleUncooked(self):
00568         self.tool.moveResourceToTop('test_rr_2.kss')
00569         self.tool.moveResourceDown('test_rr_2.kss')
00570         self.assertEqual(len(self.tool.getResources()), 3)
00571         self.assertEqual(len(self.tool.cookedresources), 3)
00572         self.assertEqual(len(self.tool.concatenatedresources), 6)
00573         styles = self.tool.getEvaluatedResources(self.portal)
00574         self.assertEqual(len(styles), 3)
00575         content = str(self.portal.restrictedTraverse('portal_kss/test_rr_2.kss'))
00576         self.failUnless('blue' in content)
00577         content = str(self.portal.restrictedTraverse('portal_kss/test_rr_1.kss'))
00578         self.failUnless('red' in content)
00579         content = str(self.portal.restrictedTraverse('portal_kss/testroot.kss'))
00580         self.failUnless('green' in content)
00581 
00582     def testLargerCompositionWithMiddleUncooked(self):
00583         self.setRoles(['Manager'])
00584         self.portal.invokeFactory('File',
00585                                    id='testpurple.kss',
00586                                    format='text/css',
00587                                    content_type='text/css',
00588                                    file='body { background-color : purple }')
00589         self.portal.invokeFactory('File',
00590                                    id='testpink.kss',
00591                                    format='text/css',
00592                                    content_type='text/css',
00593                                    file='body { background-color : pink }')
00594         self.setRoles(['Member'])
00595         self.tool.registerKineticStylesheet('testpurple.kss')
00596         self.tool.registerKineticStylesheet('testpink.kss')
00597         self.tool.moveResourceToTop('test_rr_2.kss')
00598         self.tool.moveResourceDown('test_rr_2.kss', 2)
00599         #Now have [[green,red],blue,[purple,pink]]
00600         self.assertEqual(len(self.tool.getResources()), 5)
00601         self.assertEqual(len(self.tool.cookedresources), 3)
00602         self.assertEqual(len(self.tool.concatenatedresources), 8)
00603         styles = self.tool.getEvaluatedResources(self.portal)
00604         self.assertEqual(len(styles), 3)
00605         magicIds = []
00606         for style in styles:
00607             id = style.getId()
00608             if '-cachekey' in id:
00609                 magicIds.append(id)
00610         self.assertEqual(len(magicIds), 3)
00611         content = str(self.portal.restrictedTraverse('portal_kss/%s' % magicIds[0]))
00612         self.failUnless('red' in content)
00613         self.failUnless('green' in content)
00614         self.failIf('pink' in content)
00615         self.failIf('purple' in content)
00616         content = str(self.portal.restrictedTraverse('portal_kss/%s' % magicIds[2]))
00617         self.failUnless('pink' in content)
00618         self.failUnless('purple' in content)
00619         self.failIf('red' in content)
00620         self.failIf('green' in content)
00621         content = str(self.portal.restrictedTraverse('portal_kss/test_rr_2.kss'))
00622         self.failUnless('blue' in content)
00623 
00624 class TestPublishing(FunctionalRegistryTestCase):
00625 
00626     def afterSetUp(self):
00627         self.tool = getattr(self.portal, KSSTOOLNAME)
00628         self.tool.clearResources()
00629         self.tool.registerKineticStylesheet('plone_styles.kss')
00630         self.toolpath = '/' + self.tool.absolute_url(1)
00631         self.portalpath = '/' + getToolByName(self.portal, "portal_url")(1)
00632         self.setRoles(['Manager'])
00633         self.portal.invokeFactory('Document', 'index_html')
00634         self.setRoles(['Member'])
00635 
00636     def testPublishCSSThroughTool(self):
00637         response = self.publish(self.toolpath + '/plone_styles.kss')
00638         self.assertEqual(response.getStatus(), 200)
00639         self.assertEqual(response.getHeader('Content-Type'), 'text/css;charset=utf-8')
00640 
00641     def testPublishNonMagicCSSThroughTool(self):
00642         self.setRoles(['Manager'])
00643         body = """<dtml-var "'joined' + 'string'">"""
00644         self.portal.addDTMLMethod('testmethod', file=body)
00645         self.tool.registerKineticStylesheet('testmethod')
00646         response = self.publish(self.toolpath + '/testmethod')
00647         self.assertEqual(response.getStatus(), 200)
00648         self.assertEqual(response.getHeader('Content-Type'), 'text/css;charset=utf-8')
00649 
00650 
00651 class TestFivePublishing(FunctionalRegistryTestCase):
00652     'Publishing with Five'
00653 
00654     def afterSetUp(self):
00655         # Define some resource
00656         from Products.Five.zcml import load_config
00657         import Products.ResourceRegistries.tests
00658         load_config('test.zcml', Products.ResourceRegistries.tests)
00659         self.tool = getattr(self.portal, KSSTOOLNAME)
00660         self.tool.clearResources()
00661         self.tool.registerKineticStylesheet('++resource++test_rr_1.kss')
00662         self.toolpath = '/' + self.tool.absolute_url(1)
00663         self.portalpath = '/' + getToolByName(self.portal, "portal_url")(1)
00664         self.setRoles(['Manager'])
00665         self.portal.invokeFactory('Document', 'index_html')
00666         self.setRoles(['Member'])
00667 
00668     def testPublishFiveResource(self):
00669         response = self.publish(self.toolpath + '/++resource++test_rr_1.kss')
00670         self.assertEqual(response.getStatus(), 200)
00671         self.assertEqual(response.getHeader('Content-Type')[:10], 'text/plain')
00672         self.assertEqual('body { background-color : red }' in response.getBody(), True)
00673 
00674 
00675 class TestResourcePermissions(FunctionalRegistryTestCase):
00676 
00677     def afterSetUp(self):
00678         self.tool = getattr(self.portal, KSSTOOLNAME)
00679         self.toolpath = '/' + self.tool.absolute_url(1)
00680         self.tool.clearResources()
00681         self.tool.registerKineticStylesheet('testroot.kss', cookable=False)
00682         self.tool.registerKineticStylesheet('test_rr_1.kss')
00683         self.setRoles(['Manager'])
00684         self.portal.invokeFactory('File',
00685                                    id='testroot.kss',
00686                                    format='text/css',
00687                                    content_type='text/css',
00688                                    file='body { background-color : green }')
00689 
00690         stylesheet = getattr(self.portal, 'testroot.kss')
00691 
00692         stylesheet.manage_permission('View',['Manager'], acquire=0)
00693         stylesheet.manage_permission('Access contents information',['Manager'], acquire=0)
00694         self.setRoles(['Member'])
00695 
00696 
00697     def testUnauthorizedGetItem(self):
00698         try:
00699             content = str(self.tool['testroot.kss'])
00700         except Unauthorized:
00701             return
00702 
00703         self.fail()
00704 
00705     def testUnauthorizedTraversal(self):
00706         try:
00707             content = str(self.portal.restrictedTraverse('portal_kss/testroot.kss'))
00708         except Unauthorized:
00709             return
00710 
00711         self.fail()
00712 
00713     def testUnauthorizedOnPublish(self):
00714         response = self.publish(self.toolpath + '/testroot.kss')
00715         #Will be 302 if CookieCrumbler is enabled
00716         self.failUnless(response.getStatus() in [302, 403, 401])
00717 
00718     def testRemovedFromResources(self):
00719         styles = self.tool.getEvaluatedResources(self.portal)
00720         ids = [item.getId() for item in styles]
00721         self.assertEqual(len(self.tool.concatenatedresources), 4)
00722         self.failIf('testroot.kss' in ids)
00723         self.failUnless('test_rr_1.kss' in self.tool.concatenatedresources[ids[1]])
00724 
00725     def testRemovedFromMergedResources(self):
00726         self.tool.unregisterResource('testroot.kss')
00727         self.tool.registerKineticStylesheet('testroot.kss')
00728         styles = self.tool.getEvaluatedResources(self.portal)
00729         magicId = None
00730         for style in styles:
00731             id = style.getId()
00732             if '-cachekey' in id:
00733                 magicId = id
00734         self.failUnless(magicId)
00735         content = str(self.portal.restrictedTraverse('portal_kss/%s' % magicId))
00736         self.failIf('green' in content)
00737         self.failUnless('not authorized' in content)
00738         self.failUnless('red' in content)
00739 
00740     def testAuthorizedGetItem(self):
00741         self.setRoles(['Manager'])
00742         try:
00743             content = str(self.tool['testroot.kss'])
00744         except Unauthorized:
00745             self.fail()
00746 
00747     def testAuthorizedTraversal(self):
00748         self.setRoles(['Manager'])
00749         try:
00750             content = str(self.portal.restrictedTraverse('portal_kss/testroot.kss'))
00751         except Unauthorized:
00752             self.fail()
00753 
00754     def testAuthorizedOnPublish(self):
00755         authstr = "%s:%s" % (portal_owner, default_password)
00756         response = self.publish(self.toolpath + '/testroot.kss', basic=authstr)
00757         self.failUnlessEqual(response.getStatus(), 200)
00758 
00759 class TestDebugMode(FunctionalRegistryTestCase):
00760 
00761     def afterSetUp(self):
00762         self.tool = getattr(self.portal, KSSTOOLNAME)
00763         self.tool.clearResources()
00764         self.portalpath = '/' + getToolByName(self.portal, "portal_url")(1)
00765         self.toolpath = '/' + self.tool.absolute_url(1)
00766 
00767     def testDebugModeSplitting(self):
00768         self.tool.registerKineticStylesheet('ham')
00769         self.tool.registerKineticStylesheet('spam')
00770         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 1)
00771         self.tool.setDebugMode(True)
00772         self.tool.cookResources()
00773         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 2)
00774 
00775     def testDebugModeSplitting2(self):
00776         self.tool.registerKineticStylesheet('ham')
00777         # Publish in normal mode
00778         response = self.publish(self.toolpath+'/ham')
00779         now = DateTime()
00780         days = 7
00781         soon = now + days
00782         self.assertEqual(response.getStatus(), 200)
00783         self.assertEqual(response.getHeader('Expires'), rfc1123_date(soon.timeTime()))
00784         self.assertEqual(response.getHeader('Cache-Control'), 'max-age=%d' % int(days*24*3600))
00785 
00786         # Set debug mode
00787         self.tool.setDebugMode(True)
00788         self.tool.cookResources()
00789         # Publish in debug mode
00790         response = self.publish(self.toolpath+'/ham')
00791         self.failIfEqual(response.getHeader('Expires'), rfc1123_date(soon.timeTime()))
00792         self.assertEqual(response.getHeader('Expires'), rfc1123_date(now.timeTime()))
00793         self.assertEqual(response.getHeader('Cache-Control'), 'max-age=0')
00794 
00795 
00796 class TestResourceObjects(RegistryTestCase):
00797 
00798     def afterSetUp(self):
00799         self.tool = getattr(self.portal, KSSTOOLNAME)
00800         self.tool.clearResources()
00801 
00802     def testSetEnabled(self):
00803         self.tool.registerKineticStylesheet('ham')
00804         self.tool.registerKineticStylesheet('spam')
00805         self.tool.registerKineticStylesheet('eggs')
00806         self.assertEqual(self.tool.concatenatedresources[
00807                          self.tool.cookedresources[0].getId()],
00808                          ['ham', 'spam', 'eggs'])
00809         spam = self.tool.getResource('spam')
00810         spam.setEnabled(False)
00811         self.assertEqual(self.tool.concatenatedresources[
00812                          self.tool.cookedresources[0].getId()],
00813                          ['ham', 'spam', 'eggs'])
00814         self.tool.cookResources()
00815         self.assertEqual(self.tool.concatenatedresources[
00816                          self.tool.cookedresources[0].getId()],
00817                          ['ham', 'eggs'])
00818 
00819     def testSetCookable(self):
00820         self.tool.registerKineticStylesheet('ham')
00821         self.tool.registerKineticStylesheet('spam')
00822         self.tool.registerKineticStylesheet('eggs')
00823         self.assertEqual(self.tool.concatenatedresources[
00824                          self.tool.cookedresources[0].getId()],
00825                          ['ham', 'spam', 'eggs'])
00826         spam = self.tool.getResource('spam')
00827         spam.setCookable(False)
00828         self.assertEqual(self.tool.concatenatedresources[
00829                          self.tool.cookedresources[0].getId()],
00830                          ['ham', 'spam', 'eggs'])
00831         self.tool.cookResources()
00832         self.assertEqual(self.tool.concatenatedresources[
00833                          self.tool.cookedresources[0].getId()],
00834                          ['ham'])
00835         self.assertEqual(self.tool.concatenatedresources[
00836                          self.tool.cookedresources[1].getId()],
00837                          ['spam'])
00838         self.assertEqual(self.tool.concatenatedresources[
00839                          self.tool.cookedresources[2].getId()],
00840                          ['eggs'])
00841 
00842 
00843 class TestSkinAwareness(FunctionalRegistryTestCase):
00844 
00845     def afterSetUp(self):
00846         self.tool = getattr(self.portal, KSSTOOLNAME)
00847         self.skinstool = getattr(self.portal, 'portal_skins')
00848         self.tool.clearResources()
00849         self.portalpath = '/' + getToolByName(self.portal, "portal_url")(1)
00850         self.toolpath = '/' + self.tool.absolute_url(1)
00851         self.setRoles(['Manager'])
00852         self.skinstool.manage_addFolder(id='pink')
00853         self.skinstool.manage_addFolder(id='purple')
00854         self.skinstool.pink.manage_addFile(id='skin.kss',
00855                                    content_type='text/css',
00856                                    file='body { background-color : pink }')
00857         self.skinstool.purple.manage_addFile(id='skin.kss',
00858                                     content_type='text/css',
00859                                     file='body { background-color : purple }')
00860         self.tool.registerKineticStylesheet('skin.kss')
00861         self.skinstool.addSkinSelection('PinkSkin', 'pink,ResourceRegistries')
00862         self.skinstool.addSkinSelection('PurpleSkin', 'purple,ResourceRegistries')
00863         self.setRoles(['Member'])
00864         
00865     def testRenderIncludesSkinInPath(self):
00866         self.portal.changeSkin('PinkSkin', REQUEST=self.portal.REQUEST)
00867         view = self.portal.restrictedTraverse('@@plone')
00868         viewletmanager = getMultiAdapter((self.portal, self.portal.REQUEST, view), IContentProvider, name = u'plone.resourceregistries.styles')
00869         viewletmanager.update()
00870         content = viewletmanager.render()
00871         self.failUnless('/PinkSkin/' in content)
00872         self.failIf('/PurpleSkin/' in content)
00873         self.portal.changeSkin('PurpleSkin', REQUEST=self.portal.REQUEST)
00874         view = self.portal.restrictedTraverse('@@plone')
00875         viewletmanager = getMultiAdapter((self.portal, self.portal.REQUEST, view), IContentProvider, name = u'plone.resourceregistries.styles')
00876         viewletmanager.update()
00877         content = viewletmanager.render()
00878         self.failUnless('/PurpleSkin/' in content)
00879         self.failIf('/PinkSkin/' in content)
00880 
00881     def testPublishWithSkin(self):
00882         response = self.publish(self.toolpath + '/PinkSkin/skin.kss')
00883         self.assertEqual(response.getStatus(), 200)
00884         self.failUnless('pink' in str(response))
00885         response = self.publish(self.toolpath + '/PurpleSkin/skin.kss')
00886         self.assertEqual(response.getStatus(), 200)
00887         self.failUnless('purple' in str(response))
00888 
00889 
00890 def test_suite():
00891     from unittest import TestSuite, makeSuite
00892     suite = TestSuite()
00893     suite.addTest(makeSuite(TestImplementation))
00894     suite.addTest(makeSuite(TestTool))
00895     suite.addTest(makeSuite(testZMIMethods))
00896     suite.addTest(makeSuite(TestKineticStylesheetRegistration))
00897     suite.addTest(makeSuite(TestKineticStylesheetRenaming))
00898     suite.addTest(makeSuite(TestToolSecurity))
00899     suite.addTest(makeSuite(TestToolExpression))
00900     suite.addTest(makeSuite(TestKineticStylesheetCooking))
00901     suite.addTest(makeSuite(TestPublishing))
00902     suite.addTest(makeSuite(TestFivePublishing))
00903     suite.addTest(makeSuite(TestKineticStylesheetMoving))
00904     suite.addTest(makeSuite(TestTraversal))
00905     suite.addTest(makeSuite(TestZODBTraversal))
00906     suite.addTest(makeSuite(TestMergingDisabled))
00907     suite.addTest(makeSuite(TestResourcePermissions))
00908     suite.addTest(makeSuite(TestDebugMode))
00909     suite.addTest(makeSuite(TestResourceObjects))
00910     suite.addTest(makeSuite(TestSkinAwareness))
00911 
00912     return suite