1 | import urllib, re |
---|
2 | from cStringIO import StringIO |
---|
3 | from base import * |
---|
4 | |
---|
5 | CUSTOM_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 | |
---|
13 | VIEW_METATAGS = ['DC.creator', 'DC.format', 'DC.date.modified', |
---|
14 | 'DC.date.created', 'DC.type', 'DC.distribution', 'description', |
---|
15 | 'keywords', 'robots', 'distribution'] |
---|
16 | |
---|
17 | GLOBAL_CUSTOM_METATAGS = { |
---|
18 | 'default_custom_metatags':'metatag1|global_metatag1value\nmetatag4|global_metatag4value'} |
---|
19 | |
---|
20 | class 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.canonurl = 'http://nohost/plone/test.html' |
---|
38 | self.sp.manage_changeProperties(**GLOBAL_CUSTOM_METATAGS) |
---|
39 | self.sp.manage_changeProperties(settings_use_keywords_sg=3, settings_use_keywords_lg=2) |
---|
40 | abs_path = "/%s" % my_doc.absolute_url(1) |
---|
41 | self.form_data = {'seo_description': 'it is description, test keyword1', 'seo_keywords_override:int': 1, 'seo_custommetatags_override:int': 1, |
---|
42 | 'seo_robots_override:int': 1, 'seo_robots': 'ALL', 'seo_description_override:int': 1, 'seo_canonical_override:int': 1, |
---|
43 | 'seo_keywords:list': 'keyword1', 'seo_html_comment': 'no comments', |
---|
44 | 'seo_title_override:int': 1, 'seo_title': 'hello world', 'seo_html_comment_override:int': 1, |
---|
45 | 'seo_distribution_override:int': 1, 'seo_distribution': 'Global', 'seo_canonical': self.canonurl, 'form.submitted:int': 1} |
---|
46 | st = '' |
---|
47 | for d in CUSTOM_METATAGS: |
---|
48 | st += '&seo_custommetatags.meta_name:records=%s&seo_custommetatags.meta_content:records=%s' % (d['meta_name'],d['meta_content']) |
---|
49 | self.publish(path=abs_path+'/@@seo-context-properties', basic=self.basic_auth, request_method='POST', stdin=StringIO(urllib.urlencode(self.form_data)+st)) |
---|
50 | #self.publish(abs_path+'/@@seo-context-properties?%s' % urllib.urlencode(self.form_data), self.basic_auth) |
---|
51 | |
---|
52 | wf_tool = self.portal.portal_workflow |
---|
53 | wf_tool.doActionFor(my_doc, 'publish') |
---|
54 | |
---|
55 | self.abs_path = abs_path |
---|
56 | self.my_doc = my_doc |
---|
57 | self.html = self.publish(abs_path, self.basic_auth).getBody() |
---|
58 | |
---|
59 | # now setup page with title equal to plone site's title |
---|
60 | my_doc2 = self.portal.invokeFactory('Document', id='my_doc2') |
---|
61 | my_doc2 = self.portal['my_doc2'] |
---|
62 | my_doc2.update(title=self.portal.Title()) |
---|
63 | wf_tool.doActionFor(my_doc2, 'publish') |
---|
64 | abs_path2 = "/%s" % my_doc2.absolute_url(1) |
---|
65 | self.html2 = self.publish(abs_path2, self.basic_auth).getBody() |
---|
66 | |
---|
67 | def testTitle(self): |
---|
68 | m = re.match('.*<title>\\s*hello world\\s*</title>', self.html, re.S|re.M) |
---|
69 | self.assert_(m, 'Title not set in') |
---|
70 | |
---|
71 | def testTitleDuplication(self): |
---|
72 | """If we are not overriding page title and current page title equals title of the plone site |
---|
73 | then there should be no concatenation of both titles. Only one should be displayed. |
---|
74 | """ |
---|
75 | m = re.match('.*<title>\\s*%s\\s*</title>' % self.portal.Title(), self.html2, re.S|re.M) |
---|
76 | self.assert_(m, 'Title is not set correctly, perhaps it is duplicated with plone site title') |
---|
77 | |
---|
78 | def testDescription(self): |
---|
79 | m = re.match('.*(<meta\s+(?:(?:name="description"\s*)|(?:content="it is description, test keyword1"\s*)){2}/>)', self.html, re.S|re.M) |
---|
80 | self.assert_(m, 'Description not set in') |
---|
81 | |
---|
82 | def testKeywords(self): |
---|
83 | m = re.match('.*(<meta\s+(?:(?:name="keywords"\s*)|(?:content="keyword1"\s*)){2}/>)', self.html, re.S|re.M) |
---|
84 | self.assert_(m, 'Keywords not set in') |
---|
85 | |
---|
86 | def testRobots(self): |
---|
87 | m = re.match('.*(<meta\s+(?:(?:name="robots"\s*)|(?:content="ALL"\s*)){2}/>)', self.html, re.S|re.M) |
---|
88 | self.assert_(m, 'Robots not set in') |
---|
89 | |
---|
90 | def testDistribution(self): |
---|
91 | m = re.match('.*(<meta\s+(?:(?:name="distribution"\s*)|(?:content="Global"\s*)){2}/>)', self.html, re.S|re.M) |
---|
92 | self.assert_(m, 'Distribution not set in') |
---|
93 | |
---|
94 | def testHTMLComments(self): |
---|
95 | m = re.match('.*<!--\\s*no comments\\s*-->', self.html, re.S|re.M) |
---|
96 | self.assert_(m, 'Comments not set in') |
---|
97 | |
---|
98 | def testTagsOrder(self): |
---|
99 | metatags_order = [t for t in self.sp.getProperty('metatags_order') if t in VIEW_METATAGS] |
---|
100 | m = re.search('.*'.join(['<meta.*name="%s".*/>' %t for t in metatags_order]), self.html, re.S|re.M) |
---|
101 | self.assert_(m, "Meta tags order not supported.") |
---|
102 | |
---|
103 | metatags_order.reverse() |
---|
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 | self.sp.manage_changeProperties(**{'metatags_order':metatags_order}) |
---|
108 | html = self.publish(self.abs_path, self.basic_auth).getBody() |
---|
109 | m = re.search('.*'.join(['<meta.*name="%s".*/>' %t for t in metatags_order]), self.html, re.S|re.M) |
---|
110 | self.assertFalse(m, "Meta tags order not supported.") |
---|
111 | |
---|
112 | m = re.search('.*'.join(['<meta.*name="%s".*/>' %t for t in metatags_order]), html, re.S|re.M) |
---|
113 | self.assert_(m, "Meta tags order not supported.") |
---|
114 | |
---|
115 | |
---|
116 | def testCustomMetaTags(self): |
---|
117 | for tag in CUSTOM_METATAGS: |
---|
118 | m = re.match('.*(<meta\s+(?:(?:name="%(meta_name)s"\s*)|(?:content="%(meta_content)s"\s*)){2}/>)' % 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.match('.*(<meta\s+(?:(?:name="metatag4"\s*)|(?:content="global_metatag4value"\s*)){2}/>)', self.html, re.S|re.M) |
---|
124 | self.assert_(m, "Global custom meta tag %s not applied." % 'metatag4') |
---|
125 | |
---|
126 | def testDeleteCustomMetaTags(self): |
---|
127 | self.sp.manage_changeProperties(**{'default_custom_metatags':'metatag1|global_metatag1value'}) |
---|
128 | my_doc = self.my_doc |
---|
129 | self.form_data = {'seo_custommetatags': CUSTOM_METATAGS, 'seo_custommetatags_override:int': 0, 'form.submitted:int': 1} |
---|
130 | self.publish(path=self.abs_path+'/@@seo-context-properties', basic=self.basic_auth, request_method='POST', stdin=StringIO(urllib.urlencode(self.form_data))) |
---|
131 | self.html = self.publish(self.abs_path, self.basic_auth).getBody() |
---|
132 | m = re.match('.*(<meta\s+(?:(?:name="metatag4"\s*)|(?:content="global_metatag4value"\s*)){2}/>)', self.html, re.S|re.M) |
---|
133 | self.assert_(not m, "Global custom meta tag %s is prosent in the page." % 'metatag4') |
---|
134 | m = re.match('.*(<meta\s+(?:(?:name="metatag1"\s*)|(?:content="global_metatag1value"\s*)){2}/>)', self.html, re.S|re.M) |
---|
135 | self.assert_(m, "Global custom meta tag %s not applied." % 'metatag1') |
---|
136 | |
---|
137 | def testCanonical(self): |
---|
138 | m = re.match('.*<link rel="canonical" href="%s" />' % self.canonurl, self.html, re.S|re.M) |
---|
139 | self.assert_(m, self.canonurl) |
---|
140 | |
---|
141 | def testDefaultCanonical(self): |
---|
142 | """Default canonical url mast add document absolute_url |
---|
143 | """ |
---|
144 | # Delete custom canonical url |
---|
145 | my_doc = self.portal['my_doc'] |
---|
146 | my_doc._delProperty(id='qSEO_canonical') |
---|
147 | # Get document without customized canonical url |
---|
148 | abs_path = "/%s" % my_doc.absolute_url(1) |
---|
149 | self.html = self.publish(abs_path, self.basic_auth).getBody() |
---|
150 | |
---|
151 | my_url = my_doc.absolute_url() |
---|
152 | m = re.match('.*<link rel="canonical" href="%s" />' % my_url, self.html, re.S|re.M) |
---|
153 | self.assert_(m, my_url) |
---|
154 | |
---|
155 | def test_suite(): |
---|
156 | from unittest import TestSuite, makeSuite |
---|
157 | suite = TestSuite() |
---|
158 | suite.addTest(makeSuite(TestResponse)) |
---|
159 | return suite |
---|