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