mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 05:44:24 +00:00
add person search functionality
create test to match refs #377 and adapt fixtures for tests implement search in person bundle
This commit is contained in:
parent
383fe786c7
commit
e02a10fdfa
@ -25,6 +25,7 @@ use Doctrine\Common\DataFixtures\AbstractFixture;
|
|||||||
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
|
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
|
||||||
use Doctrine\Common\Persistence\ObjectManager;
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
use Chill\PersonBundle\Entity\Person;
|
use Chill\PersonBundle\Entity\Person;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load people into database
|
* Load people into database
|
||||||
@ -32,8 +33,11 @@ use Chill\PersonBundle\Entity\Person;
|
|||||||
* @author Julien Fastré <julien arobase fastre point info>
|
* @author Julien Fastré <julien arobase fastre point info>
|
||||||
* @author Marc Ducobu <marc@champs-libres.coop>
|
* @author Marc Ducobu <marc@champs-libres.coop>
|
||||||
*/
|
*/
|
||||||
class LoadPeople extends AbstractFixture implements OrderedFixtureInterface
|
class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
||||||
|
|
||||||
public function prepare()
|
public function prepare()
|
||||||
{
|
{
|
||||||
//prepare days, month, years
|
//prepare days, month, years
|
||||||
@ -63,7 +67,24 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface
|
|||||||
|
|
||||||
public function load(ObjectManager $manager)
|
public function load(ObjectManager $manager)
|
||||||
{
|
{
|
||||||
echo "loading people...\n";
|
$this->loadRandPeople($manager);
|
||||||
|
$this->loadExpectedPeople($manager);
|
||||||
|
|
||||||
|
$manager->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function loadExpectedPeople(ObjectManager $manager)
|
||||||
|
{
|
||||||
|
echo "loading expected people...\n";
|
||||||
|
|
||||||
|
foreach ($this->peoples as $person) {
|
||||||
|
$this->addAPerson($this->fillWithDefault($person), $manager);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function loadRandPeople(ObjectManager $manager)
|
||||||
|
{
|
||||||
|
echo "loading rand people...\n";
|
||||||
|
|
||||||
$this->prepare();
|
$this->prepare();
|
||||||
|
|
||||||
@ -96,38 +117,66 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface
|
|||||||
$person = array(
|
$person = array(
|
||||||
'FirstName' => $firstName,
|
'FirstName' => $firstName,
|
||||||
'LastName' => $lastName,
|
'LastName' => $lastName,
|
||||||
'DateOfBirth' => "1960-10-12",
|
|
||||||
'PlaceOfBirth' => "Ottignies Louvain-La-Neuve",
|
|
||||||
'Genre' => $sex,
|
'Genre' => $sex,
|
||||||
'Email' => "Email d'un ami: roger@tt.com",
|
'Nationality' => (rand(0,100) > 50) ? NULL: 'BE'
|
||||||
'CountryOfBirth' => 'France',
|
|
||||||
'Nationality' => 'Russie',
|
|
||||||
'CFData' => array()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$p = new Person();
|
$this->addAPerson($this->fillWithDefault($person), $manager);
|
||||||
|
|
||||||
foreach ($person as $key => $value) {
|
|
||||||
switch ($key) {
|
|
||||||
case 'CountryOfBirth':
|
|
||||||
break;
|
|
||||||
case 'Nationality':
|
|
||||||
break;
|
|
||||||
case 'DateOfBirth':
|
|
||||||
$value = new \DateTime($value);
|
|
||||||
|
|
||||||
|
|
||||||
default:
|
|
||||||
call_user_func(array($p, 'set'.$key), $value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$manager->persist($p);
|
|
||||||
echo "add person'".$p->__toString()."'\n";
|
|
||||||
|
|
||||||
} while ($i <= 100);
|
} while ($i <= 100);
|
||||||
|
}
|
||||||
$manager->flush();
|
|
||||||
|
/**
|
||||||
|
* fill a person array with default value
|
||||||
|
*
|
||||||
|
* @param string[] $specific
|
||||||
|
*/
|
||||||
|
private function fillWithDefault(array $specific)
|
||||||
|
{
|
||||||
|
return array_merge(array(
|
||||||
|
'DateOfBirth' => "1960-10-12",
|
||||||
|
'PlaceOfBirth' => "Ottignies Louvain-La-Neuve",
|
||||||
|
'Genre' => Person::GENRE_MAN,
|
||||||
|
'Email' => "Email d'un ami: roger@tt.com",
|
||||||
|
'CountryOfBirth' => 'BE',
|
||||||
|
'Nationality' => 'BE',
|
||||||
|
'CFData' => array()
|
||||||
|
), $specific);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addAPerson(array $person, ObjectManager $manager)
|
||||||
|
{
|
||||||
|
$p = new Person();
|
||||||
|
|
||||||
|
foreach ($person as $key => $value) {
|
||||||
|
switch ($key) {
|
||||||
|
case 'CountryOfBirth':
|
||||||
|
$p->setCountryOfBirth($this->getCountry($value));
|
||||||
|
break;
|
||||||
|
case 'Nationality':
|
||||||
|
$p->setNationality($this->getCountry($value));
|
||||||
|
break;
|
||||||
|
case 'DateOfBirth':
|
||||||
|
$value = new \DateTime($value);
|
||||||
|
|
||||||
|
|
||||||
|
default:
|
||||||
|
call_user_func(array($p, 'set'.$key), $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$manager->persist($p);
|
||||||
|
echo "add person'".$p->__toString()."'\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getCountry($countryCode)
|
||||||
|
{
|
||||||
|
if ($countryCode === NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return $this->container->get('doctrine.orm.entity_manager')
|
||||||
|
->getRepository('ChillMainBundle:Country')
|
||||||
|
->findOneByCountryCode($countryCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private $firstNamesMale = array("Jean", "Mohamed", "Alfred", "Robert",
|
private $firstNamesMale = array("Jean", "Mohamed", "Alfred", "Robert",
|
||||||
@ -155,14 +204,38 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface
|
|||||||
|
|
||||||
private $peoples = array(
|
private $peoples = array(
|
||||||
array(
|
array(
|
||||||
|
'FirstName' => "Depardieu",
|
||||||
|
'LastName' => "Gérard",
|
||||||
|
'DateOfBirth' => "1948-12-27",
|
||||||
|
'PlaceOfBirth' => "Châteauroux",
|
||||||
|
'Genre' => Person::GENRE_MAN,
|
||||||
|
'CountryOfBirth' => 'FR',
|
||||||
|
'Nationality' => 'RU'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
//to have a person with same firstname as Gérard Depardieu
|
||||||
'FirstName' => "Depardieu",
|
'FirstName' => "Depardieu",
|
||||||
'LastName' => "Jean",
|
'LastName' => "Jean",
|
||||||
'DateOfBirth' => "1960-10-12",
|
'DateOfBirth' => "1960-10-12",
|
||||||
'PlaceOfBirth' => "Ottignies Louvain-La-Neuve",
|
'CountryOfBirth' => 'FR',
|
||||||
'Genre' => Person::GENRE_MAN,
|
'Nationality' => 'FR'
|
||||||
'Email' => "Email d'un ami: roger@tt.com",
|
),
|
||||||
'CountryOfBirth' => 'France',
|
array(
|
||||||
'Nationality' => 'Russie'
|
//to have a person with same birthdate of Gérard Depardieu
|
||||||
)
|
'FirstName' => 'Van Snick',
|
||||||
|
'LastName' => 'Bart',
|
||||||
|
'DateOfBirth' => '1948-12-27'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
//to have a woman with Depardieu as FirstName
|
||||||
|
'FirstName' => 'Depardieu',
|
||||||
|
'LastName' => 'Charline',
|
||||||
|
'Genre' => Person::GENRE_WOMAN
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
//to have a special character in lastName
|
||||||
|
'FirstName' => 'Manço',
|
||||||
|
'LastName' => 'Étienne'
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -22,4 +22,4 @@ services:
|
|||||||
calls:
|
calls:
|
||||||
- ['setContainer', ["@service_container"]]
|
- ['setContainer', ["@service_container"]]
|
||||||
tags:
|
tags:
|
||||||
- { name: chill.search, alias: 'person_search' }
|
- { name: chill.search, alias: 'person_regular' }
|
||||||
|
@ -192,8 +192,8 @@ class PersonSearch extends AbstractSearch
|
|||||||
|
|
||||||
foreach($grams as $key => $gram) {
|
foreach($grams as $key => $gram) {
|
||||||
$qb->andWhere($qb->expr()
|
$qb->andWhere($qb->expr()
|
||||||
->like('LOWER(CONCAT(p.firstName, \' \', p.lastName))', ':default'))
|
->like('LOWER(CONCAT(p.firstName, \' \', p.lastName))', ':default_'.$key))
|
||||||
->setParameter('default', '%'.$gram.'%');
|
->setParameter('default_'.$key, '%'.$gram.'%');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
202
Tests/Search/PersonSearchTest.php
Normal file
202
Tests/Search/PersonSearchTest.php
Normal file
@ -0,0 +1,202 @@
|
|||||||
|
<?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\PersonBundle\Tests\Search;
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test Person search
|
||||||
|
*
|
||||||
|
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||||
|
*/
|
||||||
|
class PersonSearchTest extends WebTestCase
|
||||||
|
{
|
||||||
|
public function testExpected()
|
||||||
|
{
|
||||||
|
$client = $this->getAuthenticatedClient();
|
||||||
|
|
||||||
|
$crawler = $client->request('GET', '/fr/search', array(
|
||||||
|
'q' => '@person Depardieu'
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertRegExp('/Depardieu/', $crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExpectedNamed()
|
||||||
|
{
|
||||||
|
$client = $this->getAuthenticatedClient();
|
||||||
|
|
||||||
|
$crawler = $client->request('GET', '/fr/search', array(
|
||||||
|
'q' => '@person Depardieu', 'name' => 'person_regular'
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertRegExp('/Depardieu/', $crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchByFirstName()
|
||||||
|
{
|
||||||
|
$crawler = $this->generateCrawlerForSearch('@person firstname:Depardieu');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Depardieu/', $crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchByFirstNameLower()
|
||||||
|
{
|
||||||
|
$crawler = $this->generateCrawlerForSearch('@person firstname:depardieu');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Depardieu/', $crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchByFirstNamePartim()
|
||||||
|
{
|
||||||
|
$crawler = $this->generateCrawlerForSearch('@person firstname:Dep');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Depardieu/', $crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFirstNameAccentued()
|
||||||
|
{
|
||||||
|
$this->markTestSkipped();
|
||||||
|
$crawlerSpecial = $this->generateCrawlerForSearch('@person firstname:manço');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Manço/', $crawlerSpecial->text());
|
||||||
|
|
||||||
|
|
||||||
|
$crawlerNoSpecial = $this->generateCrawlerForSearch('@person firstname:manco');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Manço/', $crawlerNoSpecial->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchByLastName()
|
||||||
|
{
|
||||||
|
$crawler = $this->generateCrawlerForSearch('@person lastname:Jean');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Depardieu/', $crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchByLastNameLower()
|
||||||
|
{
|
||||||
|
$crawler = $this->generateCrawlerForSearch('@person lastname:jean');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Depardieu/', $crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchByLastNamePartim()
|
||||||
|
{
|
||||||
|
$crawler = $this->generateCrawlerForSearch('@person lastname:ean');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Depardieu/', $crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchByLastNameAccented()
|
||||||
|
{
|
||||||
|
$this->markTestSkipped();
|
||||||
|
$crawlerSpecial = $this->generateCrawlerForSearch('@person lastname:Gérard');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Gérard/', $crawlerSpecial->text());
|
||||||
|
|
||||||
|
|
||||||
|
$crawlerNoSpecial = $this->generateCrawlerForSearch('@person lastname:Gerard');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Gérard/', $crawlerNoSpecial->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchCombineFirstnameAndNationality()
|
||||||
|
{
|
||||||
|
$crawler = $this->generateCrawlerForSearch('@person firstname:Depardieu nationality:RU');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Gérard/', $crawler->text());
|
||||||
|
//if this is a AND clause, Jean Depardieu should not appears
|
||||||
|
$this->assertNotRegExp('/Jean/', $crawler->text(),
|
||||||
|
"assert clause firstname and nationality are AND");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchCombineLastnameAndFirstName()
|
||||||
|
{
|
||||||
|
$crawler = $this->generateCrawlerForSearch('@person firstname:Depardieu lastname:Jean');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Depardieu/', $crawler->text());
|
||||||
|
//if this is a AND clause, Jean Depardieu should not appears
|
||||||
|
$this->assertNotRegExp('/Gérard/', $crawler->text(),
|
||||||
|
"assert clause firstname and nationality are AND");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchDateOfBirth()
|
||||||
|
{
|
||||||
|
$crawler = $this->generateCrawlerForSearch('@person birthdate:1948-12-27');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Gérard/', $crawler->text());
|
||||||
|
$this->assertRegExp('/Bart/', $crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchCombineDateOfBirthAndFirstName()
|
||||||
|
{
|
||||||
|
$crawler = $this->generateCrawlerForSearch('@person birthdate:1948-12-27 firstname:(Van Snick)');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Bart/', $crawler->text());
|
||||||
|
$this->assertNotRegExp('/Depardieu/', $crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchCombineGenreAndFirstName()
|
||||||
|
{
|
||||||
|
$crawler = $this->generateCrawlerForSearch('@person gender:woman firstname:(Depardieu)');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Charline/', $crawler->text());
|
||||||
|
$this->assertNotRegExp('/Gérard/', $crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchMultipleTrigramUseAndClauseInDefault()
|
||||||
|
{
|
||||||
|
$crawler = $this->generateCrawlerForSearch('@person cha dep');
|
||||||
|
|
||||||
|
$this->assertRegExp('/Charline/', $crawler->text());
|
||||||
|
$this->assertNotRegExp('/Gérard/', $crawler->text());
|
||||||
|
$this->assertNotRegExp('/Jean/', $crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private function generateCrawlerForSearch($pattern)
|
||||||
|
{
|
||||||
|
$client = $this->getAuthenticatedClient();
|
||||||
|
|
||||||
|
$crawler = $client->request('GET', '/fr/search', array(
|
||||||
|
'q' => $pattern
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertTrue($client->getResponse()->isSuccessful());
|
||||||
|
|
||||||
|
return $crawler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return \Symfony\Component\BrowserKit\Client
|
||||||
|
*/
|
||||||
|
private function getAuthenticatedClient()
|
||||||
|
{
|
||||||
|
return static::createClient(array(), array(
|
||||||
|
'PHP_AUTH_USER' => 'center a_social',
|
||||||
|
'PHP_AUTH_PW' => 'password',
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user