Permalink
Browse files

fix for multiple ids and currently broken tests case

  • Loading branch information...
1 parent 3937c75 commit eb41287d70e51f31a7e472b786eecccab95ea850 @eleddy eleddy committed Mar 1, 2012
View
@@ -9,6 +9,9 @@ Changelog
ids not caught by the old check. This fixes
http://code.google.com/p/dexterity/issues/detail?id=244
[davisagli]
+- Attempt to return an id with timestamp before returning a
+ value error after 100 id check attempts
+ [eleddy]
2.0.7 (2011-07-04)
@@ -9,7 +9,7 @@
from zExceptions import BadRequest
from plone.app.content.interfaces import INameFromTitle
-
+import time
ATTEMPTS = 100
@@ -77,6 +77,11 @@ def _findUniqueName(self, name, object):
return new_name
idx += 1
+ # give one last attempt using the current date time before giving up
+ new_name = "%s-%s%s" % (name, time.time(), ext)
+ if not check_id(new_name, required=1):
+ return new_name
+
raise ValueError("Cannot find a unique name based on %s after %d attemps." % (name, ATTEMPTS,))
def _getCheckId(self, object):
@@ -0,0 +1,72 @@
+from Products.CMFCore.utils import getToolByName
+
+from plone.app.testing import PloneSandboxLayer
+from plone.app.testing import applyProfile
+from plone.app.testing import PLONE_FIXTURE
+from plone.app.testing import IntegrationTesting
+from plone.app.testing import FunctionalTesting
+
+from zope.configuration import xmlconfig
+
+
+class PloneAppDiscussion(PloneSandboxLayer):
+
+ defaultBases = (PLONE_FIXTURE,)
+
+ USER_NAME = 'johndoe'
+ USER_PASSWORD = 'secret'
+ MEMBER_NAME = 'janedoe'
+ MEMBER_PASSWORD = 'secret'
+ USER_WITH_FULLNAME_NAME = 'jim'
+ USER_WITH_FULLNAME_FULLNAME = 'Jim Fulton'
+ USER_WITH_FULLNAME_PASSWORD = 'secret'
+ MANAGER_USER_NAME = 'manager'
+ MANAGER_USER_PASSWORD = 'secret'
+
+ def setUpZope(self, app, configurationContext):
+ # Load ZCML
+ import plone.app.content
+ xmlconfig.file('configure.zcml',
+ plone.app.content,
+ context=configurationContext)
+
+ def setUpPloneSite(self, portal):
+ # Creates some users
+ acl_users = getToolByName(portal, 'acl_users')
+ acl_users.userFolderAddUser(
+ self.USER_NAME,
+ self.USER_PASSWORD,
+ [],
+ [],
+ )
+ acl_users.userFolderAddUser(
+ self.MEMBER_NAME,
+ self.MEMBER_PASSWORD,
+ ['Member'],
+ [],
+ )
+ acl_users.userFolderAddUser(
+ self.USER_WITH_FULLNAME_NAME,
+ self.USER_WITH_FULLNAME_PASSWORD,
+ ['Member'],
+ [],
+ )
+ mtool = getToolByName(portal, 'portal_membership', None)
+ mtool.addMember('jim', 'Jim', ['Member'], [])
+ mtool.getMemberById('jim').setMemberProperties(
+ {"fullname": 'Jim Fult\xc3\xb8rn'})
+
+ acl_users.userFolderAddUser(
+ self.MANAGER_USER_NAME,
+ self.MANAGER_USER_PASSWORD,
+ ['Manager'],
+ [],
+ )
+
+PLONE_APP_CONTENT_FIXTURE = PloneAppDiscussion()
+PLONE_APP_CONTENT_INTEGRATION_TESTING = IntegrationTesting(
+ bases=(PLONE_APP_CONTENT_FIXTURE,),
+ name="PloneAppDiscussion:Integration")
+PLONE_APP_CONTENT_FUNCTIONAL_TESTING = FunctionalTesting(
+ bases=(PLONE_APP_CONTENT_FIXTURE,),
+ name="PloneAppDiscussion:Functional")
@@ -0,0 +1,42 @@
+import unittest2 as unittest
+from plone.app.content.testing import PLONE_APP_CONTENT_INTEGRATION_TESTING
+from plone.app.testing import TEST_USER_ID, TEST_USER_NAME
+from plone.app.testing import setRoles, login
+import transaction
+from plone.app.content.namechooser import ATTEMPTS
+from zope.container.interfaces import INameChooser
+from Products.CMFPlone.utils import _createObjectByType
+
+class NameChooserTest(unittest.TestCase):
+ layer = PLONE_APP_CONTENT_INTEGRATION_TESTING
+
+ def setUp(self):
+ self.portal = self.layer['portal']
+ setRoles(self.portal, TEST_USER_ID, ['Manager'])
+ login(self.portal, TEST_USER_NAME)
+
+ def test_100_or_more_unique_ids(self):
+ # add the same item 110 times. the first 100 items should be numbered.
+ # after that it should use datetime to generate the id
+ chooser = INameChooser(self.portal)
+
+ self.portal.invokeFactory("Folder", 'holder')
+ transaction.commit()
+ holder = self.portal.get('holder')
+
+ title="A Small Document"
+ # create the first object, which will have no suffix
+ id = chooser.chooseName(title, holder)
+ holder.invokeFactory("Document",id)
+ transaction.commit()
+
+ for i in range(1, ATTEMPTS + 1):
+ id = chooser.chooseName(title, holder)
+ if i < ATTEMPTS: # first addition has no suffix
+ self.assertEqual("a-small-document-%s"%i, id)
+
+ holder.invokeFactory("Document", id)
+ transaction.savepoint(optimistic=True)
+ item = self.portal.get(id)
+ assert(item)
+

0 comments on commit eb41287

Please sign in to comment.