1 | from quintagroup.canonicalpath.interfaces import ICanonicalLink |
---|
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 |
---|
7 | from quintagroup.canonicalpath.adapters import PROPERTY_LINK \ |
---|
8 | as CANONICAL_PROPERTY |
---|
9 | |
---|
10 | |
---|
11 | class TestCanonicalURL(FunctionalTestCase): |
---|
12 | |
---|
13 | def afterSetUp(self): |
---|
14 | self.basic_auth = ':'.join((portal_owner, default_password)) |
---|
15 | self.loginAsPortalOwner() |
---|
16 | # Preparation for functional testing |
---|
17 | self.portal.invokeFactory('Document', id='mydoc') |
---|
18 | self.mydoc = self.portal['mydoc'] |
---|
19 | self.mydoc_path = "/%s" % self.mydoc.absolute_url(1) |
---|
20 | self.curl = re.compile('<link\srel\s*=\s*"canonical"\s+' \ |
---|
21 | '[^>]*href\s*=\s*\"([^\"]*)\"[^>]*>', |
---|
22 | re.S | re.M) |
---|
23 | |
---|
24 | def test_NoCanonicalURL(self): |
---|
25 | html = self.publish(self.mydoc_path, self.basic_auth).getBody() |
---|
26 | foundcurls = self.curl.findall(html) |
---|
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) |
---|
30 | |
---|
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], |
---|
75 | "Wrong CANONICAL URL for document: %s, all must be: %s" % ( |
---|
76 | foundcurls, self.mydoc_path)) |
---|
77 | |
---|
78 | def test_updateCanonicalURL(self): |
---|
79 | mydoc_url_new = self.mydoc.absolute_url() + '.new' |
---|
80 | # Update canonical url property |
---|
81 | self.publish(self.mydoc_path + '/@@seo-context-properties?' \ |
---|
82 | 'seo_canonical_override=checked&seo_canonical=%s&' \ |
---|
83 | 'form.submitted=1&form.button.Save=Save' % mydoc_url_new, |
---|
84 | self.basic_auth) |
---|
85 | # Test updated canonical url |
---|
86 | html = self.publish(self.mydoc_path, self.basic_auth).getBody() |
---|
87 | foundcurls = self.curl.findall(html) |
---|
88 | |
---|
89 | qseo_url = ICanonicalLink(self.mydoc).canonical_link |
---|
90 | self.assertTrue(qseo_url == mydoc_url_new, |
---|
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)) |
---|
95 | |
---|
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)) |
---|
102 | |
---|
103 | def testCatalogUpdated(self): |
---|
104 | getToolByName(self.portal, 'portal_url') |
---|
105 | catalog = getToolByName(self.portal, 'portal_catalog') |
---|
106 | catalog.addColumn('canonical_link') |
---|
107 | |
---|
108 | # get catalog data before update |
---|
109 | mydoc_catalog_canonical = catalog(id="mydoc")[0].canonical_link |
---|
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' \ |
---|
116 | '&form.submitted=1&form.button.Save=Save' % mydoc_url_new, |
---|
117 | self.basic_auth) |
---|
118 | |
---|
119 | newcpath = ICanonicalLink(self.mydoc).canonical_link |
---|
120 | mydoc_catalog_canonical = catalog(id="mydoc")[0].canonical_link |
---|
121 | self.assertTrue(newcpath == mydoc_catalog_canonical, |
---|
122 | "canonical path get by adapter: '%s' not equals to "\ |
---|
123 | "cataloged one: '%s'" % (newcpath, |
---|
124 | mydoc_catalog_canonical)) |
---|
125 | |
---|
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?' \ |
---|
130 | 'seo_canonical_override=checked&seo_canonical=%s&'\ |
---|
131 | 'form.submitted=1&form.button.Save=Save' |
---|
132 | % wrong_canonical, self.basic_auth).getBody() |
---|
133 | self.assertTrue("wrong canonical url" in html, |
---|
134 | "Canonical url not validated") |
---|
135 | |
---|
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?' \ |
---|
144 | 'seo_canonical=%s&seo_canonical_override=&'\ |
---|
145 | 'form.submitted=1&form.button.Save=Save' % newcanonical, |
---|
146 | self.basic_auth) |
---|
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 | |
---|
153 | def test_suite(): |
---|
154 | from unittest import TestSuite, makeSuite |
---|
155 | suite = TestSuite() |
---|
156 | suite.addTest(makeSuite(TestCanonicalURL)) |
---|
157 | return suite |
---|