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

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

#227: Added test for not override existing schemas extenders

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