FormTest.php 5.2 KB

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