mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-01-02 15:31:24 +00:00
refactor: Update source code based on PHP conventions.
This commit is contained in:
@@ -1,45 +1,41 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
/**
|
||||
* Chill is a software for social workers.
|
||||
*
|
||||
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*
|
||||
* 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/>.
|
||||
* @see https://www.champs-libres.coop/
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Tests\Controller;
|
||||
|
||||
use DateTime;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\DomCrawler\Form;
|
||||
use Chill\MainBundle\Test\PrepareClientTrait;
|
||||
|
||||
/**
|
||||
* Test creation and deletion for persons
|
||||
* Test creation and deletion for persons.
|
||||
*
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class PersonControllerCreateTest extends WebTestCase
|
||||
final class PersonControllerCreateTest extends WebTestCase
|
||||
{
|
||||
<<<<<<< HEAD
|
||||
use PrepareClientTrait;
|
||||
|
||||
|
||||
const FIRSTNAME_INPUT = 'chill_personbundle_person_creation[firstName]';
|
||||
const LASTNAME_INPUT = "chill_personbundle_person_creation[lastName]";
|
||||
const GENDER_INPUT = "chill_personbundle_person_creation[gender]";
|
||||
const BIRTHDATE_INPUT = "chill_personbundle_person_creation[birthdate]";
|
||||
const CREATEDATE_INPUT = "chill_personbundle_person_creation[creation_date]";
|
||||
const CENTER_INPUT = "chill_personbundle_person_creation[center]";
|
||||
|
||||
|
||||
const LONG_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosq. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta.Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosq.";
|
||||
|
||||
public function setUp()
|
||||
@@ -48,70 +44,93 @@ class PersonControllerCreateTest extends WebTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param Form $creationForm
|
||||
*/
|
||||
private function fillAValidCreationForm(Form &$creationForm,
|
||||
private function fillAValidCreationForm(Form &$creationForm,
|
||||
$firstname = 'God', $lastname = 'Jesus')
|
||||
{
|
||||
$creationForm->get(self::FIRSTNAME_INPUT)->setValue($firstname);
|
||||
$creationForm->get(self::LASTNAME_INPUT)->setValue($lastname);
|
||||
$creationForm->get(self::GENDER_INPUT)->select("man");
|
||||
$date = new \DateTime('1947-02-01');
|
||||
$creationForm->get(self::BIRTHDATE_INPUT)->setValue($date->format('d-m-Y'));
|
||||
|
||||
return $creationForm;
|
||||
self::bootKernel();
|
||||
$em = self::$kernel->getContainer()->get('doctrine.orm.entity_manager');
|
||||
|
||||
//remove two people created during test
|
||||
$jesus = $em->getRepository('ChillPersonBundle:Person')
|
||||
->findOneBy(['firstName' => 'God']);
|
||||
|
||||
if (null !== $jesus) {
|
||||
$em->remove($jesus);
|
||||
}
|
||||
|
||||
$jesus2 = $em->getRepository('ChillPersonBundle:Person')
|
||||
->findOneBy(['firstName' => 'roger']);
|
||||
|
||||
if (null !== $jesus2) {
|
||||
$em->remove($jesus2);
|
||||
}
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test the "add a person" page : test that required elements are present
|
||||
*
|
||||
* Test the "add a person" page : test that required elements are present.
|
||||
*
|
||||
* see https://redmine.champs-libres.coop/projects/chillperson/wiki/Test_plan_for_page_%22add_a_person%22
|
||||
*/
|
||||
public function testAddAPersonPage()
|
||||
{
|
||||
|
||||
$client = $this->client;
|
||||
$crawler = $client->request('GET', '/fr/person/new');
|
||||
|
||||
$this->assertTrue($client->getResponse()->isSuccessful(),
|
||||
"The page is accessible at the URL /{_locale}/person/new");
|
||||
$form = $crawler->selectButton("Ajouter la personne")->form();
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\DomCrawler\Form', $form,
|
||||
'The page contains a butto ');
|
||||
$this->assertTrue($form->has(self::FIRSTNAME_INPUT),
|
||||
'The page contains a "firstname" input');
|
||||
$this->assertTrue($form->has(self::LASTNAME_INPUT),
|
||||
'The page contains a "lastname" input');
|
||||
$this->assertTrue($form->has(self::GENDER_INPUT),
|
||||
'The page contains a "gender" input');
|
||||
$this->assertTrue($form->has(self::BIRTHDATE_INPUT),
|
||||
'The page has a "date of birth" input');
|
||||
$this->assertTrue($form->has(self::CREATEDATE_INPUT),
|
||||
'The page contains a "creation date" input');
|
||||
|
||||
|
||||
self::assertTrue(
|
||||
$client->getResponse()->isSuccessful(),
|
||||
'The page is accessible at the URL /{_locale}/person/new'
|
||||
);
|
||||
$form = $crawler->selectButton('Ajouter la personne')->form();
|
||||
|
||||
self::assertInstanceOf(
|
||||
'Symfony\Component\DomCrawler\Form',
|
||||
$form,
|
||||
'The page contains a butto '
|
||||
);
|
||||
self::assertTrue(
|
||||
$form->has(self::FIRSTNAME_INPUT),
|
||||
'The page contains a "firstname" input'
|
||||
);
|
||||
self::assertTrue(
|
||||
$form->has(self::LASTNAME_INPUT),
|
||||
'The page contains a "lastname" input'
|
||||
);
|
||||
self::assertTrue(
|
||||
$form->has(self::GENDER_INPUT),
|
||||
'The page contains a "gender" input'
|
||||
);
|
||||
self::assertTrue(
|
||||
$form->has(self::BIRTHDATE_INPUT),
|
||||
'The page has a "date of birth" input'
|
||||
);
|
||||
self::assertTrue(
|
||||
$form->has(self::CREATEDATE_INPUT),
|
||||
'The page contains a "creation date" input'
|
||||
);
|
||||
|
||||
$genderType = $form->get(self::GENDER_INPUT);
|
||||
$this->assertEquals('radio', $genderType->getType(),
|
||||
$this->assertEquals('radio', $genderType->getType(),
|
||||
'The gender input has radio buttons');
|
||||
$this->assertEquals(3, count($genderType->availableOptionValues()),
|
||||
'The gender input has three options: man, women and undefined');
|
||||
$this->assertTrue(in_array('man', $genderType->availableOptionValues()),
|
||||
$this->assertTrue(in_array('man', $genderType->availableOptionValues()),
|
||||
'gender has "homme" option');
|
||||
$this->assertTrue(in_array('woman', $genderType->availableOptionValues()),
|
||||
$this->assertTrue(in_array('woman', $genderType->availableOptionValues()),
|
||||
'gender has "femme" option');
|
||||
$this->assertFalse($genderType->hasValue(), 'The gender input is not checked');
|
||||
|
||||
|
||||
$today = new \DateTime();
|
||||
$this->assertEquals($today->format('d-m-Y'), $form->get(self::CREATEDATE_INPUT)
|
||||
->getValue(), 'The creation date input has the current date by default');
|
||||
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Form $form
|
||||
* @depends testAddAPersonPage
|
||||
*/
|
||||
public function testForgedNullGender(Form $form)
|
||||
@@ -123,11 +142,60 @@ class PersonControllerCreateTest extends WebTestCase
|
||||
$this->client->submit($form);
|
||||
$this->assertResponseStatusCodeSame(500);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test if, for a given person if its person view page (at the url
|
||||
* fr/person/$personID/general) is accessible.
|
||||
*
|
||||
* @param int|string $personId The is of the person
|
||||
* @depends testValidForm
|
||||
*/
|
||||
public function testPersonViewAccessible($personId)
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
$client->request('GET', '/fr/person/' . $personId . '/general');
|
||||
|
||||
self::assertTrue(
|
||||
$client->getResponse()->isSuccessful(),
|
||||
'The person view page is accessible at the URL'
|
||||
. '/{_locale}/person/{personID}/general'
|
||||
);
|
||||
}
|
||||
|
||||
public function testReviewExistingDetectionInversedLastNameWithFirstName()
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
|
||||
$crawler = $client->request('GET', '/fr/person/new');
|
||||
|
||||
//test the page is loaded before continuing
|
||||
self::assertTrue($client->getResponse()->isSuccessful());
|
||||
|
||||
$form = $crawler->selectButton('Ajouter la personne')->form();
|
||||
$form = $this->fillAValidCreationForm($form, 'Charline', 'dd');
|
||||
$client->submit($form);
|
||||
|
||||
self::assertContains(
|
||||
'Depardieu',
|
||||
$client->getCrawler()->text(),
|
||||
'check that the page has detected the lastname of a person existing in database'
|
||||
);
|
||||
|
||||
//inversion
|
||||
$form = $crawler->selectButton('Ajouter la personne')->form();
|
||||
$form = $this->fillAValidCreationForm($form, 'dd', 'Charline');
|
||||
$client->submit($form);
|
||||
|
||||
self::assertContains(
|
||||
'Depardieu',
|
||||
$client->getCrawler()->text(),
|
||||
'check that the page has detected the lastname of a person existing in database'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the creation of a valid person.
|
||||
*
|
||||
* @param Form $form
|
||||
*
|
||||
* @return string The id of the created person
|
||||
* @depends testAddAPersonPage
|
||||
*/
|
||||
@@ -136,19 +204,24 @@ class PersonControllerCreateTest extends WebTestCase
|
||||
$this->fillAValidCreationForm($form);
|
||||
$client = $this->client;
|
||||
$client->submit($form);
|
||||
|
||||
$this->assertTrue((bool)$client->getResponse()->isRedirect(),
|
||||
"a valid form redirect to url /{_locale}/person/{personId}/general/edit");
|
||||
$client->followRedirect();
|
||||
|
||||
|
||||
// visualize regexp here : http://jex.im/regulex/#!embed=false&flags=&re=%2Ffr%2Fperson%2F[1-9][0-9]*%2Fgeneral%2Fedit%24
|
||||
$this->assertRegExp('|/fr/person/[1-9][0-9]*/general/edit$|',
|
||||
self::assertRegExp(
|
||||
'|/fr/person/[1-9][0-9]*/general/edit$|',
|
||||
$client->getHistory()->current()->getUri(),
|
||||
"a valid form redirect to url /{_locale}/person/{personId}/general/edit");
|
||||
'a valid form redirect to url /{_locale}/person/{personId}/general/edit'
|
||||
);
|
||||
|
||||
$regexPersonId = null;
|
||||
preg_match("/person\/([1-9][0-9]*)\/general\/edit$/",
|
||||
$client->getHistory()->current()->getUri(), $regexPersonId);
|
||||
preg_match(
|
||||
'/person\\/([1-9][0-9]*)\\/general\\/edit$/',
|
||||
$client->getHistory()->current()->getUri(),
|
||||
$regexPersonId
|
||||
);
|
||||
|
||||
return $regexPersonId[1];
|
||||
}
|
||||
|
||||
@@ -164,87 +237,89 @@ class PersonControllerCreateTest extends WebTestCase
|
||||
$client = $this->client;
|
||||
$client->request('GET', '/fr/person/'.$personId.'/general');
|
||||
|
||||
$this->assertTrue($client->getResponse()->isSuccessful(),
|
||||
$this->assertTrue($client->getResponse()->isSuccessful(),
|
||||
"The person view page is accessible at the URL"
|
||||
. "/{_locale}/person/{personID}/general");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* test adding a person with a user with multi center
|
||||
* is valid
|
||||
* is valid.
|
||||
*/
|
||||
public function testValidFormWithMultiCenterUser()
|
||||
{
|
||||
$client = $this->getClientAuthenticated('multi_center');
|
||||
|
||||
|
||||
$crawler = $client->request('GET', '/fr/person/new');
|
||||
|
||||
$this->assertTrue($client->getResponse()->isSuccessful(),
|
||||
"The page is accessible at the URL /{_locale}/person/new");
|
||||
$form = $crawler->selectButton("Ajouter la personne")->form();
|
||||
|
||||
|
||||
self::assertTrue(
|
||||
$client->getResponse()->isSuccessful(),
|
||||
'The page is accessible at the URL /{_locale}/person/new'
|
||||
);
|
||||
$form = $crawler->selectButton('Ajouter la personne')->form();
|
||||
|
||||
$this->fillAValidCreationForm($form, 'roger', 'rabbit');
|
||||
|
||||
$this->assertTrue($form->has(self::CENTER_INPUT),
|
||||
'The page contains a "center" input');
|
||||
|
||||
self::assertTrue(
|
||||
$form->has(self::CENTER_INPUT),
|
||||
'The page contains a "center" input'
|
||||
);
|
||||
$centerInput = $form->get(self::CENTER_INPUT);
|
||||
$availableValues = $centerInput->availableOptionValues();
|
||||
$lastCenterInputValue = end($availableValues);
|
||||
$centerInput->setValue($lastCenterInputValue);
|
||||
|
||||
|
||||
$client->submit($form);
|
||||
|
||||
$this->assertTrue($client->getResponse()->isRedirect(),
|
||||
"a valid form redirect to url /{_locale}/person/{personId}/general/edit");
|
||||
|
||||
self::assertTrue(
|
||||
$client->getResponse()->isRedirect(),
|
||||
'a valid form redirect to url /{_locale}/person/{personId}/general/edit'
|
||||
);
|
||||
$client->followRedirect();
|
||||
$this->assertRegExp('|/fr/person/[1-9][0-9]*/general/edit$|',
|
||||
$client->getHistory()->current()->getUri(),
|
||||
"a valid form redirect to url /{_locale}/person/{personId}/general/edit");
|
||||
|
||||
self::assertRegExp(
|
||||
'|/fr/person/[1-9][0-9]*/general/edit$|',
|
||||
$client->getHistory()->current()->getUri(),
|
||||
'a valid form redirect to url /{_locale}/person/{personId}/general/edit'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testReviewExistingDetectionInversedLastNameWithFirstName()
|
||||
{
|
||||
$client = $this->client;
|
||||
|
||||
|
||||
$crawler = $client->request('GET', '/fr/person/new');
|
||||
|
||||
|
||||
//test the page is loaded before continuing
|
||||
$this->assertTrue($client->getResponse()->isSuccessful());
|
||||
|
||||
|
||||
$form = $crawler->selectButton("Ajouter la personne")->form();
|
||||
$form = $this->fillAValidCreationForm($form, 'Charline', 'dd');
|
||||
$client->submit($form);
|
||||
|
||||
$this->assertContains('Depardieu', $client->getCrawler()->text(),
|
||||
|
||||
$this->assertContains('Depardieu', $client->getCrawler()->text(),
|
||||
"check that the page has detected the lastname of a person existing in database");
|
||||
|
||||
|
||||
//inversion
|
||||
$form = $crawler->selectButton("Ajouter la personne")->form();
|
||||
$form = $this->fillAValidCreationForm($form, 'dd', 'Charline');
|
||||
$client->submit($form);
|
||||
|
||||
$this->assertContains('Depardieu', $client->getCrawler()->text(),
|
||||
|
||||
$this->assertContains('Depardieu', $client->getCrawler()->text(),
|
||||
"check that the page has detected the lastname of a person existing in database");
|
||||
}
|
||||
|
||||
public static function tearDownAfterClass()
|
||||
|
||||
/**
|
||||
* return an authenticated client, useful for submitting form.
|
||||
*
|
||||
* @param mixed $username
|
||||
*
|
||||
* @return \Symfony\Component\BrowserKit\Client
|
||||
*/
|
||||
private function getAuthenticatedClient($username = 'center a_social')
|
||||
{
|
||||
static::bootKernel();
|
||||
$em = static::$kernel->getContainer()->get('doctrine.orm.entity_manager');
|
||||
|
||||
//remove two people created during test
|
||||
$jesus = $em->getRepository('ChillPersonBundle:Person')
|
||||
->findOneBy(array('firstName' => 'God'));
|
||||
if ($jesus !== NULL) {
|
||||
$em->remove($jesus);
|
||||
}
|
||||
|
||||
$jesus2 = $em->getRepository('ChillPersonBundle:Person')
|
||||
->findOneBy(array('firstName' => 'roger'));
|
||||
if ($jesus2 !== NULL) {
|
||||
$em->remove($jesus2);
|
||||
}
|
||||
$em->flush();
|
||||
return self::createClient([], [
|
||||
'PHP_AUTH_USER' => $username,
|
||||
'PHP_AUTH_PW' => 'password',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user