~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
= ZCML Directives =

We have a bunch of custom zcml directives in Launchpad.

== Canonical URLs ==

See canonical_url.txt for information and tests of the browser:url directive.


== A zcml context for zcml directive unittests ==

This is a context that collects actions and has a __repr__ for use in
tests that shows a pretty-printed representation of the actions.

We have to add the 'kw' arg for the EditFormDirective tests, but it isn't
important, so we just discard it.


    >>> import re, pprint
    >>> atre = re.compile(' at [0-9a-fA-Fx]+')
    >>> class Context(object):
    ...    actions = ()
    ...    def action(self, discriminator, callable, args, kw=None):
    ...        self.actions += ((discriminator, callable, args), )
    ...    def __repr__(self):
    ...        stream = StringIO()
    ...        pprinter = pprint.PrettyPrinter(stream=stream, width=60)
    ...        pprinter.pprint(self.actions)
    ...        r = stream.getvalue()
    ...        return (''.join(atre.split(r))).strip()

The code to this is actually repeated all through the Zope 3 unit tests for
zcml directives. :-(


== Setting up some interfaces and objects to test with ==

We'll put an interface in lp.testing.IFoo, and set the
default view for the IFooLayer layer.

    >>> from zope.component import queryMultiAdapter
    >>> import lp.testing
    >>> from zope.interface import Interface, implements
    >>> class IFoo(Interface):
    ...     pass
    >>> class IFooLayer(Interface):
    ...     pass
    >>> lp.testing.IFoo = IFoo
    >>> lp.testing.IFooLayer = IFooLayer

    >>> class FooObject:
    ...     implements(IFoo)
    >>> fooobject = FooObject()
    >>> class Request:
    ...     implements(IFooLayer)
    >>> request = Request()

    >>> import types
    >>> class FooView:
    ...     """classic class that is used for a view on an IFoo"""
    ...     __metaclass__ = types.ClassType
    ...
    ...     def __call__(self):
    ...         return "FooView was called"
    >>> lp.testing.FooView = FooView


== Overriding the browser:page directive ==

We override browser:page to allow us to specify the facet that the
page is associated with.

First, a unit test of the overridden directive.

    >>> from lp.services.webapp.metazcml import page
    >>> context = Context()
    >>> context.info = "INFO"

Name some variables to mirror the names used as arguments.

    >>> name = "NAME"
    >>> permission = "PERMISSION"
    >>> for_ = "FOR_"
    >>> facet = "FACET"
    >>> page(context, name, permission, for_, facet=facet)

# This is a pointless doctest - it is totally non obvious what the result
# is supposed to mean, or what is being tested, so when it breaks we have
# no idea if the test is broken or the code being tested is broken.
# -- StuartBishop 20060411
#
#     >>> context
#     ((None,
#       <function provideInterface>,
#       ('', 'FOR_')),
#      (('view',
#        'FOR_',
#        'NAME',
#        <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>,
#        <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
#       <function handler>,
#       ('Adapters',
#        'register',
#        ('FOR_',
#         <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
#        <InterfaceClass zope.interface.Interface>,
#        'NAME',
#        <class 'zope.app.publisher.browser.viewmeta.SimpleLaunchpadViewClass'>,
#        'INFO')))

Look for the SimpleLaunchpadViewClass in the data structure above, and check
its __launchpad_facetname__ attribute.

    >>> from zope.security.proxy import isinstance
    >>> def get_simplelaunchpadviewclass(actions_tuple):
    ...     for o in actions_tuple:
    ...         if isinstance(o, tuple):
    ...             o = get_simplelaunchpadviewclass(o)
    ...         if (hasattr(o, '__name__')
    ...             and o.__name__ == 'SimpleLaunchpadViewClass'):
    ...             return o
    ...     return None
    >>> cls = get_simplelaunchpadviewclass(context.actions)
    >>> cls
    <class 'zope.browserpage.metaconfigure.SimpleLaunchpadViewClass'>
    >>> cls.__launchpad_facetname__
    'FACET'

Next, a functional/integration test of the overridden directive.

    >>> print queryMultiAdapter((fooobject, request), name='+whatever')
    None
    >>> print queryMultiAdapter((fooobject, request), name='+mandrill')
    None

    >>> from zope.configuration import xmlconfig
    >>> zcmlcontext = xmlconfig.string("""
    ... <configure xmlns:browser="http://namespaces.zope.org/browser"
    ...     package="lp.services">
    ...   <include file="webapp/meta-overrides.zcml" />
    ...   <browser:page
    ...     for="lp.testing.IFoo"
    ...     name="+whatever"
    ...     permission="zope.Public"
    ...     class="lp.testing.FooView"
    ...     attribute="__call__"
    ...     facet="the_evil_facet"
    ...     layer="lp.testing.IFooLayer"
    ...     />
    ...   <browser:page
    ...     for="lp.testing.IFoo"
    ...     name="+mandrill"
    ...     permission="zope.Public"
    ...     template="../../lp/app/templates/base-layout.pt"
    ...     facet="another-mister-lizard"
    ...     layer="lp.testing.IFooLayer"
    ...     />
    ... </configure>
    ... """)

    >>> whatever_view = queryMultiAdapter(
    ...     (fooobject, request), name='+whatever')

    >>> print whatever_view.__class__.__name__
    FooView
    >>> print whatever_view.__launchpad_facetname__
    the_evil_facet
    >>> mandrill_view = queryMultiAdapter(
    ...     (fooobject, request), name='+mandrill')

    >>> print mandrill_view.__class__.__name__
    SimpleViewClass from ...base-layout.pt
    >>> print mandrill_view.__launchpad_facetname__
    another-mister-lizard


== Overriding the browser:pages directive ==

We override browser:pages to allow us to specify the facet that each
page is associated with.

First, a unit test of the overridden directive.

    >>> from lp.services.webapp.metazcml import pages
    >>> context = Context()
    >>> context.info = "INFO"

Name some variables to mirror the names used as arguments.

    >>> for_ = "FOR_"
    >>> permission = "PERMISSION"

    >>> name = "NAME"
    >>> facet = "FACET"

The facet specified for the outer pages element will be used only when a
facet is not specified for the inner page.

    >>> P = pages(context, permission, for_, facet="OUTERFACET")
    >>> P.page(context, name, facet=facet)
    >>> P.page(context, "OTHER NAME")

Look for the SimpleLaunchpadViewClass in the data structure above, and check
its __launchpad_facetname__ attribute.

    >>> cls = get_simplelaunchpadviewclass(context.actions)
    >>> cls
    <class 'zope.browserpage.metaconfigure.SimpleLaunchpadViewClass'>
    >>> cls.__launchpad_facetname__
    'FACET'
    >>> cls2 = context.actions[3][2][1]
    >>> cls2
    <class 'zope.browserpage.metaconfigure.SimpleLaunchpadViewClass'>
    >>> cls2.__launchpad_facetname__
    'OUTERFACET'

Next, a functional/integration test of the overridden directive.

    >>> print queryMultiAdapter((fooobject, request), name='+whatever2')
    None

    >>> zcmlcontext = xmlconfig.string("""
    ... <configure xmlns:browser="http://namespaces.zope.org/browser"
    ...     package="lp.services">
    ...   <include file="webapp/meta-overrides.zcml" />
    ...   <browser:pages
    ...     for="lp.testing.IFoo"
    ...     layer="lp.testing.IFooLayer"
    ...     class="lp.testing.FooView"
    ...     facet="outerspace"
    ...     permission="zope.Public">
    ...     <browser:page
    ...         name="+whatever2"
    ...         attribute="__call__"
    ...         facet="another_evil_facet"
    ...         />
    ...     <browser:page
    ...         name="+whatever3"
    ...         attribute="__call__"
    ...         />
    ...   </browser:pages>
    ... </configure>
    ... """)

    >>> whatever2_view = queryMultiAdapter(
    ...     (fooobject, request), name='+whatever2')
    >>> print whatever2_view.__class__.__name__
    FooView
    >>> print whatever2_view.__launchpad_facetname__
    another_evil_facet

    >>> whatever3_view = queryMultiAdapter(
    ...     (fooobject, request), name='+whatever3')
    >>> print whatever3_view.__class__.__name__
    FooView
    >>> print whatever3_view.__launchpad_facetname__
    outerspace


== Overriding the browser:editform directive ==

We override browser:editform to allow us to specify the facet that the
form is associated with.

First, a unit test of the overridden directive.

    >>> from lp.services.webapp.metazcml import EditFormDirective
    >>> context = Context()
    >>> context.info = "INFO"

Name some variables to mirror the names used as arguments.

    >>> for_ = "FOR_"
    >>> name = "NAME"
    >>> facet = "FACET"
    >>> schema = Interface

    >>> EF = EditFormDirective(context, for_=for_, permission=permission,
    ...     schema=schema, name=name, facet=facet)
    >>> EF()

# Another pointless test -- StuartBishop 20060411
#     >>> context
#     ((('view',
#        'FOR_',
#        'NAME',
#        <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>,
#        <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
#       <function EditViewFactory>,
#       ('NAME',
#        <InterfaceClass zope.interface.Interface>,
#        None,
#        'PERMISSION',
#        <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>,
#        'edit.pt',
#        'edit.pt',
#        (<class 'zope.app.form.browser.editview.EditView'>,
#         <class 'lp.services.webapp.metazcml.SimpleLaunchpadViewClass'>),
#        'FOR_',
#        [])),)

Look for the SimpleLaunchpadViewClass in the data structure above, and check
its __launchpad_facetname__ attribute.

    >>> cls = get_simplelaunchpadviewclass(context.actions)
    >>> print cls.__name__
    SimpleLaunchpadViewClass
    >>> print cls.__launchpad_facetname__
    FACET

Next, a functional/integration test of the overridden directive.

    >>> print queryMultiAdapter((fooobject, request), name='+someeditform')
    None

    >>> zcmlcontext = xmlconfig.string("""
    ... <configure xmlns:browser="http://namespaces.zope.org/browser"
    ...     package="lp.services">
    ...   <include file="webapp/meta-overrides.zcml" />
    ...   <browser:editform
    ...     for="lp.testing.IFoo"
    ...     name="+someeditform"
    ...     layer="lp.testing.IFooLayer"
    ...     schema="zope.interface.Interface"
    ...     facet="another_mister_lizard"
    ...     permission="zope.Public"/>
    ... </configure>
    ... """)

    >>> whatever_view = queryMultiAdapter(
    ...     (fooobject, request), name='+someeditform')
    >>> print whatever_view.__class__.__name__
    SimpleViewClass from edit.pt
    >>> print whatever_view.__launchpad_facetname__
    another_mister_lizard


== Overriding the browser:addform directive ==

We override browser:addform to allow us to specify the facet that the
form is associated with.

First, a unit test of the overridden directive.

    >>> from lp.services.webapp.metazcml import AddFormDirective
    >>> context = Context()
    >>> context.info = "INFO"

Name some variables to mirror the names used as arguments.

    >>> for_ = "FOR_"
    >>> name = "NAME"
    >>> facet = "FACET"
    >>> schema = Interface

    >>> AF = AddFormDirective(context, for_=for_, permission=permission,
    ...     schema=schema, name=name, facet=facet)
    >>> AF()

# Another pointless test -- StuartBishop 20060411
#    >>> context
#    ((('view',
#       'FOR_',
#       'NAME',
#       <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>,
#       <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
#      <function AddViewFactory>,
#      ('NAME',
#       <InterfaceClass zope.interface.Interface>,
#       None,
#       'PERMISSION',
#       <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>,
#       'add.pt',
#       'add.pt',
#       (<class 'zope.app.form.browser.add.AddView'>,
#        <class 'lp.services.webapp.metazcml.SimpleLaunchpadViewClass'>),
#       'FOR_',
#       [],
#       None,
#       None,
#       None,
#       None,
#       [])),)

Look for the SimpleLaunchpadViewClass in the data structure above, and check
its __launchpad_facetname__ attribute.

    >>> cls = get_simplelaunchpadviewclass(context.actions)
    >>> print cls.__name__
    SimpleLaunchpadViewClass
    >>> print cls.__launchpad_facetname__
    FACET

Next, a functional/integration test of the overridden directive.

    >>> print queryMultiAdapter((fooobject, request), name='+someaddform')
    None

    >>> zcmlcontext = xmlconfig.string("""
    ... <configure xmlns:browser="http://namespaces.zope.org/browser"
    ...     package="lp.services">
    ...   <include file="webapp/meta-overrides.zcml" />
    ...   <browser:addform
    ...     for="lp.testing.IFoo"
    ...     name="+someaddform"
    ...     layer="lp.testing.IFooLayer"
    ...     schema="zope.interface.Interface"
    ...     facet="yet_another_mister_lizard"
    ...     permission="zope.Public"/>
    ... </configure>
    ... """)

    >>> whatever_view = queryMultiAdapter(
    ...     (fooobject, request), name='+someaddform')
    >>> print whatever_view.__class__.__name__
    SimpleViewClass from add.pt
    >>> print whatever_view.__launchpad_facetname__
    yet_another_mister_lizard


== Overriding the browser:schemadisplay directive ==

We override browser:schemadisplay to allow us to specify the facet that the
form is associated with.

First, a unit test of the overridden directive.

    >>> from lp.services.webapp.metazcml import SchemaDisplayDirective
    >>> context = Context()
    >>> context.info = "INFO"

Name some variables to mirror the names used as arguments.

    >>> for_ = "FOR_"
    >>> name = "NAME"
    >>> facet = "FACET"
    >>> schema = Interface

    >>> SDD = SchemaDisplayDirective(context, for_=for_, permission=permission,
    ...     schema=schema, name=name, facet=facet)
    >>> SDD()

# Pointless test -- StuartBishop 20060411
#     >>> context
#     ((('view',
#        'FOR_',
#        'NAME',
#        <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>,
#        <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
#       <function DisplayViewFactory>,
#       ('NAME',
#        <InterfaceClass zope.interface.Interface>,
#        None,
#        'PERMISSION',
#        <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>,
#        'display.pt',
#        'display.pt',
#        (<class 'zope.app.form.browser.schemadisplay.DisplayView'>,
#         <class 'lp.services.webapp.metazcml.SimpleLaunchpadViewClass'>),
#        'FOR_',
#        [],
#        None)),)

Look for the SimpleLaunchpadViewClass in the data structure above, and check
its __launchpad_facetname__ attribute.

    >>> cls = get_simplelaunchpadviewclass(context.actions)
    >>> print cls.__name__
    SimpleLaunchpadViewClass
    >>> print cls.__launchpad_facetname__
    FACET

Next, a functional/integration test of the overridden directive.

    >>> print queryMultiAdapter(
    ...     (fooobject, request), name='+someschemadisplay')
    None

    >>> zcmlcontext = xmlconfig.string("""
    ... <configure xmlns:browser="http://namespaces.zope.org/browser"
    ...     package="lp.services">
    ...   <include file="webapp/meta-overrides.zcml" />
    ...   <browser:schemadisplay
    ...     for="lp.testing.IFoo"
    ...     name="+someschemadisplay"
    ...     layer="lp.testing.IFooLayer"
    ...     schema="zope.interface.Interface"
    ...     facet="hurrah_yet_another_mister_lizard"
    ...     permission="zope.Public"/>
    ... </configure>
    ... """)

    >>> schemadisplay_view = queryMultiAdapter(
    ...     (fooobject, request), name='+someschemadisplay')
    >>> print schemadisplay_view.__class__.__name__
    SimpleViewClass from display.pt
    >>> print schemadisplay_view.__launchpad_facetname__
    hurrah_yet_another_mister_lizard


== Overriding zope:configure to add a facet attribute ==

We override the grouping directive zope:configure to add a 'facet' attribute
that can be inherited by all of the directives it contains.

    >>> from lp.services.webapp.metazcml import GroupingFacet
    >>> context = Context()

Name some variables to mirror the names used as arguments.

    >>> facet = 'whole-file-facet'

    >>> gc = GroupingFacet(context, facet=facet)
    >>> gc.facet
    'whole-file-facet'

Next, a functional/integration test of the overridden directive.

    >>> print queryMultiAdapter((fooobject, request), name='+impliedfacet')
    None

    >>> zcmlcontext = xmlconfig.string("""
    ... <configure xmlns="http://namespaces.zope.org/zope"
    ...            xmlns:browser="http://namespaces.zope.org/browser"
    ...     package="lp.services">
    ...   <include file="webapp/meta.zcml" />
    ...   <include file="webapp/meta-overrides.zcml" />
    ...   <facet facet="whole-facet">
    ...     <browser:page
    ...       for="lp.testing.IFoo"
    ...       name="+impliedfacet"
    ...       permission="zope.Public"
    ...       class="lp.testing.FooView"
    ...       attribute="__call__"
    ...       layer="lp.testing.IFooLayer"
    ...       />
    ...     <browser:addform
    ...       for="lp.testing.IFoo"
    ...       name="+impliedfacet-add"
    ...       layer="lp.testing.IFooLayer"
    ...       schema="zope.interface.Interface"
    ...       permission="zope.Public"/>
    ...     <browser:editform
    ...       for="lp.testing.IFoo"
    ...       name="+impliedfacet-edit"
    ...       layer="lp.testing.IFooLayer"
    ...       schema="zope.interface.Interface"
    ...       permission="zope.Public"/>
    ...     <browser:schemadisplay
    ...       for="lp.testing.IFoo"
    ...       name="+impliedfacet-schemadisplay"
    ...       layer="lp.testing.IFooLayer"
    ...       schema="zope.interface.Interface"
    ...       permission="zope.Public"/>
    ...   </facet>
    ... </configure>
    ... """)

    >>> impliedfacet_view = queryMultiAdapter(
    ...     (fooobject, request), name='+impliedfacet')
    >>> print impliedfacet_view.__class__.__name__
    FooView
    >>> print impliedfacet_view.__launchpad_facetname__
    whole-facet

    >>> impliedfacet_add = queryMultiAdapter(
    ...     (fooobject, request), name='+impliedfacet-add')
    >>> print impliedfacet_add.__class__.__name__
    SimpleViewClass from add.pt
    >>> print impliedfacet_add.__launchpad_facetname__
    whole-facet

    >>> impliedfacet_edit = queryMultiAdapter(
    ...     (fooobject, request), name='+impliedfacet-edit')
    >>> print impliedfacet_edit.__class__.__name__
    SimpleViewClass from edit.pt
    >>> print impliedfacet_edit.__launchpad_facetname__
    whole-facet

    >>> impliedfacet_schemadisplay = queryMultiAdapter(
    ...     (fooobject, request), name='+impliedfacet-schemadisplay')
    >>> print impliedfacet_schemadisplay.__class__.__name__
    SimpleViewClass from display.pt
    >>> print impliedfacet_schemadisplay.__launchpad_facetname__
    whole-facet


== Overriding zope:permission ==

The permissions used in Launchpad must also specify the level of access
they require ('read' or 'write'), so our zope:permission directive will
register an ILaunchpadPermission with the given access_level instead of
an IPermission.

    >>> zcmlcontext = xmlconfig.string("""
    ... <configure xmlns="http://namespaces.zope.org/zope"
    ...     i18n_domain="canonical">
    ...   <include file="lib/lp/services/webapp/meta-overrides.zcml" />
    ...   <permission id="foo.bar" title="Foo Bar" access_level="read" />
    ... </configure>
    ... """)
    >>> from lp.services.webapp.metazcml import ILaunchpadPermission
    >>> from lp.services.webapp.testing import verifyObject
    >>> permission = getUtility(ILaunchpadPermission, 'foo.bar')
    >>> verifyObject(ILaunchpadPermission, permission)
    True
    >>> permission.access_level
    u'read'


== Cleaning up the interfaces and objects to test with ==

Clean up the interfaces we created for testing with.

    >>> del lp.testing.IFoo
    >>> del lp.testing.IFooLayer
    >>> del lp.testing.FooView