diff --git a/Resources/views/Event/list.html.twig b/Resources/views/Event/list.html.twig
index 0c98cb027..10d274e2d 100644
--- a/Resources/views/Event/list.html.twig
+++ b/Resources/views/Event/list.html.twig
@@ -4,7 +4,7 @@
{% if events|length > 0 %}
-
+
{{ 'Name'|trans }} |
diff --git a/Search/EventSearch.php b/Search/EventSearch.php
index 62f4e1604..d04d0e3eb 100644
--- a/Search/EventSearch.php
+++ b/Search/EventSearch.php
@@ -152,7 +152,10 @@ class EventSearch extends AbstractSearch
$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
$name = isset($terms['name']) ? $terms['name'] : $terms['_default'];
diff --git a/Tests/Search/EventSearchTest.php b/Tests/Search/EventSearchTest.php
new file mode 100644
index 000000000..ab5e732c9
--- /dev/null
+++ b/Tests/Search/EventSearchTest.php
@@ -0,0 +1,276 @@
+
+ *
+ * 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 .
+ */
+
+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é
+ */
+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');
+
+ }
+
+
+
+}