FormTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace App\Tests\Core;
  20. use App\Core\DB\DB;
  21. use App\Core\Form;
  22. use App\Entity\Actor;
  23. use App\Util\Exception\ServerException;
  24. use App\Util\Form\ArrayTransformer;
  25. use App\Util\GNUsocialTestCase;
  26. use Jchook\AssertThrows\AssertThrows;
  27. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  28. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  29. use Symfony\Component\Form\Form as SymfForm;
  30. use Symfony\Component\HttpFoundation\Request;
  31. class FormTest extends GNUsocialTestCase
  32. {
  33. use AssertThrows;
  34. public function testCreate()
  35. {
  36. parent::bootKernel();
  37. $form = Form::create($form_array = [
  38. ['content', TextareaType::class, ['label' => ' ', 'data' => '', 'attr' => ['placeholder' => 'placeholder']]],
  39. ['array_trans', TextareaType::class, ['data' => ['foo', 'bar'], 'transformer' => ArrayTransformer::class]],
  40. ['testpost', SubmitType::class, ['label' => 'Post']],
  41. ]);
  42. static::assertSame(\get_class($form), 'Symfony\\Component\\Form\\Form');
  43. foreach ($form as $name => $f) {
  44. if ($name == 'testpost') {
  45. static::assertSame(\get_class($f), 'Symfony\Component\Form\SubmitButton');
  46. } else {
  47. static::assertSame(\get_class($f), 'Symfony\Component\Form\Form');
  48. }
  49. $config = $f->getConfig();
  50. $form_options = $config->getOptions();
  51. $form_class = $config->getType()->getInnerType();
  52. $found = false;
  53. foreach ($form_array as [$array_name, $array_class, $options]) {
  54. if ($name === $array_name) {
  55. $found = true;
  56. static::assertSame(\get_class($form_class), $array_class);
  57. foreach (['label', 'attr', 'data'] as $field) {
  58. if (isset($options[$field])) {
  59. static::assertSame($form_options[$field], $options[$field]);
  60. }
  61. }
  62. break;
  63. }
  64. }
  65. static::assertTrue($found);
  66. static::assertSame(\get_class($f->getParent()), 'Symfony\\Component\\Form\\Form');
  67. }
  68. static::assertTrue(Form::isRequired($form_array, 'content'));
  69. }
  70. /**
  71. * Using 'save' or 'submit' as a form name is not allowed, becuase then they're likely to
  72. * collide with other forms in the same page
  73. */
  74. public function testDisallowedGenericFormName()
  75. {
  76. static::assertThrows(ServerException::class, fn () => Form::create([['save', SubmitType::class, []]]));
  77. }
  78. /**
  79. * Test creating a form with default values pulled from an existing object. Can be used in conjunction with `Form::hanlde` to update said object
  80. */
  81. public function testCreateUpdateObject()
  82. {
  83. $nick = 'form_testing_new_user';
  84. $actor = Actor::create(['nickname' => $nick, 'fullname' => $nick]);
  85. $form = Form::create([
  86. ['nickname', TextareaType::class, []],
  87. ['fullname', TextareaType::class, []],
  88. ['testpost', SubmitType::class, []],
  89. ], target: $actor);
  90. $options = $form['nickname']->getConfig()->getOptions();
  91. static::assertSame($nick, $options['data']);
  92. }
  93. public function testHandle()
  94. {
  95. parent::bootKernel();
  96. $data = ['fullname' => 'Full Name', 'homepage' => 'gnu.org'];
  97. $mock_request = static::createMock(Request::class);
  98. $mock_form = static::createMock(SymfForm::class);
  99. $mock_form->method('handleRequest');
  100. $mock_form->method('isSubmitted')->willReturn(true);
  101. $mock_form->method('isValid')->willReturn(true);
  102. $mock_form->method('getData')->willReturn($data);
  103. $ret = Form::handle(form_definition: [/* not normal usage */], request: $mock_request, target: null, extra_args: [], extra_step: null, create_args: [], testing_only_form: $mock_form);
  104. static::assertSame($data, $ret);
  105. $actor = Actor::create(['nickname' => 'form_testing_new_user', 'is_local' => false]);
  106. DB::persist($actor);
  107. $ret = Form::handle(form_definition: [/* not normal usage */], request: $mock_request, target: $actor, extra_args: [], extra_step: null, create_args: [], testing_only_form: $mock_form);
  108. static::assertSame($mock_form, $ret);
  109. static::assertSame($data['fullname'], $actor->getFullname());
  110. static::assertSame($data['homepage'], $actor->getHomepage());
  111. }
  112. }