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