From 985e4b8c9d672c7169c6694aa367862530282abc Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 13 Feb 2022 09:39:52 +0100 Subject: [PATCH] Add 18.1: Flagging Comments --- migrations/Version20220213083946.php | 33 +++++++++++++++++++ .../Admin/CommentCrudController.php | 1 + src/DataFixtures/AppFixtures.php | 8 +++++ src/Entity/Comment.php | 15 +++++++++ src/Repository/CommentRepository.php | 2 ++ tests/Controller/ConferenceControllerTest.php | 10 +++++- 6 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 migrations/Version20220213083946.php diff --git a/migrations/Version20220213083946.php b/migrations/Version20220213083946.php new file mode 100644 index 0000000..e8f0091 --- /dev/null +++ b/migrations/Version20220213083946.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE comment ADD state VARCHAR(255) DEFAULT \'submitted\' NOT NULL'); + $this->addSql("UPDATE comment SET state='published'"); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE comment DROP state'); + } +} diff --git a/src/Controller/Admin/CommentCrudController.php b/src/Controller/Admin/CommentCrudController.php index e28eabd..b36c979 100644 --- a/src/Controller/Admin/CommentCrudController.php +++ b/src/Controller/Admin/CommentCrudController.php @@ -51,6 +51,7 @@ public function configureFields(string $pageName): iterable ->setLabel('Photo') ->onlyOnIndex() ; + yield TextField::new('state'); $createdAt = DateTimeField::new('createdAt')->setFormTypeOptions([ 'html5' => true, diff --git a/src/DataFixtures/AppFixtures.php b/src/DataFixtures/AppFixtures.php index ee6c5ea..c5a185b 100644 --- a/src/DataFixtures/AppFixtures.php +++ b/src/DataFixtures/AppFixtures.php @@ -37,8 +37,16 @@ public function load(ObjectManager $manager): void $comment1->setAuthor('Fabien'); $comment1->setEmail('fabien@example.com'); $comment1->setText('This was a great conference.'); + $comment1->setState('published'); $manager->persist($comment1); + $comment2 = new Comment(); + $comment2->setConference($amsterdam); + $comment2->setAuthor('Lucas'); + $comment2->setEmail('lucas@example.com'); + $comment2->setText('I think this one is going to be moderated.'); + $manager->persist($comment2); + $admin = new Admin(); $admin->setRoles(['ROLE_ADMIN']); $admin->setUsername('admin'); diff --git a/src/Entity/Comment.php b/src/Entity/Comment.php index 3b8613c..3476849 100644 --- a/src/Entity/Comment.php +++ b/src/Entity/Comment.php @@ -38,6 +38,9 @@ class Comment #[ORM\Column(type: 'string', length: 255, nullable: true)] private $photoFilename; + #[ORM\Column(type: 'string', length: 255, options: ["default" => "submitted"])] + private $state = 'submitted'; + public function __toString(): string { return (string) $this->getEmail(); @@ -125,4 +128,16 @@ public function setPhotoFilename(?string $photoFilename): self return $this; } + + public function getState(): ?string + { + return $this->state; + } + + public function setState(string $state): self + { + $this->state = $state; + + return $this; + } } diff --git a/src/Repository/CommentRepository.php b/src/Repository/CommentRepository.php index d737677..000e98f 100644 --- a/src/Repository/CommentRepository.php +++ b/src/Repository/CommentRepository.php @@ -27,7 +27,9 @@ public function getCommentPaginator(Conference $conference, int $offset): Pagina { $query = $this->createQueryBuilder('c') ->andWhere('c.conference = :conference') + ->andWhere('c.state = :state') ->setParameter('conference', $conference) + ->setParameter('state', 'published') ->orderBy('c.createdAt', 'DESC') ->setMaxResults(self::PAGINATOR_PER_PAGE) ->setFirstResult($offset) diff --git a/tests/Controller/ConferenceControllerTest.php b/tests/Controller/ConferenceControllerTest.php index 76d4d5a..c845b61 100644 --- a/tests/Controller/ConferenceControllerTest.php +++ b/tests/Controller/ConferenceControllerTest.php @@ -2,6 +2,8 @@ namespace App\Tests\Controller; +use App\Repository\CommentRepository; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class ConferenceControllerTest extends WebTestCase @@ -22,10 +24,16 @@ public function testCommentSubmission() $client->submitForm('Submit', [ 'comment_form[author]' => 'Fabien', 'comment_form[text]' => 'Some feedback from an automated functional test', - 'comment_form[email]' => 'me@automat.ed', + 'comment_form[email]' => $email = 'me@automat.ed', 'comment_form[photo]' => dirname(__DIR__, 2).'/public/images/under-construction.gif', ]); $this->assertResponseRedirects(); + + // simulate comment validation + $comment = self::getContainer()->get(CommentRepository::class)->findOneByEmail($email); + $comment->setState('published'); + self::getContainer()->get(EntityManagerInterface::class)->flush(); + $client->followRedirect(); $this->assertSelectorExists('div:contains("There are 2 comments")'); }