source: products/quintagroup.seoptimizer/trunk/quintagroup/seoptimizer/tests/testContextForm.py @ 3223

Last change on this file since 3223 was 3140, checked in by zidane, 13 years ago

fixes pylint

File size: 7.4 KB
Line 
1import urllib
2from cStringIO import StringIO
3
4from quintagroup.seoptimizer.tests.base import FunctionalTestCase, \
5    IRAMCache, getUtility
6from Products.PloneTestCase.PloneTestCase import portal_owner, \
7    default_password
8import re
9
10CUSTOM_METATAGS = [
11   {'meta_name': 'metatag1', 'meta_content': 'metatag1value'},
12   {'meta_name': 'metatag2', 'meta_content': 'metatag2value'},
13   {'meta_name': 'metatag3', 'meta_content': ''}
14]
15
16VIEW_METATAGS = [
17    'DC.creator', 'DC.format', 'DC.date.modified',
18    'DC.date.created', 'DC.type', 'DC.distribution',
19    'description', 'keywords', 'robots', 'distribution'
20]
21
22FORM = {
23    'seo_robots': 'ALL',
24    'form.submitted:int': 1,
25    'form.button.Save': "Save",
26    'seo_title': 'hello world',
27    'seo_title_override:int': 1,
28    'seo_robots_override:int': 1,
29    'seo_distribution': 'Global',
30    'seo_keywords_override:int': 1,
31    'seo_keywords:list': 'keyword1',
32    'seo_description_override:int': 1,
33    'seo_html_comment': 'no comments',
34    'seo_html_comment_override:int': 1,
35    'seo_distribution_override:int': 1,
36    'seo_custommetatags_override:int': 1,
37    'seo_description': 'it is description, test keyword1',
38}
39
40METATAG = '.*(<meta\s+(?:(?:name="%s"\s*)|(?:content="%s"\s*)){2}/>)'
41
42
43class TestContextForm(FunctionalTestCase):
44
45    def afterSetUp(self):
46        self.sp = self.portal.portal_properties.seo_properties
47        self.pu = self.portal.plone_utils
48        self.wf = self.portal.portal_workflow
49
50        self.basic_auth = ':'.join((portal_owner, default_password))
51        self.loginAsPortalOwner()
52
53        # Preparation for functional testing'
54        # create document for test
55        my_doc = self.portal.invokeFactory('Document', id='my_doc')
56        my_doc = self.portal['my_doc']
57        self.abs_path = "/%s" % my_doc.absolute_url(1)
58        # prepare seo context form data
59        self.sp.manage_changeProperties(default_custom_metatags='metatag1|' \
60                                        'global_metatag1value\nmetatag4|' \
61                                        'global_metatag4value')
62        st = ''
63        for d in CUSTOM_METATAGS:
64            st += '&seo_custommetatags.meta_name:records=%s' % d['meta_name']
65            st += '&seo_custommetatags.meta_content:records=%s' \
66                  % d['meta_content']
67        # update seo properties for the test document and publish it
68        self.publish(path=self.abs_path + '/@@seo-context-properties',
69                     basic=self.basic_auth, request_method='POST',
70                     stdin=StringIO(urllib.urlencode(FORM) + st))
71        self.wf.doActionFor(my_doc, 'publish')
72        # get html view of test document
73        self.html = self.publish(self.abs_path, self.basic_auth).getBody()
74
75    def testTitle(self):
76        m = re.match('.*<title>\\s*hello world\\s*</title>', self.html,
77                     re.S | re.M)
78        self.assert_(m, 'Title not set in')
79
80    def testTitleDuplication(self):
81        """If we are not overriding page title and current page title equals
82           title of the plone site then there should be no concatenation of
83           both titles. Only one should be displayed.
84        """
85        # setup page with title equal to plone site's title
86        my_doc2 = self.portal.invokeFactory('Document', id='my_doc2',
87                                            title=self.portal.Title())
88        my_doc2 = self.portal['my_doc2']
89        self.wf.doActionFor(my_doc2, 'publish')
90        html2 = self.publish('/' + my_doc2.absolute_url(1),
91                             self.basic_auth).getBody()
92
93        m = re.match('.*<title>\\s*%s\\s*</title>' % self.portal.Title(),
94                     html2, re.S | re.M)
95        self.assert_(m, 'Title is not set correctly, perhaps it is ' \
96                     'duplicated with plone site title')
97
98    def testDescription(self):
99        m = re.match(METATAG % ("description", FORM['seo_description']),
100                     self.html, re.S | re.M)
101        self.assert_(m, 'Description not set in')
102
103    def testRobots(self):
104        m = re.match(METATAG % ("robots", FORM['seo_robots']), self.html,
105                     re.S | re.M)
106        self.assert_(m, 'Robots not set in')
107
108    def testDistribution(self):
109        m = re.match(METATAG % ("distribution", FORM['seo_distribution']),
110                     self.html, re.S | re.M)
111        self.assert_(m, 'Distribution not set in')
112
113    def testHTMLComments(self):
114        m = re.match('.*<!--\\s*no comments\\s*-->', self.html, re.S | re.M)
115        self.assert_(m, 'Comments not set in')
116
117    def testTagsOrder(self):
118        def is_match(html, mtorder):
119            return re.search('.*'.join(['<meta.*name="%s".*/>' % t \
120                                        for t in mtorder]), html, re.S | re.M)
121
122        metatags_order = [t for t in self.sp.getProperty('metatags_order') \
123                            if t in VIEW_METATAGS]
124        self.assert_(is_match(self.html, metatags_order),
125                     "Meta tags order not supported.")
126
127        metatags_order.reverse()
128        self.assertFalse(is_match(self.html, metatags_order),
129                         "Meta tags order not supported.")
130
131        self.sp.manage_changeProperties(metatags_order=metatags_order)
132        self.assertFalse(is_match(self.html, metatags_order),
133                         "Meta tags order not supported.")
134
135        html = self.publish(self.abs_path, self.basic_auth).getBody()
136        self.assert_(is_match(html, metatags_order),
137                     "Meta tags order not supported.")
138
139    def testCustomMetaTags(self):
140        for tag in CUSTOM_METATAGS:
141            m = re.match(METATAG % (tag['meta_name'], tag['meta_content']),
142                         self.html, re.S | re.M)
143            if tag['meta_content']:
144                self.assert_(m, "Custom meta tag %s not applied." \
145                             % tag['meta_name'])
146            else:
147                self.assert_(not m, "Meta tag %s has no content, but is " \
148                             "present: in the page." % tag['meta_name'])
149
150        m = re.match(METATAG % ("metatag4", "global_metatag4value"), self.html,
151                     re.S | re.M)
152        self.assert_(m, "Global custom meta tag %s not applied." % 'metatag4')
153
154    def testDeleteCustomMetaTags(self):
155        self.sp.manage_changeProperties(default_custom_metatags='metatag1|' \
156                                        'global_metatag1value')
157        form_data = {'seo_custommetatags': CUSTOM_METATAGS,
158                     'seo_custommetatags_override:int': 0,
159                     'form.button.Save': "Save",
160                     'form.submitted:int': 1}
161
162        # Clean-up ram cache
163        getUtility(IRAMCache).invalidateAll()
164
165        self.publish(path=self.abs_path + '/@@seo-context-properties',
166                     basic=self.basic_auth, request_method='POST',
167                     stdin=StringIO(urllib.urlencode(form_data)))
168        html = self.publish(self.abs_path, self.basic_auth).getBody()
169
170        m = re.match(METATAG % ("metatag4", "global_metatag4value"), html,
171                     re.S | re.M)
172        self.assert_(not m, "Global custom meta tag %s is prosent in the " \
173                     "page." % 'metatag4')
174
175        m = re.match(METATAG % ("metatag1", "global_metatag1value"), html,
176                     re.S | re.M)
177        self.assert_(m, "Global custom meta tag %s not applied." % 'metatag1')
178
179
180def test_suite():
181    from unittest import TestSuite, makeSuite
182    suite = TestSuite()
183    suite.addTest(makeSuite(TestContextForm))
184    return suite
Note: See TracBrowser for help on using the repository browser.