Skip to content

Commit

Permalink
[Form] Fix #11694 - Enforce options value type check in some form types
Browse files Browse the repository at this point in the history
  • Loading branch information
kix authored and webmozart committed Oct 16, 2014
1 parent 7dd842c commit 0af6467
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 0 deletions.
Expand Up @@ -24,6 +24,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
$resolver->setDefaults(array(
'years' => range(date('Y') - 120, date('Y')),
));

$resolver->setAllowedTypes(array(
'years' => 'array',
));
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Expand Up @@ -237,6 +237,9 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)

$resolver->setAllowedTypes(array(
'format' => array('int', 'string'),
'years' => 'array',
'months' => 'array',
'days' => 'array',
));
}

Expand Down
Expand Up @@ -55,6 +55,12 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'second_name' => 'second',
'error_bubbling' => false,
));

$resolver->setAllowedTypes(array(
'options' => 'array',
'first_options' => 'array',
'second_options' => 'array',
));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Expand Up @@ -221,6 +221,12 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'choice',
),
));

$resolver->setAllowedTypes(array(
'hours' => 'array',
'minutes' => 'array',
'seconds' => 'array',
));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/UrlType.php
Expand Up @@ -34,6 +34,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
$resolver->setDefaults(array(
'default_protocol' => 'http',
));

$resolver->setAllowedTypes(array(
'default_protocol' => array('null', 'string'),
));
}

/**
Expand Down
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

/**
* @author Stepan Anchugov <kixxx1@gmail.com>
*/
class BirthdayTypeTest extends BaseTypeTest
{
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testSetInvalidYearsOption()
{
$this->factory->create('birthday', null, array(
'years' => 'bad value',
));
}

protected function getTestedType()
{
return 'birthday';
}
}
Expand Up @@ -340,6 +340,36 @@ public function testThrowExceptionIfFormatIsInvalid()
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfYearsIsInvalid()
{
$this->factory->create('date', null, array(
'years' => 'bad value',
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfMonthsIsInvalid()
{
$this->factory->create('date', null, array(
'months' => 'bad value',
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfDaysIsInvalid()
{
$this->factory->create('date', null, array(
'days' => 'bad value',
));
}

public function testSetDataWithDifferentTimezones()
{
$form = $this->factory->create('date', null, array(
Expand Down
Expand Up @@ -72,6 +72,39 @@ public function testSetRequired()
$this->assertFalse($form['second']->isRequired());
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testSetInvalidOptions()
{
$this->factory->create('repeated', null, array(
'type' => 'text',
'options' => 'bad value',
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testSetInvalidFirstOptions()
{
$this->factory->create('repeated', null, array(
'type' => 'text',
'first_options' => 'bad value',
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testSetInvalidSecondOptions()
{
$this->factory->create('repeated', null, array(
'type' => 'text',
'second_options' => 'bad value',
));
}

public function testSetErrorBubblingToTrue()
{
$form = $this->factory->create('repeated', null, array(
Expand Down
Expand Up @@ -673,4 +673,34 @@ public function testInitializeWithSecondsAndWithoutMinutes()
'with_seconds' => true,
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfHoursIsInvalid()
{
$this->factory->create('time', null, array(
'hours' => 'bad value',
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfMinutesIsInvalid()
{
$this->factory->create('time', null, array(
'minutes' => 'bad value',
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfSecondsIsInvalid()
{
$this->factory->create('time', null, array(
'seconds' => 'bad value',
));
}
}
Expand Up @@ -70,4 +70,14 @@ public function testSubmitAddsNoDefaultProtocolIfSetToNull()
$this->assertSame('www.domain.com', $form->getData());
$this->assertSame('www.domain.com', $form->getViewData());
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfDefaultProtocolIsInvalid()
{
$this->factory->create('url', null, array(
'default_protocol' => array(),
));
}
}

0 comments on commit 0af6467

Please sign in to comment.