Handle criteria
===============

Imports
-------

    >>> from zope.component import getMultiAdapter
    >>> from plone.app.testing import setRoles
    >>> from plone.app.testing import TEST_USER_ID
    >>> from eea.facetednavigation.interfaces import ICriteria

Set up
------

Setup sandbox

    >>> portal = layer["portal"]
    >>> request = layer['request']
    >>> sandbox = portal['sandbox']
    >>> subtyper = getMultiAdapter((sandbox, request), name=u'faceted_subtyper')
    >>> subtyper.can_enable()
    True

    >>> subtyper.enable()

Cleanup default widgets
-----------------------

    >>> cids = ICriteria(sandbox).keys()
    >>> for cid in cids:
    ...     ICriteria(sandbox).delete(cid)
    >>> ICriteria(sandbox).keys()
    []

Add criteria
------------

    >>> _ = ICriteria(sandbox).add('alphabetic', 'top')
    >>> _ = ICriteria(sandbox).add('checkbox', 'top')
    >>> _ = ICriteria(sandbox).add('criteria', 'left')
    >>> _ = ICriteria(sandbox).add('daterange', 'left')
    >>> _ = ICriteria(sandbox).add('radio', 'center')
    >>> _ = ICriteria(sandbox).add('select', 'center')
    >>> _ = ICriteria(sandbox).add('sorting', 'right')
    >>> _ = ICriteria(sandbox).add('text', 'right')

Get criteria
------------

    >>> ICriteria(sandbox).keys()
    ['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7']

    >>> ICriteria(sandbox).values()
    [<eea.facetednavigation.widgets.storage.Criterion object at...>, ...]

    >>> ICriteria(sandbox).items()
    [('c0', <eea.facetednavigation.widgets.storage.Criterion object at...>), ...]

    >>> ICriteria(sandbox).get('c0')
    <eea.facetednavigation.widgets.storage.Criterion object at...>

Edit criterion
--------------

  Alphabetic criterion

    >>> ICriteria(sandbox).edit('c0', title='Alphabetic', index='Title', hidden=False)
    >>> criterion = ICriteria(sandbox).get('c0')
    >>> criterion.get('title')
    'Alphabetic'
    >>> criterion.hidden
    False
    >>> criterion.get('index')
    'Title'

  Checkbox criterion

    >>> ICriteria(sandbox).edit('c1',
    ...    title='Check boxes',
    ...    index='portal_type',
    ...    vocabulary='eea.faceted.vocabularies.FacetedPortalTypes',
    ...    catalog='portal_catalog',
    ...    maxitems=4, hidden=True
    ... )
    >>> criterion = ICriteria(sandbox).get('c1')
    >>> criterion.get('title')
    'Check boxes'
    >>> criterion.hidden
    True
    >>> criterion.get('maxitems')
    4

Delete criteria
---------------

    >>> ICriteria(sandbox).delete('c0')
    >>> ICriteria(sandbox).delete('c1')
    >>> ICriteria(sandbox).delete('c2')
    >>> ICriteria(sandbox).keys()
    ['c3', 'c4', 'c5', 'c6', 'c7']

Move up/down
------------

    >>> ICriteria(sandbox).down('c4')
    >>> ICriteria(sandbox).up('c5')
    >>> ICriteria(sandbox).keys()
    ['c5', 'c3', 'c4', 'c6', 'c7']

Change position
---------------

    >>> ICriteria(sandbox).position(
    ...     top=['c7', 'c6'],
    ...     left=['c5', 'c4'],
    ...     center=['c3'],
    ...     right=[]
    ... )
    >>> ICriteria(sandbox).keys()
    ['c7', 'c6', 'c5', 'c4', 'c3']
    >>> ICriteria(sandbox).get('c4').get('position')
    'left'

Add criteria to specific sections
---------------------------------

    >>> _ = ICriteria(sandbox).add('alphabetic', 'top', section='advanced')
    >>> _ = ICriteria(sandbox).add('alphabetic', 'top')
    >>> ICriteria(sandbox).get('c2').get('section')
    'advanced'
    >>> ICriteria(sandbox).get('c1').get('section')
    'default'
    >>> ICriteria(sandbox).keys()
    ['c7', 'c6', 'c1', 'c2', 'c5', 'c4', 'c3']
