Facetted searches
-----------------

This tests the integration of Solr's facetted searches into Plone.  We'll
use a testbrowser to check the correct display of facets.  First we need to
activate Solr support and reindex the site's content:

  >>> from collective.solr.testing import activateAndReindex
  >>> from plone.app.testing import setRoles
  >>> from plone.app.testing import TEST_USER_ID
  >>> from plone.app.testing import TEST_USER_NAME
  >>> from plone.app.testing import TEST_USER_PASSWORD
  >>> from plone.testing.z2 import Browser
  >>> from transaction import commit
  >>> portal = layer['portal']
  >>> setRoles(portal, TEST_USER_ID, ['Manager'])
  >>> portal.invokeFactory('Folder', id='folder')
  'folder'
  >>> folder = portal.folder
  >>> commit()
  >>> browser = Browser(layer['app'])
  >>> browser.open('http://nohost/plone/login_form')
  >>> browser.getControl(name='__ac_name').value = TEST_USER_NAME
  >>> browser.getControl(name='__ac_password').value = TEST_USER_PASSWORD
  >>> browser.getControl(name='submit').click()

  >>> folder.setTitle('My folder')
  >>> activateAndReindex(portal)
  >>> commit()

The search form should contain default settings for facets to be shown:

  >>> browser.open('http://nohost/plone/')
  >>> browser.contents
  '...<input type="hidden" name="facet.field"...value="portal_type" />...'

Without search results there's also no information about facets, of course:

  >>> browser.getControl('Search Site').value = 'foo'
  >>> browser.getForm(name='searchform').submit()
  >>> browser.url
  'http://nohost/plone/search?...&facet.field=portal_type...'
  >>> browser.contents
  '...Search results...
   ...No results were found...'
  >>> 'searchfacets' in browser.contents
  False

Let's try again with some results:

  >>> browser.goBack()
  >>> browser.getControl('Search Site').value = 'news'
  >>> browser.getForm(name='searchform').submit()
  >>> browser.url
  'http://nohost/plone/search?...&facet.field=portal_type...'
  >>> browser.contents
  '...Search results...2 items matching...
   ...portal-searchfacets...
   ...Content type...
   ...Folder...1...
   ...Collection...1...
   ...NewsFolder...'

In fact, information for multiple facets should be displayed:

  >>> browser.goBack()
  >>> browser.getControl('Search Site').value = 'news'
  >>> browser.getForm(name='searchform').submit()
  >>> browser.url
  'http://nohost/plone/search?...&facet.field=portal_type...&facet.field=review_state...'
  >>> browser.contents
  '...Search results...2 items matching...
   ...portal-searchfacets...
   ...Content type...
   ...Folder...1...
   ...Collection...1...
   ...Review state...
   ...published...2...
   ...NewsFolder...'

Clicking on of the facets should restrict the search:

  >>> browser.getLink('Collection').click()
  >>> browser.url
  'http://nohost/plone/search?...&fq=portal_type%3A%22Collection%22...'
  >>> 'facet.field=portal_type' in browser.url
  False
  >>> browser.contents
  '...Search results...1 items matching...
   ...portal-searchfacets...
   ...Content type...Collection...&otimes;...
   ...Review state...
   ...published...1...
   ...News...'

There should be a link to remove the selected facet, but no other details:

  >>> browser.getLink('⊗').url
  'http://nohost/plone/search?...&facet.field=portal_type...'
  >>> '>Folder</a>' in browser.contents
  False

But the other facet should still be browsable:

  >>> browser.getLink('published').click()
  >>> browser.url
  'http://nohost/plone/search?...fq=portal_type%3A%22Collection%22...fq=review_state%3A%22published%22...'
  >>> 'facet.field=' in browser.url
  False
  >>> browser.contents
  '...Search results...1 items matching...
   ...portal-searchfacets...
   ...Content type...Collection...&otimes;...
   ...Review state...published...&otimes;...
   ...News...'

Removing a previously selected facet should extend the search again:

  >>> browser.getLink('⊗').click()
  >>> browser.url
  'http://nohost/plone/search?...&facet.field=portal_type...'
  >>> 'fq=portal_type' in browser.url
  False
  >>> 'fq=review_state' in browser.url
  True
  >>> browser.contents
  '...Search results...2 items matching...
   ...portal-searchfacets...
   ...Review state...published...&otimes;...
   ...Content type...
   ...Folder...1...
   ...Collection...1...
   ...News...'

Using the search boxes, both the standard one as well as the other, which is
embedded into the results page, should provide facet information and preserve
the already selected facets:

  >>> browser.getForm(name='searchform', index=0).submit()
  >>> browser.contents
  '...Search results...2 items matching...
   ...portal-searchfacets...
   ...Review state...published...&otimes;...
   ...Content type...
   ...Folder...1...
   ...Collection...1...
   ...NewsFolder...'

  >>> browser.getForm(name='searchform', index=1).submit()
  >>> browser.contents
  '...Search results...2 items matching...
   ...portal-searchfacets...
   ...Review state...published...&otimes;...
   ...Content type...
   ...Folder...1...
   ...Collection...1...
   ...NewsFolder...'

The already selected facets should of course be displayed in case there were
no search results, so the filters can be removed by the user.  So while at
first no results will be shown, they should appear after removing the facet
selection for "review_state":

  >>> form = browser.getForm(name='searchform', index=0)
  >>> form.getControl('Search Site').value = 'my'
  >>> form.submit()
  >>> browser.contents
  '...No results were found...'
  >>> browser.getLink('⊗').click()
  >>> browser.contents
  '...Search results...1 items matching...
   ...My folder...'
