[1670] | 1 | import re |
---|
| 2 | from zope.component import queryAdapter |
---|
[1684] | 3 | from zope.component import provideAdapter |
---|
| 4 | from plone.indexer.decorator import indexer |
---|
| 5 | |
---|
| 6 | from Products.CMFCore.interfaces import IContentish |
---|
[1671] | 7 | from Products.Archetypes.interfaces import IBaseContent |
---|
| 8 | |
---|
[1721] | 9 | from quintagroup.canonicalpath.interfaces import ICanonicalPath |
---|
[1676] | 10 | from quintagroup.seoptimizer.interfaces import ISEOCanonicalPath |
---|
[1670] | 11 | from base import getToolByName, FunctionalTestCase, newSecurityManager |
---|
| 12 | from config import * |
---|
| 13 | |
---|
| 14 | class TestCanonicalURL(FunctionalTestCase): |
---|
| 15 | |
---|
| 16 | def afterSetUp(self): |
---|
| 17 | self.basic_auth = 'portal_manager:secret' |
---|
| 18 | uf = self.app.acl_users |
---|
| 19 | uf.userFolderAddUser('portal_manager', 'secret', ['Manager'], []) |
---|
| 20 | user = uf.getUserById('portal_manager') |
---|
| 21 | if not hasattr(user, 'aq_base'): |
---|
| 22 | user = user.__of__(uf) |
---|
| 23 | newSecurityManager(None, user) |
---|
| 24 | |
---|
| 25 | self.portal.invokeFactory('Document', id='mydoc') |
---|
| 26 | self.mydoc = self.portal['mydoc'] |
---|
| 27 | self.mydoc_path = "/%s" % self.mydoc.absolute_url(1) |
---|
[1671] | 28 | self.curl = re.compile('<link\srel\s*=\s*"canonical"\s+' \ |
---|
| 29 | '[^>]*href\s*=\s*\"([^\"]*)\"[^>]*>', re.S|re.M) |
---|
[1670] | 30 | |
---|
| 31 | |
---|
| 32 | def test_CanonicalURL(self): |
---|
| 33 | html = self.publish(self.mydoc_path, self.basic_auth).getBody() |
---|
| 34 | foundcurls = self.curl.findall(html) |
---|
| 35 | mydoc_url = self.mydoc.absolute_url() |
---|
| 36 | |
---|
[1671] | 37 | self.assertTrue([1 for curl in foundcurls if curl==mydoc_url], |
---|
| 38 | "Wrong CANONICAL URL for document: %s, all must be: %s" % ( |
---|
| 39 | foundcurls, mydoc_url)) |
---|
[1670] | 40 | |
---|
| 41 | def test_updateCanonicalURL(self): |
---|
| 42 | mydoc_url_new = self.mydoc.absolute_url() + '.new' |
---|
| 43 | # Update canonical url property |
---|
[1671] | 44 | self.publish(self.mydoc_path + '/@@seo-context-properties?' \ |
---|
| 45 | 'seo_canonical_override=checked&seo_canonical=%s&' \ |
---|
| 46 | 'form.submitted=1' % mydoc_url_new, self.basic_auth) |
---|
[1670] | 47 | # Test updated canonical url |
---|
| 48 | html = self.publish(self.mydoc_path, self.basic_auth).getBody() |
---|
| 49 | foundcurls = self.curl.findall(html) |
---|
| 50 | |
---|
[1671] | 51 | qseo_url = self.mydoc.getProperty('qSEO_canonical','') |
---|
| 52 | self.assertTrue(qseo_url == mydoc_url_new, |
---|
| 53 | "Not set 'qSEO_canonical' property") |
---|
| 54 | self.assertTrue([1 for curl in foundcurls if curl==mydoc_url_new], |
---|
| 55 | "Wrong CANONICAL URL for document: %s, all must be: %s" % ( |
---|
| 56 | foundcurls, mydoc_url_new)) |
---|
[1670] | 57 | |
---|
[1721] | 58 | def test_SEOCanonicalAdapterRegistration(self): |
---|
| 59 | portal_seocanonical = queryAdapter(self.portal, interface=ISEOCanonicalPath) |
---|
| 60 | self.assertTrue(portal_seocanonical is not None, |
---|
[1676] | 61 | "Not registered ISEOCanonicalPath adapter") |
---|
| 62 | |
---|
[1721] | 63 | mydoc_seocanonical = queryAdapter(self.mydoc, interface=ISEOCanonicalPath) |
---|
| 64 | self.assertTrue(mydoc_seocanonical is not None, |
---|
| 65 | "Not registered ISEOCanonicalPath adapter") |
---|
| 66 | |
---|
[1671] | 67 | def test_canonicalAdapterRegistration(self): |
---|
[1721] | 68 | canonical_portal = queryAdapter(self.portal, interface=ICanonicalPath) |
---|
| 69 | self.assertTrue(canonical_portal is not None, |
---|
| 70 | "Not registered ICanonicalPath adapter for portal root") |
---|
[1670] | 71 | |
---|
[1721] | 72 | canonical_mydoc = queryAdapter(self.mydoc, interface=ICanonicalPath) |
---|
| 73 | self.assertTrue(canonical_mydoc is not None, |
---|
| 74 | "Not registered ICanonicalPath adapter for the documnent") |
---|
| 75 | |
---|
[1671] | 76 | def test_canonicalAdapter(self): |
---|
| 77 | purl = getToolByName(self.portal, 'portal_url') |
---|
| 78 | mydoc_path_rel = '/'+'/'.join(purl.getRelativeContentPath(self.mydoc)) |
---|
| 79 | |
---|
[1676] | 80 | canonical = queryAdapter(self.mydoc, ISEOCanonicalPath) |
---|
[1671] | 81 | cpath = canonical.canonical_path() |
---|
| 82 | self.assertTrue(cpath == mydoc_path_rel, |
---|
| 83 | "By canonical path adapter got: '%s', must be: '%s'" % ( |
---|
| 84 | cpath, mydoc_path_rel)) |
---|
[1672] | 85 | |
---|
[1671] | 86 | # Update canonical url property |
---|
| 87 | mydoc_url_new = self.mydoc.absolute_url() + '.new' |
---|
| 88 | self.publish(self.mydoc_path + '/@@seo-context-properties?' \ |
---|
| 89 | 'seo_canonical_override=checked&seo_canonical=%s' \ |
---|
| 90 | '&form.submitted=1' % mydoc_url_new, self.basic_auth) |
---|
| 91 | |
---|
| 92 | mydoc_path_rel_new = mydoc_path_rel + '.new' |
---|
| 93 | newcpath = canonical.canonical_path() |
---|
| 94 | self.assertTrue(newcpath == mydoc_path_rel_new, |
---|
| 95 | "By canonical path adapter got: '%s', must be: '%s'" % ( |
---|
| 96 | newcpath, mydoc_path_rel_new)) |
---|
| 97 | |
---|
[1676] | 98 | |
---|
| 99 | def addCanonicalPathCatalogColumn(self): |
---|
| 100 | |
---|
[1684] | 101 | @indexer(IContentish) |
---|
| 102 | def canonical_path(obj, **kwargs): |
---|
[1676] | 103 | """Return canonical_path property for the object. |
---|
| 104 | """ |
---|
| 105 | cpath = queryAdapter(obj, interface=ISEOCanonicalPath) |
---|
| 106 | if cpath: |
---|
| 107 | return cpath.canonical_path() |
---|
| 108 | return None |
---|
| 109 | |
---|
[1684] | 110 | provideAdapter(canonical_path, name='canonical_path') |
---|
[1676] | 111 | catalog = getToolByName(self.portal, 'portal_catalog') |
---|
| 112 | catalog.addColumn(name='canonical_path') |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | def testCatalogUpdated(self): |
---|
| 116 | purl = getToolByName(self.portal, 'portal_url') |
---|
| 117 | catalog = getToolByName(self.portal, 'portal_catalog') |
---|
| 118 | self.addCanonicalPathCatalogColumn() |
---|
| 119 | |
---|
| 120 | canonical = queryAdapter(self.mydoc, ISEOCanonicalPath) |
---|
| 121 | cpath = canonical.canonical_path() |
---|
| 122 | |
---|
| 123 | # get catalog data before update |
---|
| 124 | mydoc_catalog_canonical = catalog(id="mydoc")[0].canonical_path |
---|
| 125 | self.assertTrue(not mydoc_catalog_canonical) |
---|
| 126 | |
---|
| 127 | # Update canonical url property |
---|
| 128 | mydoc_url_new = self.mydoc.absolute_url() + '.new' |
---|
| 129 | self.publish(self.mydoc_path + '/@@seo-context-properties?' \ |
---|
| 130 | 'seo_canonical_override=checked&seo_canonical=%s' \ |
---|
| 131 | '&form.submitted=1' % mydoc_url_new, self.basic_auth) |
---|
| 132 | |
---|
| 133 | newcpath = canonical.canonical_path() |
---|
| 134 | mydoc_catalog_canonical = catalog(id="mydoc")[0].canonical_path |
---|
| 135 | self.assertTrue(newcpath == mydoc_catalog_canonical, |
---|
| 136 | "canonical path get by adapter: '%s' not equals to cataloged one: '%s'" % ( |
---|
| 137 | newcpath, mydoc_catalog_canonical)) |
---|
| 138 | |
---|
| 139 | |
---|
[1853] | 140 | def testSEOCanonicalAdapter4OFSFolder(self): |
---|
[1856] | 141 | atct_tool = self.portal.portal_atct |
---|
[1853] | 142 | seocan = queryAdapter(self.mydoc, ISEOCanonicalPath) |
---|
[1856] | 143 | self.assertTrue(seocan is not None, |
---|
[1853] | 144 | "seo canonical adapter not found for 'ATCT Tool'") |
---|
| 145 | |
---|
| 146 | |
---|
[1670] | 147 | def test_suite(): |
---|
| 148 | from unittest import TestSuite, makeSuite |
---|
| 149 | suite = TestSuite() |
---|
| 150 | suite.addTest(makeSuite(TestCanonicalURL)) |
---|
| 151 | return suite |
---|