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

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

Fixing pep8

File size: 3.5 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.Five import fiveconfigure
11from Products.CMFCore.utils import getToolByName
12from Products.CMFCore.interfaces import IAction, IActionCategory
13from Products.CMFCore.ActionInformation import Action, ActionCategory
14from  Products.PloneTestCase import PloneTestCase as ptc
15from  Products.PloneTestCase.layer import onsetup
16
17from quintagroup.plonetabs.tests.data import PORTAL_ACTIONS, PORTAL_CONTENT
18
19#ztc.installProduct('Zope2Product')
20
21
22@onsetup
23def setup_package():
24    import quintagroup.plonetabs
25    zcml.load_config('configure.zcml', quintagroup.plonetabs)
26    #ztc.installPackage('some.other.package')
27    ztc.installPackage('quintagroup.plonetabs')
28
29setup_package()
30ptc.setupPloneSite(products=['quintagroup.plonetabs'])
31
32_marker = object()
33
34
35class 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
Note: See TracBrowser for help on using the repository browser.