|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | use Symfony\Component\EventDispatcher\EventDispatcher;
|
| 16 | +use Symfony\Component\Form\Exception\InvalidArgumentException; |
16 | 17 | use Symfony\Component\Form\FormConfigBuilder;
|
17 | 18 | use Symfony\Component\Form\NativeRequestHandler;
|
18 | 19 |
|
|
21 | 22 | */
|
22 | 23 | class FormConfigTest extends TestCase
|
23 | 24 | {
|
24 |
| - public static function getHtml4Ids() |
| 25 | + public static function provideInvalidFormInputName(): iterable |
25 | 26 | {
|
26 |
| - return [ |
27 |
| - ['z0'], |
28 |
| - ['A0'], |
29 |
| - ['A9'], |
30 |
| - ['Z0'], |
31 |
| - ['#', 'Symfony\Component\Form\Exception\InvalidArgumentException'], |
32 |
| - ['a#', 'Symfony\Component\Form\Exception\InvalidArgumentException'], |
33 |
| - ['a$', 'Symfony\Component\Form\Exception\InvalidArgumentException'], |
34 |
| - ['a%', 'Symfony\Component\Form\Exception\InvalidArgumentException'], |
35 |
| - ['a ', 'Symfony\Component\Form\Exception\InvalidArgumentException'], |
36 |
| - ["a\t", 'Symfony\Component\Form\Exception\InvalidArgumentException'], |
37 |
| - ["a\n", 'Symfony\Component\Form\Exception\InvalidArgumentException'], |
38 |
| - ['a-'], |
39 |
| - ['a_'], |
40 |
| - ['a:'], |
41 |
| - // Periods are allowed by the HTML4 spec, but disallowed by us |
42 |
| - // because they break the generated property paths |
43 |
| - ['a.', 'Symfony\Component\Form\Exception\InvalidArgumentException'], |
44 |
| - // Contrary to the HTML4 spec, we allow names starting with a |
45 |
| - // number, otherwise naming fields by collection indices is not |
46 |
| - // possible. |
47 |
| - // For root forms, leading digits will be stripped from the |
48 |
| - // "id" attribute to produce valid HTML4. |
49 |
| - ['0'], |
50 |
| - ['9'], |
51 |
| - // Contrary to the HTML4 spec, we allow names starting with an |
52 |
| - // underscore, since this is already a widely used practice in |
53 |
| - // Symfony. |
54 |
| - // For root forms, leading underscores will be stripped from the |
55 |
| - // "id" attribute to produce valid HTML4. |
56 |
| - ['_'], |
57 |
| - // Integers are allowed |
58 |
| - [0], |
59 |
| - [123], |
60 |
| - // NULL is allowed |
61 |
| - [null], |
62 |
| - ]; |
| 27 | + yield ['isindex']; |
| 28 | + |
| 29 | + yield ['#']; |
| 30 | + yield ['a#']; |
| 31 | + yield ['a$']; |
| 32 | + yield ['a%']; |
| 33 | + yield ['a ']; |
| 34 | + yield ["a\t"]; |
| 35 | + yield ["a\n"]; |
| 36 | + // Periods are allowed by the HTML4 spec, but disallowed by us |
| 37 | + // because they break the generated property paths |
| 38 | + yield ['a.']; |
63 | 39 | }
|
64 | 40 |
|
65 | 41 | /**
|
66 |
| - * @dataProvider getHtml4Ids |
| 42 | + * @dataProvider provideInvalidFormInputName |
67 | 43 | */
|
68 |
| - public function testNameAcceptsOnlyNamesValidAsIdsInHtml4($name, $expectedException = null) |
| 44 | + public function testInvalidFormInputName(string $name) |
69 | 45 | {
|
70 |
| - if (null !== $expectedException) { |
71 |
| - $this->expectException($expectedException); |
72 |
| - } |
| 46 | + $this->expectException(InvalidArgumentException::class); |
| 47 | + $this->expectExceptionMessage(sprintf('The name "%s" contains illegal characters or equals to "isindex". Names should only contain letters, digits, underscores ("_"), hyphens ("-") and colons (":").', $name)); |
73 | 48 |
|
| 49 | + new FormConfigBuilder($name, null, new EventDispatcher()); |
| 50 | + } |
| 51 | + |
| 52 | + public static function provideValidFormInputName(): iterable |
| 53 | + { |
| 54 | + yield ['z0']; |
| 55 | + yield ['A0']; |
| 56 | + yield ['A9']; |
| 57 | + yield ['Z0']; |
| 58 | + yield ['a-']; |
| 59 | + yield ['a_']; |
| 60 | + yield ['a:']; |
| 61 | + // Contrary to the HTML4 spec, we allow names starting with a |
| 62 | + // number, otherwise naming fields by collection indices is not |
| 63 | + // possible. |
| 64 | + // For root forms, leading digits will be stripped from the |
| 65 | + // "id" attribute to produce valid HTML4. |
| 66 | + yield ['0']; |
| 67 | + yield ['9']; |
| 68 | + // Contrary to the HTML4 spec, we allow names starting with an |
| 69 | + // underscore, since this is already a widely used practice in |
| 70 | + // Symfony. |
| 71 | + // For root forms, leading underscores will be stripped from the |
| 72 | + // "id" attribute to produce valid HTML4. |
| 73 | + yield ['_']; |
| 74 | + // Integers are allowed |
| 75 | + yield [0]; |
| 76 | + yield [123]; |
| 77 | + // NULL is allowed |
| 78 | + yield [null]; |
| 79 | + |
| 80 | + // Allowed in HTML 5 specification |
| 81 | + // See: https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-name |
| 82 | + yield ['_charset_']; |
| 83 | + yield ['-x']; |
| 84 | + yield [':x']; |
| 85 | + yield ['isINDEX']; |
| 86 | + |
| 87 | + // This value shouldn't be allowed. |
| 88 | + // However, many tests in Form component require empty name |
| 89 | + yield ['']; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @dataProvider provideValidFormInputName |
| 94 | + */ |
| 95 | + public function testValidFormInputName(string|int|null $name) |
| 96 | + { |
74 | 97 | $formConfigBuilder = new FormConfigBuilder($name, null, new EventDispatcher());
|
75 | 98 |
|
76 | 99 | $this->assertSame((string) $name, $formConfigBuilder->getName());
|
|
0 commit comments