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