====================
A functional doctest
====================

Set up testbrowser::

    >>> def getAuth():
    ...     try:
    ...         from plone.protect import authenticator
    ...         return authenticator.createToken()
    ...     except:  # no plone.protect auto csrf, so no worries
    ...         return ''

    >>> from plone.app.testing import login
    >>> login(layer['portal'], 'admin')
    >>> app = layer['app']
    >>> from plone.testing.z2 import Browser
    >>> browser = Browser(app)
    >>> browser.handleErrors = False
    >>> browser.addHeader('Authorization', 'Basic admin:secret')
    >>> portal = layer['portal']
    >>> portal_url = 'http://nohost/plone'

    >>> browser.open(portal_url)
    >>> browser.getLink(url=portal_url + "/folder_factories").click()
    >>> browser.getControl(label="Page").selected = True
    >>> browser.getControl(name="form.button.Add").click()
    >>> browser.getControl(name="form.widgets.IDublinCore.title").value = "Test Document"
    >>> browser.getControl("Save").click()
    >>> 'Item created' in browser.contents
    True

Create a collection for testing purposes. Just make it a collection of
everything.


    >>> portal.invokeFactory("Collection", "test-collection")
    'test-collection'
    >>> portal['test-collection'].setQuery([{
    ...      'i': 'portal_type',
    ...      'o': 'plone.app.querystring.operation.string.is',
    ...      'v': 'Document',
    ... }])
    >>> import transaction
    >>> transaction.commit()

Now open the portlet manager for the site.
    >>> browser.open(portal_url + '/@@manage-portlets?_authenticator=' + getAuth())

Use the 'Add portlet' select box to add a collection portlet.

    >>> leftcolumn_form = browser.getForm(action=portal_url, index=1)
    >>> add_portlet_select = leftcolumn_form.getControl(name=":action", index=0)
    >>> val = "/++contextportlets++plone.leftcolumn/+/plone.portlet.collection.Collection"
    >>> add_portlet_select.getControl(value=val).selected = True
    >>> leftcolumn_form.submit()
    >>> import re  # '\s' is white space, '+' is at least one, '*' is zero or more matches.
    >>> re.search('<h1\s+class="documentFirstHeading"\s*>\s*Add Collection Portlet\s*</h1>', browser.contents) is not None
    True

Use your new collection for testing. It should already be available at the root
of the site. Show only two items to make the test sanely terse.

    >>> browser.getControl(name='form.widgets.uid').value = portal['test-collection'].UID()
    >>> browser.getControl('Portlet header').value = "Test Collection"
    >>> browser.getControl('Limit').value = "2"
    >>> browser.getControl('Save').click()
    >>> browser.url == portal_url + "/@@manage-portlets"
    True

Go to the home page and see if the portlet looks OK.

    >>> browser.open(portal_url)
    >>> from lxml import etree
    >>> html = etree.HTML(browser.contents)
    >>> portlet = html.xpath('//*[contains(@class, "portletCollection")]')[0]
    >>> portlet_contents = etree.tostring(portlet)
    >>> def strip_white_space(contents):
    ...     lines = [line.strip() for line in contents.splitlines() if line.strip()]
    ...     return '\n'.join(lines)
    >>> print strip_white_space(portlet_contents)
    <section class="portlet portletCollection portlet-collection-test-collection"><header class="portletHeader">
    <span class="portletTopLeft"...
    <a href="http://nohost/plone/test-collection">
    <span>Test Collection</span>
    </a>
    <span class="portletTopRight"...
    </header>
    <ul class="portletContent">
    <li class="portletItem odd">
    <a href="http://nohost/plone/test-document" class="tile contenttype-document" title="">
    Test Document
    </a>
    </li>
    </ul>
    <footer class="portletFooter">
    <span class="portletBottomLeft"...
    <span>
    <a href="http://nohost/plone/test-collection">
    <span class="hiddenStructure">Test Collection - </span>
    <span>More...
    </a>
    </span>
    <span class="portletBottomRight"...
    </footer>
    </section>

Now test what happens if we toggle the "show more link" option.

    >>> browser.open(portal_url + '/@@manage-portlets')
    >>> browser.getLink(url="http://nohost/plone/++contextportlets++plone.leftcolumn/test-collection/edit?referer=http%3A//nohost/plone/%40%40manage-portlets").click()
    >>> re.search('<h1\s+class="documentFirstHeading"\s*>\s*Edit Collection Portlet\s*</h1>', browser.contents) is not None
    True
    >>> browser.getControl(label="Show more... link").selected = False
    >>> browser.getControl('Save').click()
    >>> print browser.url
    http://nohost/plone/@@manage-portlets

Go back to the home page and see if it looks OK.

    >>> browser.open(portal_url)
    >>> html = etree.HTML(browser.contents)
    >>> portlet = html.xpath('//*[contains(@class, "portletCollection")]')[0]
    >>> portlet_contents = etree.tostring(portlet)
    >>> '<a href="http://nohost/plone/test-collection">' in portlet_contents
    False

Now test what happens if we toggle the "show dates" option.

    >>> browser.open(portal_url + '/@@manage-portlets')
    >>> browser.getLink(url="http://nohost/plone/++contextportlets++plone.leftcolumn/test-collection/edit?referer=http%3A//nohost/plone/%40%40manage-portlets").click()
    >>> re.search('<h1\s+class="documentFirstHeading"\s*>\s*Edit Collection Portlet\s*</h1>', browser.contents) is not None
    True
    >>> browser.getControl("Show dates").selected = True
    >>> browser.getControl("Save").click()
    >>> print browser.url
    http://nohost/plone/@@manage-portlets

Make sure the dates show up::

    >>> browser.open(portal_url)
    >>> '<span class="portletItemDetails">' in browser.contents
    True

    >>> from DateTime import DateTime
    >>> cur_year = DateTime().year()
    >>> re.search('<span class="portletItemDetails">.*{}.*</span>'.format(cur_year), browser.contents) is not None
    True
