source: products/quintagroup.seoptimizer/trunk/quintagroup/seoptimizer/tests/testCanonicalURL.py @ 1674

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

Reregister ICanonicalPath adapter to IATContentType interface, which is more specific then IBaseContent (which registered by quintagroup.canonical pack)

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1import re
2from zope.component import queryAdapter
3from Products.Archetypes.interfaces import IBaseContent
4
5from quintagroup.seoptimizer.interfaces import ICanonicalPath
6from base import getToolByName, FunctionalTestCase, newSecurityManager
7from config import *
8
9class TestCanonicalURL(FunctionalTestCase):
10
11    def afterSetUp(self):
12        self.qi = self.portal.portal_quickinstaller
13        self.qi.installProduct(PROJECT_NAME)
14        #self.portal.changeSkin('Plone Default')
15
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_canonicalAdapterRegistration(self):
58        canonical = queryAdapter(self.mydoc, interface=ICanonicalPath)
59        self.assertTrue(canonical is not None,
60            "Not registered 'qseo_canonical' adapter")
61
62    def test_canonicalAdapter(self):
63        purl = getToolByName(self.portal, 'portal_url')
64        mydoc_path_rel = '/'+'/'.join(purl.getRelativeContentPath(self.mydoc))
65
66        canonical = queryAdapter(self.mydoc, ICanonicalPath)
67        cpath = canonical.canonical_path()
68        self.assertTrue(cpath == mydoc_path_rel,
69            "By canonical path adapter got: '%s', must be: '%s'" % (
70             cpath, mydoc_path_rel))
71
72        # Update canonical url property
73        mydoc_url_new = self.mydoc.absolute_url() + '.new'
74        self.publish(self.mydoc_path + '/@@seo-context-properties?' \
75            'seo_canonical_override=checked&seo_canonical=%s' \
76            '&form.submitted=1' % mydoc_url_new, self.basic_auth)
77
78        mydoc_path_rel_new = mydoc_path_rel + '.new'
79        newcpath = canonical.canonical_path()
80        self.assertTrue(newcpath == mydoc_path_rel_new,
81            "By canonical path adapter got: '%s', must be: '%s'" % (
82             newcpath, mydoc_path_rel_new))
83       
84
85def test_suite():
86    from unittest import TestSuite, makeSuite
87    suite = TestSuite()
88    suite.addTest(makeSuite(TestCanonicalURL))
89    return suite
Note: See TracBrowser for help on using the repository browser.