chill-bundles/Tests/Controller/ParticipationControllerTest.php
Julien Fastré 9459d7a287 Allow to add multiple participation
The participationController accept a new parameter : `persons_ids`,
which must receive a comma-separated list of person ids. A participation
will be create for all those peoples.

The `new` and `create` actions does not allow to receive both
`person_id` and `persons_ids`.

Tests are added.

ref #6
2016-04-09 23:45:07 +02:00

312 lines
12 KiB
PHP

<?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\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* Test the creation of participation controller
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class ParticipationControllerTest extends WebTestCase
{
/**
*
* @var \Symfony\Component\BrowserKit\Client
*/
protected $client;
/**
*
* @var \Doctrine\ORM\EntityManagerInterface
*/
protected $em;
/**
* Keep a cache for each person id given by the function getRandomPerson.
*
* This is reset by setUp()
*
* @var int[]
*/
private $personsIdsCache = array();
public function setUp()
{
self::bootKernel();
$this->client = static::createClient(array(), array(
'PHP_AUTH_USER' => 'center a_social',
'PHP_AUTH_PW' => 'password',
'HTTP_ACCEPT_LANGUAGE' => 'fr_FR'
));
$container = self::$kernel->getContainer();
$this->em = $container->get('doctrine.orm.entity_manager')
;
$this->personsIdsCache = array();
}
/**
*
*
* @return \Chill\EventBundle\Entity\Event
*/
protected function getRandomEvent($centerName = 'Center A', $circleName = 'social')
{
$center = $this->em->getRepository('ChillMainBundle:Center')
->findByName($centerName);
$circles = $this->em->getRepository('ChillMainBundle:Scope')
->findAll();
array_filter($circles, function($circle) use ($circleName) {
return in_array($circleName, $circle->getName());
});
$circle = $circles[0];
$events = $this->em->getRepository('ChillEventBundle:Event')
->findBy(array('center' => $center, 'circle' => $circle));
return $events[array_rand($events)];
}
/**
* This function does not give the same person twice
* for each test
*
* @param string $centerName
* @return \Chill\PersonBundle\Entity\Person
*/
protected function getRandomPerson($centerName = 'Center A')
{
$center = $this->em->getRepository('ChillMainBundle:Center')
->findByName($centerName);
$persons = $this->em->getRepository('ChillPersonBundle:Person')
->findBy(array('center' => $center));
$person = $persons[array_rand($persons)];
if (in_array($person->getId(), $this->personsIdsCache)) {
return $this->getRandomPerson($centerName); // we try another time
} else {
$this->personsIdsCache[] = $person->getId();
return $person;
}
}
public function testNewActionWrongParameters()
{
$event = $this->getRandomEvent();
$person = $this->getRandomPerson();
// missing person_id or persons_ids
$this->client->request('GET', '/fr/event/participation/new',
array(
'event_id' => $event->getId()
));
$this->assertEquals(400, $this->client->getResponse()->getStatusCode(),
"Test that /fr/event/participation/new fail if "
. "both person_id and persons_ids are missing");
// having both person_id and persons_ids
$this->client->request('GET', '/fr/event/participation/new',
array(
'event_id' => $event->getId(),
'persons_ids' => implode(',', array(
$this->getRandomPerson()->getId(),
$this->getRandomPerson()->getId()
)),
'person_id' => $person->getId()
));
$this->assertEquals(400, $this->client->getResponse()->getStatusCode(),
"test that /fr/event/participation/new fail if both person_id and "
. "persons_ids are set");
// missing event_id
$this->client->request('GET', '/fr/event/participation/new',
array(
'person_id' => $person->getId()
));
$this->assertEquals(400, $this->client->getResponse()->getStatusCode(),
"Test that /fr/event/participation/new fails if event_id is missing");
// persons_ids with wrong content
$this->client->request('GET', '/fr/event/participation/new',
array(
'persons_ids' => 'a,b,531',
'event_id' => $event->getId()
));
$this->assertEquals(400, $this->client->getResponse()->getStatusCode(),
"Test that /fr/event/participation/new fails if persons_ids has wrong content");
}
/**
* This method test participation creation with wrong parameters.
*
* Those request should fail before any processing.
*/
public function testCreateActionWrongParameters()
{
$event = $this->getRandomEvent();
$person = $this->getRandomPerson();
// missing person_id or persons_ids
$this->client->request('GET', '/fr/event/participation/create',
array(
'event_id' => $event->getId()
));
$this->assertEquals(400, $this->client->getResponse()->getStatusCode(),
"Test that /fr/event/participation/create fail if "
. "both person_id and persons_ids are missing");
// having both person_id and persons_ids
$this->client->request('GET', '/fr/event/participation/create',
array(
'event_id' => $event->getId(),
'persons_ids' => implode(',', array(
$this->getRandomPerson()->getId(),
$this->getRandomPerson()->getId()
)),
'person_id' => $person->getId()
));
$this->assertEquals(400, $this->client->getResponse()->getStatusCode(),
"test that /fr/event/participation/create fail if both person_id and "
. "persons_ids are set");
// missing event_id
$this->client->request('GET', '/fr/event/participation/create',
array(
'person_id' => $person->getId()
));
$this->assertEquals(400, $this->client->getResponse()->getStatusCode(),
"Test that /fr/event/participation/create fails if event_id is missing");
// persons_ids with wrong content
$this->client->request('GET', '/fr/event/participation/create',
array(
'persons_ids' => 'a,b,531',
'event_id' => $event->getId()
));
$this->assertEquals(400, $this->client->getResponse()->getStatusCode(),
"Test that /fr/event/participation/create fails if persons_ids has wrong content");
}
public function testNewSingleAction()
{
$event = $this->getRandomEvent();
// record the number of participation for the event
$nbParticipations = $event->getParticipations()->count();
$person = $this->getRandomPerson();
$crawler = $this->client->request('GET', '/fr/event/participation/new',
array(
'person_id' => $person->getId(),
'event_id' => $event->getId()
));
$this->assertEquals(200, $this->client->getResponse()->getStatusCode(),
"test that /fr/event/participation/new is successful");
$button = $crawler->selectButton('Créer');
$this->assertNotNull($button, "test the form with button 'Créer' exists");
$this->client->submit($button->form(), array(
'participation[role]' => $event->getType()->getRoles()->first()->getId(),
'participation[status]' => $event->getType()->getStatuses()->first()->getId()
));
$this->assertTrue($this->client->getResponse()->isRedirect());
$crawler = $this->client->followRedirect();
$span = $crawler->filter('table td span.entity-person a:contains("'
.$person->getFirstName().'"):contains("'.$person->getLastname().'")');
$this->assertGreaterThan(0, count($span));
// as the container has reloaded, reload the event
$event = $this->em->getRepository('ChillEventBundle:Event')->find($event->getId());
$this->em->refresh($event);
$this->assertEquals($nbParticipations + 1, $event->getParticipations()->count());
}
public function testNewMultipleAction()
{
$event = $this->getRandomEvent();
// record the number of participation for the event
$nbParticipations = $event->getParticipations()->count();
$person1 = $this->getRandomPerson();
$person2 = $this->getRandomPerson();
$crawler = $this->client->request('GET', '/fr/event/participation/new',
array(
'persons_ids' => implode(',', array($person1->getId(), $person2->getId())),
'event_id' => $event->getId()
));
$this->assertEquals(200, $this->client->getResponse()->getStatusCode(),
"test that /fr/event/participation/new is successful");
$button = $crawler->selectButton('Créer');
$this->assertNotNull($button, "test the form with button 'Créer' exists");
$this->client->submit($button->form(), array(
'form' => array(
'participations' => array(
0 => array(
'role' => $event->getType()->getRoles()->first()->getId(),
'status' => $event->getType()->getStatuses()->first()->getId()
),
1 => array(
'role' => $event->getType()->getRoles()->first()->getId(),
'status' => $event->getType()->getStatuses()->first()->getId()
),
)
)
));
$this->assertTrue($this->client->getResponse()->isRedirect());
$crawler = $this->client->followRedirect();
$span1 = $crawler->filter('table td span.entity-person a:contains("'
.$person1->getFirstName().'"):contains("'.$person1->getLastname().'")');
$this->assertGreaterThan(0, count($span1));
$span2 = $crawler->filter('table td span.entity-person a:contains("'
.$person2->getFirstName().'"):contains("'.$person2->getLastname().'")');
$this->assertGreaterThan(0, count($span2));
// as the container has reloaded, reload the event
$event = $this->em->getRepository('ChillEventBundle:Event')->find($event->getId());
$this->em->refresh($event);
$this->assertEquals($nbParticipations + 2, $event->getParticipations()->count());
}
}