source: products/quintagroup.seoptimizer/branches/refactoring2.3.0/quintagroup/seoptimizer/tests/testResponse.py @ 1834

Last change on this file since 1834 was 1834, checked in by mylan, 14 years ago

Rename all tests modules to easyer and shorter names

File size: 7.7 KB
Line 
1import urllib, re
2from cStringIO import StringIO
3from base import getToolByName, FunctionalTestCase, newSecurityManager
4from config import *
5
6class TestResponse(FunctionalTestCase):
7
8    def afterSetUp(self):
9        self.sp = self.portal.portal_properties.seo_properties
10        self.pu = self.portal.plone_utils
11        self.basic_auth = 'portal_manager:secret'
12
13        uf = self.app.acl_users
14        uf.userFolderAddUser('portal_manager', 'secret', ['Manager'], [])
15        user = uf.getUserById('portal_manager')
16        if not hasattr(user, 'aq_base'):
17            user = user.__of__(uf)
18        newSecurityManager(None, user)
19
20        '''Preparation for functional testing'''
21        my_doc = self.portal.invokeFactory('Document', id='my_doc')
22        my_doc = self.portal['my_doc']
23        self.canonurl = 'http://nohost/plone/test.html'
24        self.sp.manage_changeProperties(**GLOBAL_CUSTOM_METATAGS)
25        self.sp.manage_changeProperties(settings_use_keywords_sg=3, settings_use_keywords_lg=2)
26        abs_path = "/%s" % my_doc.absolute_url(1)
27        self.form_data = {'seo_description': 'it is description, test keyword1', 'seo_keywords_override:int': 1, 'seo_custommetatags_override:int': 1,
28                        'seo_robots_override:int': 1, 'seo_robots': 'ALL', 'seo_description_override:int': 1, 'seo_canonical_override:int': 1,
29                        'seo_keywords:list': 'keyword1', 'seo_html_comment': 'no comments',
30                        'seo_title_override:int': 1, 'seo_title': 'hello world', 'seo_html_comment_override:int': 1,
31                        'seo_distribution_override:int': 1, 'seo_distribution': 'Global', 'seo_canonical': self.canonurl, 'form.submitted:int': 1}
32        st = ''
33        for d in CUSTOM_METATAGS:
34            st += '&seo_custommetatags.meta_name:records=%s&seo_custommetatags.meta_content:records=%s' % (d['meta_name'],d['meta_content'])
35        self.publish(path=abs_path+'/@@seo-context-properties', basic=self.basic_auth, request_method='POST', stdin=StringIO(urllib.urlencode(self.form_data)+st))
36        #self.publish(abs_path+'/@@seo-context-properties?%s' % urllib.urlencode(self.form_data), self.basic_auth)
37
38        wf_tool = self.portal.portal_workflow
39        wf_tool.doActionFor(my_doc, 'publish')
40
41        self.abs_path = abs_path
42        self.my_doc = my_doc
43        self.html = self.publish(abs_path, self.basic_auth).getBody()
44
45        # now setup page with title equal to plone site's title
46        my_doc2 = self.portal.invokeFactory('Document', id='my_doc2')
47        my_doc2 = self.portal['my_doc2']
48        my_doc2.update(title=self.portal.Title())
49        wf_tool.doActionFor(my_doc2, 'publish')
50        abs_path2 = "/%s" % my_doc2.absolute_url(1)
51        self.html2 = self.publish(abs_path2, self.basic_auth).getBody()
52
53    def testTitle(self):
54        m = re.match('.*<title>\\s*hello world\\s*</title>', self.html, re.S|re.M)
55        self.assert_(m, 'Title not set in')
56
57    def testTitleDuplication(self):
58        """If we are not overriding page title and current page title equals title of the plone site
59        then there should be no concatenation of both titles. Only one should be displayed.
60        """
61        m = re.match('.*<title>\\s*%s\\s*</title>' % self.portal.Title(), self.html2, re.S|re.M)
62        self.assert_(m, 'Title is not set correctly, perhaps it is duplicated with plone site title')
63
64    def testDescription(self):
65        m = re.match('.*(<meta\s+(?:(?:name="description"\s*)|(?:content="it is description, test keyword1"\s*)){2}/>)', self.html, re.S|re.M)
66        self.assert_(m, 'Description not set in')
67
68    def testKeywords(self):
69        m = re.match('.*(<meta\s+(?:(?:name="keywords"\s*)|(?:content="keyword1"\s*)){2}/>)', self.html, re.S|re.M)
70        self.assert_(m, 'Keywords not set in')
71
72    def testRobots(self):
73        m = re.match('.*(<meta\s+(?:(?:name="robots"\s*)|(?:content="ALL"\s*)){2}/>)', self.html, re.S|re.M)
74        self.assert_(m, 'Robots not set in')
75
76    def testDistribution(self):
77        m = re.match('.*(<meta\s+(?:(?:name="distribution"\s*)|(?:content="Global"\s*)){2}/>)', self.html, re.S|re.M)
78        self.assert_(m, 'Distribution not set in')
79
80    def testHTMLComments(self):
81        m = re.match('.*<!--\\s*no comments\\s*-->', self.html, re.S|re.M)
82        self.assert_(m, 'Comments not set in')
83
84    def testTagsOrder(self):
85        metatags_order = [t for t in self.sp.getProperty('metatags_order') if t in VIEW_METATAGS]
86        m = re.search('.*'.join(['<meta.*name="%s".*/>' %t for t in metatags_order]), self.html, re.S|re.M)
87        self.assert_(m, "Meta tags order not supported.")
88
89        metatags_order.reverse()
90        m = re.search('.*'.join(['<meta.*name="%s".*/>' %t for t in metatags_order]), self.html, re.S|re.M)
91        self.assertFalse(m, "Meta tags order not supported.")
92
93        self.sp.manage_changeProperties(**{'metatags_order':metatags_order})
94        html = self.publish(self.abs_path, self.basic_auth).getBody()
95        m = re.search('.*'.join(['<meta.*name="%s".*/>' %t for t in metatags_order]), self.html, re.S|re.M)
96        self.assertFalse(m, "Meta tags order not supported.")
97
98        m = re.search('.*'.join(['<meta.*name="%s".*/>' %t for t in metatags_order]), html, re.S|re.M)
99        self.assert_(m, "Meta tags order not supported.")
100
101
102    def testCustomMetaTags(self):
103        for tag in CUSTOM_METATAGS:
104            m = re.match('.*(<meta\s+(?:(?:name="%(meta_name)s"\s*)|(?:content="%(meta_content)s"\s*)){2}/>)' % tag, self.html, re.S|re.M)
105            if tag['meta_content']:
106                self.assert_(m, "Custom meta tag %s not applied." % tag['meta_name'])
107            else:
108                self.assert_(not m, "Meta tag %s has no content, but is present in the page." % tag['meta_name'])
109        m = re.match('.*(<meta\s+(?:(?:name="metatag4"\s*)|(?:content="global_metatag4value"\s*)){2}/>)', self.html, re.S|re.M)
110        self.assert_(m, "Global custom meta tag %s not applied." % 'metatag4')
111
112    def testDeleteCustomMetaTags(self):
113        self.sp.manage_changeProperties(**{'default_custom_metatags':'metatag1|global_metatag1value'})
114        my_doc = self.my_doc
115        self.form_data = {'seo_custommetatags': CUSTOM_METATAGS,  'seo_custommetatags_override:int': 0, 'form.submitted:int': 1}
116        self.publish(path=self.abs_path+'/@@seo-context-properties', basic=self.basic_auth, request_method='POST', stdin=StringIO(urllib.urlencode(self.form_data)))
117        self.html = self.publish(self.abs_path, self.basic_auth).getBody()
118        m = re.match('.*(<meta\s+(?:(?:name="metatag4"\s*)|(?:content="global_metatag4value"\s*)){2}/>)', self.html, re.S|re.M)
119        self.assert_(not m, "Global custom meta tag %s is prosent in the page." % 'metatag4')
120        m = re.match('.*(<meta\s+(?:(?:name="metatag1"\s*)|(?:content="global_metatag1value"\s*)){2}/>)', self.html, re.S|re.M)
121        self.assert_(m, "Global custom meta tag %s not applied." % 'metatag1')
122
123    def testCanonical(self):
124        m = re.match('.*<link rel="canonical" href="%s" />' % self.canonurl, self.html, re.S|re.M)
125        self.assert_(m, self.canonurl)
126
127    def testDefaultCanonical(self):
128        """Default canonical url mast add document absolute_url
129        """
130        # Delete custom canonical url
131        my_doc = self.portal['my_doc']
132        my_doc._delProperty(id='qSEO_canonical')
133        # Get document without customized canonical url
134        abs_path = "/%s" % my_doc.absolute_url(1)
135        self.html = self.publish(abs_path, self.basic_auth).getBody()
136
137        my_url = my_doc.absolute_url()
138        m = re.match('.*<link rel="canonical" href="%s" />' % my_url, self.html, re.S|re.M)
139        self.assert_(m, my_url)
140
141def test_suite():
142    from unittest import TestSuite, makeSuite
143    suite = TestSuite()
144    suite.addTest(makeSuite(TestResponse))
145    return suite
Note: See TracBrowser for help on using the repository browser.