Back to index

plone3  3.1.7
test_linking.py
Go to the documentation of this file.
00001 import os, sys, time
00002 import unittest
00003 from sets import Set
00004 import traceback
00005 from Testing import ZopeTestCase
00006 from wickedtestcase import WickedTestCase, makeContent, TITLE2
00007 from wicked.normalize import titleToNormalizedId
00008 from wicked.at.config import BACKLINK_RELATIONSHIP
00009 from Products.CMFCore.utils import getToolByName
00010 
00011 class Base(WickedTestCase):
00012     wicked_type = 'IronicWiki'
00013     wicked_field = 'body'
00014     
00015     def replaceCreatedIndex(self):
00016         """ replace the 'created' index w/ a field index b/c we need
00017         better than 1 minute resolution for our testing """
00018         cat = getToolByName(self.portal, 'portal_catalog')
00019         cat.delIndex('created')
00020         cat.manage_addIndex('created', 'FieldIndex',
00021                             extra={'indexed_attrs':'created'})
00022         cat.manage_reindexIndex(ids=['created'])
00023     
00024     def demoCreate(self, **kw):
00025         self.login('test_user_1_')
00026         addview = self.page1.restrictedTraverse('@@wickedadd')
00027         addview.add_content(title=self.title, section=self.wicked_field, type_name=self.wicked_type)
00028 
00029     def moveContent(self, obj, target):
00030         cps = obj.aq_parent.manage_copyObjects([obj.getId()])
00031         target.manage_pasteObjects(cps)
00032 
00033 
00034 
00035 class TestWikiLinking(Base):
00036     
00037     def afterSetUp(self):
00038         super(TestWikiLinking, self).afterSetUp()
00039         self.set_text(self.page1, '((%s))' % TITLE2)
00040 
00041     def test_backlink(self):
00042         assert self.page1 in self.page2.getRefs(relationship=BACKLINK_RELATIONSHIP)
00043 
00044     def testforlink(self):
00045         self.failUnlessWickedLink(self.page1, self.page2)
00046 
00047     def testformultiplelinks(self):
00048         self.set_text(self.page1, '((DMV Computer has died))  ((Make another link))')
00049         self.failUnlessAddLink(self.page1)
00050                         
00051         self.failUnlessWickedLink(self.page1, self.page2)
00052         
00053     def testInexactTitleNotMatch(self):
00054         w1 = makeContent(self.folder, 'w1', self.wicked_type,
00055                          title='W1 Title With Extra')
00056         self.set_text(self.page1, "((W1 Title))")
00057         self.failIfWickedLink(self.page1, w1)
00058         self.failUnlessAddLink(self.page1)
00059 
00060     def testInexactTitleNotBlockLocalId(self):
00061         w1 = makeContent(self.folder, 'w1', self.wicked_type,
00062                          title='W1 Title')
00063         w2 = makeContent(self.folder, 'w2', self.wicked_type,
00064                          title='%s With Extra' % w1.id)
00065         self.set_text(self.page1, "((%s))" % w1.id)
00066         self.failUnlessWickedLink(self.page1, w1)
00067         self.failIfWickedLink(self.page1, w2)
00068 
00069     def testInexactLocalTitleNotBlockLocalTitle(self):
00070         w1 = makeContent(self.folder, 'w1', self.wicked_type,
00071                          title='W1 Title')
00072         w2 = makeContent(self.folder, 'w2', self.wicked_type,
00073                          title='%s With Extra' % w1.Title())
00074         self.set_text(self.page1, "((%s))" % w1.Title())
00075         self.failUnlessWickedLink(self.page1, w1)
00076         self.failIfWickedLink(self.page1, w2)
00077 
00078     def testInexactRemoteTitleNotBlockRemoteId(self):
00079         f2 = makeContent(self.folder, 'f2', 'Folder')
00080         w1 = makeContent(self.folder, 'w1', self.wicked_type,
00081                          title='W1 Title')
00082         w2 = makeContent(self.folder, 'w2', self.wicked_type,
00083                          title='%s With Extra' % w1.id)
00084         w3 = makeContent(f2, 'w3', self.wicked_type,
00085                          title='W3 Title')
00086         self.set_text(w3, "((%s))" % w1.id)
00087         self.failUnlessWickedLink(w3, w1)
00088         self.failIfWickedLink(w3, w2)
00089 
00090     def testInexactRemoteTitleNotBlockRemoteTitle(self):
00091         f2 = makeContent(self.folder, 'f2', 'Folder')
00092         w1 = makeContent(self.folder, 'w1', self.wicked_type,
00093                          title='W1 Title')
00094         w2 = makeContent(self.folder, 'w2', self.wicked_type,
00095                          title='%s With Extra' % w1.Title())
00096         w3 = makeContent(f2, 'w3', self.wicked_type,
00097                          title='W3 Title')
00098         self.set_text(w3, "((%s))" % w1.Title())
00099         self.failUnlessWickedLink(w3, w1)
00100         self.failIfWickedLink(w3, w2)
00101 
00102     def testDupLocalTitleMatchesOldest(self):
00103         self.replaceCreatedIndex()
00104         title = 'Duplicate Title'
00105         w1 = makeContent(self.folder, 'w1', self.wicked_type,
00106                          title=title)
00107         w2 = makeContent(self.folder, 'w2', self.wicked_type,
00108                          title=title)
00109         w3 = makeContent(self.folder, 'w3', self.wicked_type,
00110                          title=title)
00111         self.set_text(self.page1, "((%s))" % title)
00112         self.failUnlessWickedLink(self.page1, w1)
00113 
00114         # this also tests that deleting uncaches
00115         self.folder.manage_delObjects(ids=[w1.getId()])
00116 
00117         self.failUnlessWickedLink(self.page1, w2)
00118 
00119     def testDupRemoteIdMatchesOldest(self):
00120         self.replaceCreatedIndex()
00121         id = 'duplicate_id'
00122         f2 = makeContent(self.folder, 'f2', 'Folder')
00123         f3 = makeContent(self.folder, 'f3', 'Folder')
00124         f4 = makeContent(self.folder, 'f4', 'Folder')
00125         w1 = makeContent(f2, id, self.wicked_type,
00126                          title='W1 Title')
00127         # mix up the order, just to make sure
00128         w3 = makeContent(f4, id, self.wicked_type,
00129                          title='W3 Title')
00130         w2 = makeContent(f3, id, self.wicked_type,
00131                          title='W2 Title')
00132         self.set_text(self.page1, "((%s))" % id)
00133         self.failIfWickedLink(self.page1, w2)
00134         self.failIfWickedLink(self.page1, w3)
00135         self.failUnlessWickedLink(self.page1, w1)
00136 
00137         f2.manage_delObjects(ids=[w1.id])
00138         self.failIfWickedLink(self.page1, w2)
00139 
00140         # fails due to caching
00141         self.failUnlessWickedLink(self.page1, w3)
00142 
00143     def makeFolders(self, *args):
00144         folders = list()
00145         for id in args:
00146             folders.append(makeContent(self.folder, id, 'Folder'))
00147         return tuple(folders)
00148             
00149     def testDupRemoteTitleMatchesOldest(self):
00150         self.replaceCreatedIndex()
00151         title = 'Duplicate Title'
00152         
00153         f2, f3, f4 = self.makeFolders('f2', 'f3', 'f4')
00154 
00155         w1 = makeContent(f2, 'w1', self.wicked_type,
00156                          title=title)
00157         # mix up the order, just to make sure
00158         w3 = makeContent(f4, 'w3', self.wicked_type,
00159                          title=title)
00160         w2 = makeContent(f3, 'w2', self.wicked_type,
00161                          title=title)
00162         self.set_text(self.page1, "((%s))" % title)
00163         self.failUnlessWickedLink(self.page1, w1)
00164         self.failIfWickedLink(self.page1, w2)
00165         self.failIfWickedLink(self.page1, w3)
00166 
00167         f2.manage_delObjects(ids=[w1.id])
00168         self.failUnlessWickedLink(self.page1, w3)
00169         self.failIfWickedLink(self.page1, w2)
00170 
00171     def testLinkPersistsThroughMove(self):
00172         title = 'Move Me'
00173         f2, f3 = self.makeFolders('f2', 'f3')
00174         w1 = makeContent(f2, 'w1', self.wicked_type,
00175                          title=title)
00176         
00177         # check implicit resolution
00178         # this is a pre-test
00179         # should set cache
00180         self.set_text(self.page1, "((%s))" % title)
00181         self.failUnlessWickedLink(self.page1, w1)
00182 
00183         # move w1
00184         self.moveContent(w1, f3)
00185 
00186         w1.setTitle('new title to make sure we do not accidentally resolve')
00187         
00188         # check link did not change
00189         self.failUnlessWickedLink(self.page1, w1)
00190 
00191 class TestDocCreation(Base):
00192     
00193     def afterSetUp(self):
00194         WickedTestCase.afterSetUp(self)
00195         self.title = 'Create a New Document'
00196         self.demoCreate()
00197         self.set_text(self.page1, '((%s))' %self.title)
00198         
00199     def testDocAdded(self):
00200         self.failUnless(getattr(self.folder,
00201                                 titleToNormalizedId(self.title), None))
00202 
00203     def testBacklinks(self):
00204         newdoc = getattr(self.folder, titleToNormalizedId(self.title))
00205         backlinks = newdoc.getRefs(relationship=BACKLINK_RELATIONSHIP)
00206         self.failUnless(self.page1 in backlinks)
00207         
00208 class TestLinkNormalization(Base):
00209     title = 'the monkey flies at dawn'
00210 
00211     def afterSetUp(self):
00212         super(TestLinkNormalization, self).afterSetUp()
00213         title1 = self.title 
00214         self.login('test_user_1_')
00215         self.newpage = self.clickCreate(self.page1, self.title)
00216 
00217     def clickCreate(self, page, title):
00218         """
00219         simulates browser interaction
00220         """
00221         addview = page.restrictedTraverse('@@wickedadd')
00222         addview.add_content(title=title, section=self.wicked_field, type_name=self.wicked_type)
00223         self.set_text(page, "((%s))" %title ) #wha?
00224         return getattr(self.folder, titleToNormalizedId(title))
00225 
00226 # @@ demonstrates issue with ids that are not tightly coupled to Title
00227 # as of 1.0.1 still fails
00228 ##     def test_oldTitleWinsNewId(self):
00229 ##         # this will should fail
00230 ##         # if title changes don't trigger
00231 ##         # id changes
00232 ##         self.replaceCreatedIndex()
00233 ##         newtitle = 'I changed my mind'
00234 ##         self.page2.update(**dict(title=self.title))
00235 ##         self.newpage.update(**dict(title=newtitle))
00236 
00237 ##         # page one should still link to new page
00238 ##         # even though page2 has same title as link
00239 ##         self.failUnlessWickedLink(self.page1, self.newpage)
00240 
00241 ##         # delete newpage and recreate
00242 ##         # older title should beat newer id
00243 ##         self.loginAsPortalOwner()
00244 ##         self.folder.manage_delObjects([self.newpage.getId()])
00245 ##         self.newpage = self.clickCreate(self.page2, self.title)
00246 
00247 ##         self.failIfWickedLink(self.page1, self.newpage)
00248 ##         self.failUnlessWickedLink(self.page1, self.page2)
00249 
00250         
00251     def test_create_titlechange(self):
00252         # add content from link
00253         # test link
00254         # change title
00255         # test link
00256         title1 = self.title 
00257 
00258         # if this fails, wicked is not working period
00259         self.failUnlessWickedLink(self.page1, self.newpage)
00260 
00261         self.newpage.update(**dict(title='I changed my mind'))
00262         self.failUnlessWickedLink(self.page1, self.newpage)
00263 
00264 class TestRemoteLinking(Base):
00265     
00266     def afterSetUp(self):
00267         super(TestRemoteLinking, self).afterSetUp()
00268         self.set_text(self.page1, '((%s))' % TITLE2)
00269 
00270     def testLocalIdBeatsRemoteId(self):
00271         self.replaceCreatedIndex()
00272         f2 = makeContent(self.folder, 'f2', 'Folder')
00273         w1 = makeContent(f2, self.page1.getId(), self.wicked_type,
00274                          title='W1 Title')
00275         w2 = makeContent(f2, 'w2_id', self.wicked_type,
00276                          title='W2 Title')
00277         self.set_text(w2, "((%s))" % self.page1.getId())
00278         self.failIfWickedLink(w2, self.page1)
00279         self.failUnlessWickedLink(w2, w1)
00280 
00281     def testLocalTitleBeatsRemoteId(self):
00282         self.replaceCreatedIndex()
00283         f2 = makeContent(self.folder, 'f2', 'Folder')
00284         w1 = makeContent(f2, 'w1_id', self.wicked_type,
00285                          title=self.page1.id)
00286         w2 = makeContent(f2, 'w2_id', self.wicked_type,
00287                          title='W2 Title')
00288         self.set_text(w2, "((%s))" % self.page1.id)
00289         self.failUnlessWickedLink(w2, w1)
00290         self.failIfWickedLink(w2, self.page1)
00291 
00292     def testInexactLocalTitleNotBlockRemoteTitle(self):
00293         f2 = makeContent(self.folder, 'f2', 'Folder')
00294         w1 = makeContent(self.folder, 'w1', self.wicked_type,
00295                          title='W1 Title')
00296         w2 = makeContent(f2, 'w2', self.wicked_type,
00297                          title='%s With Extra' % w1.Title())
00298         w3 = makeContent(f2, 'w3', self.wicked_type,
00299                          title='W3 Title')
00300         self.set_text(w3, "((%s))" % w1.Title())
00301         self.failUnlessWickedLink(w3, w1)
00302         self.failIfWickedLink(w3, w2)
00303 
00304     def testInexactLocalTitleNotBlockRemoteId(self):
00305         f2 = makeContent(self.folder, 'f2', 'Folder')
00306         w1 = makeContent(self.folder, 'w1', self.wicked_type,
00307                          title='W1 Title')
00308         w2 = makeContent(f2, 'w2', self.wicked_type,
00309                          title='%s With Extra' % w1.id)
00310         w3 = makeContent(f2, 'w3', self.wicked_type,
00311                          title='W3 Title')
00312         self.set_text(w3, "((%s))" % w1.id)
00313         self.failUnlessWickedLink(w3, w1)
00314         self.failIfWickedLink(w3, w2)
00315         
00316 
00317 def test_suite():
00318     from wicked.at.link import test_suite as btests
00319     suites = [unittest.makeSuite(tc) for tc in TestDocCreation, TestWikiLinking, TestLinkNormalization, TestRemoteLinking]
00320     suites.append(btests())
00321     suite = unittest.TestSuite(suites)
00322     return suite
00323