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

Last change on this file was 3548, checked in by ktarasz, 12 years ago

fix pep8 in testContextForm.py

File size: 7.7 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 = [{'meta_name': 'metatag1', 'meta_content': 'metatag1value'},
11                   {'meta_name': 'metatag2', 'meta_content': 'metatag2value'},
12                   {'meta_name': 'metatag3', 'meta_content': ''}]
13
14VIEW_METATAGS = [
15    'DC.creator', 'DC.format', 'DC.date.modified',
16    'DC.date.created', 'DC.type', 'DC.distribution',
17    'description', 'keywords', 'robots', 'distribution'
18]
19
20FORM = {
21    'seo_robots': 'ALL',
22    'form.submitted:int': 1,
23    'form.button.Save': "Save",
24    'seo_title': 'hello world',
25    'seo_title_override:int': 1,
26    'seo_robots_override:int': 1,
27    'seo_distribution': 'Global',
28    'seo_keywords_override:int': 1,
29    'seo_keywords:list': 'keyword1',
30    'seo_description_override:int': 1,
31    'seo_html_comment': 'no comments',
32    'seo_html_comment_override:int': 1,
33    'seo_noframes': 'noframes set in',
34    'seo_noframes_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 testNoframes(self):
81        m = re.match('.*<noframes>\\s*noframes set in\\s*</noframes>',
82                     self.html, re.S | re.M)
83        self.assert_(m, 'Noframes not set in')
84
85    def testTitleDuplication(self):
86        """If we are not overriding page title and current page title equals
87           title of the plone site then there should be no concatenation of
88           both titles. Only one should be displayed.
89        """
90        # setup page with title equal to plone site's title
91        my_doc2 = self.portal.invokeFactory('Document', id='my_doc2',
92                                            title=self.portal.Title())
93        my_doc2 = self.portal['my_doc2']
94        self.wf.doActionFor(my_doc2, 'publish')
95        html2 = self.publish('/' + my_doc2.absolute_url(1),
96                             self.basic_auth).getBody()
97
98        m = re.match('.*<title>\\s*%s\\s*</title>' % self.portal.Title(),
99                     html2, re.S | re.M)
100        self.assert_(m, 'Title is not set correctly, perhaps it is '
101                     'duplicated with plone site title')
102
103    def testDescription(self):
104        m = re.match(METATAG % ("description", FORM['seo_description']),
105                     self.html, re.S | re.M)
106        self.assert_(m, 'Description not set in')
107
108    def testRobots(self):
109        m = re.match(METATAG % ("robots", FORM['seo_robots']), self.html,
110                     re.S | re.M)
111        self.assert_(m, 'Robots not set in')
112
113    def testDistribution(self):
114        m = re.match(METATAG % ("distribution", FORM['seo_distribution']),
115                     self.html, re.S | re.M)
116        self.assert_(m, 'Distribution not set in')
117
118    def testHTMLComments(self):
119        m = re.match('.*<!--\\s*no comments\\s*-->', self.html, re.S | re.M)
120        self.assert_(m, 'Comments not set in')
121
122    def testTagsOrder(self):
123        def is_match(html, mtorder):
124            return re.search('.*'.join(['<meta.*name="%s".*/>' % t
125                                        for t in mtorder]), html, re.S | re.M)
126
127        metatags_order = [t for t in self.sp.getProperty('metatags_order')
128                          if t in VIEW_METATAGS]
129        self.assert_(is_match(self.html, metatags_order),
130                     "Meta tags order not supported.")
131
132        metatags_order.reverse()
133        self.assertFalse(is_match(self.html, metatags_order),
134                         "Meta tags order not supported.")
135
136        self.sp.manage_changeProperties(metatags_order=metatags_order)
137        self.assertFalse(is_match(self.html, metatags_order),
138                         "Meta tags order not supported.")
139
140        html = self.publish(self.abs_path, self.basic_auth).getBody()
141        self.assert_(is_match(html, metatags_order),
142                     "Meta tags order not supported.")
143
144    def testCustomMetaTags(self):
145        for tag in CUSTOM_METATAGS:
146            m = re.match(METATAG % (tag['meta_name'], tag['meta_content']),
147                         self.html, re.S | re.M)
148            if tag['meta_content']:
149                self.assert_(m, "Custom meta tag %s not applied."
150                             % tag['meta_name'])
151            else:
152                self.assert_(not m, "Meta tag %s has no content, but is "
153                             "present: in the page." % tag['meta_name'])
154
155        m = re.match(METATAG % ("metatag4", "global_metatag4value"), self.html,
156                     re.S | re.M)
157        self.assert_(m, "Global custom meta tag %s not applied." % 'metatag4')
158
159    def testDeleteCustomMetaTags(self):
160        self.sp.manage_changeProperties(default_custom_metatags='metatag1|'
161                                        'global_metatag1value')
162        form_data = {'seo_custommetatags': CUSTOM_METATAGS,
163                     'seo_custommetatags_override:int': 0,
164                     'form.button.Save': "Save",
165                     'form.submitted:int': 1}
166
167        # Clean-up ram cache
168        getUtility(IRAMCache).invalidateAll()
169
170        self.publish(path=self.abs_path + '/@@seo-context-properties',
171                     basic=self.basic_auth, request_method='POST',
172                     stdin=StringIO(urllib.urlencode(form_data)))
173        html = self.publish(self.abs_path, self.basic_auth).getBody()
174
175        m = re.match(METATAG % ("metatag4", "global_metatag4value"), html,
176                     re.S | re.M)
177        self.assert_(not m, "Global custom meta tag %s is prosent in the "
178                     "page." % 'metatag4')
179
180        m = re.match(METATAG % ("metatag1", "global_metatag1value"), html,
181                     re.S | re.M)
182        self.assert_(m, "Global custom meta tag %s not applied." % 'metatag1')
183
184
185def test_suite():
186    from unittest import TestSuite, makeSuite
187    suite = TestSuite()
188    suite.addTest(makeSuite(TestContextForm))
189    return suite
Note: See TracBrowser for help on using the repository browser.