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(); } protected function setUp(): void { $this->client = $this->getClientAuthenticated(); } /** * 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' ); $genderType = $form->get(self::GENDER_INPUT); $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(), true), 'gender has "homme" option' ); $this->assertTrue( in_array('woman', $genderType->availableOptionValues(), true), 'gender has "femme" option' ); $this->assertFalse($genderType->hasValue(), 'The gender input is not checked'); return $form; } /** * 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->client; $client->request('GET', '/fr/person/' . $personId . '/general'); $this->assertTrue( $client->getResponse()->isSuccessful(), 'The person view page is accessible at the URL' . '/{_locale}/person/{personID}/general' ); } 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->assertStringContainsString( '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->assertStringContainsString( '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. * * @return string The id of the created person */ public function testValidForm() { $client = $this->client; $crawler = $client->request('GET', '/fr/person/new'); $form = $crawler->selectButton('Ajouter la personne')->form(); $this->fillAValidCreationForm($form); $client = $this->client; $client->submit($form); $this->assertTrue( $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->assertMatchesRegularExpression( '|/fr/person/[1-9][0-9]*/general/edit$|', $client->getHistory()->current()->getUri(), '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 ); return $regexPersonId[1]; } /** * test adding a person with a user with multi center * 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(); // create a very long name to avoid collision $this->fillAValidCreationForm($form, 'Carmela Girdana Assuntamente Castalle', 'rabbit'); $this->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' ); $client->followRedirect(); $this->assertMatchesRegularExpression( '|/fr/person/[1-9][0-9]*/general/edit$|', $client->getHistory()->current()->getUri(), 'a valid form redirect to url /{_locale}/person/{personId}/general/edit' ); } private function fillAValidCreationForm( Form &$creationForm, string $firstname = 'God', string $lastname = 'Jesus' ) { $creationForm->get(self::FIRSTNAME_INPUT)->setValue($firstname . '_' . uniqid()); $creationForm->get(self::LASTNAME_INPUT)->setValue($lastname . '_' . uniqid()); $creationForm->get(self::GENDER_INPUT)->select('man'); $date = new DateTime('1947-02-01'); $creationForm->get(self::BIRTHDATE_INPUT)->setValue($date->format('Y-m-d')); return $creationForm; } }