[2139] | 1 | from quintagroup.canonicalpath.interfaces import ICanonicalLink |
---|
[3140] | 2 | from quintagroup.seoptimizer.tests.base import FunctionalTestCase |
---|
| 3 | from Products.PloneTestCase.PloneTestCase import portal_owner, \ |
---|
| 4 | default_password |
---|
| 5 | import re |
---|
| 6 | from Products.CMFCore.utils import getToolByName |
---|
[3224] | 7 | from quintagroup.canonicalpath.adapters import PROPERTY_LINK \ |
---|
| 8 | as CANONICAL_PROPERTY |
---|
[1684] | 9 | |
---|
[3134] | 10 | |
---|
[1670] | 11 | class TestCanonicalURL(FunctionalTestCase): |
---|
| 12 | |
---|
| 13 | def afterSetUp(self): |
---|
[3134] | 14 | self.basic_auth = ':'.join((portal_owner, default_password)) |
---|
[2139] | 15 | self.loginAsPortalOwner() |
---|
| 16 | # Preparation for functional testing |
---|
[1670] | 17 | self.portal.invokeFactory('Document', id='mydoc') |
---|
| 18 | self.mydoc = self.portal['mydoc'] |
---|
| 19 | self.mydoc_path = "/%s" % self.mydoc.absolute_url(1) |
---|
[1671] | 20 | self.curl = re.compile('<link\srel\s*=\s*"canonical"\s+' \ |
---|
[3134] | 21 | '[^>]*href\s*=\s*\"([^\"]*)\"[^>]*>', |
---|
| 22 | re.S | re.M) |
---|
[1670] | 23 | |
---|
[3224] | 24 | def test_NoCanonicalURL(self): |
---|
[1670] | 25 | html = self.publish(self.mydoc_path, self.basic_auth).getBody() |
---|
| 26 | foundcurls = self.curl.findall(html) |
---|
[3224] | 27 | assert not self.mydoc.hasProperty(CANONICAL_PROPERTY) |
---|
| 28 | self.assertTrue(not foundcurls, "CANONICAL URL found, " \ |
---|
| 29 | "but object hasn't '%s' property" % CANONICAL_PROPERTY) |
---|
[1670] | 30 | |
---|
[3224] | 31 | def test_CanonicalProperty(self): |
---|
| 32 | self.assertTrue(not self.mydoc.hasProperty(CANONICAL_PROPERTY), |
---|
| 33 | 'Canonical URL property is present in new document.') |
---|
| 34 | |
---|
| 35 | def test_CanonicalPropertyEnable(self): |
---|
| 36 | curl = '/newcanonical' |
---|
| 37 | res = self.publish(self.mydoc_path + '/@@seo-context-properties?' \ |
---|
| 38 | 'seo_canonical=%s&seo_canonical_override=checked&'\ |
---|
| 39 | 'form.submitted=1&form.button.Save=Save' % curl, |
---|
| 40 | self.basic_auth).getBody() |
---|
| 41 | |
---|
| 42 | self.assertTrue(self.mydoc.hasProperty(CANONICAL_PROPERTY), |
---|
| 43 | 'Overriding Canonical URL enabled,' \ |
---|
| 44 | 'but object hasn\'t canonical url property') |
---|
| 45 | |
---|
| 46 | self.assertTrue(self.mydoc.getProperty(CANONICAL_PROPERTY) == curl, |
---|
| 47 | "Wrong Canonical URL for document: %s, all must be: %s" |
---|
| 48 | % (self.mydoc.getProperty(CANONICAL_PROPERTY), curl)) |
---|
| 49 | |
---|
| 50 | def test_CanonicalPropertyDisable(self): |
---|
| 51 | curl = '/newcanonical' |
---|
| 52 | self.mydoc.manage_addProperty(CANONICAL_PROPERTY, curl, |
---|
| 53 | 'string') |
---|
| 54 | |
---|
| 55 | assert self.mydoc.getProperty(CANONICAL_PROPERTY) == curl |
---|
| 56 | |
---|
| 57 | res = self.publish(self.mydoc_path + '/@@seo-context-properties?' \ |
---|
| 58 | 'seo_canonical=%s&seo_canonical_override=&'\ |
---|
| 59 | 'form.submitted=1&form.button.Save=Save' % curl, |
---|
| 60 | self.basic_auth).getBody() |
---|
| 61 | |
---|
| 62 | self.assertTrue(not self.mydoc.hasProperty(CANONICAL_PROPERTY), |
---|
| 63 | 'Overriding Canonical URL disabled,' \ |
---|
| 64 | 'but canonical link is present in object properties') |
---|
| 65 | |
---|
| 66 | def test_CanonicalUrlPresent(self): |
---|
| 67 | self.mydoc.manage_addProperty(CANONICAL_PROPERTY, self.mydoc_path, |
---|
| 68 | 'string') |
---|
| 69 | assert self.mydoc.hasProperty(CANONICAL_PROPERTY) |
---|
| 70 | |
---|
| 71 | html = self.publish(self.mydoc_path, self.basic_auth).getBody() |
---|
| 72 | foundcurls = self.curl.findall(html) |
---|
| 73 | |
---|
| 74 | self.assertTrue([1 for curl in foundcurls if curl == self.mydoc_path], |
---|
[1671] | 75 | "Wrong CANONICAL URL for document: %s, all must be: %s" % ( |
---|
[3224] | 76 | foundcurls, self.mydoc_path)) |
---|
[1670] | 77 | |
---|
| 78 | def test_updateCanonicalURL(self): |
---|
| 79 | mydoc_url_new = self.mydoc.absolute_url() + '.new' |
---|
| 80 | # Update canonical url property |
---|
[1671] | 81 | self.publish(self.mydoc_path + '/@@seo-context-properties?' \ |
---|
[3134] | 82 | 'seo_canonical_override=checked&seo_canonical=%s&' \ |
---|
| 83 | 'form.submitted=1&form.button.Save=Save' % mydoc_url_new, |
---|
| 84 | self.basic_auth) |
---|
[1670] | 85 | # Test updated canonical url |
---|
| 86 | html = self.publish(self.mydoc_path, self.basic_auth).getBody() |
---|
| 87 | foundcurls = self.curl.findall(html) |
---|
| 88 | |
---|
[2139] | 89 | qseo_url = ICanonicalLink(self.mydoc).canonical_link |
---|
[1671] | 90 | self.assertTrue(qseo_url == mydoc_url_new, |
---|
[3134] | 91 | "Not set 'qSEO_canonical' property") |
---|
| 92 | self.assertTrue([1 for curl in foundcurls if curl == mydoc_url_new], |
---|
| 93 | "Wrong CANONICAL URL for document: %s, all must be: %s" |
---|
| 94 | % (foundcurls, mydoc_url_new)) |
---|
[1670] | 95 | |
---|
[2139] | 96 | def test_defaultCanonical(self): |
---|
| 97 | expect = self.mydoc.absolute_url() |
---|
| 98 | cpath = ICanonicalLink(self.mydoc).canonical_link |
---|
| 99 | self.assertTrue(cpath == expect, |
---|
| 100 | "Default canonical link adapter return: '%s', must be: '%s'" % ( |
---|
| 101 | cpath, expect)) |
---|
[1676] | 102 | |
---|
| 103 | def testCatalogUpdated(self): |
---|
[3141] | 104 | getToolByName(self.portal, 'portal_url') |
---|
[1676] | 105 | catalog = getToolByName(self.portal, 'portal_catalog') |
---|
[2139] | 106 | catalog.addColumn('canonical_link') |
---|
[1676] | 107 | |
---|
| 108 | # get catalog data before update |
---|
[2139] | 109 | mydoc_catalog_canonical = catalog(id="mydoc")[0].canonical_link |
---|
[1676] | 110 | self.assertTrue(not mydoc_catalog_canonical) |
---|
| 111 | |
---|
| 112 | # Update canonical url property |
---|
| 113 | mydoc_url_new = self.mydoc.absolute_url() + '.new' |
---|
| 114 | self.publish(self.mydoc_path + '/@@seo-context-properties?' \ |
---|
| 115 | 'seo_canonical_override=checked&seo_canonical=%s' \ |
---|
[3012] | 116 | '&form.submitted=1&form.button.Save=Save' % mydoc_url_new, |
---|
| 117 | self.basic_auth) |
---|
[1676] | 118 | |
---|
[2139] | 119 | newcpath = ICanonicalLink(self.mydoc).canonical_link |
---|
| 120 | mydoc_catalog_canonical = catalog(id="mydoc")[0].canonical_link |
---|
[1676] | 121 | self.assertTrue(newcpath == mydoc_catalog_canonical, |
---|
[3134] | 122 | "canonical path get by adapter: '%s' not equals to "\ |
---|
| 123 | "cataloged one: '%s'" % (newcpath, |
---|
| 124 | mydoc_catalog_canonical)) |
---|
[1676] | 125 | |
---|
[2139] | 126 | def test_canonicalValidation(self): |
---|
| 127 | wrong_canonical = 'wrong canonical' |
---|
| 128 | # Update canonical url property |
---|
| 129 | html = self.publish(self.mydoc_path + '/@@seo-context-properties?' \ |
---|
[3134] | 130 | 'seo_canonical_override=checked&seo_canonical=%s&'\ |
---|
| 131 | 'form.submitted=1&form.button.Save=Save' |
---|
| 132 | % wrong_canonical, self.basic_auth).getBody() |
---|
[2139] | 133 | self.assertTrue("wrong canonical url" in html, |
---|
| 134 | "Canonical url not validated") |
---|
[1676] | 135 | |
---|
[2139] | 136 | def test_delCanonical(self): |
---|
| 137 | newcanonical = '/new_canonical' |
---|
| 138 | ICanonicalLink(self.mydoc).canonical_link = newcanonical |
---|
| 139 | |
---|
| 140 | assert ICanonicalLink(self.mydoc).canonical_link == newcanonical |
---|
| 141 | |
---|
| 142 | # remove canonical url customization |
---|
| 143 | self.publish(self.mydoc_path + '/@@seo-context-properties?' \ |
---|
[3134] | 144 | 'seo_canonical=%s&seo_canonical_override=&'\ |
---|
| 145 | 'form.submitted=1&form.button.Save=Save' % newcanonical, |
---|
| 146 | self.basic_auth) |
---|
[2139] | 147 | |
---|
| 148 | mydoc_canonical = ICanonicalLink(self.mydoc).canonical_link |
---|
| 149 | self.assertTrue(mydoc_canonical == self.mydoc.absolute_url(), |
---|
| 150 | "Steel customized canonical url after remove customization") |
---|
| 151 | |
---|
| 152 | |
---|
[1670] | 153 | def test_suite(): |
---|
| 154 | from unittest import TestSuite, makeSuite |
---|
| 155 | suite = TestSuite() |
---|
| 156 | suite.addTest(makeSuite(TestCanonicalURL)) |
---|
| 157 | return suite |
---|