1 | import urllib |
---|
2 | from cStringIO import StringIO |
---|
3 | import pkg_resources |
---|
4 | |
---|
5 | from OFS.interfaces import ITraversable |
---|
6 | |
---|
7 | from zope.component import getGlobalSiteManager |
---|
8 | from zope.component import queryAdapter, getMultiAdapter |
---|
9 | from zope.interface import directlyProvides |
---|
10 | from zope.viewlet.interfaces import IViewlet, IViewletManager |
---|
11 | |
---|
12 | from quintagroup.seoptimizer.browser.interfaces import IPloneSEOLayer |
---|
13 | from quintagroup.seoptimizer.browser.seo_configlet import ISEOConfigletSchema |
---|
14 | from quintagroup.canonicalpath.interfaces import ICanonicalLink |
---|
15 | from quintagroup.canonicalpath.adapters import DefaultCanonicalLinkAdapter |
---|
16 | |
---|
17 | from quintagroup.seoptimizer.tests.base import FunctionalTestCase |
---|
18 | from Products.PloneTestCase.PloneTestCase import portal_owner, \ |
---|
19 | default_password |
---|
20 | import re |
---|
21 | from Products.Five import zcml |
---|
22 | from Products.CMFCore.utils import getToolByName |
---|
23 | |
---|
24 | |
---|
25 | class TestBugs(FunctionalTestCase): |
---|
26 | |
---|
27 | def afterSetUp(self): |
---|
28 | self.basic_auth = ':'.join((portal_owner, default_password)) |
---|
29 | self.loginAsPortalOwner() |
---|
30 | # prepare test document |
---|
31 | self.portal.invokeFactory('Document', id='my_doc') |
---|
32 | self.my_doc = self.portal['my_doc'] |
---|
33 | self.mydoc_path = "/%s" % self.my_doc.absolute_url(1) |
---|
34 | |
---|
35 | def set_title(self, title='', title_override=0, comment='', |
---|
36 | comment_override=0): |
---|
37 | """ Set seo title """ |
---|
38 | portal = self.portal |
---|
39 | fp = portal['front-page'] |
---|
40 | request = portal.REQUEST |
---|
41 | view = portal.restrictedTraverse('@@plone') |
---|
42 | manager = getMultiAdapter((fp, request, view), IViewletManager, |
---|
43 | name=u'plone.htmlhead') |
---|
44 | |
---|
45 | directlyProvides(request, IPloneSEOLayer) |
---|
46 | viewlet = getMultiAdapter((fp, request, view, manager), IViewlet, |
---|
47 | name=u'plone.htmlhead.title') |
---|
48 | |
---|
49 | form_data = {'seo_title': title, |
---|
50 | 'seo_title_override:int': title_override, |
---|
51 | 'seo_html_comment': comment, |
---|
52 | 'seo_html_comment_override:int': comment_override, |
---|
53 | 'form.button.Save': "Save", |
---|
54 | 'form.submitted:int': 1} |
---|
55 | |
---|
56 | self.publish(path=fp.absolute_url(1) + '/@@seo-context-properties', |
---|
57 | basic=self.basic_auth, request_method='POST', |
---|
58 | stdin=StringIO(urllib.urlencode(form_data))) |
---|
59 | viewlet.update() |
---|
60 | seo_title_comment = viewlet.render() |
---|
61 | return seo_title_comment |
---|
62 | |
---|
63 | def test_seo_title(self): |
---|
64 | """ Test changing title """ |
---|
65 | title = "New Title" |
---|
66 | new_title = u'<title>%s</title>' % title |
---|
67 | seo_title = self.set_title(title=title, title_override=1) |
---|
68 | self.assertEqual(new_title, seo_title) |
---|
69 | |
---|
70 | def test_seo_comment(self): |
---|
71 | """ Test changing comment """ |
---|
72 | comment = "New Comment" |
---|
73 | seo_title_comment = self.set_title(comment=comment, comment_override=1) |
---|
74 | self.assert_(seo_title_comment.endswith("<!--%s-->" % comment)) |
---|
75 | |
---|
76 | def test_seo_title_comment(self): |
---|
77 | """ Test changing title and comment """ |
---|
78 | title = "New Title" |
---|
79 | comment = "New Comment" |
---|
80 | new_title = u'<title>%s</title>\n<!--%s-->' % (title, comment) |
---|
81 | seo_title_comment = self.set_title(title=title, title_override=1, |
---|
82 | comment=comment, comment_override=1) |
---|
83 | self.assertEqual(new_title, seo_title_comment) |
---|
84 | |
---|
85 | def test_modification_date(self): |
---|
86 | """ Modification date changing on SEO properties edit """ |
---|
87 | form_data = {'seo_title': 'New Title', |
---|
88 | 'seo_title_override:int': 1, |
---|
89 | 'form.button.Save': "Save", |
---|
90 | 'form.submitted:int': 1} |
---|
91 | |
---|
92 | md_before = self.my_doc.modification_date |
---|
93 | self.publish(path=self.mydoc_path + '/@@seo-context-properties', |
---|
94 | basic=self.basic_auth, request_method='POST', |
---|
95 | stdin=StringIO(urllib.urlencode(form_data))) |
---|
96 | md_after = self.my_doc.modification_date |
---|
97 | |
---|
98 | self.assertNotEqual(md_before, md_after) |
---|
99 | |
---|
100 | def test_bug_20_at_plone_org(self): |
---|
101 | portal = self.portal |
---|
102 | fp = portal['front-page'] |
---|
103 | request = portal.REQUEST |
---|
104 | view = portal.restrictedTraverse('@@plone') |
---|
105 | |
---|
106 | manager = getMultiAdapter((fp, request, view), IViewletManager, |
---|
107 | name=u'plone.htmlhead') |
---|
108 | viewlet = getMultiAdapter((fp, request, view, manager), IViewlet, |
---|
109 | name=u'plone.htmlhead.title') |
---|
110 | viewlet.update() |
---|
111 | old_title = viewlet.render() |
---|
112 | |
---|
113 | # add IPloneSEOLayer |
---|
114 | directlyProvides(request, IPloneSEOLayer) |
---|
115 | |
---|
116 | viewlet = getMultiAdapter((fp, request, view, manager), IViewlet, |
---|
117 | name=u'plone.htmlhead.title') |
---|
118 | viewlet.update() |
---|
119 | new_title = viewlet.render() |
---|
120 | |
---|
121 | self.assertEqual(old_title, new_title) |
---|
122 | |
---|
123 | def test_bug_19_23_at_plone_org(self): |
---|
124 | """overrides.zcml should present in the root of the package""" |
---|
125 | import quintagroup.seoptimizer |
---|
126 | try: |
---|
127 | zcml.load_config('overrides.zcml', quintagroup.seoptimizer) |
---|
128 | except IOError: |
---|
129 | self.fail("overrides.zcml removed from the package root") |
---|
130 | |
---|
131 | def test_escape_characters_title(self): |
---|
132 | """Change escape characters in title of SEO properties |
---|
133 | Bug url http://plone.org/products/plone-seo/issues/31 |
---|
134 | """ |
---|
135 | from cgi import escape |
---|
136 | title = 'New <i>Title</i>' |
---|
137 | form_data = {'seo_title': title, |
---|
138 | 'seo_title_override:int': 1, |
---|
139 | 'form.button.Save': "Save", |
---|
140 | 'form.submitted:int': 1} |
---|
141 | |
---|
142 | self.publish(path=self.mydoc_path + '/@@seo-context-properties', |
---|
143 | basic=self.basic_auth, request_method='POST', |
---|
144 | stdin=StringIO(urllib.urlencode(form_data))) |
---|
145 | html = self.publish(self.mydoc_path, self.basic_auth).getBody() |
---|
146 | m = re.match('.*<title>\\s*%s\\s*</title>' % escape(title), html, |
---|
147 | re.S | re.M) |
---|
148 | self.assert_(m, 'Title is not escaped properly.') |
---|
149 | |
---|
150 | def test_escape_characters_desctiption(self): |
---|
151 | """Change escape characters in desctiption of SEO properties |
---|
152 | """ |
---|
153 | from cgi import escape |
---|
154 | description = 'New <i>Description</i>' |
---|
155 | form_data = {'seo_description': description, |
---|
156 | 'seo_description_override:int': 1, |
---|
157 | 'form.button.Save': "Save", |
---|
158 | 'form.submitted:int': 1} |
---|
159 | |
---|
160 | self.publish(path=self.mydoc_path + '/@@seo-context-properties', |
---|
161 | basic=self.basic_auth, request_method='POST', |
---|
162 | stdin=StringIO(urllib.urlencode(form_data))) |
---|
163 | html = self.publish(self.mydoc_path, self.basic_auth).getBody() |
---|
164 | m = re.match('.*<meta name="description" content="%s"' % |
---|
165 | escape(description), html, re.S | re.M) |
---|
166 | self.assert_(m, 'Desctiption is not escaped properly.') |
---|
167 | |
---|
168 | def test_escape_characters_comment(self): |
---|
169 | """Change escape characters in comment of SEO properties |
---|
170 | """ |
---|
171 | from cgi import escape |
---|
172 | comment = 'New <i>comment</i>' |
---|
173 | form_data = {'seo_title': 'New Title', |
---|
174 | 'seo_title_override:int': 1, |
---|
175 | 'seo_html_comment': comment, |
---|
176 | 'seo_html_comment_override:int': 1, |
---|
177 | 'form.button.Save': "Save", |
---|
178 | 'form.submitted:int': 1} |
---|
179 | |
---|
180 | self.publish(path=self.mydoc_path + '/@@seo-context-properties', |
---|
181 | basic=self.basic_auth, request_method='POST', |
---|
182 | stdin=StringIO(urllib.urlencode(form_data))) |
---|
183 | html = self.publish(self.mydoc_path, self.basic_auth).getBody() |
---|
184 | m = re.match('.*<!--\\s*%s\\s*-->' % escape(comment), html, |
---|
185 | re.S | re.M) |
---|
186 | self.assert_(m, 'Comment is not escaped properly.') |
---|
187 | |
---|
188 | def test_bug_custom_metatags_update(self): |
---|
189 | # Prepare a page for the test |
---|
190 | page = self.portal["front-page"] |
---|
191 | request = self.portal.REQUEST |
---|
192 | directlyProvides(request, IPloneSEOLayer) |
---|
193 | seo_context_props = getMultiAdapter((page, request), |
---|
194 | name="seo-context-properties") |
---|
195 | # Set default custom meta tag without default value (tag name only) |
---|
196 | self.gseo = queryAdapter(self.portal, ISEOConfigletSchema) |
---|
197 | self.gseo.default_custom_metatags = ["test_tag", ] |
---|
198 | try: |
---|
199 | # Breakage on updating custom metatag |
---|
200 | # with seo-context-properties view |
---|
201 | seo_context_props.updateSEOCustomMetaTagsProperties([]) |
---|
202 | except IndexError: |
---|
203 | self.fail("Error in calculating of default tag value, when only "\ |
---|
204 | "tag name set in default_custom_metatags property of "\ |
---|
205 | "the configlet.") |
---|
206 | |
---|
207 | |
---|
208 | class TestBug24AtPloneOrg(FunctionalTestCase): |
---|
209 | |
---|
210 | def afterSetUp(self): |
---|
211 | # Add test users: member, editor |
---|
212 | member_id = 'test_member' |
---|
213 | editor_id = 'test_editor' |
---|
214 | test_pswd = 'pswd' |
---|
215 | uf = self.portal.acl_users |
---|
216 | uf.userFolderAddUser(member_id, test_pswd, |
---|
217 | ['Member'], []) |
---|
218 | uf.userFolderAddUser(editor_id, test_pswd, |
---|
219 | ['Member', 'Editor'], []) |
---|
220 | |
---|
221 | self.member_auth = '%s:%s' % (member_id, test_pswd) |
---|
222 | self.editor_auth = '%s:%s' % (editor_id, test_pswd) |
---|
223 | self.portal_url = '/'.join(self.portal.getPhysicalPath()) |
---|
224 | |
---|
225 | def test_not_break(self): |
---|
226 | """Default portal page should not breaks for any user""" |
---|
227 | # Anonymous |
---|
228 | resp = self.publish(path=self.portal_url) |
---|
229 | self.assertEqual(resp.getStatus(), 200) |
---|
230 | # Member |
---|
231 | resp = self.publish(path=self.portal_url, basic=self.member_auth) |
---|
232 | self.assertEqual(resp.getStatus(), 200) |
---|
233 | # Editor: this fails, althought must pass |
---|
234 | resp = self.publish(path=self.portal_url, basic=self.editor_auth) |
---|
235 | self.assertEqual(resp.getStatus(), 200) |
---|
236 | |
---|
237 | def test_tab_visibility(self): |
---|
238 | """Only Editor can view seo tab""" |
---|
239 | rexp = re.compile('<a\s+[^>]*' \ |
---|
240 | 'href="[a-zA-Z0-9\:\/_-]*/@@seo-context-properties"[^>]*>'\ |
---|
241 | '\s*SEO Properties\s*</a>', re.I | re.S) |
---|
242 | # Anonymous: NO SEO Properties link |
---|
243 | res = self.publish(path=self.portal_url).getBody() |
---|
244 | self.assertEqual(rexp.search(res), None) |
---|
245 | # Member: NO 'SEO Properties' link |
---|
246 | res = self.publish(path=self.portal_url, |
---|
247 | basic=self.member_auth).getBody() |
---|
248 | self.assertEqual(rexp.search(res), None) |
---|
249 | # Editor: PRESENT 'SEO Properties' link |
---|
250 | res = self.publish(path=self.portal_url, |
---|
251 | basic=self.editor_auth).getBody() |
---|
252 | self.assertNotEqual(rexp.search(res), None) |
---|
253 | |
---|
254 | def test_tab_access(self): |
---|
255 | """Only Editor can access 'SEO Properties' tab""" |
---|
256 | test_url = self.portal_url + '/front-page/@@seo-context-properties' |
---|
257 | # Anonymous: can NOT ACCESS |
---|
258 | headers = self.publish(path=test_url).headers |
---|
259 | self.assert_('Unauthorized' in headers.get('bobo-exception-type', ""), |
---|
260 | "No 'Unauthorized' exception rised for Anonymous on " \ |
---|
261 | "'@@seo-context-properties' view") |
---|
262 | # Member: can NOT ACCESS |
---|
263 | self.publish(path=test_url, basic=self.member_auth).headers |
---|
264 | self.assert_('Unauthorized' in headers.get('bobo-exception-type', ""), |
---|
265 | "No 'Unauthorized' exception rised for Member on " \ |
---|
266 | "'@@seo-context-properties' view") |
---|
267 | # Editor: CAN Access |
---|
268 | res = self.publish(path=test_url, basic=self.editor_auth) |
---|
269 | self.assertEqual(res.status, 200) |
---|
270 | |
---|
271 | def test_tab_edit(self): |
---|
272 | """Editor can change SEO Properties""" |
---|
273 | test_url = self.portal_url + '/front-page/@@seo-context-properties' |
---|
274 | form_data = {'seo_title': 'New Title', |
---|
275 | 'seo_title_override:int': 1, |
---|
276 | 'form.submitted:int': 1} |
---|
277 | res = self.publish(path=test_url, basic=self.editor_auth, |
---|
278 | request_method='POST', |
---|
279 | stdin=StringIO(urllib.urlencode(form_data))) |
---|
280 | self.assertNotEqual(res.status, 200) |
---|
281 | |
---|
282 | |
---|
283 | def test_suite(): |
---|
284 | from unittest import TestSuite, makeSuite |
---|
285 | suite = TestSuite() |
---|
286 | suite.addTest(makeSuite(TestBugs)) |
---|
287 | suite.addTest(makeSuite(TestBug24AtPloneOrg)) |
---|
288 | return suite |
---|