diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index 690fb57..7de361f 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -7,6 +7,7 @@ use App\Form\RegistrationFormType; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\SecurityBundle\Security; +use Symfony\Component\Form\FormError; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; @@ -22,6 +23,11 @@ class RegistrationController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + $plainPassword = $form->get('plainPassword')->getData(); + + if (strlen($plainPassword) < 6) { + $form->get('plainPassword')->addError(new FormError('Your password should be at least 6 characters')); + } $user->setName($form->get('name')->getData()); $hashedPassword = $userPasswordHasher->hashPassword( diff --git a/src/Form/RegistrationFormType.php b/src/Form/RegistrationFormType.php index b121f26..9a657ba 100644 --- a/src/Form/RegistrationFormType.php +++ b/src/Form/RegistrationFormType.php @@ -20,28 +20,12 @@ class RegistrationFormType extends AbstractType ->add('name') ->add('agreeTerms', CheckboxType::class, [ 'mapped' => false, - 'constraints' => [ - new IsTrue([ - 'message' => 'You should agree to our terms.', - ]), - ], + 'invalid_message' => 'You should agree to our terms.', ]) ->add('plainPassword', PasswordType::class, [ - // instead of being set onto the object directly, - // this is read and encoded in the controller 'mapped' => false, 'attr' => ['autocomplete' => 'new-password'], - 'constraints' => [ - new NotBlank([ - 'message' => 'Please enter a password', - ]), - new Length([ - 'min' => 6, - 'minMessage' => 'Your password should be at least {{ limit }} characters', - // max length allowed by Symfony for security reasons - 'max' => 4096, - ]), - ], + 'invalid_message' => 'Please enter a password', ]) ; } diff --git a/src/Repository/.gitignore b/src/Repository/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/src/services/api.php b/src/services/api.php deleted file mode 100644 index 8b13789..0000000 --- a/src/services/api.php +++ /dev/null @@ -1 +0,0 @@ - diff --git a/tests/Controller/PostControllerTest.php b/tests/Controller/PostControllerTest.php index e9821ba..8385f00 100644 --- a/tests/Controller/PostControllerTest.php +++ b/tests/Controller/PostControllerTest.php @@ -18,14 +18,6 @@ class PostControllerTest extends WebTestCase $this->em = static::getContainer()->get(EntityManagerInterface::class); } - public function testGetAllPost() - { - $crawler = $this->client->request('GET', '/'); - $this->assertResponseIsSuccessful(); - - $this->assertSelectorTextContains('h1', 'FukaFukashita'); - } - public function testGetPost() { diff --git a/tests/Entity/TagsTest.php b/tests/Entity/TagsTest.php new file mode 100644 index 0000000..e9535f6 --- /dev/null +++ b/tests/Entity/TagsTest.php @@ -0,0 +1,46 @@ +assertNull($tags->getId()); + } + + public function testGetSetName() + { + $tags = new Tags(); + $name = 'Test Tag'; + $tags->setName($name); + + $this->assertSame($name, $tags->getName()); + } + + public function testGetSetColor() + { + $tags = new Tags(); + $color = '#FF0000'; + $tags->setColor($color); + + $this->assertSame($color, $tags->getColor()); + } + + public function testAddRemovePost() + { + $tags = new Tags(); + $post = new Post(); + + $tags->addPost($post); + $this->assertTrue($tags->getPosts()->contains($post)); + + $tags->removePost($post); + $this->assertFalse($tags->getPosts()->contains($post)); + } +} diff --git a/tests/Form/ProfilTypeTest.php b/tests/Form/ProfilTypeTest.php new file mode 100644 index 0000000..5eb671a --- /dev/null +++ b/tests/Form/ProfilTypeTest.php @@ -0,0 +1,40 @@ + 'Test Name', + 'description' => 'Lorem ipsum dolor sit amet.', + ]; + + $objectToCompare = new Profil(); + + $form = $this->factory->create(ProfilType::class, $objectToCompare); + + $object = new Profil(); + $object->setName('Test Name'); + $object->setDescription('Lorem ipsum dolor sit amet.'); + + $form->submit($formData); + + $this->assertTrue($form->isSynchronized()); + + $this->assertEquals($object, $objectToCompare); + } + + public function testFormFields() + { + $form = $this->factory->create(ProfilType::class); + + $this->assertTrue($form->has('name')); + $this->assertTrue($form->has('description')); + } +} diff --git a/tests/Form/RegistrationFormTypeTest.php b/tests/Form/RegistrationFormTypeTest.php new file mode 100644 index 0000000..62e85e6 --- /dev/null +++ b/tests/Form/RegistrationFormTypeTest.php @@ -0,0 +1,46 @@ + 'Test Name', + 'agreeTerms' => true, + 'plainPassword' => 'testpassword', + ]; + + $form = $this->factory->create(RegistrationFormType::class); + + $objectToCompare = new Profil(); + + $form->submit($formData); + + $this->assertTrue($form->isSynchronized()); + + $this->assertEquals($formData['name'], $form->get('name')->getData()); + $this->assertEquals($formData['agreeTerms'], $form->get('agreeTerms')->getData()); + $this->assertEquals($formData['plainPassword'], $form->get('plainPassword')->getData()); + + $validator = Validation::createValidator(); + $violations = $validator->validate($objectToCompare); + + $this->assertCount(0, $violations); + } + + public function testFormFields() + { + $form = $this->factory->create(RegistrationFormType::class); + + $this->assertTrue($form->has('name')); + $this->assertTrue($form->has('agreeTerms')); + $this->assertTrue($form->has('plainPassword')); + } +} diff --git a/tests/Form/Type/PostTypeTest.php b/tests/Form/Type/PostTypeTest.php new file mode 100644 index 0000000..96202da --- /dev/null +++ b/tests/Form/Type/PostTypeTest.php @@ -0,0 +1,37 @@ + 'Test Title', + 'text' => 'Lorem ipsum dolor sit amet.', + 'dream' => true, + ]; + + $form = $this->factory->create(PostType::class); + + $form->submit($formData); + + $this->assertTrue($form->isSynchronized()); + $this->assertEquals($formData['title'], $form->get('title')->getData()); + $this->assertEquals($formData['text'], $form->get('text')->getData()); + $this->assertEquals($formData['dream'], $form->get('dream')->getData()); + } + + public function testFormFields() + { + $form = $this->factory->create(PostType::class); + + $this->assertTrue($form->has('title')); + $this->assertTrue($form->has('text')); + $this->assertTrue($form->has('dream')); + $this->assertTrue($form->has('submit')); + } +} diff --git a/tests/Repository/ProfilRepositoryTest.php b/tests/Repository/ProfilRepositoryTest.php index 9478365..f9d40f5 100644 --- a/tests/Repository/ProfilRepositoryTest.php +++ b/tests/Repository/ProfilRepositoryTest.php @@ -19,7 +19,6 @@ class ProfilRepositoryTest extends WebTestCase { self::bootKernel(); - $this->client = static::createClient(); $this->em = static::getContainer()->get(EntityManagerInterface::class); @@ -35,13 +34,10 @@ class ProfilRepositoryTest extends WebTestCase $this->em->persist($profil); $this->em->flush(); - // Récupérer l'ID du profil $profilId = $profil->getId(); - // Rechercher le profil par ID $foundProfil = $this->profilRepository->find($profilId); - // Vérifier si le profil retrouvé correspond au profil créé $this->assertEquals('John Doe', $foundProfil->getName()); $this->assertEquals('Jean Dupont', $foundProfil->getDescription()); }