1 | try: |
---|
2 | from zope.annotation.interfaces import IAnnotations |
---|
3 | IAnnotations # pyflakes |
---|
4 | except ImportError: |
---|
5 | from zope.app.annotation.interfaces import IAnnotations |
---|
6 | from plone.browserlayer.layer import mark_layer |
---|
7 | |
---|
8 | from Testing import ZopeTestCase as ztc |
---|
9 | from Products.Five import zcml |
---|
10 | from Products.CMFCore.utils import getToolByName |
---|
11 | from Products.CMFCore.ActionInformation import Action, ActionCategory |
---|
12 | from Products.PloneTestCase import PloneTestCase as ptc |
---|
13 | from Products.PloneTestCase.layer import onsetup |
---|
14 | |
---|
15 | from quintagroup.plonetabs.tests.data import PORTAL_ACTIONS, PORTAL_CONTENT |
---|
16 | |
---|
17 | #ztc.installProduct('Zope2Product') |
---|
18 | |
---|
19 | |
---|
20 | @onsetup |
---|
21 | dejf setup_package(): |
---|
22 | import quintagroup.plonetabs |
---|
23 | zcml.load_config('configure.zcml', quintagroup.plonetabs) |
---|
24 | #ztc.installPackage('some.other.package') |
---|
25 | ztc.installPackage('quintagroup.plonetabs') |
---|
26 | |
---|
27 | setup_package() |
---|
28 | if ptc.PLONE30: |
---|
29 | ptc.setupPloneSite(products=["plone.browserlayer"]) |
---|
30 | ptc.setupPloneSite(products=['quintagroup.plonetabs']) |
---|
31 | |
---|
32 | _marker = object() |
---|
33 | |
---|
34 | |
---|
35 | class PloneTabsTestCase(ptc.PloneTestCase): |
---|
36 | """Common test base class""" |
---|
37 | |
---|
38 | def afterSetUp(self): |
---|
39 | # due to some reason plone.browserlayer is not marking REQUEST |
---|
40 | # with installed products layer interfaces |
---|
41 | # so I'm doing it manually here |
---|
42 | class DummyEvent(object): |
---|
43 | def __init__(self, request): |
---|
44 | self.request = request |
---|
45 | mark_layer(self.portal, DummyEvent(self.portal.REQUEST)) |
---|
46 | |
---|
47 | def purgeCache(self, request): |
---|
48 | annotations = IAnnotations(request) |
---|
49 | cache = annotations.get('plone.memoize', _marker) |
---|
50 | if cache is not _marker: |
---|
51 | del annotations['plone.memoize'] |
---|
52 | |
---|
53 | def purgeActions(self): |
---|
54 | for obj in self.tool.objectValues(): |
---|
55 | self.tool._delObject(obj.id) |
---|
56 | #if IAction.providedBy(obj): |
---|
57 | #self.tool._delObject(obj.id) |
---|
58 | #elif IActionCategory.providedBy(obj): |
---|
59 | #obj.manage_delObjects(ids=obj.objectIds()) |
---|
60 | |
---|
61 | def setupActions(self, parent, kids=PORTAL_ACTIONS): |
---|
62 | ids = parent.objectIds() |
---|
63 | for id, child in kids: |
---|
64 | if child['type'] == 'action' and id not in ids: |
---|
65 | parent._setObject(id, Action(id, **child)) |
---|
66 | continue |
---|
67 | if child['type'] == 'category': |
---|
68 | if id not in ids: |
---|
69 | parent._setObject(id, ActionCategory(id)) |
---|
70 | if child.get('children', {}): |
---|
71 | self.setupActions(getattr(parent, id), child['children']) |
---|
72 | |
---|
73 | def purgeContent(self): |
---|
74 | ids = [obj.id for obj in self.portal.listFolderContents()] |
---|
75 | self.portal.manage_delObjects(ids=ids) |
---|
76 | |
---|
77 | def setupContent(self, parent, kids=PORTAL_CONTENT): |
---|
78 | ids = parent.objectIds() |
---|
79 | for id, child in kids: |
---|
80 | if id not in ids: |
---|
81 | self._createType(parent, child['type'], id, **child) |
---|
82 | if child.get('children', {}) and id in ids: |
---|
83 | self.setupContent(getattr(parent, id), child['children']) |
---|
84 | |
---|
85 | def _createType(self, container, portal_type, id, **kwargs): |
---|
86 | """Helper method to create content objects""" |
---|
87 | ttool = getToolByName(container, 'portal_types') |
---|
88 | portal_catalog = getToolByName(container, 'portal_catalog') |
---|
89 | |
---|
90 | fti = ttool.getTypeInfo(portal_type) |
---|
91 | fti.constructInstance(container, id, **kwargs) |
---|
92 | obj = getattr(container.aq_inner.aq_explicit, id) |
---|
93 | |
---|
94 | # publish and reindex |
---|
95 | #self._publish_item(portal, obj) |
---|
96 | portal_catalog.indexObject(obj) |
---|
97 | return obj |
---|