Back to index

plone3  3.1.7
test_news_portlet.py
Go to the documentation of this file.
00001 from zope.component import getUtility, getMultiAdapter
00002 from zope.app.component.hooks import setHooks, setSite
00003 
00004 from Products.GenericSetup.utils import _getDottedName
00005 
00006 from plone.portlets.interfaces import IPortletType
00007 from plone.portlets.interfaces import IPortletManager
00008 from plone.portlets.interfaces import IPortletAssignment
00009 from plone.portlets.interfaces import IPortletDataProvider
00010 from plone.portlets.interfaces import IPortletRenderer
00011 
00012 from plone.app.portlets.portlets import news
00013 from plone.app.portlets.storage import PortletAssignmentMapping
00014 
00015 from plone.app.portlets.tests.base import PortletsTestCase
00016 
00017 class TestPortlet(PortletsTestCase):
00018 
00019     def afterSetUp(self):
00020         setHooks()
00021         setSite(self.portal)
00022         self.setRoles(('Manager',))
00023 
00024     def testPortletTypeRegistered(self):
00025         portlet = getUtility(IPortletType, name='portlets.News')
00026         self.assertEquals(portlet.addview, 'portlets.News')
00027 
00028     def testRegisteredInterfaces(self):
00029         portlet = getUtility(IPortletType, name='portlets.News')
00030         registered_interfaces = [_getDottedName(i) for i in portlet.for_] 
00031         registered_interfaces.sort() 
00032         self.assertEquals(['plone.app.portlets.interfaces.IColumn',
00033           'plone.app.portlets.interfaces.IDashboard'],
00034           registered_interfaces)
00035 
00036     def testInterfaces(self):
00037         portlet = news.Assignment(count=5)
00038         self.failUnless(IPortletAssignment.providedBy(portlet))
00039         self.failUnless(IPortletDataProvider.providedBy(portlet.data))
00040 
00041     def testInvokeAddview(self):
00042         portlet = getUtility(IPortletType, name='portlets.News')
00043         mapping = self.portal.restrictedTraverse('++contextportlets++plone.leftcolumn')
00044         for m in mapping.keys():
00045             del mapping[m]
00046         addview = mapping.restrictedTraverse('+/' + portlet.addview)
00047 
00048         addview.createAndAdd(data={})
00049 
00050         self.assertEquals(len(mapping), 1)
00051         self.failUnless(isinstance(mapping.values()[0], news.Assignment))
00052 
00053     def testInvokeEditView(self):
00054         mapping = PortletAssignmentMapping()
00055         request = self.folder.REQUEST
00056 
00057         mapping['foo'] = news.Assignment(count=5)
00058         editview = getMultiAdapter((mapping['foo'], request), name='edit')
00059         self.failUnless(isinstance(editview, news.EditForm))
00060 
00061     def testRenderer(self):
00062         context = self.folder
00063         request = self.folder.REQUEST
00064         view = self.folder.restrictedTraverse('@@plone')
00065         manager = getUtility(IPortletManager, name='plone.leftcolumn', context=self.portal)
00066         assignment = news.Assignment(count=5)
00067 
00068         renderer = getMultiAdapter((context, request, view, manager, assignment), IPortletRenderer)
00069         self.failUnless(isinstance(renderer, news.Renderer))
00070 
00071 class TestRenderer(PortletsTestCase):
00072 
00073     def afterSetUp(self):
00074         setHooks()
00075         setSite(self.portal)
00076         # Make sure News Items use simple_publication_workflow
00077         self.portal.portal_workflow.setChainForPortalTypes(['News Item'], ['simple_publication_workflow'])
00078 
00079     def renderer(self, context=None, request=None, view=None, manager=None, assignment=None):
00080         context = context or self.folder
00081         request = request or self.folder.REQUEST
00082         view = view or self.folder.restrictedTraverse('@@plone')
00083         manager = manager or getUtility(IPortletManager, name='plone.leftcolumn', context=self.portal)
00084         assignment = assignment or news.Assignment(template='portlet_recent', macro='portlet')
00085 
00086         return getMultiAdapter((context, request, view, manager, assignment), IPortletRenderer)
00087 
00088     def test_published_news_items(self):
00089         self.setRoles(('Manager',))
00090         self.portal.invokeFactory('News Item', 'n1')
00091         self.portal.invokeFactory('News Item', 'n2')
00092         self.portal.portal_workflow.doActionFor(self.portal.n1, 'publish')
00093         
00094         r = self.renderer(assignment=news.Assignment(count=5, state=('draft',)))
00095         self.assertEquals(0, len(r.published_news_items()))
00096         r = self.renderer(assignment=news.Assignment(count=5, state=('published', )))
00097         self.assertEquals(1, len(r.published_news_items()))
00098         r = self.renderer(assignment=news.Assignment(count=5, state=('published', 'private',)))
00099         self.assertEquals(2, len(r.published_news_items()))
00100 
00101     def test_all_news_link(self):
00102         r = self.renderer(assignment=news.Assignment(count=5))
00103         self.failUnless(r.all_news_link().endswith('/news'))
00104         self.portal._delObject('news')
00105         self.assertEqual(r.all_news_link(), None)
00106 
00107 def test_suite():
00108     from unittest import TestSuite, makeSuite
00109     suite = TestSuite()
00110     suite.addTest(makeSuite(TestPortlet))
00111     suite.addTest(makeSuite(TestRenderer))
00112     return suite