Skip to content

[Form] Hardened form type tests in 2.8 #21878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
390 changes: 282 additions & 108 deletions src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
abstract class BaseTypeTest extends TypeTestCase
{
const TESTED_TYPE = '';

public function testPassDisabledAsOption()
{
$form = $this->factory->create($this->getTestedType(), null, array('disabled' => true));
Expand Down Expand Up @@ -47,7 +49,7 @@ public function testStripLeadingUnderscoresAndDigitsFromId()

public function testPassIdAndNameToViewWithParent()
{
$view = $this->factory->createNamedBuilder('parent', 'Symfony\Component\Form\Extension\Core\Type\FormType')
$view = $this->factory->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE)
->add('child', $this->getTestedType())
->getForm()
->createView();
Expand All @@ -59,8 +61,8 @@ public function testPassIdAndNameToViewWithParent()

public function testPassIdAndNameToViewWithGrandParent()
{
$builder = $this->factory->createNamedBuilder('parent', 'Symfony\Component\Form\Extension\Core\Type\FormType')
->add('child', 'Symfony\Component\Form\Extension\Core\Type\FormType');
$builder = $this->factory->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE)
->add('child', FormTypeTest::TESTED_TYPE);
$builder->get('child')->add('grand_child', $this->getTestedType());
$view = $builder->getForm()->createView();

Expand All @@ -71,18 +73,18 @@ public function testPassIdAndNameToViewWithGrandParent()

public function testPassTranslationDomainToView()
{
$form = $this->factory->create($this->getTestedType(), null, array(
$view = $this->factory->create($this->getTestedType(), null, array(
'translation_domain' => 'domain',
));
$view = $form->createView();
))
->createView();

$this->assertSame('domain', $view->vars['translation_domain']);
}

public function testInheritTranslationDomainFromParent()
{
$view = $this->factory
->createNamedBuilder('parent', 'Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, array(
'translation_domain' => 'domain',
))
->add('child', $this->getTestedType())
Expand All @@ -95,7 +97,7 @@ public function testInheritTranslationDomainFromParent()
public function testPreferOwnTranslationDomain()
{
$view = $this->factory
->createNamedBuilder('parent', 'Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, array(
'translation_domain' => 'parent_domain',
))
->add('child', $this->getTestedType(), array(
Expand All @@ -109,7 +111,7 @@ public function testPreferOwnTranslationDomain()

public function testDefaultTranslationDomain()
{
$view = $this->factory->createNamedBuilder('parent', 'Symfony\Component\Form\Extension\Core\Type\FormType')
$view = $this->factory->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE)
->add('child', $this->getTestedType())
->getForm()
->createView();
Expand All @@ -119,19 +121,32 @@ public function testDefaultTranslationDomain()

public function testPassLabelToView()
{
$form = $this->factory->createNamed('__test___field', $this->getTestedType(), null, array('label' => 'My label'));
$view = $form->createView();
$view = $this->factory->createNamed('__test___field', $this->getTestedType(), null, array('label' => 'My label'))
->createView();

$this->assertSame('My label', $view->vars['label']);
}

public function testPassMultipartFalseToView()
{
$form = $this->factory->create($this->getTestedType());
$view = $form->createView();
$view = $this->factory->create($this->getTestedType())
->createView();

$this->assertFalse($view->vars['multipart']);
}

abstract protected function getTestedType();
public function testSubmitNull($expected = null, $norm = null, $view = null)
{
$form = $this->factory->create($this->getTestedType());
$form->submit(null);

$this->assertSame($expected, $form->getData());
$this->assertSame($norm, $form->getNormData());
$this->assertSame($view, $form->getViewData());
}

protected function getTestedType()
{
return static::TESTED_TYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
/**
* @author Stepan Anchugov <kixxx1@gmail.com>
*/
class BirthdayTypeTest extends BaseTypeTest
class BirthdayTypeTest extends DateTypeTest
{
const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\BirthdayType';

/**
* @group legacy
*/
Expand All @@ -31,13 +33,8 @@ public function testLegacyName()
*/
public function testSetInvalidYearsOption()
{
$this->factory->create('Symfony\Component\Form\Extension\Core\Type\BirthdayType', null, array(
$this->factory->create(static::TESTED_TYPE, null, array(
'years' => 'bad value',
));
}

protected function getTestedType()
{
return 'Symfony\Component\Form\Extension\Core\Type\BirthdayType';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class ButtonTypeTest extends BaseTypeTest
{
const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\ButtonType';

/**
* @group legacy
*/
Expand All @@ -28,11 +30,6 @@ public function testLegacyName()

public function testCreateButtonInstances()
{
$this->assertInstanceOf('Symfony\Component\Form\Button', $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ButtonType'));
}

protected function getTestedType()
{
return 'Symfony\Component\Form\Extension\Core\Type\ButtonType';
$this->assertInstanceOf('Symfony\Component\Form\Button', $this->factory->create(static::TESTED_TYPE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Test\TypeTestCase;

class CheckboxTypeTest extends TypeTestCase
class CheckboxTypeTest extends BaseTypeTest
{
const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\CheckboxType';

/**
* @group legacy
*/
Expand All @@ -28,7 +29,7 @@ public function testLegacyName()

public function testDataIsFalseByDefault()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType');
$form = $this->factory->create(static::TESTED_TYPE);

$this->assertFalse($form->getData());
$this->assertFalse($form->getNormData());
Expand All @@ -37,42 +38,42 @@ public function testDataIsFalseByDefault()

public function testPassValueToView()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array('value' => 'foobar'));
$view = $form->createView();
$view = $this->factory->create(static::TESTED_TYPE, null, array('value' => 'foobar'))
->createView();

$this->assertEquals('foobar', $view->vars['value']);
}

public function testCheckedIfDataTrue()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType');
$form->setData(true);
$view = $form->createView();
$view = $this->factory->create(static::TESTED_TYPE)
->setData(true)
->createView();

$this->assertTrue($view->vars['checked']);
}

public function testCheckedIfDataTrueWithEmptyValue()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array('value' => ''));
$form->setData(true);
$view = $form->createView();
$view = $this->factory->create(static::TESTED_TYPE, null, array('value' => ''))
->setData(true)
->createView();

$this->assertTrue($view->vars['checked']);
}

public function testNotCheckedIfDataFalse()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType');
$form->setData(false);
$view = $form->createView();
$view = $this->factory->create(static::TESTED_TYPE)
->setData(false)
->createView();

$this->assertFalse($view->vars['checked']);
}

public function testSubmitWithValueChecked()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'value' => 'foobar',
));
$form->submit('foobar');
Expand All @@ -83,7 +84,7 @@ public function testSubmitWithValueChecked()

public function testSubmitWithRandomValueChecked()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'value' => 'foobar',
));
$form->submit('krixikraxi');
Expand All @@ -94,7 +95,7 @@ public function testSubmitWithRandomValueChecked()

public function testSubmitWithValueUnchecked()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'value' => 'foobar',
));
$form->submit(null);
Expand All @@ -105,7 +106,7 @@ public function testSubmitWithValueUnchecked()

public function testSubmitWithEmptyValueChecked()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'value' => '',
));
$form->submit('');
Expand All @@ -116,7 +117,7 @@ public function testSubmitWithEmptyValueChecked()

public function testSubmitWithEmptyValueUnchecked()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'value' => '',
));
$form->submit(null);
Expand All @@ -127,7 +128,7 @@ public function testSubmitWithEmptyValueUnchecked()

public function testSubmitWithEmptyValueAndFalseUnchecked()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'value' => '',
));
$form->submit(false);
Expand All @@ -138,7 +139,7 @@ public function testSubmitWithEmptyValueAndFalseUnchecked()

public function testSubmitWithEmptyValueAndTrueChecked()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'value' => '',
));
$form->submit(true);
Expand All @@ -162,7 +163,7 @@ function ($value) {
}
);

$form = $this->factory->createBuilder('Symfony\Component\Form\Extension\Core\Type\CheckboxType')
$form = $this->factory->createBuilder(static::TESTED_TYPE)
->addModelTransformer($transformer)
->getForm();

Expand All @@ -181,4 +182,9 @@ public function provideCustomModelTransformerData()
array('unchecked', false),
);
}

public function testSubmitNull($expected = null, $norm = null, $view = null)
{
parent::testSubmitNull(false, false, null);
}
}
Loading