chill-bundles/Tests/Controller/AccompanyingPeriodControllerTest.php
Julien Fastré 513d9aefbc Implement user right refs #263
Merge of branch add_acl

Squashed commit of the following:

commit a3265a71014fc74bcc456aa9436494fc4ed434ea
Merge: 2522634 b231b14
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Tue Jun 30 09:58:23 2015 +0200

    Merge remote-tracking branch 'origin/master' into add_acl

    in order to future merging of add_acl into master

    Conflicts:
    	Tests/Controller/PersonControllerCreateTest.php

commit 252263400b1fb54091a0cd36d9ab84812c3a9942
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Mon Jun 29 23:00:25 2015 +0200

    check acl on timeline page

commit 5f08806585ce4ce1d58c6c507b191221b924d861
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 22:24:18 2015 +0200

    remove dead code

commit 860d62d9c0d9db2186eee725e7ef553ddf1f98f3
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 22:19:02 2015 +0200

    add access condition for link to person view in person view

commit e7b295000dd420309288e83920f6656679d36173
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 22:13:04 2015 +0200

    add ACL on person view

commit 7b1f90a2a28c1aec5ee0eb78206c75a9f1e09c60
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 21:47:49 2015 +0200

    add autorization for filtering person results in search

commit c6669164e2e36b555bed0745d5c7c12b53468b97
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 21:47:26 2015 +0200

    add fixtures for roles defined by person bundle

commit 8f30f5222efe531fb77a2e22b6af8069c5814d00
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 20:13:17 2015 +0200

    [transfer purpose][ci-skip]

commit e8ca4ce4842e619342a90d4343b725c2946f7d70
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 17:31:27 2015 +0200

    should fix reference problem

commit 8c32524a0fd61d504e9640705b97d10bd9b2b993
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 17:29:48 2015 +0200

    fix test about inheritance

commit d93cef2c596aba7fc977cc8db354241d6a77e6e2
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Thu Jun 18 21:38:59 2015 +0200

    add role hierarchy + test

commit 5066cc5318d67f20e3cff9f91f6d2b6a94d5f9c1
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Thu Jun 18 01:28:40 2015 +0200

    add tests for person voter

commit cf3359b11b16972db3c55102a3c8b2aae2c27789
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Thu Jun 18 01:07:30 2015 +0200

    fix "pass by reference" error

commit 94942eb34330813a10124ba58ec814ca685930c5
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Wed Jun 17 22:04:49 2015 +0200

    fix syntax error ($this->$is not correct)

commit 82d25df6b0e21b4032f4fc82dee203ccd1a067dd
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Wed Jun 17 21:51:41 2015 +0200

    add first test for person voter

commit 84e0d05be07477175db1ef95f3716a3c420291ce
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Wed Jun 17 21:50:06 2015 +0200

    define person voter as a service + CHILL_PERSON_SEE

commit bd55e505a179b0df4133903f766d9a8feb469900
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Tue Jun 16 20:57:25 2015 +0200

    add test for creating person with user multi center

commit d29147c8e70b2ae8313d973fe4b3d45d3f064d9b
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Mon Jun 15 09:11:56 2015 +0200

    fix tests - add center to persons

    since a center is associated with center, people used in tests must be associated with a center.
    => associate a center to people used in tests

commit cfac1ea68aaf615b5f8e9e67388f5407e5095d72
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Mon Jun 15 09:06:24 2015 +0200

    remove errors on deprecation

commit e1df87df87e831f3827721313603bcf41b3da891
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 12 22:31:03 2015 +0200

    fix migration error

commit 733fa96a2d874b84d2a0ece6664e4a64eed10b4d
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Wed Jun 10 21:17:15 2015 +0200

    add center type on creation forms

    tests are not adapted yet [ci skip]

commit c0d1d1b1c8fd58d9c15bf59d66de28a848df6d35
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Tue Jun 9 23:11:07 2015 +0200

    fix bug in migration

commit 693abfb7253dfe955beb63de3aebc05a15363cdb
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Tue Jun 9 22:41:21 2015 +0200

    add checks on migration

    check that there are data or not in database and, if there are data,
    create default center if required.

    If not required, the default center is the first in the list.

