source: products/quintagroup.seoptimizer/branches/refactoring2.3.0/quintagroup/seoptimizer/tests/testCanonicalURL.py @ 1856

Last change on this file since 1856 was 1856, checked in by mylan, 14 years ago

#156: Fix Canonical URL adatper test

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