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