diff --git a/Controller/HistoryController.php b/Controller/HistoryController.php index d73076f95..29a858c05 100644 --- a/Controller/HistoryController.php +++ b/Controller/HistoryController.php @@ -208,6 +208,11 @@ class HistoryController extends Controller $this->get('session')->getFlashBag() ->add('danger', $this->get('translator') ->trans('History closing form is not valide')); + + foreach ($form->getErrors() as $error) { + $this->get('session')->getFlashBag() + ->add('info', $error->getMessage()); + } } } diff --git a/Entity/Person.php b/Entity/Person.php index ad7b6e6cd..470a293bc 100644 --- a/Entity/Person.php +++ b/Entity/Person.php @@ -133,6 +133,10 @@ class Person { $this->history->add($history); } + public function removeHistoryFile(PersonHistoryFile $history) { + $this->history->remove($history); + } + /** * set the Person file as open at the given date. * diff --git a/Form/PersonHistoryFileType.php b/Form/PersonHistoryFileType.php index 4274d4800..3f57e4b0d 100644 --- a/Form/PersonHistoryFileType.php +++ b/Form/PersonHistoryFileType.php @@ -22,8 +22,11 @@ class PersonHistoryFileType extends AbstractType //if the period_action is close, date opening should not be seen if ($options['period_action'] !== 'close') { $builder - ->add('date_opening', 'date', array("required" => true, - 'widget' => 'single_text')); + ->add('date_opening', 'date', array( + "required" => true, + 'widget' => 'single_text', + 'format' => 'dd-MM-yyyy' + )); } // the closingDate should be seen only if period_action = close @@ -41,7 +44,7 @@ class PersonHistoryFileType extends AbstractType ($options['period_action'] === 'update' AND !$accompanyingPeriod->isOpen()) ){ $form->add('date_closing', 'date', array('required' => true, - 'widget' => 'single_text')); + 'widget' => 'single_text', 'format' => 'dd-MM-yyyy')); $form->add('closingMotive', 'closing_motive'); } }); diff --git a/Resources/views/History/form.html.twig b/Resources/views/History/form.html.twig index 0fdaa7c9c..9b7dbb7fb 100644 --- a/Resources/views/History/form.html.twig +++ b/Resources/views/History/form.html.twig @@ -25,7 +25,7 @@ {{ form_rest(form) }}
- +
{{ form_end(form) }} diff --git a/Tests/Controller/AccompanyingPeriodControllerTest.php b/Tests/Controller/AccompanyingPeriodControllerTest.php new file mode 100644 index 000000000..07b489fd4 --- /dev/null +++ b/Tests/Controller/AccompanyingPeriodControllerTest.php @@ -0,0 +1,197 @@ + + * + * 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 . + */ + +namespace Chill\PersonBundle\Tests\Controller; + +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Chill\PersonBundle\Entity\PersonHistoryFile as AccompanyingPeriod; +use Chill\PersonBundle\Entity\Person; +use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive; + +/** + * Test the creation or deletion of accompanying periods + * + * @author Julien Fastré + */ +class AccompanyingPeriodControllerTest extends WebTestCase +{ + /** + * + * @var \Symfony\Component\BrowserKit\Client + */ + private $client; + + /** + * + * @var Person + */ + private $person; + + /** + * + * @var \Doctrine\ORM\EntityManagerInterface + */ + private static $em; + + const OPENING_INPUT = 'cl_chill_personbundle_personhistoryfile[date_opening]'; + const CLOSING_INPUT = 'cl_chill_personbundle_personhistoryfile[date_closing]'; + const CLOSING_MOTIVE_INPUT = 'cl_chill_personbundle_personhistoryfile[closingMotive]'; + + public static function setUpBeforeClass() + { + static::bootKernel(); + + static::$em = static::$kernel->getContainer() + ->get('doctrine.orm.entity_manager'); + } + + public function setUp() + { + $this->client = static::createClient(array(), array( + 'PHP_AUTH_USER' => 'center a_social', + 'PHP_AUTH_PW' => 'password', + )); + + $this->person = (new Person(new \DateTime('2015-01-05'))) + ->setFirstName('Roland') + ->setLastName('Gallorime') + ->setGenre(Person::GENRE_MAN) + ; + + //remove Accompanying periods + $this->person->close($this->person->getCurrentHistory() + ->setDateClosing(new \DateTime('2015-01-05'))); + foreach($this->person->getHistories() as $accompanyingPeriod) { + $this->person->removeHistoryFile($accompanyingPeriod); + } + static::$em->persist($this->person); + static::$em->flush(); + } + + public function tearDown() + { + //static::$em->refresh($this->person); + //static::$em->remove($this->person); + + //static::$em->flush(); + } + + private function generatePeriods(array $periods) + { + foreach ($periods as $periodDef) { + $period = new AccompanyingPeriod(new \DateTime($periodDef['openingDate'])); + var_dump((new \DateTime($periodDef['openingDate']))->format('d-m-Y')); + if (array_key_exists('closingDate', $periodDef)) { + if (!array_key_exists('closingMotive', $periodDef)) { + throw new \LogicalException('you must define a closing ' + . 'motive into your periods fixtures'); + } + + $period->setDateClosing(new \DateTime($periodDef['closingDate'])) + ->setClosingMotive($periodDef['closingMotive']); + } + + $this->person->addHistoryFile($period); + + static::$em->persist($period); + + } + + static::$em->flush(); + } + + private function getLastValueOnClosingMotive(\Symfony\Component\DomCrawler\Form $form) + { + return end($form->get(self::CLOSING_MOTIVE_INPUT) + ->availableOptionValues()); + } + + /** + * Test the closing of a periods + * + * Given that a person as an accompanying period opened since 2015-01-05 + * and we fill the close form (at /en/person/[id]/history/close + * with : dateClosing: 2015-02-01 + * with : the last closing motive in list + * Then the response should redirect to history view + * And the next page should have a `.error` element present in page + * + * @todo + */ + public function testClosingCurrentPeriod() + { + + $this->markTestSkipped('not implemented fully'); + + $this->generatePeriods(array( [ + 'openingDate' => '2015-01-05' + ] + )); + + $crawler = $this->client->request('GET', '/en/person/' + .$this->person->getId().'/history/close'); + + $form = $crawler->selectButton('Submit')->form(); + + $form->get(self::CLOSING_MOTIVE_INPUT) + ->setValue($this->getLastValueOnClosingMotive($form)); + $form->get(self::CLOSING_INPUT) + ->setValue((new \DateTime('2015-02-01'))->format('d-m-Y')); + var_dump((new \DateTime('2015-02-01'))->format('d-m-Y')); + $cr = $this->client->submit($form); + var_dump($cr->text()); + $this->assertTrue($this->client->getResponse()->isRedirect( + '/en/person/'.$this->person->getId().'/history'), + 'the server redirects to /history page'); + $this->assertGreaterThan(0, $this->client->followRedirect() + ->filter('.success')->count(), + "a 'success' message is shown"); + } + + + public function testAddNewPeriodBeforeActual() + { + $this->generatePeriods(array( + [ + 'openingDate' => '2015-01-25' + ] + )); + + $crawler = $this->client->request('GET', '/en/person/' + .$this->person->getId().'/history/create'); + + $form = $crawler->selectButton('Submit')->form();; + $form->get(self::CLOSING_MOTIVE_INPUT) + ->setValue($this->getLastValueOnClosingMotive($form)); + $form->get(self::CLOSING_INPUT) + ->setValue('31-12-2014'); + $form->get(self::OPENING_INPUT) + ->setValue('01-01-2014'); + + $this->client->submit($form); + + $this->assertTrue($this->client->getResponse()->isRedirect( + '/en/person/'.$this->person->getId().'/history'), + 'the server redirects to /history page'); + $this->assertGreaterThan(0, $this->client->followRedirect() + ->filter('.success')->count(), + "a 'success' message is shown"); + } +}