Adding, Editing, Moving and Deleting a Document
===============================================

Does what the title says, really.  Plus some assertions.

    >>> from plone.testing.zope import Browser
    >>> from plone.app.testing import SITE_OWNER_NAME
    >>> from plone.app.testing import SITE_OWNER_PASSWORD
    >>> app = layer['app']
    >>> portal = layer['portal']
    >>> browser = Browser(app)
    >>> browser.open('http://nohost/plone/login_form')

First, we need to log in.

    >>> browser.getControl('Login Name').value = SITE_OWNER_NAME
    >>> browser.getControl('Password').value = SITE_OWNER_PASSWORD
    >>> browser.getControl('Log in').click()

We need to allow multiple languages to see the result of our changes:

    >>> from plone.registry.interfaces import IRegistry
    >>> from zope.component import getUtility
    >>> from plone.i18n.interfaces import ILanguageSchema
    >>> settings = getUtility(IRegistry).forInterface(
    ...     ILanguageSchema,
    ...     prefix='plone')
    >>> settings.available_languages = ['de', 'en']
    >>> import transaction; transaction.commit()

Add Document
------------

What we do next is we

  - go to our home folder,
  - add a new page,
  - edit some values,
  - save the page,
  - and along the way we check that everything works as expected.

Note that this is slightly different to how the testrecorder result
looks like because we're navigating the page with a JavaScript- less
browser.

    >>> browser.getLink('Page').click()
    >>> #'Edit Page' in browser.contents # ouch, Plone has <span> around 'Page'
    >>> browser.url
    'http://nohost/plone/++add++Document'

Now that the document has been added, we can edit it, but we pretend
we don't know better and forget to type in a Title:

    >>> browser.getControl(name='form.widgets.IRichTextBehavior.text').value = 'About me'
    >>> browser.getControl('Save').click()
    >>> 'Required input is missing.' in browser.contents
    True

Oops, let's fill that in quickly:

    >>> browser.getControl('Title').value = 'My Page'
    >>> browser.getControl('Save').click()
    >>> 'Item created' in browser.contents
    True
    >>> 'My Page' in browser.contents
    True

We edit the document a second time:

    >>> browser.getLink('Edit').click()
    >>> browser.getControl('Title').value = 'Ons Bier'
    >>> browser.getControl('Save').click()
    >>> 'Ons Bier' in browser.contents
    True

We go now to the document absolute url:

    >>> browser.open('http://nohost/plone/my-page')

Editing the document and clicking the Cancel button doesn't modify it:

    >>> browser.getLink('Edit').click()
    >>> browser.getControl('Title').value = 'Min sida'
    >>> browser.getControl('Cancel').click()
    >>> 'Min sida' not in browser.contents
    True

We are now at the document absolute url:

    >>> browser.url
    'http://nohost/plone/my-page'

If we click the Edit link twice and then Cancel, we end at the
canonical view of the document:

    >>> browser.getLink('Edit').click()
    >>> browser.getLink('Edit').click()
    >>> browser.getControl('Cancel').click()
    >>> browser.url
    'http://nohost/plone/my-page'


Editing Properties
------------------

We edit the document properties:

    >>> browser.getLink('Edit').click()
    >>> browser.url
    'http://nohost/plone/my-page/edit...'

We make sure the language is English at the beginning:

    >>> browser.getControl(name='form.widgets.IDublinCore.language').displayValue
    ['English']

And the content language is set to the default:

    >>> browser.headers.get('content-language')
    'en'

Now set the language to German:

    >>> browser.getControl(name='form.widgets.IDublinCore.language').value = ['de']
    >>> browser.getControl(name='form.widgets.IDublinCore.language').displayValue
    ['Deutsch']

    >>> browser.getControl('Save').click()
    >>> 'Changes saved' in browser.contents
    True

Make sure the content language was updated:

    >>> browser.headers.get('content-language')
    'de'

Cut and paste Document
----------------------

We'll first cut our Document. Then we'll create folder where we paste
the Document into. First off, we'll cut our document:

    >>> browser.getLink('Cut').click()

Before doing the actual paste, we create a folder to paste into.

    >>> browser.open('http://nohost/plone')
    >>> browser.getLink('Folder').click()
    >>> browser.getControl('Title').value = 'My Folder'
    >>> browser.getControl('Save').click()
    >>> 'Item created' in browser.contents
    True

The Folder has been created, now we can paste the Document in our copy
buffer here:

    >>> browser.getLink('View').click()
    >>> browser.getLink('Paste').click()
    >>> browser.url
    'http://nohost/plone/my-folder...'
    >>> 'Item(s) pasted.' in browser.contents
    True
    >>> browser.getLink('View').click()
    >>> 'Ons Bier' in browser.contents
    True

Delete Document
---------------

Delete the document.

    >>> browser.open('http://nohost/plone/my-folder/my-page')
    >>> browser.getLink('Edit').click()
    >>> browser.getControl('Cancel').click()
    >>> browser.getLink('Delete').click()
    >>> browser.getControl('Delete').click()
    >>> 'Ons Bier has been deleted.' in browser.contents
    True

Paste deleted document
----------------------

When the document has been moved or deleted by someone else in the time
between cut and paste, the page should say so and not give a stacktrace.
(https://dev.plone.org/ticket/13577)

    >>> browser.open('http://nohost/plone')
    >>> browser.getLink('Page').click()
    >>> browser.getControl(name='form.widgets.IRichTextBehavior.text').value = ''
    >>> browser.getControl('Title').value = 'Op een dag'
    >>> browser.getControl('Save').click()
    >>> 'Op een dag' in browser.contents
    True

We go now to the document absolute url:

    >>> browser.open('http://nohost/plone/op-een-dag')

We'll first cut our Document.

    >>> browser.getLink('Cut').click()

Login another user:

    >>> browser2 = Browser(app)
    >>> browser2.open('http://nohost/plone/login_form')
    >>> browser2.getControl('Login Name').value = SITE_OWNER_NAME
    >>> browser2.getControl('Password').value = SITE_OWNER_PASSWORD
    >>> browser2.getControl('Log in').click()

Other user removes document:
(It's good enough to log in as the same user on a different Browser,
the main thing is that Plone's session is unable to remove the cut content
from its clipboard.)

    >>> browser2.open('http://nohost/plone/op-een-dag')
    >>> browser2.getLink('Delete').click()
    >>> browser2.getControl('Delete').click()
    >>> 'Op een dag has been deleted.' in browser2.contents
    True

This should fail graciously, that is:
- Not give a server error
- Not give a traceback
- Show a message that says what's wrong

    >>> browser.open('http://nohost/plone/my-folder')
    >>> browser.getLink('Paste').click()
    >>> browser.url
    'http://nohost/plone/my-folder...'
    >>> 'Traceback' not in browser.contents
    True
    >>> 'may have been moved or deleted after you copied' in browser.contents
    True