commit 8127ea8d49e094c81ce65e838416953bc8d47678
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Mon Jun 8 00:33:39 2015 +0200

    add first impl of PersonVoter (not tested) [ci skip]

commit 2ea674dbcf8848603f0fefa109abf2412722013f
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Mon Jun 8 00:32:46 2015 +0200

    add center to person

commit 0b1e3f137ace9eeb959b80cdbdfa3636c75af715
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Sat Jun 6 11:02:49 2015 +0200

    add phpunit bridge to handle deprecation msg

commit 70a4fd16181badad8ac9b87fd24e1000c7b1a734
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 5 12:42:01 2015 +0200

    remove deprecdation warning in phpunit

commit b3b8d1deefbdc72505606df7ca217771277e3555
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 5 11:41:36 2015 +0200

    fix deprecation of pattern in routing

    pattern => path in routing.yml

commit eea1252d8517ee582c958dc6045f05c1438dd9d1
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 5 10:56:02 2015 +0200

    switch to symfony 2.7 [ci-skip]
2015-06-30 14:41:31 +02:00

461 lines
17 KiB
PHP

<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <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\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive;
/**
* Test the creation or deletion of accompanying periods
*
* 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
{
/**
*
* @var \Symfony\Component\BrowserKit\Client
*/
protected $client;
/**
*
* @var Person
*/
protected $person;
/**
*
* @var \Doctrine\ORM\EntityManagerInterface
*/
protected static $em;
const OPENING_INPUT = 'chill_personbundle_accompanyingperiod[date_opening]';
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(
'PHP_AUTH_USER' => 'center a_social',
'PHP_AUTH_PW' => 'password',
));
$center = static::$em->getRepository('ChillMainBundle:Center')
->findOneBy(array('name' => 'Center A'));
$this->person = (new Person(new \DateTime('2015-01-05')))
->setFirstName('Roland')
->setLastName('Gallorime')
->setCenter($center)
->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);
static::$em->remove($this->person);
static::$em->flush();
}
protected function generatePeriods(array $periods)
{
foreach ($periods as $periodDef) {
$period = new AccompanyingPeriod(new \DateTime($periodDef['openingDate']));
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->addAccompanyingPeriod($period);
static::$em->persist($period);
}
static::$em->flush();
}
protected function getLastValueOnClosingMotive(\Symfony\Component\DomCrawler\Form $form)
{
$values = $form->get(self::CLOSING_MOTIVE_INPUT)
->availableOptionValues();
return end($values);
}
protected function getRandomClosingMotive()
{
$motives = static::$em
->getRepository('ChillPersonBundle:AccompanyingPeriod\ClosingMotive')
->findAll();
return end($motives);
}
/**
* 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]/accompanying-period/close
* with : dateClosing: 2015-02-01
* with : the last closing motive in list
* Then the response should redirect to period view
* And the next page should have a `.error` element present in page
*
* @todo
*/
public function testClosingCurrentPeriod()
{
$crawler = $this->client->request('GET', '/en/person/'
.$this->person->getId().'/accompanying-period/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'));
$cr = $this->client->submit($form);
$this->assertTrue($this->client->getResponse()->isRedirect(
'/en/person/'.$this->person->getId().'/accompanying-period'),
'the server redirects to /accompanying-period page');
$this->assertGreaterThan(0, $this->client->followRedirect()
->filter('.success')->count(),
"a 'success' element is shown");
}
/**
* 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]/accompanying-period/close
* with : dateClosing: 2014-01-01
* with : the last closing motive in list
* Then the response should redirect to period view
* And the next page should have a `.error` element present in page
*
* @todo
*/
public function testClosingCurrentPeriodWithDateClosingBeforeOpeningFails()
{
$crawler = $this->client->request('GET', '/en/person/'
.$this->person->getId().'/accompanying-period/close');
$form = $crawler->selectButton('Submit')->form();
$form->get(self::CLOSING_MOTIVE_INPUT)
->setValue($this->getLastValueOnClosingMotive($form));
$form->get(self::CLOSING_INPUT)
->setValue((new \DateTime('2014-01-01'))->format('d-m-Y'));
$crawlerResponse = $this->client->submit($form);
$this->assertFalse($this->client->getResponse()->isRedirect(),
'the server stays on the /close page');
$this->assertGreaterThan(0, $crawlerResponse
->filter('.error')->count(),
"an '.error' element is shown");
}
/**
* Test the creation of a new period
*
* Given that a person as an accompanying period opened since 2015-01-05
* and we create a new period
* with : dateClosing: 2014-12-31
* with : dateOpening: 2014-01-01
* with : the last closing motive in list
* Then the response should redirect to period view
*/
public function testAddNewPeriodBeforeActual()
{
$crawler = $this->client->request('GET', '/en/person/'
.$this->person->getId().'/accompanying-period/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().'/accompanying-period'),
'the server redirects to /accompanying-period page');
$this->assertGreaterThan(0, $this->client->followRedirect()
->filter('.success')->count(),
"a 'success' element is shown");
}
/**
* Create a period with closing after current fails
*
* Given that a person as an accompanying period opened since 2015-01-05
* and we create a new period
* with : dateClosing: 2015-02-01 (after 2015-01-05)
* with : dateOpening: 2014-12-31
* with : the last closing motive in list
* Then the response should not redirect to any page
* and an error element is shown
*/
public function testCreatePeriodWithClosingAfterCurrentFails()
{
$crawler = $this->client->request('GET', '/en/person/'
.$this->person->getId().'/accompanying-period/create');
$form = $crawler->selectButton('Submit')->form();
$form->get(self::CLOSING_MOTIVE_INPUT)
->setValue($this->getLastValueOnClosingMotive($form));
$form->get(self::CLOSING_INPUT)
->setValue('01-02-2015');
$form->get(self::OPENING_INPUT)
->setValue('31-12-2014');
$crawler = $this->client->submit($form);
$this->assertFalse($this->client->getResponse()->isRedirect(),
'the server stay on form page');
$this->assertGreaterThan(0, $crawler->filter('.error')->count(),
"an 'error' element is shown");
}
/**
* Create a period after a current opened period fails
*
* Given that a person as an accompanying period opened since 2015-01-05
* and we create a new period
* with : dateClosing: 2015-03-01
* with : dateOpening: 2015-02-01
* with : the last closing motive in list
* Then the response should not redirect to any page
* and an error element is shown
*/
public function testCreatePeriodWithOpeningAndClosingAfterCurrentFails()
{
$crawler = $this->client->request('GET', '/en/person/'
.$this->person->getId().'/accompanying-period/create');
$form = $crawler->selectButton('Submit')->form();
$form->get(self::CLOSING_MOTIVE_INPUT)
->setValue($this->getLastValueOnClosingMotive($form));
$form->get(self::CLOSING_INPUT)
->setValue('01-03-2015');
$form->get(self::OPENING_INPUT)
->setValue('01-02-2015');
$crawler = $this->client->submit($form);
$this->assertFalse($this->client->getResponse()->isRedirect(),
'the server stay on form page');
$this->assertGreaterThan(0, $crawler->filter('.error')->count(),
"an 'error' element is shown");
}
/**
* create a period with date end between another period must fails
*
* Given that a person as an accompanying period opened since 2015-01-05
* and that this person has another accompanying period between 2014-01-01 and 2014-12-31
* and we create a new period
* with : dateClosing: 2014-16-01
* with : dateOpening: 2013-01-01
* with : the last closing motive in list
* Then the response should not redirect
* and a error element is shown on the response page
*/
public function testCreatePeriodWithDateEndBetweenAnotherPeriodFails()
{
$this->generatePeriods(array(
[
'openingDate' => '2014-01-01',
'closingDate' => '2014-12-31',
'closingMotive' => $this->getRandomClosingMotive()
]
));
$crawler = $this->client->request('GET', '/en/person/'
.$this->person->getId().'/accompanying-period/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-02-2015');
$crawlerResponse = $this->client->submit($form);
$this->assertFalse($this->client->getResponse()->isRedirect(),
'the server stay on form page');
$this->assertGreaterThan(0, $crawlerResponse->filter('.error')->count(),
"an 'error' element is shown");
}
/**
* create a period with date closing after opening fails
*
* Given that a person as an accompanying period opened since 2015-01-05
* and we create a new period
* with : dateClosing: 2014-01-01 (before opening)
* with : dateOpening: 2015-01-01
* with : the last closing motive in list
* Then the response should redirect to period view
*/
public function testCreatePeriodWithClosingBeforeOpeningFails()
{
$crawler = $this->client->request('GET', '/en/person/'
.$this->person->getId().'/accompanying-period/create');
$form = $crawler->selectButton('Submit')->form();
$form->get(self::CLOSING_MOTIVE_INPUT)
->setValue($this->getLastValueOnClosingMotive($form));
$form->get(self::CLOSING_INPUT)
->setValue('01-01-2014');
$form->get(self::OPENING_INPUT)
->setValue('01-01-2015');
$crawler = $this->client->submit($form);
$this->assertFalse($this->client->getResponse()->isRedirect(),
'the server stay on form page');
$this->assertGreaterThan(0, $crawler->filter('.error')->count(),
"an 'error' element is shown");
}
/**
* create a period with date closing and date opening inside another period
* fails
*
* Given that a person as an accompanying period opened since 2015-01-05
* and that this person has another accompanying period between 2014-01-01 and 2014-12-31
* and we create a new period
* with : dateClosing: 2014-02-01
* with : dateOpening: 2014-03-01
* with : the last closing motive in list
* Then the response should not redirect
* and a error element is shown on the response page
*/
public function testCreatePeriodAfterOpeningFails()
{
$this->generatePeriods(array(
[
'openingDate' => '2014-01-01',
'closingDate' => '2014-12-31',
'closingMotive' => $this->getRandomClosingMotive()
]
));
$crawler = $this->client->request('GET', '/en/person/'
.$this->person->getId().'/accompanying-period/create');
$form = $crawler->selectButton('Submit')->form();
$form->get(self::CLOSING_MOTIVE_INPUT)
->setValue($this->getLastValueOnClosingMotive($form));
$form->get(self::CLOSING_INPUT)
->setValue('2014-02-01');
$form->get(self::OPENING_INPUT)
->setValue('01-03-2014');
$crawlerResponse = $this->client->submit($form);
$this->assertFalse($this->client->getResponse()->isRedirect(),
'the server stay on form page');
$this->assertGreaterThan(0, $crawlerResponse->filter('.error')->count(),
"an 'error' element is shown");
}
/**
* Create a period with dateOpening between another period must fails
*
* Given that a person as an accompanying period opened since 2015-01-05
* and that this person has another accompanying period between 2014-01-01 and 2014-12-31
* and we create a new period
* with : dateClosing: 2015-01-01
* with : dateOpening: 2014-06-01
* with : the last closing motive in list
* Then the response should not redirect
* and a error element is shown on the response page
*/
public function testCreatePeriodWithDateOpeningBetweenAnotherPeriodFails()
{
$this->generatePeriods(array(
[
'openingDate' => '2014-01-01',
'closingDate' => '2014-12-31',
'closingMotive' => $this->getRandomClosingMotive()
]
));
$crawler = $this->client->request('GET', '/en/person/'
.$this->person->getId().'/accompanying-period/create');
$form = $crawler->selectButton('Submit')->form();
$form->get(self::CLOSING_MOTIVE_INPUT)
->setValue($this->getLastValueOnClosingMotive($form));
$form->get(self::CLOSING_INPUT)
->setValue('2015-01-01');
$form->get(self::OPENING_INPUT)
->setValue('01-06-2014');
$crawlerResponse = $this->client->submit($form);
$this->assertFalse($this->client->getResponse()->isRedirect(),
'the server stay on form page');
$this->assertGreaterThan(0, $crawlerResponse->filter('.error')->count(),
"an 'error' element is shown");
}
}