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

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

#161: Remove keywords tests from testResponce - it's tested in testUsageKeywords

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