[1646] | 1 | import urllib, re |
---|
| 2 | from Acquisition import aq_inner |
---|
| 3 | from base import getToolByName, FunctionalTestCase, newSecurityManager |
---|
| 4 | from config import * |
---|
| 5 | |
---|
[1872] | 6 | KWSTMPL = '.*(<meta\s+(?:(?:name="keywords"\s*)|(?:content="%s"\s*)){2}/>)' |
---|
[1792] | 7 | |
---|
[1646] | 8 | class TestUsageKeywords(FunctionalTestCase): |
---|
| 9 | |
---|
| 10 | def afterSetUp(self): |
---|
| 11 | self.sp = self.portal.portal_properties.seo_properties |
---|
| 12 | self.pu = self.portal.plone_utils |
---|
| 13 | |
---|
| 14 | self.basic_auth = 'portal_manager:secret' |
---|
| 15 | uf = self.app.acl_users |
---|
| 16 | uf.userFolderAddUser('portal_manager', 'secret', ['Manager'], []) |
---|
| 17 | user = uf.getUserById('portal_manager') |
---|
| 18 | if not hasattr(user, 'aq_base'): |
---|
| 19 | user = user.__of__(uf) |
---|
| 20 | newSecurityManager(None, user) |
---|
| 21 | |
---|
| 22 | '''Preparation for functional testing''' |
---|
| 23 | self.my_doc = self.portal.invokeFactory('Document', id='my_doc') |
---|
| 24 | self.my_doc = self.portal['my_doc'] |
---|
| 25 | |
---|
[1872] | 26 | |
---|
| 27 | def test_noDefaultKeywords(self): |
---|
| 28 | """No keywords added for the content by default""" |
---|
[1646] | 29 | metatags = self.pu.listMetaTags(self.my_doc) |
---|
| 30 | self.assert_('keywords' not in metatags) |
---|
[1872] | 31 | |
---|
[1646] | 32 | |
---|
[1872] | 33 | def testrender_SEOKeywords(self): |
---|
| 34 | """ """ |
---|
[1646] | 35 | self.my_doc.setText('<p>foo bar</p>') |
---|
[1872] | 36 | self.my_doc.manage_addProperty('qSEO_keywords', [], 'lines') |
---|
[1646] | 37 | |
---|
[1872] | 38 | for seokws in [('foo',), ('foo', 'bar')]: |
---|
| 39 | self.my_doc._updateProperty('qSEO_keywords', seokws) |
---|
| 40 | html = str(self.publish(self.portal.id+'/my_doc', self.basic_auth)) |
---|
| 41 | expect = ',\s*'.join(seokws) |
---|
| 42 | self.assert_(re.match(KWSTMPL % expect, html, re.S|re.M), |
---|
| 43 | "No '%s' keyword found" % str(seokws)) |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | def testbehave_NoSEOKeywordsOnlySubject(self): |
---|
| 47 | self.my_doc.setText('<p>local subject</p>') |
---|
| 48 | self.my_doc.setSubject('subject') |
---|
[1646] | 49 | html = str(self.publish(self.portal.id+'/my_doc', self.basic_auth)) |
---|
| 50 | |
---|
[1872] | 51 | expect = "subject" |
---|
| 52 | self.assert_(re.match(KWSTMPL % expect, html, re.S|re.M), |
---|
| 53 | "No '%s' keyword find" % expect) |
---|
[1646] | 54 | |
---|
[1872] | 55 | def testbehave_SEOKeywordsOverrideSubject(self): |
---|
| 56 | SEOKWS = ('local',) |
---|
| 57 | self.my_doc.setText('<p>local subject</p>') |
---|
| 58 | self.my_doc.setSubject('subject') |
---|
| 59 | self.my_doc.manage_addProperty('qSEO_keywords', SEOKWS, 'lines') |
---|
| 60 | html = str(self.publish(self.portal.id+'/my_doc', self.basic_auth)) |
---|
[1646] | 61 | |
---|
[1872] | 62 | expect = ',\s*'.join(SEOKWS) |
---|
| 63 | self.assert_(re.match(KWSTMPL % expect, html, re.S|re.M), |
---|
| 64 | "No '%s' keywords find" % SEOKWS) |
---|
[1646] | 65 | |
---|
[1872] | 66 | def testbehave_noSEOKeywordsNoSubject(self): |
---|
| 67 | """Nor seo keywords not subject added""" |
---|
| 68 | html = str(self.publish(self.portal.id+'/my_doc', self.basic_auth)) |
---|
| 69 | self.assertFalse(re.match('.*(<meta\s[^\>]*name="keywords"[^\>]*>)', |
---|
| 70 | html, re.S|re.M), "'keyword' meta tag found") |
---|
| 71 | |
---|
| 72 | |
---|
[1646] | 73 | def test_suite(): |
---|
| 74 | from unittest import TestSuite, makeSuite |
---|
| 75 | suite = TestSuite() |
---|
| 76 | suite.addTest(makeSuite(TestUsageKeywords)) |
---|
| 77 | return suite |
---|