source: products/quintagroup.analytics/trunk/quintagroup/analytics/tests.py @ 3017

Last change on this file since 3017 was 3017, checked in by fenix, 13 years ago

added test for 'ovnership by state' view.

File size: 11.5 KB
Line 
1import unittest
2import transaction
3
4from AccessControl.SecurityManagement import newSecurityManager
5from zope.component import testing, queryMultiAdapter
6from Testing import ZopeTestCase as ztc
7
8from Products.Five import zcml
9from Products.Five import fiveconfigure
10from Products.PloneTestCase import PloneTestCase as ptc
11from Products.PloneTestCase import setup as ptc_setup
12from Products.PloneTestCase.layer import PloneSite
13import quintagroup.analytics
14ptc.setupPloneSite()
15
16class Installed(PloneSite):
17
18    @classmethod
19    def setUp(cls):
20        fiveconfigure.debug_mode = True
21        zcml.load_config('configure.zcml',
22                         quintagroup.analytics)
23        fiveconfigure.debug_mode = False
24        ztc.installPackage('quintagroup.analytics')
25        app = ztc.app()
26        portal = app[ptc_setup.portal_name]
27
28        # Sets the local site/manager
29        ptc_setup._placefulSetUp(portal)
30
31        qi = getattr(portal, 'portal_quickinstaller', None)
32        qi.installProduct('quintagroup.analytics')
33        transaction.commit()
34
35    @classmethod
36    def tearDown(cls):
37            pass
38
39class SetUpContent(Installed):
40
41    max = 10
42    types_ = ['Document', 'Event', 'Folder']
43    users = [('user%s'%i, 'user%s'%i, 'Member', None)
44             for i in xrange(max)]
45
46    @classmethod
47    def setupUsers(cls, portal):
48        """ Creates users."""
49        acl_users = portal.acl_users
50        mp = portal.portal_membership
51        map(acl_users._doAddUser, *zip(*cls.users))
52        if not mp.memberareaCreationFlag:
53            mp.setMemberareaCreationFlag()
54        map(mp.createMemberArea, [u[0] for u in cls.users])
55
56    @classmethod
57    def setupContent(cls, portal):
58        """ Creates test content."""
59        uf = portal.acl_users
60        pm = portal.portal_membership
61        pc = portal.portal_catalog
62        users = [u[0] for u in cls.users]
63        for u in users:
64            folder = pm.getHomeFolder(u)
65            user = uf.getUserById(u)
66            if not hasattr(user, 'aq_base'):
67                user = user.__of__(uf)
68            newSecurityManager(None, user)
69            for i in xrange(users.index(u)+cls.max):
70                map(folder.invokeFactory, cls.types_, [t+str(i) for t in cls.types_])
71        transaction.commit()
72
73
74    @classmethod
75    def setUp(cls):
76        app = ztc.app()
77        portal = app[ptc_setup.portal_name]
78        cls.setupUsers(portal)
79        cls.setupContent(portal)
80
81    @classmethod
82    def tearDown(cls):
83        pass
84
85class TestCase(ptc.PloneTestCase):
86    layer = Installed
87
88#TO DO:=====================================================================
89#      add tests for every views methods;
90#      add doc tests to validate if all needed elements are present on page;
91
92class TestQAInstallation(TestCase):
93    """ This class veryfies registrations of all needed views and
94        actions.
95    """
96
97    def test_cp_action_installation(self):
98        """This test validates control panel action. """
99        control_panel = self.portal.portal_controlpanel
100        self.assert_('QAnalytics' in [a.id for a in control_panel.listActions()],
101                     "Configlet for quintagroup.analitycs isn't registered.")
102
103    def test_OwnershipByType(self):
104        """ This test validates registration of
105            ownership_by_type view.
106        """
107        view = queryMultiAdapter((self.portal, self.portal.REQUEST),
108                                 name="ownership_by_type")
109
110        self.assert_(view, "Ownership by type view isn't registered")
111
112    def test_OwnershipByState(self):
113        """ This test validates registration of
114            ownership_by_state view.
115        """
116        view = queryMultiAdapter((self.portal, self.portal.REQUEST),
117                                 name="ownership_by_state")
118
119        self.assert_(view, "Ownership by state view isn't registered")
120
121    def test_TypeByState(self):
122        """ This test validates registration of
123            type_by_state view.
124        """
125        view = queryMultiAdapter((self.portal, self.portal.REQUEST),
126                                 name="type_by_state")
127
128        self.assert_(view, "Type by state view isn't registered")
129
130    def test_LegacyPortlets(self):
131        """ This test validates registration of
132            legacy_portlets view.
133        """
134        view = queryMultiAdapter((self.portal, self.portal.REQUEST),
135                                 name="legacy_portlets")
136
137        self.assert_(view, "Legacy Portlets view isn't registered")
138
139    def test_PropertiesStats(self):
140        """ This test validates registration of
141            properties_stats view.
142        """
143        view = queryMultiAdapter((self.portal, self.portal.REQUEST),
144                                 name="properties_stats")
145
146        self.assert_(view, "Properties Stats view isn't registered")
147
148
149    def test_PortletsStats(self):
150        """ This test validates registration of
151            portlets_stats view.
152        """
153        view = queryMultiAdapter((self.portal, self.portal.REQUEST),
154                                 name="portlets_stats")
155
156        self.assert_(view, "Portlets Stats view isn't registered")
157
158class TestOwnershipByType(TestCase):
159    """Tests all ownership by type view methods."""
160
161    layer = SetUpContent
162
163    def afterSetUp(self):
164        self.view = queryMultiAdapter((self.portal, self.portal.REQUEST),
165                                 name="ownership_by_type")
166        self.pc = self.portal.portal_catalog
167
168    def test_getUsers(self):
169        """ Tests method that returns ordered list of users."""
170        users = [u[0] for u in self.layer.users]
171        users.reverse()
172        self.assert_(False not in map(lambda u1, u2:u1==u2,
173                     users, self.view.getUsers()))
174
175    def test_getTypes(self):
176        """ Tests method that returns ordered list of types."""
177        data = {}
178        index = self.pc._catalog.getIndex('portal_type')
179        for k in index._index.keys():
180            if not k:
181                continue
182            haslen = hasattr(index._index[k], '__len__')
183            if haslen:
184                data[k] = len(index._index[k])
185            else:
186                data[k] = 1
187        data = data.items()
188        data.sort(lambda a, b: a[1] - b[1])
189        data.reverse()
190        types = [i[0] for i in data]
191        self.assert_(False not in map(lambda t1, t2:t1==t2,
192                     self.view.getTypes(), types))
193
194    def test_getContent(self):
195        """ This test verifies method that returns list of numbers.
196            Each number is amount of specified content type objects
197            that owned  by particular user.
198        """
199        # we need to login in to the site as Manager to be able to
200        # see catalog results
201        self.loginAsPortalOwner()
202
203        for type_ in self.layer.types_:
204            self.assert_(False not in \
205            map(lambda i, j:i==j, [len(self.pc(portal_type=type_, Creator=user))
206                                   for user in self.view.getUsers()],
207                                  self.view.getContent(type_)))
208
209    def test_getChart(self):
210        """ This test verifies creation of chart image tag."""
211        chart_tag = """<img src="http://chart.apis.google.com/chart?chxt=y&amp;
212                       chds=0,57&amp;chd=t:19.0,18.0,17.0,16.0,15.0,14.0,
213                       13.0,12.0,11.0,10.0|19.0,18.0,17.0,16.0,15.0,14.0,13.0,
214                       12.0,11.0,10.0|19.0,18.0,17.0,16.0,15.0,14.0,13.0,12.0,
215                       11.0,10.0|0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0|&amp;
216                       chxr=0,0,57&amp;chco=669933,cc9966,993300,ff6633,e8e4e3,
217                       a9a486,dcb57e,ffcc99,996633,333300,00ff00&amp;chl=user9|
218                       user8|user7|user6|user5|user4|user3|user2|user1|user0&amp;
219                       chbh=a,10,0&amp;chs=800x375&amp;cht=bvs&amp;
220                       chtt=Content+ownership+by+type&amp;chdl=Folder|Document|
221                       Event|Topic|Other+types&amp;chdlp=b" />"""
222        self.loginAsPortalOwner()
223        self.assertEqual(*map(lambda s:''.join(s.split()),
224                              [chart_tag, self.view.getChart()]))
225
226class TestOwnershipByState(TestCase):
227    """Tests all ownership by state view methods."""
228
229    layer = SetUpContent
230
231    states = ['private', 'published', 'pending']
232
233    def afterSetUp(self):
234        self.view = queryMultiAdapter((self.portal, self.portal.REQUEST),
235                                 name="ownership_by_state")
236        self.pc = self.portal.portal_catalog
237
238    def test_getUsers(self):
239        """ Tests method that returns ordered list of users."""
240        users = [u[0] for u in self.layer.users]
241        users.reverse()
242        self.assert_(False not in map(lambda u1, u2:u1==u2,
243                     users, self.view.getUsers()))
244
245    def test_getStates(self):
246        """ Tests method that returns ordered list of states."""
247        self.assert_(False not in map(lambda s1, s2:s1==s2,
248                     ['private', 'published'], self.view.getStates()))
249
250    def test_getContent(self):
251        """ This test verifies method that returns list of numbers.
252            Each number is amount of specified content type objects
253            that are in particular workflow state.
254        """
255        # we need to login in to the site as Manager to be able to
256        # see catalog results
257        self.loginAsPortalOwner()
258
259        for state in self.states:
260            self.assert_(False not in \
261            map(lambda i, j:i==j,[len(self.pc(review_state=state, Creator=user))
262                                  for user in self.view.getUsers()],
263                                 self.view.getContent(state)))
264
265    def test_getChart(self):
266        """ This test verifies creation of chart image tag."""
267        chart_tag = """<imgsrc="http://chart.apis.google.com/chart?chxt=y&amp;
268                       chds=0,57&amp;chd=t:57.0,54.0,51.0,48.0,45.0,42.0,39.0,
269                       36.0,33.0,30.0|0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,
270                       0.0|0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0&amp;chxr=0,
271                       0,57&amp;chco=669933,cc9966,993300,ff6633,e8e4e3,a9a486,
272                       dcb57e,ffcc99,996633,333300,00ff00&amp;chl=user9|user8|
273                       user7|user6|user5|user4|user3|user2|user1|user0&amp;
274                       chbh=a,10,0&amp;chs=800x375&amp;cht=bvs&amp;
275                       chtt=Content+ownership+by+state&amp;chdl=private|
276                       published|No+workflow&amp;chdlp=b"/>"""
277        self.loginAsPortalOwner()
278        self.assertEqual(*map(lambda s:''.join(s.split()),
279                              [chart_tag, self.view.getChart()]))
280def test_suite():
281    from unittest import TestSuite, makeSuite
282
283    test_suite = unittest.TestSuite([
284
285        # Unit tests
286        #doctestunit.DocFileSuite(
287        #    'README.txt', package='quintagroup.contentstats',
288        #    setUp=testing.setUp, tearDown=testing.tearDown),
289
290        #doctestunit.DocTestSuite(
291        #    module='quintagroup.contentstats.mymodule',
292        #    setUp=testing.setUp, tearDown=testing.tearDown),
293
294
295        # Integration tests that use PloneTestCase
296        #ztc.ZopeDocFileSuite(
297        #    'README.txt', package='quintagroup.contentstats',
298        #    test_class=TestCase),
299
300        #ztc.FunctionalDocFileSuite(
301        #    'browser.txt', package='quintagroup.contentstats',
302        #    test_class=TestCase),
303
304        ])
305
306    test_suite.addTest(makeSuite(TestQAInstallation))
307    test_suite.addTest(makeSuite(TestOwnershipByType))
308    test_suite.addTest(makeSuite(TestOwnershipByState))
309    return test_suite
310
311if __name__ == '__main__':
312    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the repository browser.