source: products/quintagroup.plonegooglesitemaps/trunk/quintagroup/plonegooglesitemaps/tests/testNewsSitemaps.py @ 2422

Last change on this file since 2422 was 2422, checked in by mylan, 9 years ago

#131: Updated News sitemaps tests

  • Property svn:eol-style set to native
File size: 8.6 KB
Line 
1from base import *
2from DateTime import DateTime
3
4from zope.component import getSiteManager, getGlobalSiteManager
5from archetypes.schemaextender.interfaces import ISchemaExtender
6
7from Products.CMFPlone.utils import _createObjectByType
8
9class TestNewsSitemapsXML(FunctionalTestCase):
10
11    def afterSetUp(self):
12        super(TestNewsSitemapsXML, self).afterSetUp()
13        # Create news sitemaps
14        _createObjectByType("Sitemap", self.portal, id="news-sitemaps",
15                            sitemapType="news", portalTypes=("News Item",))
16        self.portal["news-sitemaps"].at_post_create_script()
17        # Add testing news item to portal
18        self.pubdate = (DateTime()+1).strftime("%Y-%m-%d")
19        my_news = self.portal.invokeFactory("News Item", id="my_news")
20        self.my_news = self.portal["my_news"]
21        self.my_news.edit(text="Test news item", title="First news (test)", language="ua",
22                          effectiveDate=self.pubdate, gsm_access="Registration",
23                          gsm_genres=("PressRelease",), gsm_stock="NASDAQ:AMAT, BOM:500325")
24        self.portal.portal_workflow.doActionFor(self.my_news, "publish")
25        self.reParse()
26
27    def reParse(self):
28        # Parse news sitemap
29        self.sitemap = self.publish("/"+self.portal.absolute_url(1) + "/news-sitemaps",
30                                    "%s:%s" % (portal_owner, default_password)).getBody()
31        parsed_sitemap = parse(self.sitemap)
32        self.start = parsed_sitemap["start"]
33        self.data = parsed_sitemap["data"]
34
35    def test_urlset(self):
36        self.assert_("urlset" in self.start.keys())
37        urlset = self.start["urlset"]
38        self.assertEqual(urlset.get("xmlns", ""), "http://www.sitemaps.org/schemas/sitemap/0.9")
39        self.assertEqual(urlset.get("xmlns:n", ""), "http://www.google.com/schemas/sitemap-news/0.9")
40
41    def test_url(self):
42        self.assert_("url" in self.start.keys())
43
44    def test_loc(self):
45        self.assert_("loc" in self.start.keys())
46        self.assert_(self.portal.absolute_url() + "/my_news" in self.data)
47
48    def test_nnews(self):
49        self.assert_("n:news" in self.start.keys())
50       
51    def test_npublication(self):
52        self.assert_("n:publication" in self.start.keys())
53        self.assert_("n:name" in self.start.keys())
54        self.assert_("First news" in self.data, "No 'First news' in data")
55        self.assert_("n:language" in self.start.keys())
56        self.assert_("ua" in self.data, "No 'ua' in data")
57
58    def test_npublication_date(self):
59        self.assert_("n:publication_date" in self.start.keys())
60        self.assert_(self.pubdate in self.data, "No %s in data" % self.pubdate)
61       
62    def test_ntitle(self):
63        self.assert_("n:title" in self.start.keys())
64        self.assert_("First news (test)" in self.data, "No 'First news (test)' in data")
65
66    def test_naccess(self):
67        # Test when access present
68        self.assert_("n:access" in self.start.keys())
69        self.assert_("Registration" in self.data, "No 'Registration' in data")
70
71    def test_ngenres(self):
72        # Test when genres present
73        self.assert_("n:genres" in self.start.keys())
74        self.assert_("PressRelease" in self.data, "No 'PressRelease' in data")
75
76    def test_ngenresMultiple(self):
77        # Test multiple genres
78        self.my_news.edit(gsm_genres=("PressRelease", "Blog"))
79        self.my_news.reindexObject()
80        self.reParse()
81        self.assert_("n:genres" in self.start.keys())
82        self.assert_("PressRelease, Blog" in self.data, "No 'PressRelease, Blog' in data")
83
84    def test_ngenresEmpty(self):
85        # No genres should present if it's not updated
86        self.my_news.edit(gsm_genres=[])
87        self.my_news.reindexObject()
88        self.reParse()
89        self.assertNotEqual("n:genres" in self.start.keys(), True)
90
91    def test_ngenresForNotExtended(self):
92        # No genres should present for not extended content type
93        self.portal.invokeFactory("Document", id="my_doc")
94        my_doc = getattr(self.portal, "my_doc")
95        my_doc.edit(text="Test document")
96        self.portal.portal_workflow.doActionFor(my_doc, "publish")
97        self.portal["news-sitemaps"].edit(portalTypes=("Document",))
98        self.reParse()
99        open("/tmp/news.sm.docs.xml", "w").write(self.sitemap)
100        self.assertNotEqual("n:genres" in self.start.keys(), True)
101
102    def test_nstock_tickers(self):
103        # Test n:stock_tickers
104        self.assert_("n:stock_tickers" in self.start.keys())
105        self.assert_("NASDAQ:AMAT, BOM:500325" in self.data, "No 'NASDAQ:AMAT, BOM:500325' in data")
106
107
108class TestNewsSitemapsXMLDefaultObject(FunctionalTestCase):
109
110    def afterSetUp(self):
111        super(TestNewsSitemapsXMLDefaultObject, self).afterSetUp()
112        # Create news sitemaps
113        _createObjectByType("Sitemap", self.portal, id="news-sitemaps",
114                            sitemapType="news", portalTypes=("News Item",))
115        self.portal["news-sitemaps"].at_post_create_script()
116        # Add minimal testing news item to portal
117        self.pubdate = (DateTime()+1).strftime("%Y-%m-%d")
118        my_news = self.portal.invokeFactory("News Item", id="my_news")
119        self.my_news = self.portal["my_news"]
120        self.my_news.edit(effectiveDate=self.pubdate)
121        self.portal.portal_workflow.doActionFor(self.my_news, "publish")
122        self.reParse()
123
124    def reParse(self):
125        # Parse news sitemap
126        self.sitemap = self.publish("/"+self.portal.absolute_url(1) + "/news-sitemaps",
127                                    "%s:%s" % (portal_owner, default_password)).getBody()
128        parsed_sitemap = parse(self.sitemap)
129        self.start = parsed_sitemap["start"]
130        self.data = parsed_sitemap["data"]
131
132    def test_nnews(self):
133        self.assert_("n:news" in self.start.keys())
134       
135    def test_npublication(self):
136        self.assert_("n:publication" in self.start.keys())
137        self.assert_("n:name" in self.start.keys())
138        self.assert_("my_news" in self.data, "No 'First news' in data")
139        self.assert_("n:language" in self.start.keys())
140        self.assert_("en" in self.data, "No 'en' in data")
141
142    def test_npublication_date(self):
143        self.assert_("n:publication_date" in self.start.keys())
144        self.assert_(self.pubdate in self.data, "No %s in data" % self.pubdate)
145       
146    def test_ntitle(self):
147        self.assert_("n:title" in self.start.keys())
148        self.assert_("my_news" in self.data, "No 'First news (test)' in data")
149
150    def test_no_naccess(self):
151        self.assert_("n:access" not in self.start.keys())
152
153    def test_no_ngenres(self):
154        self.assert_("n:genres" not in self.start.keys())
155
156    def test_no_keywords(self):
157        self.assert_("n:keywords" not in self.start.keys())
158
159    def test_no_keywords(self):
160        self.assert_("n:stock_tickers" not in self.start.keys())
161
162
163from Products.ATContentTypes.interface import IATNewsItem
164from quintagroup.plonegooglesitemaps.content.newsextender import NewsExtender
165
166class TestSchemaExtending(TestCase):
167
168    def afterSetUp(self):
169        super(TestSchemaExtending, self).afterSetUp()
170        self.loginAsPortalOwner()
171        # Add testing news item to portal
172        my_news = self.portal.invokeFactory("News Item", id="my_news")
173        self.my_news = self.portal["my_news"]
174        my_doc = self.portal.invokeFactory("Document", id="my_doc")
175        self.my_doc = self.portal["my_doc"]
176
177    def testExtendNewsItemByDefault(self):
178        # Neither of object has extended fields
179        self.assertNotEqual(self.my_news.getField("gsm_access"), None)
180        self.assertNotEqual(self.my_news.getField("gsm_genres"), None)
181        self.assertNotEqual(self.my_news.getField("gsm_stock"), None)
182        self.assertEqual(self.my_doc.getField("gsm_access"), None)
183        self.assertEqual(self.my_doc.getField("gsm_genres"), None)
184        self.assertEqual(self.my_doc.getField("gsm_stock"), None)
185   
186    def testRegistrationOnLocalSM(self):
187        """SchemaExtender adapters must be registered
188           in Local SiteManager only.
189        """
190        localsm = getSiteManager(self.portal)
191        globalsm = getGlobalSiteManager()
192        # Now register SchemaExtender adapter and
193        # check if it present in Local SiteManger only
194        self.assertNotEqual(localsm, globalsm)
195        self.assertNotEqual(localsm.queryAdapter(self.my_news, ISchemaExtender), None)
196        self.assertEqual(globalsm.queryAdapter(self.my_news, ISchemaExtender), None)
197
198
199def test_suite():
200    from unittest import TestSuite, makeSuite
201    suite = TestSuite()
202    suite.addTest(makeSuite(TestNewsSitemapsXML))
203    suite.addTest(makeSuite(TestNewsSitemapsXMLDefaultObject))
204    suite.addTest(makeSuite(TestSchemaExtending))
205    return suite
Note: See TracBrowser for help on using the repository browser.