2021-07-22 13:31:19 +01:00
< ? php
2021-10-10 09:26:18 +01:00
declare ( strict_types = 1 );
2021-07-22 13:31:19 +01:00
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
2022-03-14 18:33:47 +00:00
namespace App\Test\Core ;
2021-07-22 13:31:19 +01:00
2022-03-13 18:53:53 +00:00
use App\Core\ActorLocalRoles ;
2022-03-27 15:19:09 +01:00
use App\Core\DB ;
2021-07-22 13:31:19 +01:00
use App\Core\Form ;
2021-09-18 03:22:27 +01:00
use App\Entity\Actor ;
2021-08-19 19:14:37 +01:00
use App\Util\Exception\ServerException ;
2021-07-22 13:31:19 +01:00
use App\Util\Form\ArrayTransformer ;
use App\Util\GNUsocialTestCase ;
2021-08-19 19:14:37 +01:00
use Jchook\AssertThrows\AssertThrows ;
2021-07-22 13:31:19 +01:00
use Symfony\Component\Form\Extension\Core\Type\SubmitType ;
use Symfony\Component\Form\Extension\Core\Type\TextareaType ;
use Symfony\Component\Form\Form as SymfForm ;
use Symfony\Component\HttpFoundation\Request ;
class FormTest extends GNUsocialTestCase
{
2021-08-19 19:14:37 +01:00
use AssertThrows ;
2021-07-22 13:31:19 +01:00
public function testCreate ()
{
parent :: bootKernel ();
$form = Form :: create ( $form_array = [
[ 'content' , TextareaType :: class , [ 'label' => ' ' , 'data' => '' , 'attr' => [ 'placeholder' => 'placeholder' ]]],
[ 'array_trans' , TextareaType :: class , [ 'data' => [ 'foo' , 'bar' ], 'transformer' => ArrayTransformer :: class ]],
2021-08-19 19:14:37 +01:00
[ 'testpost' , SubmitType :: class , [ 'label' => 'Post' ]],
2021-07-22 13:31:19 +01:00
]);
2021-10-10 09:26:18 +01:00
static :: assertSame ( \get_class ( $form ), 'Symfony\\Component\\Form\\Form' );
2021-07-22 13:31:19 +01:00
foreach ( $form as $name => $f ) {
2021-08-19 01:09:42 +01:00
if ( $name == 'testpost' ) {
2021-10-10 09:26:18 +01:00
static :: assertSame ( \get_class ( $f ), 'Symfony\Component\Form\SubmitButton' );
2021-07-22 13:31:19 +01:00
} else {
2021-10-10 09:26:18 +01:00
static :: assertSame ( \get_class ( $f ), 'Symfony\Component\Form\Form' );
2021-07-22 13:31:19 +01:00
}
$config = $f -> getConfig ();
$form_options = $config -> getOptions ();
$form_class = $config -> getType () -> getInnerType ();
$found = false ;
foreach ( $form_array as [ $array_name , $array_class , $options ]) {
if ( $name === $array_name ) {
$found = true ;
2021-10-10 09:26:18 +01:00
static :: assertSame ( \get_class ( $form_class ), $array_class );
2021-07-22 13:31:19 +01:00
foreach ([ 'label' , 'attr' , 'data' ] as $field ) {
if ( isset ( $options [ $field ])) {
static :: assertSame ( $form_options [ $field ], $options [ $field ]);
}
}
break ;
}
}
static :: assertTrue ( $found );
2021-10-10 09:26:18 +01:00
static :: assertSame ( \get_class ( $f -> getParent ()), 'Symfony\\Component\\Form\\Form' );
2021-07-22 13:31:19 +01:00
}
static :: assertTrue ( Form :: isRequired ( $form_array , 'content' ));
}
2021-08-19 19:14:37 +01:00
/**
* Using 'save' or 'submit' as a form name is not allowed , becuase then they ' re likely to
* collide with other forms in the same page
*/
public function testDisallowedGenericFormName ()
{
static :: assertThrows ( ServerException :: class , fn () => Form :: create ([[ 'save' , SubmitType :: class , []]]));
}
2021-07-22 13:31:19 +01:00
/**
* Test creating a form with default values pulled from an existing object . Can be used in conjunction with `Form::hanlde` to update said object
*/
public function testCreateUpdateObject ()
{
2021-11-15 19:27:39 +00:00
$nick = 'form_testing_new_user' ;
$actor = Actor :: create ([ 'nickname' => $nick , 'fullname' => $nick ]);
$form = Form :: create ([
[ 'nickname' , TextareaType :: class , []],
[ 'fullname' , TextareaType :: class , []],
[ 'testpost' , SubmitType :: class , []],
], target : $actor );
2021-07-22 13:31:19 +01:00
$options = $form [ 'nickname' ] -> getConfig () -> getOptions ();
static :: assertSame ( $nick , $options [ 'data' ]);
}
public function testHandle ()
{
parent :: bootKernel ();
$data = [ 'fullname' => 'Full Name' , 'homepage' => 'gnu.org' ];
$mock_request = static :: createMock ( Request :: class );
2022-03-13 18:53:53 +00:00
$mock_request -> method ( 'getMethod' ) -> willReturn ( 'POST' );
$mock_form = static :: createMock ( SymfForm :: class );
2021-07-22 13:31:19 +01:00
$mock_form -> method ( 'handleRequest' );
$mock_form -> method ( 'isSubmitted' ) -> willReturn ( true );
$mock_form -> method ( 'isValid' ) -> willReturn ( true );
$mock_form -> method ( 'getData' ) -> willReturn ( $data );
2022-02-15 21:16:58 +00:00
$ret = Form :: handle ( form_definition : [ /* not normal usage */ ], request : $mock_request , target : null , extra_args : [], before_step : null , after_step : null , create_args : [], testing_only_form : $mock_form );
2021-07-22 13:31:19 +01:00
static :: assertSame ( $data , $ret );
2022-03-13 18:53:53 +00:00
$actor = Actor :: create ([ 'nickname' => 'form_testing_new_user' , 'is_local' => false , 'roles' => ActorLocalRoles :: PARTICIPANT | ActorLocalRoles :: VISITOR , 'type' => Actor :: PERSON ]);
2021-11-15 19:27:39 +00:00
DB :: persist ( $actor );
2022-02-15 21:16:58 +00:00
$ret = Form :: handle ( form_definition : [ /* not normal usage */ ], request : $mock_request , target : $actor , extra_args : [], before_step : null , after_step : null , create_args : [], testing_only_form : $mock_form );
2021-07-22 13:31:19 +01:00
static :: assertSame ( $mock_form , $ret );
2021-11-15 19:27:39 +00:00
static :: assertSame ( $data [ 'fullname' ], $actor -> getFullname ());
static :: assertSame ( $data [ 'homepage' ], $actor -> getHomepage ());
2021-07-22 13:31:19 +01:00
}
}