[1493] | 1 | import urllib, re |
---|
| 2 | from cStringIO import StringIO |
---|
[1877] | 3 | from base import * |
---|
[1493] | 4 | |
---|
[1889] | 5 | CUSTOM_METATAGS = [ |
---|
| 6 | {'meta_name': 'metatag1', 'meta_content': 'metatag1value'}, |
---|
| 7 | {'meta_name': 'metatag2', 'meta_content': 'metatag2value'}, |
---|
| 8 | {'meta_name': 'metatag3', 'meta_content': ''} |
---|
| 9 | ] |
---|
[1877] | 10 | |
---|
[1889] | 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 | ] |
---|
[1877] | 16 | |
---|
[1890] | 17 | FORM = { |
---|
[1889] | 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 | } |
---|
[1877] | 33 | |
---|
[1890] | 34 | METATAG = '.*(<meta\s+(?:(?:name="%s"\s*)|(?:content="%s"\s*)){2}/>)' |
---|
| 35 | |
---|
[1892] | 36 | class TestContextForm(FunctionalTestCase): |
---|
[1493] | 37 | |
---|
| 38 | def afterSetUp(self): |
---|
| 39 | self.sp = self.portal.portal_properties.seo_properties |
---|
| 40 | self.pu = self.portal.plone_utils |
---|
[1889] | 41 | self.wf = self.portal.portal_workflow |
---|
[1493] | 42 | |
---|
[1889] | 43 | self.basic_auth = ':'.join((portal_owner,default_password)) |
---|
| 44 | self.loginAsPortalOwner() |
---|
[1493] | 45 | |
---|
[1889] | 46 | # Preparation for functional testing' |
---|
| 47 | # create document for test |
---|
[1493] | 48 | my_doc = self.portal.invokeFactory('Document', id='my_doc') |
---|
| 49 | my_doc = self.portal['my_doc'] |
---|
[1889] | 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') |
---|
[1493] | 55 | st = '' |
---|
| 56 | for d in CUSTOM_METATAGS: |
---|
[1889] | 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', |
---|
[1890] | 62 | stdin=StringIO(urllib.urlencode(FORM)+st)) |
---|
[1889] | 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() |
---|
[1493] | 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 | """ |
---|
[1889] | 76 | # setup page with title equal to plone site's title |
---|
[1890] | 77 | my_doc2 = self.portal.invokeFactory('Document', id='my_doc2', |
---|
| 78 | title=self.portal.Title()) |
---|
[1889] | 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) |
---|
[1493] | 84 | self.assert_(m, 'Title is not set correctly, perhaps it is duplicated with plone site title') |
---|
| 85 | |
---|
| 86 | def testDescription(self): |
---|
[1890] | 87 | m = re.match(METATAG % ("description", FORM['seo_description']), self.html, re.S|re.M) |
---|
[1493] | 88 | self.assert_(m, 'Description not set in') |
---|
| 89 | |
---|
| 90 | def testRobots(self): |
---|
[1890] | 91 | m = re.match(METATAG % ("robots", FORM['seo_robots']), self.html, re.S|re.M) |
---|
[1493] | 92 | self.assert_(m, 'Robots not set in') |
---|
| 93 | |
---|
| 94 | def testDistribution(self): |
---|
[1890] | 95 | m = re.match(METATAG % ("distribution", FORM['seo_distribution']), self.html, re.S|re.M) |
---|
[1493] | 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): |
---|
[1890] | 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 | |
---|
[1781] | 107 | metatags_order = [t for t in self.sp.getProperty('metatags_order') if t in VIEW_METATAGS] |
---|
[1890] | 108 | self.assert_(is_match(self.html, metatags_order), "Meta tags order not supported.") |
---|
[1493] | 109 | |
---|
[1781] | 110 | metatags_order.reverse() |
---|
[1890] | 111 | self.assertFalse(is_match(self.html, metatags_order), "Meta tags order not supported.") |
---|
[1493] | 112 | |
---|
[1890] | 113 | self.sp.manage_changeProperties(metatags_order = metatags_order) |
---|
| 114 | self.assertFalse(is_match(self.html, metatags_order), "Meta tags order not supported.") |
---|
| 115 | |
---|
[1493] | 116 | html = self.publish(self.abs_path, self.basic_auth).getBody() |
---|
[1890] | 117 | self.assert_(is_match(html, metatags_order), "Meta tags order not supported.") |
---|
[1493] | 118 | |
---|
| 119 | |
---|
| 120 | def testCustomMetaTags(self): |
---|
| 121 | for tag in CUSTOM_METATAGS: |
---|
[1890] | 122 | m = re.match(METATAG % (tag['meta_name'], tag['meta_content']), |
---|
| 123 | self.html, re.S|re.M) |
---|
[1493] | 124 | if tag['meta_content']: |
---|
| 125 | self.assert_(m, "Custom meta tag %s not applied." % tag['meta_name']) |
---|
| 126 | else: |
---|
[1890] | 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) |
---|
[1493] | 131 | self.assert_(m, "Global custom meta tag %s not applied." % 'metatag4') |
---|
| 132 | |
---|
| 133 | def testDeleteCustomMetaTags(self): |
---|
[1890] | 134 | self.sp.manage_changeProperties( |
---|
| 135 | default_custom_metatags = 'metatag1|global_metatag1value') |
---|
[1889] | 136 | form_data = {'seo_custommetatags': CUSTOM_METATAGS, |
---|
| 137 | 'seo_custommetatags_override:int': 0, |
---|
| 138 | 'form.submitted:int': 1} |
---|
[1908] | 139 | |
---|
| 140 | # Clean-up ram cache |
---|
| 141 | getUtility(IRAMCache).invalidateAll() |
---|
| 142 | |
---|
[1889] | 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))) |
---|
[1890] | 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) |
---|
[1493] | 149 | self.assert_(not m, "Global custom meta tag %s is prosent in the page." % 'metatag4') |
---|
[1890] | 150 | |
---|
| 151 | m = re.match(METATAG % ("metatag1", "global_metatag1value"), html, re.S|re.M) |
---|
[1493] | 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() |
---|
[1892] | 157 | suite.addTest(makeSuite(TestContextForm)) |
---|
[1493] | 158 | return suite |
---|