mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
331 lines
12 KiB
PHP
331 lines
12 KiB
PHP
<?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.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\PersonBundle\Tests\Controller;
|
|
|
|
use Chill\MainBundle\Test\PrepareClientTrait;
|
|
use Chill\PersonBundle\Entity\Person;
|
|
use Closure;
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
|
|
/**
|
|
* Test the edition of persons.
|
|
*
|
|
* As I am logged in as "center a_social"
|
|
*
|
|
* @internal
|
|
* @coversNothing
|
|
*/
|
|
final class PersonControllerUpdateTest extends WebTestCase
|
|
{
|
|
use PrepareClientTrait;
|
|
|
|
/**
|
|
* @var string The url using for editing the person's information
|
|
*/
|
|
private $editUrl;
|
|
|
|
/**
|
|
* @var \Doctrine\ORM\EntityManagerInterface The entity manager
|
|
*/
|
|
private $em;
|
|
|
|
/**
|
|
* @var Person The person on which the test is executed
|
|
*/
|
|
private $person;
|
|
|
|
/**
|
|
* @var string The url using for seeing the person's information
|
|
*/
|
|
private $viewUrl;
|
|
|
|
/**
|
|
* Prepare client and create a random person.
|
|
*/
|
|
protected function setUp(): void
|
|
{
|
|
self::bootKernel();
|
|
|
|
$this->em = self::$kernel->getContainer()
|
|
->get('doctrine.orm.entity_manager');
|
|
|
|
$center = $this->em->getRepository(\Chill\MainBundle\Entity\Center::class)
|
|
->findOneBy(['name' => 'Center A']);
|
|
|
|
$this->person = (new Person())
|
|
->setLastName('My Beloved')
|
|
->setFirstName('Jesus')
|
|
->setCenter($center)
|
|
->setGender(Person::MALE_GENDER);
|
|
|
|
$this->em->persist($this->person);
|
|
$this->em->flush();
|
|
|
|
$this->editUrl = '/fr/person/' . $this->person->getId() . '/general/edit';
|
|
$this->viewUrl = '/fr/person/' . $this->person->getId() . '/general';
|
|
|
|
$this->client = $this->getClientAuthenticated();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
$this->refreshPerson();
|
|
$this->em->remove($this->person);
|
|
$this->em->flush();
|
|
}
|
|
|
|
public function providesInvalidFieldsValues()
|
|
{
|
|
return [
|
|
['firstName', $this->getVeryLongText()],
|
|
['lastName', $this->getVeryLongText()],
|
|
['firstName', ''],
|
|
['lastName', ''],
|
|
['birthdate', 'false date'],
|
|
];
|
|
}
|
|
|
|
public function testEditLanguages()
|
|
{
|
|
$crawler = $this->client->request('GET', $this->editUrl);
|
|
$selectedLanguages = ['en', 'an', 'bbj'];
|
|
|
|
$form = $crawler->selectButton('Enregistrer')
|
|
->form();
|
|
$form->get('chill_personbundle_person[spokenLanguages]')
|
|
->setValue($selectedLanguages);
|
|
|
|
$this->client->submit($form);
|
|
$this->refreshPerson();
|
|
|
|
$this->assertTrue(
|
|
$this->client->getResponse()->isRedirect($this->viewUrl),
|
|
'the page is redirected to /general view'
|
|
);
|
|
//retrieve languages codes present in person
|
|
foreach ($this->person->getSpokenLanguages() as $lang) {
|
|
$languagesCodesPresents[] = $lang->getId();
|
|
}
|
|
$this->assertEquals(
|
|
asort($selectedLanguages),
|
|
asort($languagesCodesPresents),
|
|
'the person speaks the expected languages'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Test the edit page of a given person are not accessible for an
|
|
* administrative user.
|
|
*/
|
|
public function testEditPageDeniedForUnauthorizedInsideCenter()
|
|
{
|
|
$client = self::createClient([], [
|
|
'PHP_AUTH_USER' => 'center a_administrative',
|
|
'PHP_AUTH_PW' => 'password',
|
|
]);
|
|
|
|
$client->request('GET', $this->editUrl);
|
|
$this->assertEquals(403, $client->getResponse()->getStatusCode());
|
|
}
|
|
|
|
/**
|
|
* Test if the edit page of a given person is not accessible for a user
|
|
* of another center of the person.
|
|
*/
|
|
public function testEditPageDeniedForUnauthorizedOutsideCenter()
|
|
{
|
|
$client = self::createClient([], [
|
|
'PHP_AUTH_USER' => 'center b_social',
|
|
'PHP_AUTH_PW' => 'password',
|
|
]);
|
|
|
|
$client->request('GET', $this->editUrl);
|
|
$this->assertEquals(
|
|
403,
|
|
$client->getResponse()->getStatusCode(),
|
|
'The edit page of a person of a center A must not be accessible for user of center B'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Test the edit page are accessible.
|
|
*/
|
|
public function testEditPageIsSuccessful()
|
|
{
|
|
$this->client->request('GET', $this->editUrl);
|
|
$this->assertTrue(
|
|
$this->client->getResponse()->isSuccessful(),
|
|
'The person edit form is accessible'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Test the edition of a field.
|
|
*
|
|
* Given I fill the field with $value
|
|
* And I submit the form
|
|
* Then I am redirected to the 'general' page
|
|
* And the person is updated in the db
|
|
*
|
|
* @dataProvider validTextFieldsProvider
|
|
*
|
|
* @param string $field
|
|
* @param string $value
|
|
*/
|
|
public function testEditTextField($field, $value, Closure $callback)
|
|
{
|
|
$crawler = $this->client->request('GET', $this->editUrl);
|
|
|
|
$form = $crawler->selectButton('Enregistrer')
|
|
->form();
|
|
//transform countries into value if needed
|
|
switch ($field) {
|
|
case 'nationality':
|
|
case 'countryOfBirth':
|
|
if (false === empty($value)) {
|
|
$country = $this->em->getRepository('ChillMainBundle:Country')
|
|
->findOneByCountryCode($value);
|
|
$transformedValue = $country->getId();
|
|
} else {
|
|
$transformedValue = '';
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
$transformedValue = $value;
|
|
}
|
|
|
|
$form->get('chill_personbundle_person[' . $field . ']')
|
|
->setValue($transformedValue);
|
|
|
|
$this->client->submit($form);
|
|
$this->refreshPerson();
|
|
|
|
$this->assertTrue(
|
|
$this->client->getResponse()->isRedirect($this->viewUrl),
|
|
'the page is redirected to general view'
|
|
);
|
|
$this->assertEquals(
|
|
$value,
|
|
$callback($this->person),
|
|
'the value ' . $field . ' is updated in db'
|
|
);
|
|
|
|
$crawler = $this->client->followRedirect();
|
|
$this->assertGreaterThan(
|
|
0,
|
|
$crawler->filter('.alert-success')->count(),
|
|
'a element .success is shown'
|
|
);
|
|
|
|
if ('birthdate' === $field || 'memo' === $field || 'countryOfBirth' === $field || 'nationality' === $field
|
|
|| 'gender' === $field) {
|
|
// we do not perform test on the web page contents.
|
|
} else {
|
|
$this->assertGreaterThan(0, $crawler->filter('html:contains("' . $value . '")')->count());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test the configurable fields are present.
|
|
*
|
|
* @group configurable_fields
|
|
*/
|
|
public function testHiddenFielsArePresent()
|
|
{
|
|
$crawler = $this->client->request('GET', $this->editUrl);
|
|
$configurables = ['placeOfBirth', 'phonenumber', 'email',
|
|
'countryOfBirth', 'nationality', 'spokenLanguages', 'maritalStatus', ];
|
|
$form = $crawler->selectButton('Enregistrer')->form(); //;
|
|
|
|
foreach ($configurables as $key) {
|
|
$this->assertTrue($form->has('chill_personbundle_person[' . $key . ']'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test tbe detection of invalid data during the update procedure.
|
|
*
|
|
* @dataProvider providesInvalidFieldsValues
|
|
*
|
|
* @param string $field
|
|
* @param string $value
|
|
*/
|
|
public function testInvalidFields($field, $value)
|
|
{
|
|
$crawler = $this->client->request('GET', $this->editUrl);
|
|
|
|
$form = $crawler->selectButton('Enregistrer')
|
|
->form();
|
|
$form->get('chill_personbundle_person[' . $field . ']')
|
|
->setValue($value);
|
|
|
|
$crawler = $this->client->submit($form);
|
|
|
|
$this->assertFalse(
|
|
$this->client->getResponse()->isRedirect(),
|
|
'the page is not redirected to /general'
|
|
);
|
|
$this->assertGreaterThan(
|
|
0,
|
|
$crawler->filter('.alert-danger')->count(),
|
|
'a element .error is shown'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* provide valid values to test, with field name and
|
|
* a function to find the value back from person entity.
|
|
*
|
|
* @return mixed[]
|
|
*/
|
|
public function validTextFieldsProvider()
|
|
{
|
|
return [
|
|
['firstName', 'random Value', static function (Person $person) { return $person->getFirstName(); }],
|
|
['lastName', 'random Value', static function (Person $person) { return $person->getLastName(); }],
|
|
// reminder: this value is capitalized
|
|
['placeOfBirth', 'A PLACE', static function (Person $person) { return $person->getPlaceOfBirth(); }],
|
|
['birthdate', '1980-12-15', static function (Person $person) { return $person->getBirthdate()->format('Y-m-d'); }],
|
|
// TODO test on phonenumber update
|
|
// ['phonenumber', '+32123456789', static function (Person $person) { return $person->getPhonenumber(); }],
|
|
['memo', 'jfkdlmq jkfldmsq jkmfdsq', static function (Person $person) { return $person->getMemo(); }],
|
|
['countryOfBirth', 'BE', static function (Person $person) { return $person->getCountryOfBirth()->getCountryCode(); }],
|
|
['nationality', 'FR', static function (Person $person) { return $person->getNationality()->getCountryCode(); }],
|
|
['placeOfBirth', '', static function (Person $person) { return $person->getPlaceOfBirth(); }],
|
|
['birthdate', '', static function (Person $person) { return $person->getBirthdate(); }],
|
|
//['phonenumber', '', static function (Person $person) { return $person->getPhonenumber(); }],
|
|
['memo', '', static function (Person $person) { return $person->getMemo(); }],
|
|
['countryOfBirth', null, static function (Person $person) { return $person->getCountryOfBirth(); }],
|
|
['nationality', null, static function (Person $person) { return $person->getNationality(); }],
|
|
['gender', Person::FEMALE_GENDER, static function (Person $person) { return $person->getGender(); }],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Reload the person from the db.
|
|
*/
|
|
protected function refreshPerson()
|
|
{
|
|
$this->person = $this->em->getRepository(\Chill\PersonBundle\Entity\Person::class)
|
|
->find($this->person->getId());
|
|
}
|
|
|
|
private function getVeryLongText()
|
|
{
|
|
return <<<'EOT'
|
|
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse molestie at enim id auctor. Vivamus malesuada elit ipsum, ac mollis ex facilisis sit amet. Phasellus accumsan, quam ut aliquet accumsan, augue ligula consequat erat, condimentum iaculis orci magna egestas eros. In vel blandit sapien. Duis ut dui vitae tortor iaculis malesuada vitae vitae lorem. Morbi efficitur dolor orci, a rhoncus urna blandit quis. Aenean at placerat dui, ut tincidunt nulla. In ultricies tempus ligula ac rutrum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Fusce urna nibh, placerat vel auctor sed, maximus quis magna. Vivamus quam ante, consectetur vel feugiat quis, aliquet id ante. Integer gravida erat dignissim ante commodo mollis. Donec imperdiet mauris elit, nec blandit dolor feugiat ut. Proin iaculis enim ut tortor pretium commodo. Etiam aliquet hendrerit dolor sed fringilla. Vestibulum facilisis nibh tincidunt dui egestas, vitae congue mi imperdiet. Duis vulputate ultricies lectus id cursus. Fusce bibendum sem dignissim, bibendum purus quis, mollis ex. Cras ac est justo. Duis congue mattis ipsum, vitae sagittis justo dictum sit amet. Duis aliquam pharetra sem, non laoreet ante laoreet ac. Mauris ornare mi tempus rutrum consequat.
|
|
EOT;
|
|
}
|
|
}
|