**************************************
Content Rules: Set layout
**************************************

Functional test: Using the control panel form
====================================================

Preparing the test
---------------------

First we will set some variables to be used on this test::

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

    >>> import transaction
    >>> from plone.app.testing import setRoles
    >>> from plone.app.testing import TEST_USER_ID
    >>> setRoles(portal, TEST_USER_ID, ['Manager'])
    >>> folder_id = portal.invokeFactory('Folder', 'news', title='News')
    >>> transaction.commit()

    >>> from plone.testing.z2 import Browser
    >>> from plone.app.testing import SITE_OWNER_NAME
    >>> from plone.app.testing import SITE_OWNER_PASSWORD

    >>> browser = Browser(app)
    >>> browser.handleErrors = False
    >>> portal_url = portal.absolute_url()
    >>> panel_url = portal_url + '/@@overview-controlpanel'

Logging in
---------------------

We will authenticate as manager using the *login_form*::

    >>> browser.open(portal_url + '/login_form')
    >>> browser.getControl(name='__ac_name').value = SITE_OWNER_NAME
    >>> browser.getControl(name='__ac_password').value = SITE_OWNER_PASSWORD
    >>> browser.getControl(name='submit').click()

If everything worked as expected we will see the welcome message::

    >>> 'You are now logged in' in browser.contents
    True

Accessing the control panel
-------------------------------

Now, authenticated as an user with manager role, we will access the Plone
control panel.::

    >>> browser.open(panel_url)

 and look for the Content Rules configlet::

    >>> 'Content Rules' in browser.contents
    True

Clicking it will take us to the Content Rules configuration::

    >>> browser.getLink('Content Rules').click()
    >>> browser.url.endswith('@@rules-controlpanel')
    True

    >>> 'Content rules' in browser.contents
    True


Creating a new content rule
-------------------------------

We will create a new content rule so we can test our add and edit views::

    >>> browser.getControl('Add content rule').click()

Fill the title, description and select 'Object added to this container' as
trigger::

    >>> browser.getControl(name='form.title').value = 'Organize News Items'
    >>> browser.getControl(name='form.description').value = 'Organize published News Items'
    >>> browser.getControl(name='form.event').value = ['Object added to this container']

Click save::

    >>> browser.getControl(name='form.actions.save').click()

And we are back to the Content Rules control panel::

    >>> browser.open('http://nohost/plone/@@rules-controlpanel')

Now we edit our, just created, content rule::

    >>> browser.getLink('Organize News Items').click()
    >>> browser.url.endswith('@@manage-elements')
    True

Adding a new 'Group by date action'::

    >>> browser.getControl('Add action').value = ['sc.contentrules.actions.groupbydate']
    >>> browser.getControl(name='form.button.AddAction').click()

And we should be redirected to our add view:

    >>> 'Add group by date folder action' in browser.contents
    True

We configure or action to use Folder as container and create the structure
using year, month, day ::

    >>> browser.getControl(name='form.base_folder.query.selection').value = ['./',]
    >>> browser.getControl(name='form.base_folder.query.apply').click()

    >>> browser.getControl(name='form.container').value = ['Folder',]
    >>> browser.getControl(name='form.structure').value = '%Y/%m/%d'
    >>> browser.getControl(name='form.actions.save').click()

Then we are back to the management of 'Organize News Items' content rule::

    >>> browser.open('@@manage-elements')

We will edit our action::

    >>> browser.getControl(name='form.button.EditAction').click()
    >>> browser.url.endswith('++action++0/edit')
    True

And change the base folder. First we search for News::

    >>> browser.getControl(name='form.base_folder.query').value = 'News'
    >>> browser.getControl(name='form.base_folder.search').click()

Then we confirm the search result and save::

    >>> browser.getControl(name='form.base_folder', index=1).options[2]
    '/news'
    >>> browser.getControl(name='form.base_folder', index=1).value = ['/news']
    >>> browser.getControl(name='form.base_folder.update').click()
    >>> browser.getControl(name='form.actions.save').click()
