def test_menu(self): request = FakeRequest() # XXX not sure why we need this.. request.getURL = lambda: 'http://www.infrae.com' menu = globalBrowserMenuService.getMenu('testmenu', self.folder.test, request) self.assertEquals(3, len(menu)) # sort menu items by title so we get a stable testable result menu.sort(lambda x, y: cmp(x['title'], y['title'])) self.assertEquals('Test Menu Item', menu[0]['title']) self.assertEquals('seagull.html', menu[0]['action']) self.assertEquals('Test Menu Item 2', menu[1]['title']) self.assertEquals('parakeet.html', menu[1]['action'])
def test_page_security(self): decl = """ <configure xmlns="http://namespaces.zope.org/zope" xmlns:browser="http://namespaces.zope.org/browser"> <browser:page for="Products.Five.tests.dummy.IDummy" class="Products.Five.tests.dummy.DummyView" attribute="foo" name="test_page_security" permission="zope2.ViewManagementScreens" /> </configure> """ zcml.load_string(decl) request = FakeRequest() # Wrap into an acquisition so that imPermissionRole objects # can be evaluated. view = getView(Dummy1(), 'test_page_security', request) ac = getattr(view, '__ac_permissions__') # It's protecting the object with the permission, and not the # attribute, so we get ('',) instead of ('foo',). ex_ac = (('View management screens', ('',)),) self.assertEquals(ac, ex_ac) # Wrap into an acquisition so that imPermissionRole objects # can be evaluated. __roles__ is a imPermissionRole object. view = view.__of__(self.folder) view_roles = getattr(view, '__roles__', None) self.failIf(view_roles is None) self.failIf(view_roles == ()) self.assertEquals(view_roles, ('Manager',))
def test_page_security(self): self.failIf(hasattr(self.dummy1, '__ac_permissions__')) decl = """ <configure xmlns="http://namespaces.zope.org/zope" xmlns:browser="http://namespaces.zope.org/browser"> <browser:page for="Products.Five.tests.dummy.IDummy" class="Products.Five.tests.dummy.DummyView" attribute="foo" name="foo.txt" permission="zope2.ViewManagementScreens" /> </configure> """ zcml.string(decl) request = FakeRequest() view = getView(Dummy1(), 'foo.txt', request) ac = getattr(view, '__ac_permissions__') ex_ac = (('View management screens', ('foo',)),) self.assertEquals(ac, ex_ac) foo_roles = getattr(view, 'foo__roles__', None) self.failIf(foo_roles is None) self.failIf(foo_roles == ()) self.assertEquals(foo_roles.__of__(view), ('Manager',))
def test_five_product(self): basket = self._makeOne() basket.pdist_fname = os.path.join(self.fixtures, 'pdist-fiveproduct.txt') sys.path.append(self.fixtures) self.working_set.add_entry(self.fixtures) self.failIf(sys.modules.has_key('Products.fiveproduct')) productcontext = DummyProductContext('Basket') basket.preinitialize() result = basket.initialize(productcontext) import Products.fiveproduct self.failUnless(sys.modules.has_key('Products.fiveproduct')) from Products.Five.fiveconfigure import findProducts from Products.Five.fiveconfigure import loadProducts from zope.configuration import xmlconfig products = findProducts() self.assert_(Products.fiveproduct in products) # do what Five.loadProduct does sitezcml = """\ <configure xmlns="http://namespaces.zope.org/zope" xmlns:five="http://namespaces.zope.org/five"> <include package="Products.Five" /> <include package="Products.fiveproduct"/> </configure>""" xmlconfig.string(sitezcml) # verify that the zcml had the correct effect from Products.fiveproduct.module import SampleAdapter from Products.fiveproduct.module import ISampleAdapter from Products.fiveproduct.module import ExtraSampleAdapter from Products.fiveproduct.module import IExtraSampleAdapter context = None adapter = ISampleAdapter(context) self.assertEqual(adapter.__class__, SampleAdapter) self.assertEqual(adapter.context, context) adapter = IExtraSampleAdapter(context) self.assertEqual(adapter.__class__, ExtraSampleAdapter) self.assertEqual(adapter.context, context) view = getView(object(), 'example_view', FakeRequest()) self.failUnless(view is not None)
def test_get_widgets_for_schema_fields(self): salutation = Choice(title=u'Salutation', values=("Mr.", "Mrs.", "Captain", "Don")) contactname = TextLine(title=u'Name') request = FakeRequest() salutation = salutation.bind(request) contactname = contactname.bind(request) view1 = getViewProviding(contactname, IInputWidget, request) self.assertEquals(view1.__class__, zope.app.form.browser.textwidgets.TextWidget) view2 = getViewProviding(salutation, IInputWidget, request) self.assertEquals(view2.__class__, zope.app.form.browser.itemswidgets.DropdownWidget)
def test_protocol_macro(self): portal = self.portal self.loginAsPortalOwner() portal.createObject("testdocument", "DummyContent") # Initialize the process doc = portal.testdocument request = FakeRequest() zope.interface.directlyProvides( request, zope.publisher.interfaces.browser.IDefaultBrowserLayer) protocol_view = zope.component.getMultiAdapter( (doc, request), name='workflow_protocol') self.assertEquals('startTag', protocol_view.index.macros['protocol'][3][0]) self.assertEquals('table', protocol_view.index.macros['protocol'][3][1][0])
def test_five_product_with_no_Products_namespace(self): basket = self._makeOne() from Products.Basket import monkeypatches monkeypatches.patch_findProducts(basket) basket.pdist_fname = os.path.join(self.fixtures, 'pdist-fiveproduct2.txt') sys.path.append(self.fixtures) self.working_set.add_entry(self.fixtures) self.failIf(sys.modules.has_key('fiveproduct2')) productcontext = DummyProductContext('Basket') basket.preinitialize() result = basket.initialize(productcontext) import fiveproduct2 self.failUnless(sys.modules.has_key('fiveproduct2')) from Products.Five.fiveconfigure import findProducts from zope.configuration import xmlconfig products = findProducts() self.assert_(fiveproduct2 in products) # do what Five.loadProduct does sitezcml = """\ <configure xmlns="http://namespaces.zope.org/zope" xmlns:five="http://namespaces.zope.org/five"> <include package="Products.Five" /> <include package="fiveproduct2"/> </configure>""" xmlconfig.string(sitezcml) # verify that the zcml had the correct effect context = None view = getView(object(), 'example_view', FakeRequest()) self.failUnless(view is not None)
def getSubObject(self, name, REQUEST, RESPONSE=None): obj = self.__p4a_z2utils_orig_getSubObject(name, REQUEST, RESPONSE) if obj is not None: return obj # The following is a copy from Five's __bobo_traverse__ stuff, # see Products.Five.traversable for details. # Basically we're forcing Archetypes to look up the correct # Five way: # 1) check for data object first # 2) check for zope3 view # 3) return nothing so that AT's default __bobo_traverse__ will use aq if not IBrowserRequest.providedBy(REQUEST): # Try to get the REQUEST by acquisition REQUEST = getattr(self, 'REQUEST', None) if not IBrowserRequest.providedBy(REQUEST): REQUEST = FakeRequest() setDefaultSkin(REQUEST) # Con Zope 3 into using Zope 2's checkPermission Products.Five.security.newInteraction() # Use the ITraverser adapter (which in turn uses ITraversable # adapters) to traverse to a view. Note that we're mixing # object-graph and object-publishing traversal here, but Zope # 2 has no way to tell us when to use which... # TODO Perhaps we can decide on object-graph vs. # object-publishing traversal depending on whether REQUEST is # a stub or not? try: return ITraverser(self).traverse( path=[name], request=REQUEST).__of__(self) except (ComponentLookupError, LookupError, TypeError, AttributeError, KeyError, NotFound): pass return None