source: products/quintagroup.plonegooglesitemaps/branches/test_refactoring/quintagroup/plonegooglesitemaps/tests/testMobileSitemaps.py @ 2529

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

#206: Improve code coverage - added test for MobileSitemap?, MobileSitemapView?.

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1from base import *
2from DateTime import DateTime
3
4from zope.interface import Interface, alsoProvides
5from zope.component import queryMultiAdapter
6from zope.component import getSiteManager, getGlobalSiteManager
7from archetypes.schemaextender.interfaces import ISchemaExtender
8
9from Products.CMFPlone.utils import _createObjectByType
10
11from quintagroup.plonegooglesitemaps.browser import mobilesitemapview
12from quintagroup.plonegooglesitemaps.browser.commonview import CommonSitemapView
13from quintagroup.plonegooglesitemaps.browser.mobilesitemapview import MobileSitemapView
14
15class IMobileMarker(Interface):
16    """Marker interface for mobile objects"""
17
18class TestMobileSitemapsXML(FunctionalTestCase):
19
20    def afterSetUp(self):
21        super(TestMobileSitemapsXML, self).afterSetUp()
22        # patch mobile sitemap view
23        self.orig_mobile_ifaces = mobilesitemapview.MOBILE_INTERFACES
24        mobilesitemapview.MOBILE_INTERFACES = [IMobileMarker.__identifier__,]
25          # Create mobile sitemaps
26        _createObjectByType("Sitemap", self.portal, id="mobile-sitemap.xml",
27                            sitemapType="mobile", portalTypes=("Document",))
28        self.portal["mobile-sitemap.xml"].at_post_create_script()
29        # Add testing mobile item to portal
30        self.pubdate = (DateTime()+1).strftime("%Y-%m-%d")
31        my_mobile = self.portal.invokeFactory("Document", id="my_mobile")
32        self.my_mobile = self.portal["my_mobile"]
33        alsoProvides(self.my_mobile, IMobileMarker)
34        self.my_mobile.edit(text="Test mobile item", title="First mobile (test)",
35                            effectiveDate=self.pubdate)
36        self.portal.portal_workflow.doActionFor(self.my_mobile, "publish")
37        self.reParse()
38
39    def beforeTearDown(self):
40        mobilesitemapview.MOBILE_INTERFACES = self.orig_mobile_ifaces
41        super(TestMobileSitemapsXML, self).beforeTearDown() 
42
43    def reParse(self):
44        # Parse mobile sitemap
45        self.sitemap = self.publish("/"+self.portal.absolute_url(1) + "/mobile-sitemap.xml",
46                                    "%s:%s" % (portal_owner, default_password)).getBody()
47        parsed_sitemap = parse(self.sitemap)
48        self.start = parsed_sitemap["start"]
49        self.data = parsed_sitemap["data"]
50
51    def test_urlset(self):
52        self.assert_("urlset" in self.start.keys())
53        urlset = self.start["urlset"]
54        self.assertEqual(urlset.get("xmlns", ""), "http://www.sitemaps.org/schemas/sitemap/0.9")
55        self.assertEqual(urlset.get("xmlns:mobile", ""), "http://www.google.com/schemas/sitemap-mobile/1.0")
56
57    def test_url(self):
58        self.assert_("url" in self.start.keys())
59
60    def test_loc(self):
61        self.assert_("loc" in self.start.keys())
62        self.assert_(self.portal.absolute_url() + "/my_mobile" in self.data)
63
64    def test_lastmod(self):
65        md = [f for k,f in mobilesitemapview.MobileSitemapView.additional_maps \
66              if k=='modification_date'][0]
67        bmobile = self.portal.portal_catalog(id="my_mobile")[0]
68        self.assert_("lastmod" in self.start.keys())
69        self.assert_(md(bmobile) in self.data, "Wrong 'modified date':" \
70                     " must be '%s', but exist: '%s'" % (md(bmobile), self.data))
71
72class TestMobileSitemaps(TestCase):
73
74    def afterSetUp(self):
75        super(TestMobileSitemaps, self).afterSetUp()
76        # create mobile sitemap
77        _createObjectByType("Sitemap", self.portal, id="mobile-sitemap.xml",
78                            sitemapType="mobile", portalTypes=("Document",))
79        mobile_sm = self.portal["mobile-sitemap.xml"]
80        mobile_sm.at_post_create_script()
81        self.default_layout = mobile_sm.getProperty('layout', "")
82        self.mobile_view = queryMultiAdapter((mobile_sm, self.portal.REQUEST),
83                               name=self.default_layout)
84
85    def testLayout(self):
86        self.assert_(self.default_layout == "mobile-sitemap.xml")
87
88    def testInterface(self):
89        self.assert_(mobilesitemapview.ISitemapView.providedBy(self.mobile_view))
90
91    def testClasses(self):
92        self.assert_(isinstance(self.mobile_view, MobileSitemapView))
93        self.assert_(isinstance(self.mobile_view, CommonSitemapView))
94
95    def testAdditionalMaps(self):
96        self.assert_(hasattr(self.mobile_view, "additional_maps"))
97        self.assert_([1 for k,f in self.mobile_view.additional_maps \
98                 if k=="modification_date"])
99   
100
101def test_suite():
102    from unittest import TestSuite, makeSuite
103    suite = TestSuite()
104    suite.addTest(makeSuite(TestMobileSitemapsXML))
105    suite.addTest(makeSuite(TestMobileSitemaps))
106    return suite
Note: See TracBrowser for help on using the repository browser.