add testing for customfield, correcting request injection

This commit is contained in:
2014-11-08 15:07:15 +01:00
parent 8b71a9e7cf
commit 7daa1a240e
14 changed files with 225 additions and 1762 deletions

View File

@@ -0,0 +1,86 @@
<?php
/*
* Chill is a software for social workers
* Copyright (C) 2014 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\CustomFieldsBundle\Tests;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Chill\CustomFieldsBundle\Entity\CustomField;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\DomCrawler\Crawler;
/**
* Give useful method to prepare tests regarding custom fields
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class CustomFieldTestHelper
{
/**
* Prepare a crawler containing the rendering of a customField
*
* @internal This method will mock a customFieldGroup containing $field, and
* rendering the customfield, using Type\CustomFieldType, to a simple form row
*
* @param CustomField $field
* @param KernelTestCase $testCase
* @param KernelInterface $kernel
* @param type $locale
* @return Crawler
*/
public static function getCrawlerForField(CustomField $field, KernelTestCase $testCase, KernelInterface $kernel, $locale = 'en')
{
//check a kernel is accessible
$customFieldsGroup = $testCase->getMock('Chill\CustomFieldsBundle\Entity\CustomFieldsGroup');
$customFieldsGroup->expects($testCase->once())
->method('getCustomFields')
->will($testCase->returnValue(array($field)))
;
$request = $testCase->getMock('Symfony\Component\HttpFoundation\Request');
$request->expects($testCase->any())
->method('getLocale')
->will($testCase->returnValue($locale))
;
$kernel->getContainer()->get('request_stack')->push($request);
$builder = $kernel->getContainer()->get('form.factory')->createBuilder();
$form = $builder->add('tested', 'custom_field',
array('group' => $customFieldsGroup))
->getForm()
;
$kernel->getContainer()->get('twig.loader')
->addPath(__DIR__.'/Fixtures/App/app/Resources/views/',
$namespace = 'test');
$content = $kernel
->getContainer()->get('templating')
->render('@test/CustomField/simple_form_render.html.twig', array(
'form' => $form->createView(),
'inputKeys' => array('tested')
));
$crawler = new Crawler();
$crawler->addHtmlContent($content);
return $crawler;
}
}

View File

@@ -0,0 +1,102 @@
<?php
/*
* Chill is a software for social workers
* Copyright (C) 2014 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\CustomFieldsBundle\Tests;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Chill\CustomFieldsBundle\Entity\CustomField;
use Chill\CustomFieldsBundle\CustomFields\CustomFieldText;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\DomCrawler\Crawler;
use Chill\CustomFieldsBundle\Tests\CustomFieldTestHelper;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class CustomFieldsTextTest extends KernelTestCase
{
/**
*
* @var \Chill\CustomFieldsBundle\Service\CustomFieldProvider
*/
private $customFieldProvider;
public function setUp()
{
static::bootKernel();
$this->customFieldProvider = static::$kernel->getContainer()
->get('chill.custom_field.provider');
}
public function testCustomFieldsTextExists()
{
$customField = $this->customFieldProvider->getCustomFieldByType('text');
$this->assertInstanceOf('Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface',
$customField);
$this->assertInstanceOf('Chill\CustomFieldsBundle\CustomFields\CustomFieldText',
$customField);
}
public function testPublicFormRenderingLengthLessThan256()
{
$customField = new CustomField();
$customField->setType('text')
->setOptions(array(CustomFieldText::MAX_LENGTH => 255))
->setSlug('slug')
->setOrdering(10)
->setActive(true)
->setName(array('en' => 'my label'))
;
$crawler = CustomFieldTestHelper::getCrawlerForField($customField, $this, static::$kernel);
$this->assertCount(1, $crawler->filter("input[type=text]"));
$this->assertCount(1, $crawler->filter("label:contains('my label')"));
}
public function testPublicFormRenderingLengthMoreThan25()
{
$customField = new CustomField();
$customField->setType('text')
->setOptions(array(CustomFieldText::MAX_LENGTH => 256))
->setSlug('slug')
->setOrdering(10)
->setActive(true)
->setName(array('en' => 'my label'))
;
$crawler = CustomFieldTestHelper::getCrawlerForField($customField, $this, static::$kernel);
$this->assertCount(1, $crawler->filter("textarea"));
$this->assertCount(1, $crawler->filter("label:contains('my label')"));
}
}

View File

@@ -13,7 +13,8 @@ class AppKernel extends Kernel
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new \Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle()
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new \Chill\MainBundle\ChillMainBundle,
#add here all the required bundle (some bundle are not required)
);
}

View File

@@ -0,0 +1,3 @@
{% for key in inputKeys %}
{{ form_row(form[key]) }}
{% endfor %}

View File

@@ -24,4 +24,7 @@ doctrine:
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
auto_mapping: true
chill_main:
available_languages: [ fr, nl, en ]

View File

@@ -3,4 +3,5 @@ parameters:
database_port: 5432
database_name: test0
database_user: postgres
database_password: postgres
database_password: postgres
locale: fr

View File

@@ -3,4 +3,5 @@ parameters:
database_port: 5434
database_name: symfony
database_user: symfony
database_password: symfony
database_password: symfony
locale: fr