def test_order_events(self): # Prepare the setup:: from zope.container.contained import ContainerModifiedEvent # Prepare some objects:: from zope.container.ordered import OrderedContainer oc = OrderedContainer() oc['foo'] = 'bar' oc['baz'] = 'quux' oc['zork'] = 'grue' self.assertEqual(oc.keys(), ['foo', 'baz', 'zork']) # Now change the order:: clearEvents() oc.updateOrder(['baz', 'foo', 'zork']) self.assertEqual(oc.keys(), ['baz', 'foo', 'zork']) # Check what events have been sent:: events = getEvents() self.assertEqual(1, len(events)) self.assertIsInstance(events[0], ContainerModifiedEvent) # This is in fact a specialized modification event:: from zope.lifecycleevent.interfaces import IObjectModifiedEvent self.assertTrue(IObjectModifiedEvent.providedBy(events[0]))
def test_deny_dublincore_view(self): """Tests the denial of dublincore view permissions to anonymous. Users who can view a folder contents page but cannot view dublin core should still be able to see the folder items' names, but not their title, modified, and created info. """ # add an item that can be viewed from the root folder obj = OrderedContainer() alsoProvides(obj, IAttributeAnnotatable) self.getRootFolder()['obj'] = obj IZopeDublinCore(obj).title = u'My object' # deny zope.app.dublincore.view to zope.Anonymous prm = IRolePermissionManager(self.getRootFolder()) prm.denyPermissionToRole('zope.dublincore.view', 'zope.Anonymous') # Try both spellings just in case we are used with an older zope.dc prm.denyPermissionToRole('zope.app.dublincore.view', 'zope.Anonymous') transaction.commit() response = self.publish('/') self.assertEquals(response.getStatus(), 200) body = response.getBody() # confirm we can see the file name self.assert_(body.find('<a href="obj">obj</a>') != -1) # confirm we *cannot* see the metadata title self.assert_(body.find('My object') == -1)
def test_order_adding_none(self): # This is a regression test: adding None to an OrderedContainer # used to corrupt its internal data structure (_order and _data # would get out of sync, causing KeyErrors when you tried to iterate). from zope.container.ordered import OrderedContainer oc = OrderedContainer() oc['foo'] = None self.assertEqual(oc.keys(), ['foo']) self.assertEqual(oc.values(), [None]) self.assertEqual(oc.items(), [('foo', None)]) # None got proxied, so 'is None is not true self.assertIsNotNone(oc['foo']) self.assertEqual(None, oc['foo'])
def test_order_updateOrder_bytes(self): # https://github.com/zopefoundation/zope.container/issues/21 from zope.container.ordered import OrderedContainer keys = [u'a', u'b'] oc = OrderedContainer() oc[keys[0]] = 0 oc[keys[1]] = 1 self.assertEqual(keys, oc.keys()) # Updating with bytes keys... oc.updateOrder((b'b', b'a')) # still produces text keys text_type = str if str is not bytes else unicode self.assertEqual(list(reversed(keys)), oc.keys()) self.assertIsInstance(oc.keys()[0], text_type)
def listItems(self): # Now this is tricky: we want to construct a small object graph using # old state pickles without ever calling __setstate__ on a real # Persistent object, as _that_ would poison ZODB in-memory caches # in a nasty way (LP #487243). container = OrderedContainer() container.__setstate__(self.state) if isinstance(container._data, PersistentDict): old_data_state = IObjectHistory(container._data).loadState( self.tid) container._data = PersistentDict() container._data.__setstate__(old_data_state) if isinstance(container._order, PersistentList): old_order_state = IObjectHistory(container._order).loadState( self.tid) container._order = PersistentList() container._order.__setstate__(old_order_state) return container.items()
def test_order_all_items_available_at_object_added_event(self): # Now register an event subscriber to object added events. import zope.component from zope.lifecycleevent.interfaces import IObjectAddedEvent keys = [] @zope.component.adapter(IObjectAddedEvent) def printContainerKeys(event): keys.extend(event.newParent.keys()) zope.component.provideHandler(printContainerKeys) # Now we are adding an object to the container. from zope.container.ordered import OrderedContainer oc = OrderedContainer() oc['foo'] = 'FOO' self.assertEqual(keys, ['foo'])
def test_default_view_permissions(self): """Tests the default view permissions. """ # add an item that can be viewed from the root folder obj = OrderedContainer() alsoProvides(obj, IAttributeAnnotatable) self.getRootFolder()['obj'] = obj IZopeDublinCore(obj).title = u'My object' transaction.commit() response = self.publish('/') self.assertEquals(response.getStatus(), 200) body = response.getBody() # confirm we can see the file name self.assert_(body.find('<a href="obj">obj</a>') != -1) # confirm we can see the metadata title self.assert_(body.find('<td><span>My object</span></td>') != -1)
def __init__(self, title): self.title = title self.states = OrderedContainer() locate(self.states, self, 'states') self.system_titles = OrderedContainer() locate(self.system_titles, self, 'system_titles')