mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-28 10:33:49 +00:00
fix folder name
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Julien Fastré <julien.fastre@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\ReportBundle\Tests\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Symfony\Component\BrowserKit\Client;
|
||||
|
||||
/**
|
||||
* This class is much well writtend than ReportControllerTest class, and will
|
||||
* replace ReportControllerTest in the future.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class ReportControllerNextTest extends WebTestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var Person
|
||||
*/
|
||||
protected $person;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var CustomFieldsGroup
|
||||
*/
|
||||
protected $group;
|
||||
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
static::bootKernel();
|
||||
// get person from fixture
|
||||
$em = static::$kernel->getContainer()
|
||||
->get('doctrine.orm.entity_manager');
|
||||
|
||||
$this->person = $em
|
||||
->getRepository('ChillPersonBundle:Person')
|
||||
->findOneBy(array(
|
||||
'lastName' => 'Charline',
|
||||
'firstName' => 'Depardieu'
|
||||
)
|
||||
);
|
||||
|
||||
if ($this->person === NULL) {
|
||||
throw new \RuntimeException("The expected person is not present in the database. "
|
||||
. "Did you run `php app/console doctrine:fixture:load` before launching tests ? "
|
||||
. "(expecting person is 'Charline Depardieu'");
|
||||
}
|
||||
|
||||
// get custom fields group from fixture
|
||||
$customFieldsGroups = static::$kernel->getContainer()
|
||||
->get('doctrine.orm.entity_manager')
|
||||
->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')
|
||||
->findBy(array('entity' => 'Chill\ReportBundle\Entity\Report'))
|
||||
;
|
||||
//filter customFieldsGroup to get only "situation de logement"
|
||||
$filteredCustomFieldsGroupHouse = array_filter($customFieldsGroups,
|
||||
function(CustomFieldsGroup $group) {
|
||||
return in_array("Situation de logement", $group->getName());
|
||||
});
|
||||
$this->group = $filteredCustomFieldsGroupHouse[0];
|
||||
}
|
||||
|
||||
public function testValidCreate()
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
$form = $this->getReportForm($this->person, $this->group, $client);
|
||||
|
||||
$form->get('chill_reportbundle_report[date]')->setValue(
|
||||
(new \DateTime())->format('d-m-Y'));
|
||||
|
||||
$client->submit($form);
|
||||
|
||||
$this->assertTrue($client->getResponse()->isRedirect(),
|
||||
"The next page is a redirection to the new report's view page");
|
||||
|
||||
}
|
||||
|
||||
public function testUngrantedUserIsDeniedAccessOnListReports()
|
||||
{
|
||||
$client = $this->getAuthenticatedClient('center b_social');
|
||||
$client->request('GET', sprintf('/fr/person/%d/report/list',
|
||||
$this->person->getId()));
|
||||
|
||||
$this->assertEquals(403, $client->getResponse()->getStatusCode(),
|
||||
'assert that user for center b has a 403 status code when listing'
|
||||
. 'reports on person from center a');
|
||||
}
|
||||
|
||||
public function testUngrantedUserIsDeniedAccessOnReport()
|
||||
{
|
||||
$client = $this->getAuthenticatedClient('center b_social');
|
||||
$reports = static::$kernel->getContainer()->get('doctrine.orm.entity_manager')
|
||||
->getRepository('ChillReportBundle:Report')
|
||||
->findBy(array('person' => $this->person));
|
||||
$report = $reports[0];
|
||||
|
||||
$client->request('GET', sprintf('/fr/person/%d/report/%d/view',
|
||||
$this->person->getId(), $report->getId()));
|
||||
|
||||
$this->assertEquals(403, $client->getResponse()->getStatusCode(),
|
||||
'assert that user for center b has a 403 status code when '
|
||||
. 'trying to watch a report from person from center a');
|
||||
}
|
||||
|
||||
public function testUngrantedUserIsDeniedReportNew()
|
||||
{
|
||||
$client = $this->getAuthenticatedClient('center b_social');
|
||||
|
||||
$client->request('GET', sprintf('fr/person/%d/report/cfgroup/%d/new',
|
||||
$this->person->getId(), $this->group->getId()));
|
||||
|
||||
$this->assertEquals(403, $client->getResponse()->getStatusCode(),
|
||||
'assert that user is denied on trying to show a form "new" for'
|
||||
. ' a person on another center');
|
||||
}
|
||||
|
||||
public function testUngrantedUserIsDeniedReportCreate()
|
||||
{
|
||||
$clientCenterA = $this->getAuthenticatedClient('center a_social');
|
||||
|
||||
$form = $this->getReportForm($this->person, $this->group, $clientCenterA);
|
||||
|
||||
$clientCenterB = $this->getAuthenticatedClient('center b_social');
|
||||
$clientCenterB->submit($form);
|
||||
|
||||
$this->assertEquals(403, $clientCenterB->getResponse()->getStatusCode(),
|
||||
'assert that user is denied on trying to show a form "new" for'
|
||||
. ' a person on another center');
|
||||
}
|
||||
|
||||
protected function getAuthenticatedClient($username = 'center a_social')
|
||||
{
|
||||
return static::createClient(array(), array(
|
||||
'PHP_AUTH_USER' => $username,
|
||||
'PHP_AUTH_PW' => 'password',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Person $person
|
||||
* @param CustomFieldsGroup $group
|
||||
* @param Client $client
|
||||
* @return \Symfony\Component\DomCrawler\Form
|
||||
*/
|
||||
protected function getReportForm(Person $person, CustomFieldsGroup $group, Client $client)
|
||||
{
|
||||
$url = sprintf('fr/person/%d/report/cfgroup/%d/new', $person->getId(),
|
||||
$group->getId());
|
||||
$crawler = $client->request('GET', $url);
|
||||
|
||||
return $crawler->selectButton('Ajouter le rapport')
|
||||
->form();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,443 @@
|
||||
<?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\ReportBundle\Tests\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\DomCrawler\Form;
|
||||
use Symfony\Component\DomCrawler\Link;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
|
||||
/**
|
||||
* Test the life cycles of controllers, according to
|
||||
* https://redmine.champs-libres.coop/projects/report/wiki/Test_plan_for_report_lifecycle
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class ReportControllerTest extends WebTestCase
|
||||
{
|
||||
|
||||
const REPORT_NAME_FIELD = 'cFGroup';
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Chill\PersonBundle\Entity\Person
|
||||
*/
|
||||
private static $person;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \SClientymfony\Component\BrowserKit\
|
||||
*/
|
||||
private static $client;
|
||||
|
||||
private static $user;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var CustomFieldsGroup
|
||||
*/
|
||||
private static $group;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Doctrine\ORM\EntityManagerInterface
|
||||
*/
|
||||
private static $em;
|
||||
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
static::bootKernel();
|
||||
|
||||
static::$em = static::$kernel->getContainer()
|
||||
->get('doctrine.orm.entity_manager');
|
||||
|
||||
//get a random person
|
||||
static::$person = static::$kernel->getContainer()
|
||||
->get('doctrine.orm.entity_manager')
|
||||
->getRepository('ChillPersonBundle:Person')
|
||||
->findOneBy(array(
|
||||
'lastName' => 'Charline',
|
||||
'firstName' => 'Depardieu'
|
||||
)
|
||||
);
|
||||
|
||||
if (static::$person === NULL) {
|
||||
throw new \RuntimeException("The expected person is not present in the database. "
|
||||
. "Did you run `php app/console doctrine:fixture:load` before launching tests ? "
|
||||
. "(expecting person is 'Charline Depardieu'");
|
||||
}
|
||||
|
||||
$customFieldsGroups = static::$kernel->getContainer()
|
||||
->get('doctrine.orm.entity_manager')
|
||||
->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')
|
||||
->findBy(array('entity' => 'Chill\ReportBundle\Entity\Report'))
|
||||
;
|
||||
//filter customFieldsGroup to get only "situation de logement"
|
||||
$filteredCustomFieldsGroupHouse = array_filter($customFieldsGroups,
|
||||
function(CustomFieldsGroup $group) {
|
||||
return in_array("Situation de logement", $group->getName());
|
||||
});
|
||||
static::$group = $filteredCustomFieldsGroupHouse[0];
|
||||
|
||||
static::$user = static::$kernel->getContainer()
|
||||
->get('doctrine.orm.entity_manager')
|
||||
->getRepository('ChillMainBundle:User')
|
||||
->findOneBy(array('username' => "center a_social"));
|
||||
}
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
static::$client = static::createClient(array(), array(
|
||||
'PHP_AUTH_USER' => 'center a_social',
|
||||
'PHP_AUTH_PW' => 'password',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param type $username
|
||||
* @return Client
|
||||
*/
|
||||
public function getAuthenticatedClient($username = 'center a_social')
|
||||
{
|
||||
return static::createClient(array(), array(
|
||||
'PHP_AUTH_USER' => $username,
|
||||
'PHP_AUTH_PW' => 'password',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up the browser to be at a random person general page (/fr/person/%d/general),
|
||||
* check if there is a menu link for adding a new report and return this link (as producer)
|
||||
*
|
||||
* We assume that :
|
||||
* - we are on a "person" page
|
||||
* - there are more than one report model
|
||||
*
|
||||
*/
|
||||
public function testMenu()
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
$crawlerPersonPage = $client->request('GET', sprintf('/fr/person/%d/general',
|
||||
static::$person->getId()));
|
||||
|
||||
if (! $client->getResponse()->isSuccessful()) {
|
||||
var_dump($crawlerPersonPage->html());
|
||||
throw new \RuntimeException('the request at person page failed');
|
||||
}
|
||||
|
||||
$link = $crawlerPersonPage->selectLink("AJOUT D'UN RAPPORT")->link();
|
||||
$this->assertInstanceOf('Symfony\Component\DomCrawler\Link', $link,
|
||||
"There is a \"add a report\" link in menu");
|
||||
$this->assertContains(sprintf("/fr/person/%d/report/select/type/for/creation",
|
||||
static::$person->getId()), $link->getUri(),
|
||||
"There is a \"add a report\" link in menu");
|
||||
|
||||
return $link;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param \Symfony\Component\DomCrawler\Link $link
|
||||
* @return type
|
||||
* @depends testMenu
|
||||
*/
|
||||
public function testChooseReportModelPage(Link $link)
|
||||
{
|
||||
// When I click on "add a report" link in menu
|
||||
$client = $this->getAuthenticatedClient();
|
||||
$crawlerAddAReportPage = $client->click($link);
|
||||
|
||||
$form = $crawlerAddAReportPage->selectButton("Créer un nouveau rapport")->form();
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\DomCrawler\Form', $form,
|
||||
'I can see a form with a button "add a new report" ');
|
||||
|
||||
$this->assertGreaterThan(1, count($form->get(self::REPORT_NAME_FIELD)
|
||||
->availableOptionValues()),
|
||||
"I can choose between report models");
|
||||
|
||||
$possibleOptionsValue = $form->get(self::REPORT_NAME_FIELD)
|
||||
->availableOptionValues();
|
||||
$form->get(self::REPORT_NAME_FIELD)->setValue(
|
||||
$possibleOptionsValue[array_rand($possibleOptionsValue)]);
|
||||
|
||||
$client->submit($form);
|
||||
|
||||
$this->assertTrue($client->getResponse()->isRedirect());
|
||||
return $client->followRedirect();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param \Symfony\Component\DomCrawler\Crawler $crawlerNewReportPage
|
||||
* @return type
|
||||
* @depends testChooseReportModelPage
|
||||
*/
|
||||
public function testNewReportPage(Crawler $crawlerNewReportPage)
|
||||
{
|
||||
|
||||
$addForm = $crawlerNewReportPage
|
||||
->selectButton('Ajouter le rapport')
|
||||
->form();
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\DomCrawler\Form', $addForm,
|
||||
'I have a report form');
|
||||
|
||||
$this->isFormAsExpected($addForm);
|
||||
|
||||
return $addForm;
|
||||
}
|
||||
|
||||
/**
|
||||
* get a form for report new
|
||||
*
|
||||
* @param \Chill\ReportBundle\Tests\Controller\Person $person
|
||||
* @param CustomFieldsGroup $group
|
||||
* @param \Symfony\Component\BrowserKit\Client $client
|
||||
* @return Form
|
||||
*/
|
||||
protected function getReportForm(Person $person, CustomFieldsGroup $group,
|
||||
\Symfony\Component\BrowserKit\Client $client)
|
||||
{
|
||||
$url = sprintf('fr/person/%d/report/cfgroup/%d/new', $person->getId(),
|
||||
$group->getId());
|
||||
$crawler = $client->request('GET', $url);
|
||||
|
||||
return $crawler->selectButton('Ajouter le rapport')
|
||||
->form();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the expected field are present
|
||||
*
|
||||
* @param Form $form
|
||||
* @param boolean $isDefault if the form should be at default values
|
||||
*
|
||||
*/
|
||||
private function isFormAsExpected(Form $form, $isDefault = true)
|
||||
{
|
||||
$this->assertTrue($form->has('chill_reportbundle_report[date]'),
|
||||
'the report form have a field "date"' );
|
||||
$this->assertTrue($form->has('chill_reportbundle_report[user]'),
|
||||
'the report form have field "user" ');
|
||||
|
||||
$this->assertEquals('select', $form->get('chill_reportbundle_report[user]')
|
||||
->getType(), "the user field is a select input");
|
||||
|
||||
if ($isDefault) {
|
||||
$date = new \DateTime('now');
|
||||
$this->assertEquals(
|
||||
$date->format('d-m-Y'),
|
||||
$form->get('chill_reportbundle_report[date]')->getValue(),
|
||||
"the date field contains the current date by default"
|
||||
);
|
||||
|
||||
//resolve the user
|
||||
$userId = $form->get('chill_reportbundle_report[user]')->getValue();
|
||||
|
||||
$this->assertEquals('center a_social', static::$user->getUsername(),
|
||||
"the user field is the current user by default");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* fill the form with correct data
|
||||
*
|
||||
* @param Form $form
|
||||
*/
|
||||
private function fillCorrectForm(Form $form)
|
||||
{
|
||||
$form->get('chill_reportbundle_report[date]')->setValue(
|
||||
(new \DateTime())->format('d-m-Y'));
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that setting a Null date redirect to an error page
|
||||
*
|
||||
*/
|
||||
public function testNullDate()
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
$form = $this->getReportForm(static::$person, static::$group,
|
||||
$client);
|
||||
//var_dump($form);
|
||||
$filledForm = $this->fillCorrectForm($form);
|
||||
$filledForm->get('chill_reportbundle_report[date]')->setValue('');
|
||||
//$this->markTestSkipped();
|
||||
$crawler = $this->getAuthenticatedClient('center a_administrative')->submit($filledForm);
|
||||
|
||||
$this->assertFalse($client->getResponse()->isRedirect());
|
||||
$this->assertGreaterThan(0, $crawler->filter('.error')->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that setting a Null date redirect to an error page
|
||||
*
|
||||
* @param Form $form
|
||||
* @depends testNewReportPage
|
||||
*/
|
||||
public function testInvalidDate(Form $form)
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
$this->markTestSkipped("This test raise an error since symfony 2.7. "
|
||||
. "The user is not correctly reloaded from database.");
|
||||
$filledForm = $this->fillCorrectForm($form);
|
||||
$filledForm->get('chill_reportbundle_report[date]')->setValue('invalid date value');
|
||||
|
||||
$crawler = $client->submit($filledForm);
|
||||
|
||||
$this->assertFalse($client->getResponse()->isRedirect());
|
||||
$this->assertGreaterThan(0, $crawler->filter('.error')->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a incorrect value in user will show an error page
|
||||
*
|
||||
* @depends testNewReportPage
|
||||
* @param Form $form
|
||||
*/
|
||||
public function testInvalidUser(Form $form)
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
$filledForm = $this->fillCorrectForm($form);
|
||||
$select = $filledForm->get('chill_reportbundle_report[user]')
|
||||
->disableValidation()
|
||||
->setValue(-1);
|
||||
|
||||
$crawler = $client->submit($filledForm);
|
||||
|
||||
$this->assertFalse($client->getResponse()->isRedirect());
|
||||
$this->assertGreaterThan(0, $crawler->filter('.error')->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the creation of a report
|
||||
*
|
||||
* depends testNewReportPage
|
||||
* param Form $form
|
||||
*/
|
||||
public function testValidCreate()
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
//$this->markTestSkipped("This test raise an error since symfony 2.7. "
|
||||
// . "The user is not correctly reloaded from database.");
|
||||
$addForm = $this->getReportForm(self::$person, self::$group, $client);
|
||||
$filledForm = $this->fillCorrectForm($addForm);
|
||||
$c = $client->submit($filledForm);
|
||||
|
||||
$this->assertTrue($client->getResponse()->isRedirect(),
|
||||
"The next page is a redirection to the new report's view page");
|
||||
$client->followRedirect();
|
||||
|
||||
$this->assertRegExp("|/fr/person/".static::$person->getId()."/report/[0-9]*/view$|",
|
||||
$client->getHistory()->current()->getUri(),
|
||||
"The next page is a redirection to the new report's view page");
|
||||
|
||||
$matches = array();
|
||||
preg_match('|/report/([0-9]*)/view$|',
|
||||
$client->getHistory()->current()->getUri(), $matches);
|
||||
|
||||
return $matches[1];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @depends testValidCreate
|
||||
* @param int $reportId
|
||||
*/
|
||||
public function testList($reportId)
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
$crawler = $client->request('GET', sprintf('/fr/person/%s/report/list',
|
||||
static::$person->getId()));
|
||||
|
||||
$this->assertTrue($client->getResponse()->isSuccessful());
|
||||
|
||||
$linkSee = $crawler->filter('.bt-view')->links();
|
||||
$this->assertGreaterThan(0, count($linkSee));
|
||||
$this->assertRegExp(sprintf('|/fr/person/%s/report/[0-9]*/view$|',
|
||||
static::$person->getId(), $reportId), $linkSee[0]->getUri());
|
||||
|
||||
$linkUpdate = $crawler->filter('.bt-update')->links();
|
||||
$this->assertGreaterThan(0, count($linkUpdate));
|
||||
$this->assertRegExp(sprintf('|/fr/person/%s/report/[0-9]*/edit$|',
|
||||
static::$person->getId(), $reportId), $linkUpdate[0]->getUri());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the view of a report
|
||||
*
|
||||
* @depends testValidCreate
|
||||
* @param int $reportId
|
||||
*/
|
||||
public function testView($reportId)
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
$client->request('GET',
|
||||
sprintf('/fr/person/%s/report/%s/view', static::$person->getId(), $reportId));
|
||||
|
||||
$this->assertTrue($client->getResponse()->isSuccessful(),
|
||||
'the page is shown');
|
||||
}
|
||||
|
||||
/**
|
||||
* test the update form
|
||||
*
|
||||
* @depends testValidCreate
|
||||
* @param int $reportId
|
||||
*/
|
||||
public function testUpdate($reportId)
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
$crawler = $client->request('GET',
|
||||
sprintf('/fr/person/%s/report/%s/edit', static::$person->getId(), $reportId));
|
||||
|
||||
$this->assertTrue($client->getResponse()->isSuccessful());
|
||||
|
||||
$form = $crawler
|
||||
->selectButton('Enregistrer le rapport')
|
||||
->form();
|
||||
|
||||
$form->get('chill_reportbundle_report[date]')->setValue(
|
||||
(new \DateTime('yesterday'))->format('d-m-Y'));
|
||||
|
||||
$client->submit($form);
|
||||
|
||||
$this->assertTrue($client->getResponse()->isRedirect(
|
||||
sprintf('/fr/person/%s/report/%s/view',
|
||||
static::$person->getId(), $reportId)));
|
||||
|
||||
$this->assertEquals(new \DateTime('yesterday'), static::$kernel->getContainer()
|
||||
->get('doctrine.orm.entity_manager')
|
||||
->getRepository('ChillReportBundle:Report')
|
||||
->find($reportId)
|
||||
->getDate());
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\ReportBundle\Tests\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
class ChillReportExtensionTest extends KernelTestCase
|
||||
{
|
||||
/*
|
||||
* Check if class Chill\ReportBundle\Entity\Report is in chill_custom_fields.customizables_entities
|
||||
*/
|
||||
public function testDeclareReportAsCustomizable()
|
||||
{
|
||||
self::bootKernel(array('environment' => 'test'));
|
||||
$customizablesEntities = static::$kernel->getContainer()
|
||||
->getParameter('chill_custom_fields.customizables_entities');
|
||||
|
||||
$reportFounded = false;
|
||||
foreach ($customizablesEntities as $customizablesEntity) {
|
||||
if($customizablesEntity['class'] === 'Chill\ReportBundle\Entity\Report') {
|
||||
$reportFounded = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(! $reportFounded) {
|
||||
throw new Exception("Class Chill\ReportBundle\Entity\Report not found in chill_custom_fields.customizables_entities", 1);
|
||||
}
|
||||
}
|
||||
}
|
126
src/Bundle/ChillReportBundle/Tests/Search/ReportSearchTest.php
Normal file
126
src/Bundle/ChillReportBundle/Tests/Search/ReportSearchTest.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Chill is a suite of a modules, Chill is a software for social workers
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
namespace Chill\ReportBundle\Tests\Search;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
/**
|
||||
* Test for report search
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class ReportSearchTest extends WebTestCase
|
||||
{
|
||||
|
||||
public function testSearchExpectedDefault()
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
|
||||
$crawler = $client->request('GET', '/fr/search', array(
|
||||
'q' => '@report 2015-01-05'
|
||||
));
|
||||
|
||||
$this->assertTrue($client->getResponse()->isSuccessful());
|
||||
$this->assertRegExp('/Situation de logement/i', $crawler->text());
|
||||
}
|
||||
|
||||
public function testNamedSearch()
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
|
||||
$crawler = $client->request('GET', '/fr/search', array(
|
||||
'q' => '@report '.(new \DateTime('tomorrow'))->format('Y-m-d'), //there should be any result for future. And future is tomorrow
|
||||
'name' => 'report'
|
||||
));
|
||||
|
||||
$this->assertTrue($client->getResponse()->isSuccessful());
|
||||
}
|
||||
|
||||
public function testSearchByDate()
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
|
||||
$crawler = $client->request('GET', '/fr/search', array(
|
||||
'q' => '@report date:2015-01-05'
|
||||
));
|
||||
|
||||
$this->assertTrue($client->getResponse()->isSuccessful());
|
||||
$this->assertRegExp('/Situation de logement/i', $crawler->text());
|
||||
}
|
||||
|
||||
public function testSearchDoubleDate()
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
|
||||
$crawler = $client->request('GET', '/fr/search', array(
|
||||
'q' => '@report date:2014-05-01 2014-05-06'
|
||||
));
|
||||
|
||||
$this->assertGreaterThan(0, $crawler->filter('.error')->count());
|
||||
}
|
||||
|
||||
public function testSearchEmtpy()
|
||||
{
|
||||
$client = $this->getAuthenticatedClient();
|
||||
|
||||
$crawler = $client->request('GET', '/fr/search', array(
|
||||
'q' => '@report '
|
||||
));
|
||||
|
||||
$this->assertGreaterThan(0, $crawler->filter('.error')->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the user do not see unauthorized results
|
||||
*
|
||||
* We test for that that :
|
||||
* - we do not see any unauthorized scope mention
|
||||
*/
|
||||
public function testUsersDoNotSeeUnauthorizedResults()
|
||||
{
|
||||
$clientSocial = $this->getAuthenticatedClient();
|
||||
$clientAdministrative = $this->getAuthenticatedClient('center a_administrative');
|
||||
|
||||
$params = array('q' => '@report date:2015-01-05');
|
||||
|
||||
$crawlerSocial = $clientSocial->request('GET', '/fr/search', $params);
|
||||
$crawlerAdministrative = $clientAdministrative->request('GET', '/fr/search', $params);
|
||||
|
||||
|
||||
$this->assertNotContains('social', $crawlerAdministrative->filter('.content')
|
||||
->text());
|
||||
$this->assertNotContains('administrative', $crawlerAdministrative->filter('.content')
|
||||
->text());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return \Symfony\Component\BrowserKit\Client
|
||||
*/
|
||||
private function getAuthenticatedClient($username = 'center a_social')
|
||||
{
|
||||
return static::createClient(array(), array(
|
||||
'PHP_AUTH_USER' => $username,
|
||||
'PHP_AUTH_PW' => 'password',
|
||||
));
|
||||
}
|
||||
}
|
@@ -0,0 +1,193 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Julien Fastré <julien.fastre@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\ReportBundle\Tests\Security\Authorization;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\MainBundle\Test\PrepareUserTrait;
|
||||
use Chill\MainBundle\Test\PrepareCenterTrait;
|
||||
use Chill\MainBundle\Test\PrepareScopeTrait;
|
||||
use Chill\ReportBundle\Entity\Report;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class ReportVoterTest extends KernelTestCase
|
||||
{
|
||||
|
||||
use PrepareUserTrait, PrepareCenterTrait, PrepareScopeTrait;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Chill\ReportBundle\Security\Authorization\ReportVoter
|
||||
*/
|
||||
protected $voter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Prophecy\Prophet
|
||||
*/
|
||||
protected $prophet;
|
||||
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
static::bootKernel();
|
||||
$this->voter = static::$kernel->getContainer()
|
||||
->get('chill.report.security.authorization.report_voter');
|
||||
$this->prophet = new \Prophecy\Prophet();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProvider
|
||||
* @param type $expectedResult
|
||||
* @param Report $report
|
||||
* @param User $user
|
||||
* @param type $action
|
||||
* @param type $message
|
||||
*/
|
||||
public function testAccess($expectedResult, Report $report, $action,
|
||||
$message, User $user = null)
|
||||
{
|
||||
$token = $this->prepareToken($user);
|
||||
$result = $this->voter->vote($token, $report, [$action]);
|
||||
$this->assertEquals($expectedResult, $result, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* prepare a person
|
||||
*
|
||||
* The only properties set is the center, others properties are ignored.
|
||||
*
|
||||
* @param Center $center
|
||||
* @return Person
|
||||
*/
|
||||
protected function preparePerson(Center $center)
|
||||
{
|
||||
return (new Person())
|
||||
->setCenter($center)
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* prepare a token interface with correct rights
|
||||
*
|
||||
* if $permissions = null, user will be null (no user associated with token
|
||||
*
|
||||
* @param User $user
|
||||
* @return \Symfony\Component\Security\Core\Authentication\Token\TokenInterface
|
||||
*/
|
||||
protected function prepareToken(User $user = null)
|
||||
{
|
||||
$token = $this->prophet->prophesize();
|
||||
$token
|
||||
->willImplement('\Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
||||
if ($user === NULL) {
|
||||
$token->getUser()->willReturn(null);
|
||||
} else {
|
||||
$token->getUser()->willReturn($user);
|
||||
}
|
||||
|
||||
return $token->reveal();
|
||||
}
|
||||
|
||||
public function dataProvider()
|
||||
{
|
||||
$centerA = $this->prepareCenter(1, 'center A');
|
||||
$centerB = $this->prepareCenter(2, 'center B');
|
||||
$scopeA = $this->prepareScope(1, 'scope default');
|
||||
$scopeB = $this->prepareScope(2, 'scope B');
|
||||
$scopeC = $this->prepareScope(3, 'scope C');
|
||||
|
||||
$userA = $this->prepareUser(array(
|
||||
array(
|
||||
'center' => $centerA,
|
||||
'permissionsGroup' => array(
|
||||
['scope' => $scopeB, 'role' => 'CHILL_REPORT_SEE'],
|
||||
['scope' => $scopeA, 'role' => 'CHILL_REPORT_UPDATE']
|
||||
)
|
||||
),
|
||||
array(
|
||||
'center' => $centerB,
|
||||
'permissionsGroup' => array(
|
||||
['scope' => $scopeA, 'role' => 'CHILL_REPORT_SEE'],
|
||||
)
|
||||
)
|
||||
|
||||
));
|
||||
|
||||
$reportA = (new Report)
|
||||
->setScope($scopeA)
|
||||
->setPerson($this->preparePerson($centerA))
|
||||
;
|
||||
$reportB = (new Report())
|
||||
->setScope($scopeB)
|
||||
->setPerson($this->preparePerson($centerA))
|
||||
;
|
||||
$reportC = (new Report())
|
||||
->setScope($scopeC)
|
||||
->setPerson($this->preparePerson($centerB))
|
||||
;
|
||||
|
||||
|
||||
return array(
|
||||
array(
|
||||
VoterInterface::ACCESS_DENIED,
|
||||
$reportA,
|
||||
'CHILL_REPORT_SEE',
|
||||
"assert is denied to a null user",
|
||||
null
|
||||
),
|
||||
array(
|
||||
VoterInterface::ACCESS_GRANTED,
|
||||
$reportA,
|
||||
'CHILL_REPORT_SEE',
|
||||
"assert access is granted to a user with inheritance UPDATE > SEE",
|
||||
$userA
|
||||
),
|
||||
array(
|
||||
VoterInterface::ACCESS_GRANTED,
|
||||
$reportB,
|
||||
'CHILL_REPORT_SEE',
|
||||
"assert access is granted to a user without inheritance",
|
||||
$userA
|
||||
),
|
||||
array(
|
||||
VoterInterface::ACCESS_DENIED,
|
||||
$reportC,
|
||||
'CHILL_REPORT_SEE',
|
||||
'assert access is denied to a report',
|
||||
$userA
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
* Copyright (C) 2015 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\ReportBundle\Tests\Timeline;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\ReportBundle\Entity\Report;
|
||||
use Chill\MainBundle\Tests\TestHelper as MainTestHelper;
|
||||
use Chill\MainBundle\Entity\Scope;
|
||||
|
||||
/**
|
||||
* Test a report is shown into timeline
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @author Champs Libres <info@champs-libres.coop>
|
||||
*/
|
||||
class TimelineProviderTest extends WebTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Doctrine\ORM\EntityManager
|
||||
*/
|
||||
private static $em;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var Person
|
||||
*/
|
||||
private $person;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var Report
|
||||
*/
|
||||
private $report;
|
||||
|
||||
/**
|
||||
* Create a person with a report associated with the person
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
static::bootKernel();
|
||||
|
||||
static::$em = static::$kernel->getContainer()
|
||||
->get('doctrine.orm.entity_manager');
|
||||
|
||||
$center = static::$em->getRepository('ChillMainBundle:Center')
|
||||
->findOneBy(array('name' => 'Center A'));
|
||||
|
||||
$person = (new Person(new \DateTime('2015-05-01')))
|
||||
->setGender(Person::FEMALE_GENDER)
|
||||
->setFirstName('Nelson')
|
||||
->setLastName('Mandela')
|
||||
->setCenter($center);
|
||||
static::$em->persist($person);
|
||||
$this->person = $person;
|
||||
|
||||
$scopesSocial = array_filter(static::$em
|
||||
->getRepository('ChillMainBundle:Scope')
|
||||
->findAll(),
|
||||
function(Scope $scope) { return $scope->getName()['en'] === 'social'; })
|
||||
;
|
||||
|
||||
$report = (new Report)
|
||||
->setUser(static::$em->getRepository('ChillMainBundle:User')
|
||||
->findOneByUsername('center a_social'))
|
||||
->setDate(new \DateTime('2015-05-02'))
|
||||
->setPerson($this->person)
|
||||
->setCFGroup($this->getHousingCustomFieldsGroup())
|
||||
->setCFData(['has_logement' => 'own_house',
|
||||
'house-desc' => 'blah blah'])
|
||||
->setScope(end($scopesSocial));
|
||||
|
||||
static::$em->persist($report);
|
||||
$this->report = $report;
|
||||
|
||||
|
||||
|
||||
static::$em->flush();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a report is shown in timeline
|
||||
*/
|
||||
public function testTimelineReport()
|
||||
{
|
||||
$client = static::createClient(array(),
|
||||
MainTestHelper::getAuthenticatedClientOptions()
|
||||
);
|
||||
|
||||
$crawler = $client->request('GET', '/fr/person/'.$this->person->getId()
|
||||
.'/timeline');
|
||||
|
||||
$this->assertTrue($client->getResponse()->isSuccessful(),
|
||||
'The page timeline is loaded successfully');
|
||||
$this->assertContains('a ajouté un rapport', $crawler->text(),
|
||||
'the page contains the text "a publié un rapport"');
|
||||
}
|
||||
|
||||
public function testTimelineReportWithSummaryField()
|
||||
{
|
||||
//load the page
|
||||
$client = static::createClient(array(),
|
||||
MainTestHelper::getAuthenticatedClientOptions()
|
||||
);
|
||||
|
||||
$crawler = $client->request('GET', '/fr/person/'.$this->person->getId()
|
||||
.'/timeline');
|
||||
|
||||
//performs tests
|
||||
$this->assertTrue($client->getResponse()->isSuccessful(),
|
||||
'The page timeline is loaded successfully');
|
||||
$this->assertGreaterThan(0, $crawler->filter('.report_entry .summary')
|
||||
->count(),
|
||||
'the page contains a .report .summary element');
|
||||
$this->assertContains('blah blah', $crawler->filter('.report_entry .summary')
|
||||
->text(),
|
||||
'the page contains the text "blah blah"');
|
||||
$this->assertContains('Propriétaire', $crawler->filter('.report_entry .summary')
|
||||
->text(),
|
||||
'the page contains the mention "Propriétaire"');
|
||||
}
|
||||
|
||||
public function testReportIsNotVisibleToUngrantedUsers()
|
||||
{
|
||||
$client = static::createClient(array(),
|
||||
MainTestHelper::getAuthenticatedClientOptions('center a_administrative')
|
||||
);
|
||||
|
||||
$crawler = $client->request('GET', '/fr/person/'.$this->person->getId()
|
||||
.'/timeline');
|
||||
|
||||
$this->assertEquals(0, $crawler->filter('.report_entry .summary')
|
||||
->count(),
|
||||
'the page does not contains a .report .summary element');
|
||||
}
|
||||
|
||||
/**
|
||||
* get a random custom fields group
|
||||
*
|
||||
* @return \Chill\CustomFieldsBundle\Entity\CustomFieldsGroup
|
||||
*/
|
||||
private function getHousingCustomFieldsGroup()
|
||||
{
|
||||
$groups = static::$em
|
||||
->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')
|
||||
->findAll();
|
||||
|
||||
foreach ($groups as $group) {
|
||||
if ($group->getName()['fr'] === 'Situation de logement') {
|
||||
return $group;
|
||||
}
|
||||
}
|
||||
|
||||
return $groups[rand(0, count($groups) -1)];
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
//static::$em->refresh($this->person);
|
||||
//static::$em->refresh($this->report);
|
||||
// static::$em->remove($this->person);
|
||||
//static::$em->remove($this->report);
|
||||
}
|
||||
}
|
7
src/Bundle/ChillReportBundle/Tests/bootstrap.php
Normal file
7
src/Bundle/ChillReportBundle/Tests/bootstrap.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
if (!is_file($autoloadFile = __DIR__.'/../vendor/autoload.php')) {
|
||||
throw new \LogicException('Could not find autoload.php in vendor/. Did you run "composer install --dev"?');
|
||||
}
|
||||
|
||||
require $autoloadFile;
|
Reference in New Issue
Block a user