add test to eventSearch

ref #10
This commit is contained in:
Julien Fastré 2016-05-31 17:30:35 +02:00
parent 9be006a946
commit 69af3e8d43
3 changed files with 281 additions and 2 deletions

View File

@ -4,7 +4,7 @@
{% if events|length > 0 %} {% if events|length > 0 %}
<table> <table class="events">
<thead> <thead>
<tr> <tr>
<th class="chill-red">{{ 'Name'|trans }}</th> <th class="chill-red">{{ 'Name'|trans }}</th>

View File

@ -152,7 +152,10 @@ class EventSearch extends AbstractSearch
$qb->andWhere($orWhere); $qb->andWhere($orWhere);
} }
if (isset($terms['name']) OR isset($terms['_default'])) { if (
(isset($terms['name']) OR isset($terms['_default']))
AND
(!empty($terms['name']) OR !empty($terms['_default']))) {
// the form with name:"xyz" has precedence // the form with name:"xyz" has precedence
$name = isset($terms['name']) ? $terms['name'] : $terms['_default']; $name = isset($terms['name']) ? $terms['name'] : $terms['_default'];

View File

@ -0,0 +1,276 @@
<?php
/*
* Copyright (C) 2016 Champs-Libres <info@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\EventBundle\Tests\Search;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Chill\EventBundle\Entity\Event;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Chill\EventBundle\Search\EventSearch;
/**
* Test the EventSearch class
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class EventSearchTest extends WebTestCase
{
/**
* The eventSearch service, which is used to search events
*
* @var \Chill\EventBundle\Search\EventSearch
*/
protected $eventSearch;
/**
*
* @var \Doctrine\ORM\EntityManagerInterface
*/
protected $entityManager;
/**
* The center A
*
* @var \Chill\MainBundle\Entity\Center
*/
protected $centerA;
/**
* a random event type
*
* @var \Chill\EventBundle\Entity\EventType
*/
protected $eventType;
/**
* Events created during this test
*
* @var Event[]
*/
protected $events = array();
/**
*
* @var \Prophecy\Prophet
*/
protected $prophet;
/**
*
* @var \Symfony\Component\BrowserKit\Client
*/
protected $client;
public function setUp()
{
self::bootKernel();
/* @var $kernel \Symfony\Component\HttpKernel\KernelInterface */
$kernel = self::$kernel;
$this->client = static::createClient(array(), array(
'PHP_AUTH_USER' => 'center a_social',
'PHP_AUTH_PW' => 'password',
'HTTP_ACCEPT_LANGUAGE' => 'fr_FR'
));
$this->prophet = new \Prophecy\Prophet;
$this->entityManager = self::$kernel->getContainer()
->get('doctrine.orm.entity_manager')
;
$this->centerA = $this->entityManager
->getRepository('ChillMainBundle:Center')
->findOneBy(array('name' => 'Center A'));
$this->eventType = $this->entityManager
->getRepository('ChillEventBundle:EventType')
->findAll()[0];
$this->createEvents();
}
public function tearDown()
{
foreach ($this->events as $event) {
$this->entityManager->createQuery('DELETE FROM ChillEventBundle:Event e WHERE e.id = :event_id')
->setParameter('event_id', $event->getId())
->execute();
}
$this->events = array();
}
protected function createEvents()
{
$event1 = (new Event())
->setCenter($this->centerA)
->setDate(new \DateTime('2016-05-30'))
->setName('Printemps européen')
->setType($this->eventType)
->setCircle($this->getCircle())
;
$this->entityManager->persist($event1);
$this->events[] = $event1;
$event2 = (new Event())
->setCenter($this->centerA)
->setDate(new \DateTime('2016-06-24'))
->setName('Hiver de la droite')
->setType($this->eventType)
->setCircle($this->getCircle())
;
$this->entityManager->persist($event2);
$this->events[] = $event2;
$this->entityManager->flush();
}
/**
*
* @param string $name the name of the circle
* @return \Chill\MainBundle\Entity\Scope
*/
protected function getCircle($name = 'social')
{
$circles = $this->entityManager->getRepository('ChillMainBundle:Scope')
->findAll();
/* @var $circle \Chill\MainBundle\Entity\Scope */
foreach($circles as $circle) {
if (in_array($name, $circle->getName())) {
return $circle;
}
}
}
public function testDisplayAll()
{
$crawler = $this->client->request('GET', '/fr/search', array(
'q' => '@events'
));
$this->assertGreaterThan(2, $crawler->filter('table.events tr')->count(),
'assert than more than 2 tr are present');
}
public function testSearchByDefault()
{
$crawler = $this->client->request('GET', '/fr/search', array(
'q' => '@events printemps'
));
$this->assertEquals(
1,
$crawler->filter('table.events tr')->count() - 1 /* as the header is a th */,
'assert than more than 2 tr are present');
$this->assertEquals(
1,
$crawler->filter('tr:contains("Printemps")')->count(),
'assert that the word "printemps" is present');
}
public function testSearchByName()
{
$crawler = $this->client->request('GET', '/fr/search', array(
'q' => '@events name:printemps'
));
$this->assertEquals(
1,
$crawler->filter('table.events tr')->count() - 1 /* as the header is a th */,
'assert than more than 2 tr are present');
$this->assertEquals(
1,
$crawler->filter('tr:contains("Printemps")')->count(),
'assert that the word "printemps" is present');
}
public function testSearchByDate()
{
// search with date from
$crawler = $this->client->request('GET', '/fr/search', array(
'q' => '@events date-from:2016-05-30'
));
$this->assertEquals(
1,
$crawler->filter('tr:contains("Printemps")')->count(),
'assert that the word "printemps" is present');
$this->assertEquals(
1,
$crawler->filter('tr:contains("Hiver")')->count(),
'assert that the word "Hiver" is present');
// serach with date from **and** date-to
$crawler = $this->client->request('GET', '/fr/search', array(
'q' => '@events date-from:2016-05-30 date-to:2016-06-20'
));
$this->assertEquals(
1,
$crawler->filter('tr:contains("Printemps")')->count(),
'assert that the word "printemps" is present');
$this->assertEquals(
0,
$crawler->filter('tr:contains("Hiver")')->count(),
'assert that the word "Hiver" is not present');
// serach with date from **and** date-to
$crawler = $this->client->request('GET', '/fr/search', array(
'q' => '@events date:2016-05-30'
));
$this->assertEquals(
1,
$crawler->filter('tr:contains("Printemps")')->count(),
'assert that the word "printemps" is present');
$this->assertEquals(
0,
$crawler->filter('tr:contains("Hiver")')->count(),
'assert that the word "Hiver" is not present');
}
/**
* Test that a user connected with an user with the wrong center does not
* see the events
*/
public function testDisplayAllWrongUser()
{
$client = static::createClient(array(), array(
'PHP_AUTH_USER' => 'center b_social',
'PHP_AUTH_PW' => 'password',
'HTTP_ACCEPT_LANGUAGE' => 'fr_FR'
));
$crawler = $client->request('GET', '/fr/search', array(
'q' => '@events printemps'
));
$this->assertEquals(0, $crawler->filter('tr:contains("Printemps")')->count(),
'assert that the word "printemps" is present');
}
}