diff --git a/Controller/AccompanyingPeriodController.php b/Controller/AccompanyingPeriodController.php index 2af8c11a5..08843c667 100644 --- a/Controller/AccompanyingPeriodController.php +++ b/Controller/AccompanyingPeriodController.php @@ -4,7 +4,7 @@ * Chill is a software for social workers * * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, - * + * , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -40,7 +40,6 @@ class AccompanyingPeriodController extends Controller return $this->render('ChillPersonBundle:AccompanyingPeriod:list.html.twig', array('accompanying_periods' => $person->getAccompanyingPeriodsOrdered(), 'person' => $person)); - } public function createAction($person_id) { @@ -51,40 +50,34 @@ class AccompanyingPeriodController extends Controller } $accompanyingPeriod = new AccompanyingPeriod(new \DateTime()); - $accompanyingPeriod->setPerson($person) - ->setDateOpening(new \DateTime()) - ->setDateClosing(new \DateTime()); + $accompanyingPeriod->setDateClosing(new \DateTime()); + $person->addAccompanyingPeriod( + $accompanyingPeriod); $form = $this->createForm(new AccompanyingPeriodType(), - $accompanyingPeriod, array('period_action' => 'update')); + $accompanyingPeriod, array('period_action' => 'update')); $request = $this->getRequest(); if ($request->getMethod() === 'POST') { - $form->handleRequest($request); - $errors = $this->_validatePerson($person); - $flashBag = $this->get('session')->getFlashBag(); if ($form->isValid(array('Default', 'closed')) - && count($errors) === 0) { + && count($errors) === 0) { + $em = $this->getDoctrine()->getManager(); - $em->persist($accompanyingPeriod); - $em->flush(); - $flashBag->add('success', - $this->get('translator')->trans( - 'Period created!')); + $this->get('translator')->trans( + 'Period created!')); return $this->redirect($this->generateUrl('chill_person_accompanying_period_list', array('person_id' => $person->getId()))); } else { - $flashBag->add('danger', $this->get('translator') ->trans('Error! Period not created!')); @@ -94,8 +87,6 @@ class AccompanyingPeriodController extends Controller } } - - return $this->render('ChillPersonBundle:AccompanyingPeriod:form.html.twig', array( 'form' => $form->createView(), @@ -117,18 +108,13 @@ class AccompanyingPeriodController extends Controller } $person = $accompanyingPeriod->getPerson(); - - $form = $this->createForm(new AccompanyingPeriodType(), - $accompanyingPeriod, array('period_action' => 'update')); - + $form = $this->createForm(new AccompanyingPeriodType(), + $accompanyingPeriod, array('period_action' => 'update')); $request = $this->getRequest(); if ($request->getMethod() === 'POST') { - $form->handleRequest($request); - $errors = $this->_validatePerson($person); - $flashBag = $this->get('session')->getFlashBag(); if ($form->isValid(array('Default', 'closed')) @@ -136,13 +122,12 @@ class AccompanyingPeriodController extends Controller $em->flush(); $flashBag->add('success', - $this->get('translator')->trans( - 'Period updating done')); + $this->get('translator')->trans( + 'Period updating done')); return $this->redirect($this->generateUrl('chill_person_accompanying_period_list', array('person_id' => $person->getId()))); } else { - $flashBag->add('danger', $this->get('translator') ->trans('Error when updating the period')); @@ -152,8 +137,6 @@ class AccompanyingPeriodController extends Controller } } - - return $this->render('ChillPersonBundle:AccompanyingPeriod:form.html.twig', array( 'form' => $form->createView(), @@ -171,9 +154,9 @@ class AccompanyingPeriodController extends Controller if ($person->isOpen() === false) { $this->get('session')->getFlashBag() - ->add('danger', $this->get('translator') - ->trans('Beware period is closed', - array('%name%' => $person->__toString()))); + ->add('danger', $this->get('translator') + ->trans('Beware period is closed', + array('%name%' => $person->__toString()))); return $this->redirect( $this->generateUrl('chill_person_accompanying_period_list', array( @@ -182,7 +165,6 @@ class AccompanyingPeriodController extends Controller } $current = $person->getCurrentAccompanyingPeriod(); - $form = $this->createForm(new AccompanyingPeriodType(), $current, array( 'period_action' => 'close' )); @@ -198,9 +180,9 @@ class AccompanyingPeriodController extends Controller if (count($errors) === 0) { $this->get('session')->getFlashBag() - ->add('success', $this->get('translator') - ->trans('Period closed!', - array('%name%' => $person->__toString()))); + ->add('success', $this->get('translator') + ->trans('Period closed!', + array('%name%' => $person->__toString()))); $this->getDoctrine()->getManager()->flush(); @@ -247,14 +229,14 @@ class AccompanyingPeriodController extends Controller * @return \Symfony\Component\Validator\ConstraintViolationListInterface */ private function _validatePerson(Person $person) { - $errors = $this->get('validator')->validate($person, - array('Default')); - $errors_accompanying_period = $this->get('validator')->validate($person, - array('accompanying_period_consistent')); + $errors = $this->get('validator')->validate($person, + array('Default')); + $errors_accompanying_period = $this->get('validator')->validate($person, + array('accompanying_period_consistent')); foreach($errors_accompanying_period as $error ) { - $errors->add($error); - } + $errors->add($error); + } return $errors; } @@ -270,11 +252,11 @@ class AccompanyingPeriodController extends Controller $request = $this->getRequest(); //in case the person is already open - if ($person->isOpen() === true) { + if ($person->isOpen()) { $this->get('session')->getFlashBag() - ->add('danger', $this->get('translator') - ->trans('Error! Period %name% is not closed ; it can be open', - array('%name%' => $person->__toString()))); + ->add('danger', $this->get('translator') + ->trans('Error! Period %name% is not closed ; it can be open', + array('%name%' => $person->__toString()))); return $this->redirect( $this->generateUrl('chill_person_accompanying_period_list', array( @@ -304,21 +286,19 @@ class AccompanyingPeriodController extends Controller $this->getDoctrine()->getManager()->flush(); return $this->redirect( - $this->generateUrl('chill_person_accompanying_period_list', array( - 'person_id' => $person->getId() - )) - ); + $this->generateUrl('chill_person_accompanying_period_list', array( + 'person_id' => $person->getId() + ))); } else { $this->get('session')->getFlashBag() - ->add('danger', $this->get('translator') - ->trans('Period not opened')); + ->add('danger', $this->get('translator') + ->trans('Period not opened')); foreach ($errors as $error) { $this->get('session')->getFlashBag() ->add('info', $error->getMessage()); } } - } else { // if errors in forms $this->get('session')->getFlashBag() ->add('danger', $this->get('translator') @@ -327,17 +307,14 @@ class AccompanyingPeriodController extends Controller } return $this->render('ChillPersonBundle:AccompanyingPeriod:form.html.twig', - array( - 'form' => $form->createView(), - 'person' => $person, - 'accompanying_period' => $accompanyingPeriod - )); + array('form' => $form->createView(), + 'person' => $person, + 'accompanying_period' => $accompanyingPeriod)); } private function _getPerson($id) { return $this->getDoctrine()->getManager() - ->getRepository('ChillPersonBundle:Person') - ->find($id); + ->getRepository('ChillPersonBundle:Person') + ->find($id); } - } diff --git a/Entity/AccompanyingPeriod.php b/Entity/AccompanyingPeriod.php index 60c0900c1..94c642161 100644 --- a/Entity/AccompanyingPeriod.php +++ b/Entity/AccompanyingPeriod.php @@ -4,7 +4,7 @@ * Chill is a software for social workers * * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, - * + * , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -206,8 +206,6 @@ class AccompanyingPeriod return $this; } - - /// VALIDATION function public function isDateConsistent(ExecutionContextInterface $context) { if ($this->isOpen()) { diff --git a/Entity/Person.php b/Entity/Person.php index 2212ff311..6aee958c2 100644 --- a/Entity/Person.php +++ b/Entity/Person.php @@ -5,8 +5,8 @@ namespace Chill\PersonBundle\Entity; /* * Chill is a software for social workers * - * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, - * + * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, + * , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -567,93 +567,67 @@ class Person { * * This method add violation errors. */ - public function isAccompanyingPeriodValid(ExecutionContextInterface $context) { - $r = $this->checkAccompanyingPeriodIsNotCovering(); + public function isAccompanyingPeriodValid(ExecutionContextInterface $context) + { + $r = $this->checkAccompanyingPeriodsAreNotCollapsing(); if ($r !== true) { - if ($r['result'] === self::ERROR_OPENING_NOT_CLOSED_IS_BEFORE_NEW_LINE) { - $context->addViolationAt('history', - 'Accompanying period not closed is before the new line', - array() ); - return; - } + if ($r['result'] === self::ERROR_PERIODS_ARE_COLLAPSING) { + $context->addViolationAt('accompanyingPeriods', + 'Two accompanying periods have days in commun', + array()); + } - $context->addViolationAt('history', 'Periods are collapsing', - array( - '%dateOpening%' => $r['dateOpening']->format('d-m-Y'), - '%dateClosing%' => $r['dateClosing']->format('d-m-Y'), - '%date%' => $r['date']->format('d-m-Y') - ) - ); + if ($r['result'] === self::ERROR_ADDIND_PERIOD_AFTER_AN_OPEN_PERIOD) { + $context->addViolationAt('accompanyingPeriods', + 'A period is opened and a period is added after it', + array()); + } } } - const ERROR_OPENING_IS_INSIDE_CLOSING = 1; - const ERROR_OPENING_NOT_CLOSED_IS_BEFORE_NEW_LINE = 2; - const ERROR_OPENING_NOT_CLOSE_IS_INSIDE_CLOSED_ACCOMPANYING_PERIOD_LINE = 3; - const ERROR_OPENING_IS_BEFORE_OTHER_LINE_AND_CLOSED_IS_AFTER_THIS_LINE = 4; + const ERROR_PERIODS_ARE_COLLAPSING = 1; // when two different periods + // have days in commun + const ERROR_ADDIND_PERIOD_AFTER_AN_OPEN_PERIOD = 2; // where there exist + // a period opened and another one after it - public function checkAccompanyingPeriodIsNotCovering() - { + /** + * Function used for validation that check if the accompanying periods of + * the person are not collapsing (i.e. have not shared days) or having + * a period after an open period. + * + * @return true | array True if the accompanying periods are not collapsing, + * an array with data for displaying the error + */ + public function checkAccompanyingPeriodsAreNotCollapsing() + { $periods = $this->getAccompanyingPeriodsOrdered(); - - foreach ($periods as $key => $period) { - //accompanying period is open : we must check the arent any period after - if ($period->isOpen()) { - foreach ($periods as $subKey => $against) { - //if we are checking the same, continue - if ($key === $subKey) { - continue; - } - - if ($period->getDateOpening() > $against->getDateOpening() - && $period->getDateOpening() < $against->getDateOpening()) { - // the period date opening is inside another opening line - return array( - 'result' => self::ERROR_OPENING_NOT_CLOSE_IS_INSIDE_CLOSED_ACCOMPANYING_PERIOD_LINE, - 'dateOpening' => $against->getDateOpening(), - 'dateClosing' => $against->getDateClosing(), - 'date' => $period->getDateOpening() - ); - } - - if ($period->getDateOpening() < $against->getDateOpening() - && $period->getDateClosing() > $against->getDateClosing()) { - // the period date opening is inside another opening line - return array( - 'result' => self::ERROR_OPENING_IS_BEFORE_OTHER_LINE_AND_CLOSED_IS_AFTER_THIS_LINE, - 'dateOpening' => $against->getDateOpening(), - 'dateClosing' => $against->getDateClosing(), - 'date' => $period->getDateOpening() - ); - } - - //if we have an aopening later... - if ($period->getDateOpening() < $against->getDateClosing()) { - return array( 'result' => self::ERROR_OPENING_NOT_CLOSED_IS_BEFORE_NEW_LINE, - 'dateOpening' => $against->getDateOpening(), - 'dateClosing' => $against->getDateClosing(), - 'date' => $period->getDateOpening() - ); - } - } - } else { - //we must check there is not covering lines - - foreach ($periods as $subKey => $against) { - //check if dateOpening is inside an `against` line - if ($period->getDateOpening() > $against->getDateOpening() - && $period->getDateOpening() < $against->getDateClosing()) { - return array( - 'result' => self::ERROR_OPENING_IS_INSIDE_CLOSING, - 'dateOpening' => $against->getDateOpening(), - 'dateClosing' => $against->getDateClosing(), - 'date' => $period->getDateOpening() - ); - } - } + $periodsNbr = sizeof($periods); + $i = 0; + + while($i < $periodsNbr - 1) { + $periodI = $periods[$i]; + $periodAfterI = $periods[$i + 1]; + + if($periodI->isOpen()) { + return array( + 'result' => self::ERROR_ADDIND_PERIOD_AFTER_AN_OPEN_PERIOD, + 'dateOpening' => $periodAfterI->getDateOpening(), + 'dateClosing' => $periodAfterI->getDateClosing(), + 'date' => $periodI->getDateOpening() + ); + } elseif ($periodI->getDateClosing() >= $periodAfterI->getDateOpening()) { + return array( + 'result' => self::ERROR_PERIODS_ARE_COLLAPSING, + 'dateOpening' => $periodI->getDateOpening(), + + 'dateClosing' => $periodI->getDateClosing(), + 'date' => $periodAfterI->getDateOpening() + ); } + $i++; } + return true; } } \ No newline at end of file diff --git a/Tests/Controller/AccompanyingPeriodControllerTest.php b/Tests/Controller/AccompanyingPeriodControllerTest.php index b95efad1b..fedd7d4c1 100644 --- a/Tests/Controller/AccompanyingPeriodControllerTest.php +++ b/Tests/Controller/AccompanyingPeriodControllerTest.php @@ -29,8 +29,9 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive; /** * Test the creation or deletion of accompanying periods - * - * @author Julien Fastré + * + * The person on which the test is done has a (current) period opened (and not + * closed) starting the 2015-01-05. */ class AccompanyingPeriodControllerTest extends WebTestCase { @@ -56,14 +57,19 @@ class AccompanyingPeriodControllerTest extends WebTestCase const CLOSING_INPUT = 'chill_personbundle_accompanyingperiod[date_closing]'; const CLOSING_MOTIVE_INPUT = 'chill_personbundle_accompanyingperiod[closingMotive]'; + /** + * Setup before the first test of this class (see phpunit doc) + */ public static function setUpBeforeClass() { static::bootKernel(); - static::$em = static::$kernel->getContainer() ->get('doctrine.orm.entity_manager'); } + /** + * Setup before each test method (see phpunit doc) + */ public function setUp() { $this->client = static::createClient(array(), array( @@ -74,13 +80,15 @@ class AccompanyingPeriodControllerTest extends WebTestCase $this->person = (new Person(new \DateTime('2015-01-05'))) ->setFirstName('Roland') ->setLastName('Gallorime') - ->setGenre(Person::GENRE_MAN) - ; + ->setGenre(Person::GENRE_MAN); static::$em->persist($this->person); static::$em->flush(); } + /** + * TearDown after each test method (see phpunit doc) + */ public function tearDown() { static::$em->refresh($this->person); @@ -207,7 +215,6 @@ class AccompanyingPeriodControllerTest extends WebTestCase */ public function testAddNewPeriodBeforeActual() { - $crawler = $this->client->request('GET', '/en/person/' .$this->person->getId().'/accompanying-period/create'); @@ -244,8 +251,6 @@ class AccompanyingPeriodControllerTest extends WebTestCase */ public function testCreatePeriodWithClosingAfterCurrentFails() { - $this->markTestSkipped('this feature is not yet implemented'); - $crawler = $this->client->request('GET', '/en/person/' .$this->person->getId().'/accompanying-period/create'); @@ -280,8 +285,6 @@ class AccompanyingPeriodControllerTest extends WebTestCase */ public function testCreatePeriodWithOpeningAndClosingAfterCurrentFails() { - $this->markTestSkipped('this feature is not yet implemented'); - $crawler = $this->client->request('GET', '/en/person/' .$this->person->getId().'/accompanying-period/create'); diff --git a/Tests/Entity/PersonTest.php b/Tests/Entity/PersonTest.php index 23a3a0cc5..0e7ef97c5 100644 --- a/Tests/Entity/PersonTest.php +++ b/Tests/Entity/PersonTest.php @@ -126,9 +126,9 @@ class PersonTest extends \PHPUnit_Framework_TestCase $period = $p->getCurrentAccompanyingPeriod()->setDateClosing($g); $p->close($period); - $r = $p->checkAccompanyingPeriodIsNotCovering(); + $r = $p->checkAccompanyingPeriodsAreNotCollapsing(); - $this->assertEquals($r['result'], Person::ERROR_OPENING_IS_INSIDE_CLOSING); + $this->assertEquals($r['result'], Person::ERROR_PERIODS_ARE_COLLAPSING); } /** @@ -147,8 +147,8 @@ class PersonTest extends \PHPUnit_Framework_TestCase $f = new \DateTime("2013/1/1"); $p->open(new AccompanyingPeriod($f)); - $r = $p->checkAccompanyingPeriodIsNotCovering(); + $r = $p->checkAccompanyingPeriodsAreNotCollapsing(); - $this->assertEquals($r['result'], Person::ERROR_OPENING_NOT_CLOSED_IS_BEFORE_NEW_LINE); + $this->assertEquals($r['result'], Person::ERROR_ADDIND_PERIOD_AFTER_AN_OPEN_PERIOD); } }