8000 [Form] Fix #11694 - Enforce options value type check in some form types · symfony/symfony@0af6467 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0af6467

Browse files
kixwebmozart
authored andcommitted
[Form] Fix #11694 - Enforce options value type check in some form types
1 parent 7dd842c commit 0af6467

File tree

10 files changed

+159
-0
lines changed

10 files changed

+159
-0
lines changed

src/Symfony/Component/Form/Extension/Core/Type/BirthdayType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
2424
$resolver->setDefaults(array(
2525
'years' => range(date('Y') - 120, date('Y')),
2626
));
27+
28+
$resolver->setAllowedTypes(array(
29+
'years' => 'array',
30+
));
2731
}
2832

2933
/**

src/Symfony/Component/Form/Extension/Core/Type/DateType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
237237

238238
$resolver->setAllowedTypes(array(
239239
'format' => array('int', 'string'),
240+
'years' => 'array',
241+
'months' => 'array',
242+
'days' => 'array',
240243
));
241244
}
242245

src/Symfony/Component/Form/Extension/Core/Type/RepeatedType.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
5555
'second_name' => 'second',
5656
'error_bubbling' => false,
5757
));
58+
59+
$resolver->setAllowedTypes(array(
60+
'options' => 'array',
61+
'first_options' => 'array',
62+
'second_options' => 'array',
63+
));
5864
}
5965

6066
/**

src/Symfony/Component/Form/Extension/Core/Type/TimeType.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
221221
'choice',
222222
),
223223
));
224+
225+
$resolver->setAllowedTypes(array(
226+
'hours' => 'array',
227+
'minutes' => 'array',
228+
'seconds' => 'array',
229+
));
224230
}
225231

226232
/**

src/Symfony/Component/Form/Extension/Core/Type/UrlType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
3434
$resolver->setDefaults(array(
3535
'default_protocol' => 'http',
3636
));
37+
38+
$resolver->setAllowedTypes(array(
39+
'default_protocol' => array('null', 'string'),
40+
));
3741
}
3842

3943
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
13+
14+
/**
15+
* @author Stepan Anchugov <kixxx1@gmail.com>
16+
*/
17+
class BirthdayTypeTest extends BaseTypeTest
18+
{
19+
/**
20+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
21+
*/
22+
public function testSetInvalidYearsOption()
23+
{
24+
$this->factory->create('birthday', null, array(
25+
'years' => 'bad value',
26+
));
27+
}
28+
29+
protected function getTestedType()
30+
{
31+
return 'birthday';
32+
}
33+
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,36 @@ public function testThrowExceptionIfFormatIsInvalid()
340340
));
341341
}
342342

343+
/**
344+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
345+
*/
346+
public function testThrowExceptionIfYearsIsInvalid()
347+
{
348+
$this->factory->create('date', null, array(
349+
'years' => 'bad value',
350+
));
351+
}
352+
353+
/**
354+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
355+
*/
356+
public function testThrowExceptionIfMonthsIsInvalid()
357+
{
358+
$this->factory->create('date', null, array(
359+
'months' => 'bad value',
360+
));
361+
}
362+
363+
/**
364+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
365+
*/
366+
public function testThrowExceptionIfDaysIsInvalid()
367+
{
368+
$this->factory->create('date', null, array(
369+
'days' => 'bad value',
370+
));
371+
}
372+
343373
public function testSetDataWithDifferentTimezones()
344374
{
345375
$form = $this->factory->create('date', null, array(

src/Symfony/Component/Form/Tests/Extension/Core/Type/RepeatedTypeTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,39 @@ public function testSetRequired()
7272
$this->assertFalse($form['second']->isRequired());
7373
}
7474

75+
/**
76+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
77+
*/
78+
public function testSetInvalidOptions()
79+
{
80+
$this->factory->create('repeated', null, array(
81+
'type' => 'text',
82+
'options' => 'bad value',
83+
));
84+
}
85+
86+
/**
87+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
88+
*/
89+
public function testSetInvalidFirstOptions()
90+
{
91+
$this->factory->create('repeated', null, array(
92+
'type' => 'text',
93+
'first_options' => 'bad value',
94+
));
95+
}
96+
97+
/**
98+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
99+
*/
100+
public function testSetInvalidSecondOptions()
101+
{
102+
$this->factory->create('repeated', null, array(
103+
'type' => 'text',
104+
'second_options' => 'bad value',
105+
));
106+
}
107+
75108
public function testSetErrorBubblingToTrue()
76109
{
77110
$form = $this->factory->create('repeated', null, array(

src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,4 +673,34 @@ public function testInitializeWithSecondsAndWithoutMinutes()
673673
'with_seconds' => true,
674674
));
675675
}
676+
677+
/**
678+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
679+
*/
680+
public function testThrowExceptionIfHoursIsInvalid()
681+
{
682+
$this->factory->create('time', null, array(
683+
'hours' => 'bad value',
684+
));
685+
}
686+
687+
/**
688+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
689+
*/
690+
public function testThrowExceptionIfMinutesIsInvalid()
691+
{
692+
$this->factory->create('time', null, array(
693+
'minutes' => 'bad value',
694+
));
695+
}
696+
697+
/**
698+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
699+
*/
700+
public function testThrowExceptionIfSecondsIsInvalid()
701+
{
702+
$this->factory->create('time', null, array(
703+
'seconds' => 'bad value',
704+
));
705+
}
676706
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,14 @@ public function testSubmitAddsNoDefaultProtocolIfSetToNull()
7070
$this->assertSame('www.domain.com', $form->getData());
7171
$this->assertSame('www.domain.com', $form->getViewData());
7272
}
73+
74+
/**
75+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
76+
*/
77+
public function testThrowExceptionIfDefaultProtocolIsInvalid()
78+
{
79+
$this->factory->create('url', null, array(
80+
'default_protocol' => array(),
81+
));
82+
}
7383
}

0 commit comments

Comments
 (0)
0