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