source: products/quintagroup.plonetabs/branches/tests/quintagroup/plonetabs/tests/base.py @ 3403

Last change on this file since 3403 was 3403, checked in by potar, 8 years ago

Fixed code according with pylint, pyflakes

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