Skip to content

Commit 776e90c

Browse files
committed
fixed getting canonical url property
1 parent 02acdc6 commit 776e90c

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

quintagroup/seoptimizer/browser/views.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def _getSEOTags(self):
6464
"meta_keywords": self.getSEOProperty('qSEO_keywords',
6565
'Subject', ()),
6666
"seo_keywords": self.getSEOProperty('qSEO_keywords', default=()),
67-
"seo_canonical": self.getCanonical(),
67+
"seo_canonical": self.getSEOProperty(CANONICAL_PROPERTY),
6868
# Add test properties
6969
"has_seo_title": self.context.hasProperty('qSEO_title'),
7070
"has_seo_robots": self.context.hasProperty('qSEO_robots'),
@@ -157,9 +157,13 @@ def seo_globalCustomMetaTags(self):
157157
name_value[1] or ''})
158158
return result
159159

160+
# Not used
160161
def getCanonical(self):
161-
canonical = queryAdapter(self.context, ICanonicalLink)
162-
return canonical and canonical.canonical_link or ""
162+
# TODO: rewrite function structure
163+
if self.context.hasProperty(CANONICAL_PROPERTY):
164+
canonical = queryAdapter(self.context, ICanonicalLink)
165+
return canonical and canonical.canonical_link or ""
166+
return ""
163167

164168

165169
class SEOContextPropertiesView(BrowserView):

quintagroup/seoptimizer/tests/testCanonicalURL.py

+51-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
default_password
55
import re
66
from Products.CMFCore.utils import getToolByName
7+
from quintagroup.canonicalpath.adapters import PROPERTY_LINK \
8+
as CANONICAL_PROPERTY
79

810

911
class TestCanonicalURL(FunctionalTestCase):
@@ -19,14 +21,59 @@ def afterSetUp(self):
1921
'[^>]*href\s*=\s*\"([^\"]*)\"[^>]*>',
2022
re.S | re.M)
2123

22-
def test_CanonicalURL(self):
24+
def test_NoCanonicalURL(self):
2325
html = self.publish(self.mydoc_path, self.basic_auth).getBody()
2426
foundcurls = self.curl.findall(html)
25-
mydoc_url = self.mydoc.absolute_url()
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)
2630

27-
self.assertTrue([1 for curl in foundcurls if curl == mydoc_url],
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],
2875
"Wrong CANONICAL URL for document: %s, all must be: %s" % (
29-
foundcurls, mydoc_url))
76+
foundcurls, self.mydoc_path))
3077

3178
def test_updateCanonicalURL(self):
3279
mydoc_url_new = self.mydoc.absolute_url() + '.new'

0 commit comments

Comments
 (0)