[Product-Developers] Traversing to an object like the publisher does
Jan Hackel
plonecode at hackel.name
Fri May 15 20:34:13 UTC 2009
On Thursday 14 May 2009 Jan Hackel wrote:
> I need to write a test that essentially needs to traverse to an operation
> like the following tal-expression would:
>
> tal:condition="context/@@plone_context_state/is_view_template"
>
> It seems I cannot use "(un)restrictedTraverse" because it does not trigger
> my custom IPublishTraverse adapter. Using "Request.traverse" looks
> promising but I cannot get authorization to work with it, i.e. I get an
> UnauthorizedException.
>
> Is there a canonical way of simulating the tal-expression given above?
Here is the solution which I for now have to settle with. Mind that it is used
for testing only. Protect your eyes!
>>> from ZPublisher.tests.testBaseRequest import DummyResponse
>>> from ZPublisher.HTTPRequest import HTTPRequest
>>> from ZPublisher.HTTPResponse import HTTPResponse
>>> from Products.PloneTestCase import PloneTestCase
>>> import base64
>>> user_id = PloneTestCase.default_user
>>> password = PloneTestCase.default_password
>>> encoded = base64.encodestring( '%s:%s' % ( user_id, password ) )
>>> auth_header = 'basic %s' % encoded
>>> resp = DummyResponse()
>>> env={'SERVER_URL':'http://nohost/plone',
... 'URL':'http://nohost/plone',
... 'HTTP_AUTHORIZATION': auth_header,
... 'REQUEST_METHOD': 'GET',
... 'steps': [],
... '_hacked_path': 0,
... '_test_counter': 0,
... 'response' : resp
... }
>>> request = HTTPRequest(stdin=None, environ=env, response=resp)
>>> request['PARENTS'] = [self.getPortal()]
Now the traversal up the context state view.
>>> contextState = request.traverse("weblog/issue193/"
... "@@plone_context_state")
The call to "request.traverse" differs from what TAL-traversal does. The
request URL has the view name '@@plone_context_state' appended. In my case I
had to remove that before the actual call.
>>> request['ACTUAL_URL'] = 'http://nohost/weblog/issue193'
>>> contextState.is_view_template()
True
Grüße
Jan Hackel
More information about the Product-Developers
mailing list