behaviour of /search path

add exception catching and message
create tests

refs #223 refs #377
This commit is contained in:
Julien Fastré 2015-01-04 00:13:00 +01:00
parent eea91de0e0
commit a6e118f37d
8 changed files with 223 additions and 7 deletions

View File

@ -24,6 +24,9 @@ namespace Chill\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Chill\MainBundle\Search\UnknowSearchDomainException;
use Chill\MainBundle\Search\UnknowSearchNameException;
use Chill\MainBundle\Search\ParsingException;
/**
*
@ -31,16 +34,52 @@ use Symfony\Component\HttpFoundation\Request;
* @author julien.fastre@champs-libres.coop
* @author marc@champs-libres.coop
*/
class SearchController extends Controller
{
public function searchAction(Request $request)
{
$results = $this->get('chill.main.search_provider')
->getResults($request->query->get('q'));
$pattern = $request->query->get('q', '');
if ($pattern === ''){
return $this->render('ChillMainBundle:Search:error.html.twig',
array(
'message' => $this->get('translator')->trans("Your search is empty. "
. "Please provide search terms.")
));
}
$name = $request->query->get('name', NULL);
try {
if ($name === NULL) {
$results = $this->get('chill.main.search_provider')
->getSearchResults($request->query->get('q'));
} else {
$results = [$this->get('chill.main.search_provider')
->getResultByName($pattern, $name)];
}
} catch (UnknowSearchDomainException $ex) {
return $this->render('ChillMainBundle:Search:error.html.twig',
array(
"message" => $this->get('translator')->trans("The domain %domain% "
. "is unknow. Please check your search.", array('%domain%' => $ex->getDomain()))
));
} catch (UnknowSearchNameException $ex) {
throw $this->createNotFoundException("The name ".$ex->getName()." is not found");
} catch (ParsingException $ex) {
return $this->render('ChillMainBundle:Search:error.html.twig',
array(
"message" => $this->get('translator')->trans('Invalid terms').
": ".$this->get('translator')->trans($ex->getMessage())
));
}
return $this->render('ChillMainBundle:Search:list.html.twig',
array('results' => $results)
);
}
}

View File

@ -30,7 +30,7 @@ chill_main_admin_central:
icons: [gears]
chill_main_search:
pattern: /{_locale}/person/search
pattern: /{_locale}/search
defaults: { _controller: ChillMainBundle:Search:search }
login:

View File

@ -8,4 +8,10 @@ Invalid CSRF token.: Votre session a expirée ou est devenue invalide.
Username: Nom d'utilisateur
Password: Mot de passe
Welcome to %installation_name%: Bienvenue à %installation_name%
Login to %installation_name%: Connexion à %installation_name%
Login to %installation_name%: Connexion à %installation_name%
#serach
Your search is empty. Please provide search terms.: La recherche est vide. Merci de fournir des termes de recherche.
The domain %domain% is unknow. Please check your search.: Le domaine de recherche "%domain%" est inconnu. Merci de vérifier votre recherche.
Invalid terms : Recherche invalide
You should not have more than one domain. : Vous ne devriez pas avoir plus d'un domaine de recherche.

View File

@ -0,0 +1,23 @@
{#
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
#}
{% extends "ChillMainBundle::layout.html.twig" %}
{% block content %}
<p>{{ message }}</p>
{% endblock %}

View File

@ -68,7 +68,7 @@ class SearchProvider
preg_match_all('/@([a-z]+)/', $subject, $terms);
if (count($terms[0]) > 1) {
throw new ParsingException('You should not have more than one domain');
throw new ParsingException('You should not have more than one domain.');
}
//add pattern to be extracted

View File

@ -27,8 +27,17 @@ namespace Chill\MainBundle\Search;
*/
class UnknowSearchNameException extends \Exception
{
private $name;
public function __construct($name)
{
parent::__construct( "No module search supports with the name $name");
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}

View File

@ -0,0 +1,139 @@
<?php
/*
* Chill is a software for social workers
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Chill\MainBundle\Search\SearchInterface;
/**
* Test the search controller
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class SearchControllerTest extends WebTestCase
{
/*
public function setUp()
{
static::bootKernel();
//add a default service
$this->addSearchService(
$this->createDefaultSearchService('<p>I am default</p>', 10), 'default'
);
//add a domain service
$this->addSearchService(
$this->createDefaultSearchService('<p>I am domain bar</p>', 20), 'bar'
);
}
/**
* Test the behaviour when no domain is provided in the search pattern :
* the default search should be enabled
*/
public function testSearchPath()
{
$client = $this->getAuthenticatedClient();
$crawler = $client->request('GET', '/fr/search', array('q' => 'default search'));
$this->assertTrue($client->getResponse()->isSuccessful(),
"The page is loaded without errors");
}
public function testSearchPathEmpty()
{
$client = $this->getAuthenticatedClient();
$crawler = $client->request('GET', '/fr/search?q=');
$this->assertGreaterThan(0, $crawler->filter('*:contains("Merci de fournir des termes de recherche.")')->count());
}
public function testDomainUnknow()
{
$client = $this->getAuthenticatedClient();
$crawler = $client->request('GET', '/fr/search', array('q' => '@unknow domain'));
$this->assertTrue($client->getResponse()->isSuccessful(),
"The page is loaded without errors");
$this->assertGreaterThan(0, $crawler->filter('*:contains("Le domaine de recherche "unknow" est inconnu.")')->count(),
"Message domain unknow is shown");
}
public function testParsingIncorrect()
{
$client = $this->getAuthenticatedClient();
$crawler = $client->request('GET', '/fr/search',
array('q' => '@domaine @domain double domaine'));
$this->assertGreaterThan(0, $crawler->filter('*:contains("Recherche invalide")')
->count());
}
public function testUnknowName()
{
$client = $this->getAuthenticatedClient();
$client->request('GET', '/fr/search',
array('q' => 'default search', 'name' => 'unknow'));
$this->assertTrue($client->getResponse()->isNotFound());
}
public function testSearchWithinSpecificSearchName()
{
/*
//add a search service which will be supported
$this->addSearchService(
$this->createNonDefaultDomainSearchService("<p>I am domain foo</p>", 100, TRUE), 'foo'
);
$client = $this->getAuthenticatedClient();
$crawler = $client->request('GET', '/fr/search',
array('q' => '@foo default search', 'name' => 'foo'));
//$this->markTestSkipped();
$this->assertEquals(0, $crawler->filter('p:contains("I am default")')->count(),
"The mocked default results are not shown");
$this->assertEquals(0, $crawler->filter('p:contains("I am domain bar")')->count(),
"The mocked non-default results are not shown");
$this->assertEquals(1, $crawler->filter('p:contains("I am domain foo")')->count(),
"The mocked nnon default results for foo are shown");
*/
}
private function getAuthenticatedClient()
{
return static::createClient(array(), array(
'PHP_AUTH_USER' => 'center b_social',
'PHP_AUTH_PW' => 'password',
));
}
}

View File

@ -26,7 +26,7 @@ security:
default:
anonymous: ~
http_basic: ~
form_login:
csrf_parameter: _csrf_token
intention: authenticate