add new / create address + tests

This commit is contained in:
Julien Fastré 2016-03-13 22:14:40 +01:00
parent 545e155334
commit ff5bda12b6
10 changed files with 477 additions and 17 deletions

View File

@ -37,6 +37,92 @@ use Symfony\Component\HttpFoundation\Request;
*/
class PersonAddressController extends Controller
{
public function listAction($person_id)
{
$person = $this->getDoctrine()->getManager()
->getRepository('ChillPersonBundle:Person')
->find($person_id);
if ($person === NULL) {
throw $this->createNotFoundException("Person with id $person_id not"
. " found ");
}
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person,
"You are not allowed to edit this person.");
return $this->render('ChillPersonBundle:Address:list.html.twig', array(
'person' => $person
));
}
public function newAction($person_id)
{
$person = $this->getDoctrine()->getManager()
->getRepository('ChillPersonBundle:Person')
->find($person_id);
if ($person === NULL) {
throw $this->createNotFoundException("Person with id $person_id not"
. " found ");
}
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
"You are not allowed to edit this person.");
$address = new Address();
$form = $this->createCreateForm($person, $address);
return $this->render('ChillPersonBundle:Address:new.html.twig', array(
'person' => $person,
'form' => $form->createView()
));
}
public function createAction($person_id, Request $request)
{
$person = $this->getDoctrine()->getManager()
->getRepository('ChillPersonBundle:Person')
->find($person_id);
if ($person === NULL) {
throw $this->createNotFoundException("Person with id $person_id not"
. " found ");
}
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
"You are not allowed to edit this person.");
$address = new Address();
$form = $this->createCreateForm($person, $address);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$address = $form->getData();
$person->addAddress($address);
$em = $this->getDoctrine()->getManager();
$em->flush();
$this->addFlash('success',
$this->get('translator')->trans('The new address was created successfully')
);
return $this->redirectToRoute('chill_person_address_list', array(
'person_id' => $person->getId()
));
}
return $this->render('ChillPersonBundle:Address:new.html.twig', array(
'person' => $person,
'form' => $form->createView()
));
}
public function editAction($person_id, $address_id)
{
$person = $this->getDoctrine()->getManager()
@ -91,7 +177,7 @@ class PersonAddressController extends Controller
$this->addFlash('success', $this->get('translator')->trans(
"The address has been successfully updated"));
return $this->redirectToRoute('chill_person_view', array(
return $this->redirectToRoute('chill_person_address_list', array(
'person_id' => $person->getId()
));
}
@ -126,6 +212,28 @@ class PersonAddressController extends Controller
return $form;
}
/**
*
* @param Person $person
* @param Address $address
* @return \Symfony\Component\Form\Form
*/
protected function createCreateForm(Person $person, Address $address)
{
$form = $this->createForm(AddressType::class, $address, array(
'method' => 'POST',
'action' => $this->generateUrl('chill_person_address_create', array(
'person_id' => $person->getId()
))
));
$form->add('submit', 'submit', array(
'label' => 'Submit'
));
return $form;
}
/**
*
* @param Person $person

View File

@ -619,8 +619,10 @@ class Person implements HasCenterInterface {
}
/**
* By default, the addresses are ordered by date, descending (the most
* recent first)
*
* @return \Chill\MainBundle\Entity\Address[]@return Address[]
* @return \Chill\MainBundle\Entity\Address[]
*/
public function getAddresses()
{
@ -633,21 +635,14 @@ class Person implements HasCenterInterface {
$date = new \DateTime('now');
}
$lastAddress = null;
$addresses = $this->getAddresses();
foreach ($this->getAddresses() as $address) {
if ($address->getValidFrom() < $date) {
if ($lastAddress === NULL) {
$lastAddress = $address;
} else {
if ($lastAddress->getValidFrom() < $address->getValidFrom()) {
$lastAddress = $address;
}
}
}
if ($addresses == null) {
return null;
}
return $lastAddress;
return $addresses->first();
}
/**

View File

@ -74,6 +74,7 @@ Chill\PersonBundle\Entity\Person:
referencedColumnName: id
addresses:
targetEntity: Chill\MainBundle\Entity\Address
orderBy: { 'validFrom': 'DESC' }
joinTable:
name: chill_person_persons_to_addresses
cascade: [persist, remove, merge, detach]

View File

@ -70,6 +70,19 @@ chill_person_accompanying_period_open:
path: /{_locale}/person/{person_id}/accompanying-period/open
defaults: { _controller: ChillPersonBundle:AccompanyingPeriod:open }
chill_person_address_list:
path: /{_locale}/person/{person_id}/address/list
defaults: { _controller: ChillPersonBundle:PersonAddress:list }
chill_person_address_create:
path: /{_locale}/person/{person_id}/address/create
defaults: { _controller: ChillPersonBundle:PersonAddress:create }
methods: [POST]
chill_person_address_new:
path: /{_locale}/person/{person_id}/address/new
defaults: { _controller: ChillPersonBundle:PersonAddress:new }
chill_person_address_edit:
path: /{_locale}/person/{person_id}/address/{address_id}/edit
defaults: { _controller: ChillPersonBundle:PersonAddress:edit }

View File

@ -76,6 +76,12 @@ Since %date%: Depuis le %date%
No address given: Pas d'adresse renseignée
The address has been successfully updated: L'adresse a été mise à jour avec succès
Update address for %name%: Mettre à jour une adresse pour %name%
Addresses'history for %name%: Historique des adresses de %name%
Addresses'history: Historique des adresses
New address for %name% : Nouvelle adresse pour %name%
The new address was created successfully: La nouvelle adresse a été créée
Add an address: Ajouter une adresse
Back to the person details: Retour aux détails de la personne
#timeline
'An accompanying period is opened for %person% on %date%': Une période d'accompagnement a été ouverte le %date% pour %person%

View File

@ -18,10 +18,12 @@
{% set activeRouteKey = '' %}
{% block title %}{{ 'Update address for %name%'|trans({ '%name%': person.firstName|capitalize ~ ' ' ~ person.lastName } )|capitalize }}{% endblock %}
{% block title 'Update address for %name%'|trans({ '%name%': person.firstName ~ ' ' ~ person.lastName } ) %}
{% block personcontent %}
<h1>{{ 'Update address for %name%'|trans({ '%name%': person.firstName ~ ' ' ~ person.lastName } ) }}</h1>
{{ form_start(form) }}
{{ form_row(form.streetAddress1) }}
@ -29,7 +31,16 @@
{{ form_row(form.postCode) }}
{{ form_row(form.validFrom) }}
{{ form_row(form.submit, { 'attr' : { 'class': 'sc-button bt-edit' } } ) }}
<ul class="record_actions">
<li>
<a href="{{ path('chill_person_address_list', { 'person_id' : person.id } ) }}" class="sc-button btn-cancel">
{{ 'Back to the list'|trans }}
</a>
</li>
<li>
{{ form_row(form.submit, { 'attr' : { 'class': 'sc-button bt-edit' } } ) }}
</li>
</ul>
{{ form_end(form) }}

View File

@ -0,0 +1,84 @@
{#
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
*
* 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/>.
#}
{% extends "ChillPersonBundle::layout.html.twig" %}
{% import 'ChillMainBundle:Address:macro.html.twig' as address_macros %}
{% set activeRouteKey = '' %}
{% block title %}{{ 'Addresses\'history for %name%'|trans({ '%name%': person.firstName ~ ' ' ~ person.lastName } ) }}{% endblock %}
{% block personcontent %}
<h1>{{ 'Addresses\'history for %name%'|trans({ '%name%': person.firstName ~ ' ' ~ person.lastName } ) }}</h1>
<table class="records_list">
<thead>
<tr>
<th>{{ 'Valid from'|trans }}</th>
<th>{{ 'Address'|trans }}</th>
<th>&nbsp;</th>
</tr>
</thead>
<tbody>
{% if person.addresses|length == 0 %}
<tr>
<td colspan="3">
<span class="chill-no-data-statement">{{ 'No address given'|trans }}</span>
<a href="{{ path('chill_person_address_new', { 'person_id' : person.id } ) }}">
{{ 'Add an address'|trans }}
</a>
</td>
</tr>
{% else %}
{% for address in person.addresses %}
<tr>
<td><strong>{{ 'Since %date%'|trans( { '%date%' : address.validFrom|localizeddate('long', 'none') } ) }}</strong></td>
<td>
{{ address_macros._render(address, { 'with_valid_from' : false } ) }}
</td>
<td>
<ul class="record_actions">
<li>
<a href="{{ path('chill_person_address_edit', { 'person_id': person.id, 'address_id' : address.id } ) }}" class="sc-button bt-edit">
{{ 'Edit'|trans }}
</a>
</li>
</ul>
</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
<ul class="record_actions">
<li>
<a href="{{ path('chill_person_view', { 'person_id' : person.id } ) }}" class="sc-button btn-cancel">
{{ 'Back to the person details'|trans }}
</a>
</li>
<li>
<a href="{{ path('chill_person_address_new', { 'person_id' : person.id } ) }}" class="sc-button btn-create">
{{ 'Add an address'|trans }}
</a>
</li>
</ul>
{% endblock personcontent %}

View File

@ -0,0 +1,47 @@
{#
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
*
* 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/>.
#}
{% extends "ChillPersonBundle::layout.html.twig" %}
{% set activeRouteKey = '' %}
{% block title %}{{ 'New address for %name%'|trans({ '%name%': person.firstName|capitalize ~ ' ' ~ person.lastName } )|capitalize }}{% endblock %}
{% block personcontent %}
<h1>{{ 'New address for %name%'|trans({ '%name%': person.firstName ~ ' ' ~ person.lastName } ) }}</h1>
{{ form_start(form) }}
{{ form_row(form.streetAddress1) }}
{{ form_row(form.streetAddress2) }}
{{ form_row(form.postCode) }}
{{ form_row(form.validFrom) }}
<ul class="record_actions">
<li>
<a href="{{ path('chill_person_address_list', { 'person_id' : person.id } ) }}" class="sc-button btn-cancel">
{{ 'Back to the list'|trans }}
</a>
</li>
<li>
{{ form_row(form.submit, { 'attr' : { 'class': 'sc-button bt-create' } } ) }}
</li>
</ul>
{{ form_end(form) }}
{% endblock personcontent %}

View File

@ -175,9 +175,15 @@ This view should receive those arguments:
{{ address._render(person.lastAddress) }}
<a href="{{ path('chill_person_address_edit', { 'person_id': person.id, 'address_id' : person.lastAddress.id } ) }}">
{{ 'Edit'|trans }}
</a><br/>
<a href="{{ path('chill_person_address_list', { 'person_id': person.id } ) }}">
{{ 'Addresses\'history'|trans }}
</a>
{%- else -%}
<span class="chill-no-data-statement">{{ 'No address given'|trans }}</span>
<span class="chill-no-data-statement">{{ 'No address given'|trans }}</span>
<a href="{{ path('chill_person_address_new', { 'person_id' : person.id } ) }}" class="">
{{ 'Add an address'|trans }}
</a>
{%- endif -%}
</dd>
</dl>

View File

@ -0,0 +1,189 @@
<?php
/*
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
*
* 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/>.
*/
namespace Chill\PersonBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Chill\PersonBundle\Entity\Person;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class PersonAddressControllerTest extends WebTestCase
{
/** @var \Doctrine\ORM\EntityManagerInterface The entity manager */
protected $em;
/** @var Person The person on which the test is executed */
protected static $person;
/**
*
* @var \Chill\MainBundle\Entity\PostalCode
*/
protected $postalCode;
/**
*
* @var \Symfony\Component\BrowserKit\Client
*/
protected $client;
public static function setUpBeforeClass()
{
static::bootKernel();
$em = static::$kernel->getContainer()
->get('doctrine.orm.entity_manager');
$center = $em->getRepository('ChillMainBundle:Center')
->findOneBy(array('name' => 'Center A'));
self::$person = (new Person())
->setLastName("Tested person")
->setFirstName("Test")
->setCenter($center)
->setGender(Person::MALE_GENDER);
$em->persist(self::$person);
$em->flush();
}
/**
* Prepare client and create a random person
*/
public function setUp()
{
static::bootKernel();
$this->em = static::$kernel->getContainer()
->get('doctrine.orm.entity_manager');
$this->postalCode = $this->em->getRepository('ChillMainBundle:PostalCode')
->findOneBy(array('code' => 1000));
$this->client = static::createClient(array(), array(
'PHP_AUTH_USER' => 'center a_social',
'PHP_AUTH_PW' => 'password',
));
}
public static function tearDownAfter()
{
$this->refreshPerson();
$this->em->remove(self::$person);
$this->em->flush();
}
/**
* Reload the person from the db
*/
protected function refreshPerson()
{
self::$person = $this->em->getRepository('ChillPersonBundle:Person')
->find(self::$person->getId());
}
public function testEmptyList()
{
$crawler = $this->client->request('GET', '/fr/person/'.
self::$person->getId().'/address/list');
$this->assertTrue($this->client->getResponse()->isSuccessful());
$this->assertEquals(1, $crawler->filter('td:contains("Pas d\'adresse renseignée")')
->count(),
"assert that a message say 'no address given'");
}
/**
* @depends testEmptyList
*/
public function testCreateAddress()
{
$crawler = $this->client->request('GET', '/fr/person/'.
self::$person->getId().'/address/new');
$this->assertTrue($this->client->getResponse()->isSuccessful());
$form = $crawler->selectButton('Envoi')->form(array(
'address[streetAddress1]' => 'Rue de la Paix, 50',
'address[streetAddress2]' => $this->postalCode->getId(),
'address[validFrom]' => '15-01-2016'
));
$this->client->submit($form);
$crawler = $this->client->followRedirect();
$this->assertRegexp('|/fr/person/[0-9]{1,}/address/list|',
$this->client->getHistory()->current()->getUri(),
"assert that the current page is on |/fr/person/[0-9]{1,}/address/list|");
$this->assertEquals(1, $crawler
->filter('div.flash_message.success')
->count(),
"Asserting that the response page contains a success flash message");
$this->assertEquals(1, $crawler
->filter('td:contains("Rue de la Paix, 50")')
->count(),
"Asserting that the page contains the new address");
}
/**
* @depends testCreateAddress
*/
public function testUpdateAddress()
{
$this->refreshPerson();
$address = self::$person->getLastAddress();
$crawler = $this->client->request('GET', '/fr/person/'.self::$person->getId()
.'/address/'.$address->getId().'/edit');
$this->assertTrue($this->client->getResponse()->isSuccessful());
$form = $crawler->selectButton('Envoi')->form(array(
'address[streetAddress1]' => 'Rue du Trou Normand, 15',
'address[validFrom]' => '15-01-2015'
));
$this->client->submit($form);
$crawler = $this->client->followRedirect();
$this->assertRegexp('|/fr/person/[0-9]{1,}/address/list|',
$this->client->getHistory()->current()->getUri(),
"assert that the current page is on |/fr/person/[0-9]{1,}/address/list|");
$this->assertGreaterThan(0, $crawler
->filter('div.flash_message.success')
->count(),
"Asserting that the response page contains a success flash message");
$this->assertEquals(1, $crawler
->filter('td:contains("Rue du Trou Normand")')
->count(),
"Asserting that the page contains the new address");
}
}