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