Integration test for the lockingbehavior
========================================

Imports:

    >>> from plone.testing.z2 import Browser
    >>> from plone.app.testing import setRoles
    >>> from plone.dexterity.fti import DexterityFTI
    >>> from plone.dexterity.utils import createContentInContainer
    >>> import transaction

Globals:

    >>> app = layer['app']
    >>> portal = layer['portal']


For testing the locking, we need two users. Let's create "foo" and
"bar":

    >>> acl_users = portal.acl_users
    >>> acl_users.userFolderAddUser('foo', 'foo', ['Member'], [])
    >>> setRoles(portal, 'foo', ['Member', 'Manager', 'Contributor', 'Editor', 'Reader'])
    >>> acl_users.userFolderAddUser('bar', 'bar', ['Member'], [])
    >>> setRoles(portal, 'bar', ['Member', 'Manager', 'Contributor', 'Editor', 'Reader'])
    >>> transaction.commit()


Log in both users with an own browser each:

    >>> foo_browser = Browser(app)
    >>> foo_browser.handleErrors = False
    >>> foo_browser.open(portal.absolute_url() + '/login_form')
    >>> foo_browser.getControl(name='__ac_name').value = 'foo'
    >>> foo_browser.getControl(name='__ac_password').value = 'foo'
    >>> foo_browser.getControl(name='submit').click()
    >>> 'You are now logged in' in foo_browser.contents
    True


    >>> bar_browser = Browser(app)
    >>> bar_browser.handleErrors = False
    >>> bar_browser.open(portal.absolute_url() + '/login_form')
    >>> bar_browser.getControl(name='__ac_name').value = 'bar'
    >>> bar_browser.getControl(name='__ac_password').value = 'bar'
    >>> bar_browser.getControl(name='submit').click()
    >>> 'You are now logged in' in bar_browser.contents
    True


Let's create a FTI for testing with:

    >>> fti = DexterityFTI('LockableType',
    ...                    factory='LockableType')
    >>> fti.behaviors = (
    ...         'plone.app.lockingbehavior.behaviors.ILocking',
    ... )
    >>> fti.global_allow = True
    >>> portal.portal_types._setObject('LockableType', fti)
    'LockableType'
    >>> schema = fti.lookupSchema()

Create an object:

    >>> obj = createContentInContainer(portal, 'LockableType',
    ...                                title='lockable object',
    ...                                checkConstraints=False)
    >>> obj
    <Item at /plone/lockabletype>
    >>> transaction.commit()


Now let foo edit the object:

    >>> foo_browser.open(obj.absolute_url())
    >>> foo_browser.url
    'http://nohost/plone/lockabletype'
    >>> foo_browser.open('http://nohost/plone/lockabletype/edit')
    >>> foo_browser.getControl('Save')
    <SubmitControl name='form.buttons.save' type='submit'>


It should be locked for bar at the moment:

    >>> bar_browser.open('http://nohost/plone/lockabletype')
    >>> 'This item was locked' in bar_browser.contents
    True

When bar tries to access /edit directly, he will be redirected back:

    >>> bar_browser.open('http://nohost/plone/lockabletype/edit')
    Traceback (most recent call last):
    ...
    Redirect: http://nohost/plone/lockabletype

New let foo save the object:

    >>> foo_browser.getControl('Save').click()
    >>> foo_browser.url
    'http://nohost/plone/lockabletype'


It should not be locked any more for bar:

    >>> bar_browser.open('http://nohost/plone/lockabletype')
    >>> 'This item was locked' in bar_browser.contents
    False

Now let bar edit the object:

    >>> bar_browser.open('http://nohost/plone/lockabletype/edit')
    >>> bar_browser.getControl('Save')
    <SubmitControl name='form.buttons.save' type='submit'>

Now its locked for foo:

    >>> foo_browser.open('http://nohost/plone/lockabletype')
    >>> 'This item was locked' in foo_browser.contents
    True

Let bar cancel the edit:

    >>> bar_browser.getControl('Cancel').click()
    >>> bar_browser.url
    'http://nohost/plone/lockabletype'
    >>> 'This item was locked' in bar_browser.contents
    False

It should not be locked for foo now:

    >>> foo_browser.open('http://nohost/plone/lockabletype')
    >>> 'This item was locked' in foo_browser.contents
    False

If we edit a content without chanig anything, the lock should also be
released:

    >>> bar_browser.open('http://nohost/plone/lockabletype/edit')
    >>> foo_browser.open('http://nohost/plone/lockabletype')
    >>> 'This item was locked' in foo_browser.contents
    True
    >>> bar_browser.getControl('Save').click()
    >>> foo_browser.open('http://nohost/plone/lockabletype')
    >>> 'This item was locked' in foo_browser.contents
    False
