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

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

fixes pep8

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