Back to index

plone3  3.1.7
test_FAQ.py
Go to the documentation of this file.
00001 # Tests which use the FAQ 'product' in this folder.
00002 # This is a folderish content type which also uses references.
00003 #
00004 # Nastily patch Products to get FAQ accepted
00005 import os
00006 import Products
00007 Products.__path__.insert(0, os.path.dirname(os.path.abspath(__file__)))
00008 
00009 from Products.PloneTestCase import PloneTestCase
00010 
00011 PloneTestCase.installProduct('FAQ')
00012 PloneTestCase.setupPloneSite(products=('FAQ', ))
00013 
00014 from Products.CMFEditions import PACKAGE_HOME
00015 
00016 class TestATContents(PloneTestCase.PloneTestCase):
00017 
00018     def afterSetUp(self):
00019         self.membership = self.portal.portal_membership
00020         self.catalog = self.portal.portal_catalog
00021         self.workflow = self.portal.portal_workflow
00022         self.portal_repository = self.portal.portal_repository
00023         self.portal_archivist = self.portal.portal_archivist
00024         self.folder.manage_permission('Add FAQ content', ['Member', 'Manager'])
00025 
00026     def set_metadata(self, obj, text):
00027         obj.setTitle(text + ' title')
00028         obj.setSubject(text + ' subject')
00029         obj.setDescription(text + ' description')
00030         obj.setContributors(text + ' contributors')
00031         obj.setLanguage(text + ' language')
00032         obj.setRights(text + ' rights')
00033 
00034     def metadata_test(self, obj, text):
00035         self.assertEqual(obj.Title(), text + ' title')
00036         self.assertEqual(obj.Subject(), (text + ' subject',))
00037         self.assertEqual(obj.Description(), text + ' description')
00038         self.assertEqual(obj.Contributors(), (text + ' contributors',))
00039         self.assertEqual(obj.Language(), text + ' language')
00040         self.assertEqual(obj.Rights(), text + ' rights')
00041 
00042     def getPermissionsOfRole(self, role):
00043         perms = self.portal.permissionsOfRole(role)
00044         return [p['name'] for p in perms if p['selected']]
00045 
00046     def setFAQ(self, content, tag):
00047         content.setSummary('summary '+tag)
00048         content.setIntroduction('intro '+tag)
00049         content.setSections(('sections '+tag+' 1', 'sections '+tag+' 2'))
00050         self.set_metadata(content, 'content '+tag)
00051 
00052     def verifyFAQ(self, content, tag):
00053         self.assertEqual(content.getSummary(), 'summary '+tag)
00054         self.assertEqual(content.getIntroduction(), 'intro '+tag)
00055         self.assertEqual(content.getSections(), ('sections '+tag+' 1', 'sections '+tag+' 2'))
00056         self.metadata_test(content, 'content '+tag)
00057         
00058     def testFAQ(self):
00059         self.folder.invokeFactory('FAQ', id='test_faq')
00060         portal_repository = self.portal_repository
00061         content = self.folder.test_faq
00062         self.setFAQ(content, 'v1')
00063         portal_repository.applyVersionControl(content, comment='save no 1')
00064         self.setFAQ(content, 'v2')
00065         portal_repository.save(content, comment='save no 2')
00066         vdata = portal_repository.retrieve(content, 0)
00067         obj = vdata.object
00068         self.verifyFAQ(obj, 'v1')
00069         vdata = portal_repository.retrieve(content, 1)
00070         obj = vdata.object
00071         self.verifyFAQ(obj, 'v2')
00072         portal_repository.revert(content, 0)
00073         self.verifyFAQ(content, 'v1')
00074 
00075     def setQuestion(self, question, tag):
00076         question.setSection('section '+tag)
00077         question.setTitle('title '+tag)
00078         question.setAnswer('answer '+tag)
00079 
00080     def verifyQuestion(self, question, tag):
00081         self.assertEqual(question.getSection(), 'section '+tag)
00082         self.assertEqual(question.getTitle(), 'title '+tag)
00083         self.assertEqual(question.getAnswer(), 'answer '+tag)
00084 
00085     def testFAQWithContent(self):
00086         self.folder.invokeFactory('FAQ', id='test_faq')
00087         portal_repository = self.portal_repository
00088         faq = self.folder.test_faq
00089         self.setFAQ(faq, 'v1')
00090         for id in ('q1', 'q2'):
00091             faq.invokeFactory('FAQQuestion', id=id)
00092             question = getattr(faq, id)
00093             self.setQuestion(question, id+' v1')
00094 
00095         portal_repository.applyVersionControl(faq, comment='save no 1')
00096         self.setFAQ(faq, 'v2')
00097         for id in ('q1', 'q2'):
00098             question = getattr(faq, id)
00099             self.setQuestion(question, id+' v2')
00100         portal_repository.save(faq, comment='save no 2')
00101 
00102         vdata = portal_repository.retrieve(faq, 0)
00103         obj = vdata.object
00104         self.verifyFAQ(obj, 'v1')
00105         for id in ('q1', 'q2'):
00106             question = getattr(obj, id)
00107             self.verifyQuestion(question, id+' v1')
00108 
00109         vdata = portal_repository.retrieve(faq, 1)
00110         obj = vdata.object
00111         self.verifyFAQ(obj, 'v2')
00112         for id in ('q1', 'q2'):
00113             question = getattr(obj, id)
00114             self.verifyQuestion(question, id+' v2')
00115 
00116         for id in ('q1', 'q2'):
00117             question = getattr(faq, id)
00118             self.verifyQuestion(question, id+' v2')
00119         portal_repository.revert(faq, 0)
00120         self.verifyFAQ(faq, 'v1')
00121         for id in ('q1', 'q2'):
00122             question = getattr(faq, id)
00123             self.verifyQuestion(question, id+' v1')
00124 
00125     def createImage(self, id='image'):
00126         self.folder.invokeFactory('Image', id=id)
00127         img1 = open(os.path.join(PACKAGE_HOME, 'tests/images/img1.png'), 'rb').read()
00128         portal_repository = self.portal_repository
00129         portal_archivist = self.portal_archivist
00130         content = getattr(self.folder, id)
00131         content.edit(file=img1)
00132         return content
00133 
00134     def testContentWithReferences(self):
00135         self.folder.invokeFactory('FAQ', id='test_faq')
00136         image = self.createImage()
00137         image2 = self.createImage(id='image2')
00138         portal_repository = self.portal_repository
00139         faq = self.folder.test_faq
00140         self.setFAQ(faq, 'v1')
00141         portal_repository.save(faq, comment='save no links')
00142         faq.setLinks([image.UID()])
00143         portal_repository.save(faq, comment='save with link')
00144         faq.setLinks([image2.UID()])
00145         portal_repository.save(faq, comment='save with other link')
00146         bothLinks = [image2.UID(), image.UID()]
00147         bothLinks.sort()
00148         faq.setLinks(bothLinks)
00149         portal_repository.save(faq, comment='save with other link')
00150         self.assertEquals(2, len(faq.getRawLinks()))
00151 #         print "Link stuff", faq.getLinks()
00152 #         print "Raw Link stuff", faq.getRawLinks()
00153 
00154         expected = [ (0, []),
00155             (1, [image.UID()]),
00156             (2, [image2.UID()]),
00157             (3, bothLinks)
00158             ]
00159 
00160         for version, explinks in expected:
00161             portal_repository.revert(faq, version)
00162 
00163             # Reverting reverts back the references
00164             links = faq.getRawLinks()
00165             links.sort()
00166 
00167             folderLinks = [ ref.targetUID for ref in faq.at_references.objectValues()]
00168             folderLinks.sort()
00169 
00170 #             print "folderlinks", folderLinks
00171 #             print "Link stuff", faq.getLinks()
00172 #             print "Raw Link stuff", faq.getRawLinks()
00173 
00174             # XXX This assertion checks that the reference engine has
00175             # not been corrupted, but currently it fails.
00176             # XXX
00177             self.assertEquals(folderLinks, links,
00178                  "Version %d: links on object do not match reference catalog\nfolder %r, links %r" % (version, explinks, links))
00179             
00180             explinks.sort()
00181             self.assertEquals(explinks, folderLinks,
00182                 "Version %d, expected %r, actual %r" % (version, explinks, folderLinks))
00183 
00184 def test_suite():
00185     from unittest import TestSuite, makeSuite
00186     suite = TestSuite()
00187     suite.addTest(makeSuite(TestATContents))
00188     return suite