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

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

Remove config module from tests, move all constants into testInstallation and testResponce modules

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