Test for reportExport

This commit is contained in:
Marc Ducobu 2015-02-20 17:40:32 +01:00
parent 0cd8b9af3c
commit ebc5e6ea11

View File

@ -2,12 +2,14 @@
/* /*
* Chill is a software for social workers * Chill is a software for social workers
* Copyright (C) 2014 Julien Fastré <julien.fastre@champs-libres.coop> *
* 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 * 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 * it under the terms of the GNU Affero General Public License as
* the Free Software Foundation, either version 3 of the License, or * published by the Free Software Foundation, either version 3 of the
* (at your option) any later version. * License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -22,6 +24,8 @@ namespace Chill\ReportBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DomCrawler\Form; use Symfony\Component\DomCrawler\Form;
use Symfony\Component\DomCrawler\Link;
use Symfony\Component\DomCrawler\Crawler;
/** /**
* Test the life cycles of controllers, according to * Test the life cycles of controllers, according to
@ -42,7 +46,7 @@ class ReportControllerTest extends WebTestCase
/** /**
* *
* @var \Symfony\Component\BrowserKit\Client * @var \SClientymfony\Component\BrowserKit\
*/ */
private static $client; private static $client;
@ -68,11 +72,11 @@ class ReportControllerTest extends WebTestCase
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
->getRepository('ChillMainBundle:User') ->getRepository('ChillMainBundle:User')
->findOneBy(array('username' => "center a_social")); ->findOneBy(array('username' => "center a_social"));
} }
/** /**
* * 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 assume that :
* - we are on a "person" page * - we are on a "person" page
@ -81,7 +85,6 @@ class ReportControllerTest extends WebTestCase
*/ */
public function testMenu() public function testMenu()
{ {
$crawlerPersonPage = static::$client->request('GET', sprintf('/fr/person/%d/general', $crawlerPersonPage = static::$client->request('GET', sprintf('/fr/person/%d/general',
static::$person->getId())); static::$person->getId()));
@ -106,9 +109,8 @@ class ReportControllerTest extends WebTestCase
* @return type * @return type
* @depends testMenu * @depends testMenu
*/ */
public function testChooseReportModelPage(\Symfony\Component\DomCrawler\Link $link) public function testChooseReportModelPage(Link $link)
{ {
// When I click on "adda report" link in menu // When I click on "adda report" link in menu
$crawlerAddAReportPage = static::$client->click($link); $crawlerAddAReportPage = static::$client->click($link);
@ -130,8 +132,6 @@ class ReportControllerTest extends WebTestCase
$this->assertTrue(static::$client->getResponse()->isRedirect()); $this->assertTrue(static::$client->getResponse()->isRedirect());
return static::$client->followRedirect(); return static::$client->followRedirect();
} }
/** /**
@ -140,7 +140,7 @@ class ReportControllerTest extends WebTestCase
* @return type * @return type
* @depends testChooseReportModelPage * @depends testChooseReportModelPage
*/ */
public function testNewReportPage(\Symfony\Component\DomCrawler\Crawler $crawlerNewReportPage) public function testNewReportPage(Crawler $crawlerNewReportPage)
{ {
$addForm = $crawlerNewReportPage $addForm = $crawlerNewReportPage
@ -355,6 +355,97 @@ class ReportControllerTest extends WebTestCase
->getRepository('ChillReportBundle:Report') ->getRepository('ChillReportBundle:Report')
->find($reportId) ->find($reportId)
->getDate()); ->getDate());
}
/**
* Test that in the general export page there is an Export reports link
* that leads to export/report/select/type
*
* @return \Symfony\Component\DomCrawler\Link The link to the the
* form use for selecting which type of report to export
*/
public function testLinkToTheExportReport()
{
$crawlerReportExportPage = static::$client->request('GET', '/fr/export');
if (! static::$client->getResponse()->isSuccessful()) {
var_dump($crawlerReportExportPage->html());
throw new \RuntimeException('The get request at export page failed');
}
$link = $crawlerReportExportPage->selectLink("Export reports")->link();
$this->assertInstanceOf('Symfony\Component\DomCrawler\Link', $link,
"There is a \"export reports\" link in the export menu");
$this->assertContains("export/report/select/type", $link->getUri(),
"The \"export reports\" link in the export menu points to export/report/select/type");
return $link;
}
/**
* Test the export ReportAction :
* - follow the given link ( export/report/select/type )
* - choose randomly a type of report (CustomFieldsGroup)
* - submit the form
* - check if a csv file is well receiv
* - check if the number of row of the csv file is as expected (number of report of this type + 1)
*
* @depends testLinkToTheExportReport
*/
public function testExportAction(Link $link)
{
$crawlerExportReportPage = static::$client->click($link);
$form = $crawlerExportReportPage->selectButton("Export this kind of reports")->form();
$this->assertInstanceOf('Symfony\Component\DomCrawler\Form', $form,
'I can see a form with a button "Export this kind of reports" ');
$this->assertGreaterThan(1, count($form->get(self::REPORT_NAME_FIELD)
->availableOptionValues()),
"I can choose between report types");
$possibleOptionsValue = $form->get(self::REPORT_NAME_FIELD)
->availableOptionValues();
$cfGroupId = $possibleOptionsValue[array_rand($possibleOptionsValue)];
$form->get(self::REPORT_NAME_FIELD)->setValue($cfGroupId);
static::$client->submit($form);
$this->assertTrue(static::$client->getResponse()->isRedirect());
static::$client->followRedirect();
// TO DO CREATE A NEW FUNCTION
$response = static::$client->getResponse();
$this->assertTrue(
strpos($response->headers->get('Content-Type'),'text/csv') !== false,
'The csv file is well received');
echo gettype(static::$kernel->getContainer()
->get('doctrine.orm.entity_manager'));
$content = $response->getContent();
$rows = (explode("\n", $content));
$em = static::$kernel->getContainer()->get('doctrine.orm.entity_manager');
$cfGroup = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($cfGroupId);
$reports = $em->getRepository('ChillReportBundle:Report')->findByCFGroup($cfGroup);
if($rows[sizeof($rows) -1] == "") {
array_pop($rows);
}
$this->assertTrue(
sizeof($rows) == (sizeof($reports) + 1),
'The csv file has a number of row equivalent than the number of reports in the db'
);
} }
} }