fix the possibility to add multiple participation

ref #6
This commit is contained in:
2016-04-11 23:59:14 +02:00
parent fe8994d775
commit c1b9069138
6 changed files with 225 additions and 18 deletions

View File

@@ -44,7 +44,9 @@ class ParticipationControllerTest extends WebTestCase
/**
* Keep a cache for each person id given by the function getRandomPerson.
*
* This is reset by setUp()
* You may ask to ignore some people by adding their id to the array.
*
* This is reset by setUp().
*
* @var int[]
*/
@@ -92,8 +94,31 @@ class ParticipationControllerTest extends WebTestCase
}
/**
* Return a random event only if he has more than one participation.
*
* @param string $centerName
* @param type $circleName
* @return \Chill\EventBundle\Entity\Event
*/
protected function getRandomEventWithMultipleParticipations(
$centerName = 'Center A',
$circleName = 'social')
{
$event = $this->getRandomEvent($centerName, $circleName);
return $event->getParticipations()->count() > 1 ?
$event :
$this->getRandomEventWithMultipleParticipations($centerName, $circleName);
}
/**
* Returns a person randomly.
*
* This function does not give the same person twice
* for each test
* for each test.
*
* You may ask to ignore some people by adding their id to the property
* `$this->personsIdsCache`
*
* @param string $centerName
* @return \Chill\PersonBundle\Entity\Person
@@ -226,7 +251,7 @@ class ParticipationControllerTest extends WebTestCase
'person_id' => $person->getId(),
'event_id' => $event->getId()
));
$this->assertEquals(200, $this->client->getResponse()->getStatusCode(),
"test that /fr/event/participation/new is successful");
@@ -257,8 +282,17 @@ class ParticipationControllerTest extends WebTestCase
public function testNewMultipleAction()
{
$event = $this->getRandomEvent();
// record the number of participation for the event
// record the number of participation for the event (used later in this test)
$nbParticipations = $event->getParticipations()->count();
// make ignore the people already in the event from the function getRandomPerson
$this->personsIdsCache = array_merge(
$this->personsIdsCache,
$event->getParticipations()->map(
function($p) { return $p->getPerson()->getId(); }
)
->toArray()
);
// get some random people
$person1 = $this->getRandomPerson();
$person2 = $this->getRandomPerson();
@@ -267,7 +301,7 @@ class ParticipationControllerTest extends WebTestCase
'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");
@@ -307,5 +341,83 @@ class ParticipationControllerTest extends WebTestCase
$this->assertEquals($nbParticipations + 2, $event->getParticipations()->count());
}
public function testMultipleWithAllPeopleParticipating()
{
$event = $this->getRandomEventWithMultipleParticipations();
$persons_id = implode(',', $event->getParticipations()->map(
function($p) { return $p->getPerson()->getId(); }
)->toArray());
$crawler = $this->client->request('GET', '/fr/event/participation/new',
array(
'persons_ids' => $persons_id,
'event_id' => $event->getId()
));
$this->assertEquals(302, $this->client->getResponse()->getStatusCode(),
"test that /fr/event/participation/new is redirecting");
}
public function testMultipleWithSomePeopleParticipating()
{
$event = $this->getRandomEventWithMultipleParticipations();
// record the number of participation for the event (used later in this test)
$nbParticipations = $event->getParticipations()->count();
// get the persons_id participating on this event
$persons_id = $event->getParticipations()->map(
function($p) { return $p->getPerson()->getId(); }
)->toArray();
// exclude the existing persons_ids from the new person
$this->personsIdsCache = array_merge($this->personsIdsCache, $persons_id);
// get a random person
$newPerson = $this->getRandomPerson();
// build the `persons_ids` parameter
$persons_ids_string = implode(',', array_merge($persons_id,
array($newPerson->getId())));
$crawler = $this->client->request('GET', '/fr/event/participation/new',
array(
'persons_ids' => $persons_ids_string,
'event_id' => $event->getId()
));
$this->assertEquals(200, $this->client->getResponse()->getStatusCode(),
"test that /fr/event/participation/new is successful");
// count that the one UL contains the new person string
$firstPerson = $event->getParticipations()->first()->getPerson();
$ul = $crawler->filter('ul:contains("'.$firstPerson->getLastName().'")'
. ':contains("'.$firstPerson->getFirstName().'")');
$this->assertEquals(1, $ul->count(),
"assert an ul containing the name of ignored people is present");
$this->assertEquals($event->getParticipations()->count(), $ul->children()->count(),
"assert the li listing ignored people has the correct number");
// test a form is present on the page
$button = $crawler->selectButton('Créer');
$this->assertNotNull($button, "test the form with button 'Créer' exists");
// submit the form
$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());
// reload the event and test there is a new participation
$event = $this->em->getRepository('ChillEventBundle:Event')
->find($event->getId());
$this->em->refresh($event);
$this->assertEquals($nbParticipations + 1, $event->getParticipations()->count(),
"Test we have persisted a new participation associated to the test");
}
}