cs: Fix code style (safe rules only).

This commit is contained in:
Pol Dellaiera
2021-11-23 14:06:38 +01:00
parent 149d7ce991
commit 8f96a1121d
1223 changed files with 65199 additions and 64625 deletions

View File

@@ -1,16 +1,27 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\EventBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @internal
* @coversNothing
*/
class EventControllerTest extends WebTestCase
{
public function testSkipped()
{
$this->markTestSkipped();
}
/*
public function testCompleteScenario()
{
@@ -56,5 +67,5 @@ class EventControllerTest extends WebTestCase
$this->assertNotRegExp('/Foo/', $client->getResponse()->getContent());
}
*/
*/
}

View File

@@ -1,15 +1,27 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\EventBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @internal
* @coversNothing
*/
class EventTypeControllerTest extends WebTestCase
{
public function testSkipped()
{
$this->markTestSkipped();
}
/*
public function testCompleteScenario()
{
@@ -55,5 +67,5 @@ class EventTypeControllerTest extends WebTestCase
$this->assertNotRegExp('/Foo/', $client->getResponse()->getContent());
}
*/
*/
}

View File

@@ -1,20 +1,10 @@
<?php
/*
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
/**
* Chill is a software for social workers
*
* 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/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\EventBundle\Tests\Controller;
@@ -22,263 +12,220 @@ namespace Chill\EventBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* Test the creation of participation controller
*
* Test the creation of participation controller.
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* @internal
* @coversNothing
*/
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.
*
*
* You may ask to ignore some people by adding their id to the array.
*
*
* This is reset by setUp().
*
* @var int[]
* @var int[]
*/
private $personsIdsCache = array();
private $personsIdsCache = [];
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'
));
$this->client = static::createClient([], [
'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();
$this->em = $container->get('doctrine.orm.entity_manager');
$this->personsIdsCache = [];
}
/**
*
*
* @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)];
}
/**
* 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.
*
* You may ask to ignore some people by adding their id to the property
* `$this->personsIdsCache`
*
* @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");
$this->client->request(
'GET',
'/fr/event/participation/create',
[
'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");
$this->client->request(
'GET',
'/fr/event/participation/create',
[
'event_id' => $event->getId(),
'persons_ids' => implode(',', [
$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");
$this->client->request(
'GET',
'/fr/event/participation/create',
[
'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");
$this->client->request(
'GET',
'/fr/event/participation/create',
[
'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()
public function testEditMultipleAction()
{
/* @var $event \Chill\EventBundle\Entity\Event */
$event = $this->getRandomEventWithMultipleParticipations();
$crawler = $this->client->request('GET', '/fr/event/participation/' . $event->getId() .
'/edit_multiple');
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
$button = $crawler->selectButton('Mettre à jour');
$this->assertEquals(1, $button->count(), "test the form with button 'mettre à jour' exists ");
$this->client->submit($button->form(), [
'form[participations][0][role]' => $event->getType()->getRoles()->first()->getId(),
'form[participations][0][status]' => $event->getType()->getStatuses()->first()->getId(),
'form[participations][1][role]' => $event->getType()->getRoles()->last()->getId(),
'form[participations][1][status]' => $event->getType()->getStatuses()->last()->getId(),
]);
$this->assertTrue($this->client->getResponse()
->isRedirect('/fr/event/event/' . $event->getId() . '/show'));
}
public function testNewActionWrongParameters()
{
$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());
// missing person_id or persons_ids
$this->client->request(
'GET',
'/fr/event/participation/new',
[
'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',
[
'event_id' => $event->getId(),
'persons_ids' => implode(',', [
$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',
[
'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',
[
'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'
);
}
public function testNewMultipleAction()
{
$event = $this->getRandomEvent();
@@ -286,79 +233,91 @@ class ParticipationControllerTest extends WebTestCase
$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(); }
)
$this->personsIdsCache,
$event->getParticipations()->map(
function ($p) { return $p->getPerson()->getId(); }
)
->toArray()
);
);
// get some random people
$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");
$crawler = $this->client->request(
'GET',
'/fr/event/participation/new',
[
'persons_ids' => implode(',', [$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(
$this->client->submit($button->form(), [
'form' => [
'participations' => [
0 => [
'role' => $event->getType()->getRoles()->first()->getId(),
'status' => $event->getType()->getStatuses()->first()->getId()
),
1 => array(
'status' => $event->getType()->getStatuses()->first()->getId(),
],
1 => [
'role' => $event->getType()->getRoles()->first()->getId(),
'status' => $event->getType()->getStatuses()->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().'")');
. $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().'")');
. $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());
}
public function testNewMultipleWithAllPeopleParticipating()
{
$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");
function ($p) { return $p->getPerson()->getId(); }
)->toArray());
$crawler = $this->client->request(
'GET',
'/fr/event/participation/new',
[
'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 testNewMultipleWithSomePeopleParticipating()
{
$event = $this->getRandomEventWithMultipleParticipations();
@@ -366,83 +325,194 @@ class ParticipationControllerTest extends WebTestCase
$nbParticipations = $event->getParticipations()->count();
// get the persons_id participating on this event
$persons_id = $event->getParticipations()->map(
function($p) { return $p->getPerson()->getId(); }
)->toArray();
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");
$persons_ids_string = implode(',', array_merge(
$persons_id,
[$newPerson->getId()]
));
$crawler = $this->client->request(
'GET',
'/fr/event/participation/new',
[
'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");
$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(
$this->client->submit($button->form(), [
'participation[role]' => $event->getType()->getRoles()->first()->getId(),
'participation[status]' => $event->getType()->getStatuses()->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());
->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");
$this->assertEquals(
$nbParticipations + 1,
$event->getParticipations()->count(),
'Test we have persisted a new participation associated to the test'
);
}
public function testEditMultipleAction()
public function testNewSingleAction()
{
/* @var $event \Chill\EventBundle\Entity\Event */
$event = $this->getRandomEventWithMultipleParticipations();
$crawler = $this->client->request('GET', '/fr/event/participation/'.$event->getId().
'/edit_multiple');
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
$button = $crawler->selectButton('Mettre à jour');
$this->assertEquals(1, $button->count(), "test the form with button 'mettre à jour' exists ");
$this->client->submit($button->form(), array(
'form[participations][0][role]' => $event->getType()->getRoles()->first()->getId(),
'form[participations][0][status]' => $event->getType()->getStatuses()->first()->getId(),
'form[participations][1][role]' => $event->getType()->getRoles()->last()->getId(),
'form[participations][1][status]' => $event->getType()->getStatuses()->last()->getId(),
));
$this->assertTrue($this->client->getResponse()
->isRedirect('/fr/event/event/'.$event->getId().'/show'));
$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',
[
'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(), [
'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());
}
/**
* @param mixed $centerName
* @param mixed $circleName
*
* @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(['center' => $center, 'circle' => $circle]);
return $events[array_rand($events)];
}
/**
* 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.
*
* You may ask to ignore some people by adding their id to the property
* `$this->personsIdsCache`
*
* @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(['center' => $center]);
$person = $persons[array_rand($persons)];
if (in_array($person->getId(), $this->personsIdsCache)) {
return $this->getRandomPerson($centerName); // we try another time
}
$this->personsIdsCache[] = $person->getId();
return $person;
}
}

View File

@@ -1,15 +1,27 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\EventBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @internal
* @coversNothing
*/
class RoleControllerTest extends WebTestCase
{
public function testSkipped()
{
$this->markTestSkipped();
}
/*
public function testCompleteScenario()
{
@@ -55,5 +67,5 @@ class RoleControllerTest extends WebTestCase
$this->assertNotRegExp('/Foo/', $client->getResponse()->getContent());
}
*/
*/
}

View File

@@ -1,15 +1,27 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\EventBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @internal
* @coversNothing
*/
class StatusControllerTest extends WebTestCase
{
public function testSkipped()
{
$this->markTestSkipped();
}
/*
public function testCompleteScenario()
{
@@ -55,5 +67,5 @@ class StatusControllerTest extends WebTestCase
$this->assertNotRegExp('/Foo/', $client->getResponse()->getContent());
}
*/
*/
}

View File

@@ -1,398 +1,385 @@
<?php
/*
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
/**
* Chill is a software for social workers
*
* 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/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
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;
use DateTime;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* Test the EventSearch class
* Test the EventSearch class.
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* @internal
* @coversNothing
*/
class EventSearchTest extends WebTestCase
{
/**
* The eventSearch service, which is used to search events
* The center A.
*
* @var \Chill\MainBundle\Entity\Center
*/
protected $centerA;
/**
* @var \Symfony\Component\BrowserKit\Client
*/
protected $client;
/**
* @var \Doctrine\ORM\EntityManagerInterface
*/
protected $entityManager;
/**
* Events created during this test.
*
* @var Event[]
*/
protected $events = [];
/**
* 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
* 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->client = static::createClient([], [
'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')
;
->get('doctrine.orm.entity_manager');
$this->centerA = $this->entityManager
->getRepository('ChillMainBundle:Center')
->findOneBy(array('name' => 'Center A'));
->getRepository('ChillMainBundle:Center')
->findOneBy(['name' => 'Center A']);
$this->eventType = $this->entityManager
->getRepository('ChillEventBundle:EventType')
->findAll()[0];
->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();
->setParameter('event_id', $event->getId())
->execute();
}
$this->events = array();
$this->events = [];
}
public function testDisplayAll()
{
$crawler = $this->client->request('GET', '/fr/search', [
'q' => '@events',
]);
$this->assertGreaterThanOrEqual(
2,
$crawler->filter('table.events tr')->count(),
'assert than more than 2 tr are present'
);
}
/**
* Test that a user connected with an user with the wrong center does not
* see the events.
*/
public function testDisplayAllWrongUser()
{
$client = static::createClient([], [
'PHP_AUTH_USER' => 'center b_social',
'PHP_AUTH_PW' => 'password',
'HTTP_ACCEPT_LANGUAGE' => 'fr_FR',
]);
$crawler = $client->request('GET', '/fr/search', [
'q' => '@events printemps',
]);
$this->assertEquals(
0,
$crawler->filter('tr:contains("Printemps")')->count(),
'assert that the word "printemps" is present'
);
}
public function testSearchByDateDateBetween()
{
// serach with date from **and** date-to
$crawler = $this->client->request('GET', '/fr/search', [
'q' => '@events date-from:2016-05-30 date-to:2016-06-20',
]);
/* @var $dateFrom \DateTime the date from in DateTime */
$dateFrom = DateTime::createFromFormat('Y-m-d', '2016-05-30');
$dateTo = DateTime::createFromFormat('Y-m-d', '2016-06-20');
$dates = $this->iterateOnRowsToFindDate($crawler->filter('tr'));
foreach ($dates as $date) {
$this->assertGreaterThanOrEqual($dateFrom, $date);
$this->assertLessThanOrEqual($dateTo, $date);
}
// there should not have any other results, but if any other bundle
// add some other event, we go on next pages
if ($crawler->selectLink('Voir tous les résultats')->count() == 0) {
return;
}
// click on link "Voir tous les résultats"
$crawlerAllResults = $this->client->click($crawler
->selectLink('Voir tous les résultats')->link());
$dates = $this->iterateOnRowsToFindDate($crawlerAllResults->filter('tr'));
foreach ($dates as $date) {
$this->assertGreaterThanOrEqual($dateFrom, $date);
$this->assertLessThanOrEqual($dateTo, $date);
}
//iterate on pagination
$crawlerAllResults->filter('.pagination a')->each(function ($a, $i) use ($dateFrom) {
$page = $this->client->click($a->link());
$dates = $this->iterateOnRowsToFindDate($page->filter('tr'));
foreach ($dates as $date) {
$this->assertGreaterThanOrEqual($dateFrom, $date);
$this->assertLessThanOrEqual($dateTo, $date);
}
});
}
public function testSearchByDateDateFromOnly()
{
// search with date from
$crawler = $this->client->request('GET', '/fr/search', [
'q' => '@events date-from:2016-05-30',
]);
/* @var $dateFrom \DateTime the date from in DateTime */
$dateFrom = DateTime::createFromFormat('Y-m-d', '2016-05-30');
$dates = $this->iterateOnRowsToFindDate($crawler->filter('tr'));
foreach ($dates as $date) {
$this->assertGreaterThanOrEqual($dateFrom, $date);
}
// click on link "Voir tous les résultats"
$crawlerAllResults = $this->client->click($crawler
->selectLink('Voir tous les résultats')->link());
$dates = $this->iterateOnRowsToFindDate($crawlerAllResults->filter('tr'));
foreach ($dates as $date) {
$this->assertGreaterThanOrEqual($dateFrom, $date);
}
//iterate on pagination
$crawlerAllResults->filter('.pagination a')->each(function ($a, $i) use ($dateFrom) {
$page = $this->client->click($a->link());
$dates = $this->iterateOnRowsToFindDate($page->filter('tr'));
foreach ($dates as $date) {
$this->assertGreaterThanOrEqual($dateFrom, $date);
}
});
}
public function testSearchByDateDateTo()
{
// serach with date from **and** date-to
$crawler = $this->client->request('GET', '/fr/search', [
'q' => '@events date:2016-05-30',
]);
/* @var $dateFrom \DateTime the date from in DateTime */
$dateTo = DateTime::createFromFormat('Y-m-d', '2016-05-30');
$dates = $this->iterateOnRowsToFindDate($crawler->filter('tr'));
foreach ($dates as $date) {
$this->assertLessThanOrEqual($dateTo, $date);
}
if ($crawler->selectLink('Voir tous les résultats')->count() == 0) {
return;
}
// click on link "Voir tous les résultats"
$crawlerAllResults = $this->client->click($crawler
->selectLink('Voir tous les résultats')->link());
$dates = $this->iterateOnRowsToFindDate($crawlerAllResults->filter('tr'));
foreach ($dates as $date) {
$this->assertLessThanOrEqual($dateTo, $date);
}
//iterate on pagination
$crawlerAllResults->filter('.pagination a')->each(function ($a, $i) {
$page = $this->client->click($a->link());
$dates = $this->iterateOnRowsToFindDate($page->filter('tr'));
foreach ($dates as $date) {
$this->assertLessThanOrEqual($dateTo, $date);
}
});
}
public function testSearchByDefault()
{
$crawler = $this->client->request('GET', '/fr/search', [
'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', [
'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'
);
}
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())
;
->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())
;
->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();
->findAll();
/* @var $circle \Chill\MainBundle\Entity\Scope */
foreach($circles as $circle) {
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->assertGreaterThanOrEqual(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 testSearchByDateDateFromOnly()
{
// search with date from
$crawler = $this->client->request('GET', '/fr/search', array(
'q' => '@events date-from:2016-05-30'
));
/* @var $dateFrom \DateTime the date from in DateTime */
$dateFrom = \DateTime::createFromFormat("Y-m-d", "2016-05-30");
$dates = $this->iterateOnRowsToFindDate($crawler->filter("tr"));
foreach($dates as $date) {
$this->assertGreaterThanOrEqual($dateFrom, $date);
}
// click on link "Voir tous les résultats"
$crawlerAllResults = $this->client->click($crawler
->selectLink("Voir tous les résultats")->link());
$dates = $this->iterateOnRowsToFindDate($crawlerAllResults->filter("tr"));
foreach ($dates as $date) {
$this->assertGreaterThanOrEqual($dateFrom, $date);
}
//iterate on pagination
$crawlerAllResults->filter(".pagination a")->each(function($a, $i) use ($dateFrom) {
$page = $this->client->click($a->link());
$dates = $this->iterateOnRowsToFindDate($page->filter("tr"));
foreach($dates as $date) {
$this->assertGreaterThanOrEqual($dateFrom, $date);
}
});
}
public function testSearchByDateDateBetween()
{
// 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'
));
/* @var $dateFrom \DateTime the date from in DateTime */
$dateFrom = \DateTime::createFromFormat("Y-m-d", "2016-05-30");
$dateTo = \DateTime::createFromFormat("Y-m-d", "2016-06-20");
$dates = $this->iterateOnRowsToFindDate($crawler->filter("tr"));
foreach($dates as $date) {
$this->assertGreaterThanOrEqual($dateFrom, $date);
$this->assertLessThanOrEqual($dateTo, $date);
}
// there should not have any other results, but if any other bundle
// add some other event, we go on next pages
if ($crawler->selectLink("Voir tous les résultats")->count() == 0) {
return ;
}
// click on link "Voir tous les résultats"
$crawlerAllResults = $this->client->click($crawler
->selectLink("Voir tous les résultats")->link());
$dates = $this->iterateOnRowsToFindDate($crawlerAllResults->filter("tr"));
foreach ($dates as $date) {
$this->assertGreaterThanOrEqual($dateFrom, $date);
$this->assertLessThanOrEqual($dateTo, $date);
}
//iterate on pagination
$crawlerAllResults->filter(".pagination a")->each(function($a, $i) use ($dateFrom) {
$page = $this->client->click($a->link());
$dates = $this->iterateOnRowsToFindDate($page->filter("tr"));
foreach($dates as $date) {
$this->assertGreaterThanOrEqual($dateFrom, $date);
$this->assertLessThanOrEqual($dateTo, $date);
}
});
}
public function testSearchByDateDateTo()
{
// serach with date from **and** date-to
$crawler = $this->client->request('GET', '/fr/search', array(
'q' => '@events date:2016-05-30'
));
/* @var $dateFrom \DateTime the date from in DateTime */
$dateTo = \DateTime::createFromFormat("Y-m-d", "2016-05-30");
$dates = $this->iterateOnRowsToFindDate($crawler->filter("tr"));
foreach($dates as $date) {
$this->assertLessThanOrEqual($dateTo, $date);
}
if ($crawler->selectLink("Voir tous les résultats")->count() == 0) {
return ;
}
// click on link "Voir tous les résultats"
$crawlerAllResults = $this->client->click($crawler
->selectLink("Voir tous les résultats")->link());
$dates = $this->iterateOnRowsToFindDate($crawlerAllResults->filter("tr"));
foreach ($dates as $date) {
$this->assertLessThanOrEqual($dateTo, $date);
}
//iterate on pagination
$crawlerAllResults->filter(".pagination a")->each(function($a, $i) use ($dateFrom) {
$page = $this->client->click($a->link());
$dates = $this->iterateOnRowsToFindDate($page->filter("tr"));
foreach($dates as $date) {
$this->assertLessThanOrEqual($dateTo, $date);
}
});
}
/**
* this function iterate on row from results of events and return the content
* of the second column (which should contains the date) in DateTime objects
*
* @param \Symfony\Component\DomCrawler\Crawler $trs
* @return \DateTime[]
* of the second column (which should contains the date) in DateTime objects.
*
* @return DateTime[]
*/
private function iterateOnRowsToFindDate(\Symfony\Component\DomCrawler\Crawler $trs)
{
$months = array(
"janvier" => 1,
"février" => 2,
"mars" => 3,
"avril" => 4,
"mai" => 5,
"juin" => 6,
"juillet" => 7,
"août" => 8,
"septembre" => 9,
"octobre" => 10,
"novembre" => 11,
"décembre" => 12
);
$results = $trs->each(function($tr, $i) use ($months) {
$months = [
'janvier' => 1,
'février' => 2,
'mars' => 3,
'avril' => 4,
'mai' => 5,
'juin' => 6,
'juillet' => 7,
'août' => 8,
'septembre' => 9,
'octobre' => 10,
'novembre' => 11,
'décembre' => 12,
];
$results = $trs->each(function ($tr, $i) use ($months) {
// we skip the first row
if ($i > 0) {
if (0 < $i) {
// get the second node, which should contains a date
$tdDate = $tr->filter("td")->eq(1);
$tdDate = $tr->filter('td')->eq(1);
// transform the date, which should be in french, into a DateTime object
$parts = explode(" ", $tdDate->text());
return \DateTime::createFromFormat("Y-m-d", $parts[2].
"-".$months[$parts[1]]."-".$parts[0]);
$parts = explode(' ', $tdDate->text());
return DateTime::createFromFormat('Y-m-d', $parts[2] .
'-' . $months[$parts[1]] . '-' . $parts[0]);
}
});
// remove the first row
unset($results[0]);
return $results;
}
/**
* 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');
}
}