source: products/quintagroup.seoptimizer/trunk/quintagroup/seoptimizer/tests/testQSEOptimizerResponse.py @ 1493

Last change on this file since 1493 was 1493, checked in by liebster, 14 years ago

Modified structure tests

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