Skip to content
This repository was archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Add 18.1: Flagging Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Feb 13, 2022
1 parent 58a6097 commit 985e4b8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
33 changes: 33 additions & 0 deletions migrations/Version20220213083946.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220213083946 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}
1 change: 1 addition & 0 deletions src/Controller/Admin/CommentCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function configureFields(string $pageName): iterable
->setLabel('Photo')
->onlyOnIndex()
;
yield TextField::new('state');

$createdAt = DateTimeField::new('createdAt')->setFormTypeOptions([
'html5' => true,
Expand Down
8 changes: 8 additions & 0 deletions src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ public function load(ObjectManager $manager): void
$comment1->setAuthor('Fabien');
$comment1->setEmail('[email protected]');
$comment1->setText('This was a great conference.');
$comment1->setState('published');
$manager->persist($comment1);

$comment2 = new Comment();
$comment2->setConference($amsterdam);
$comment2->setAuthor('Lucas');
$comment2->setEmail('[email protected]');
$comment2->setText('I think this one is going to be moderated.');
$manager->persist($comment2);

$admin = new Admin();
$admin->setRoles(['ROLE_ADMIN']);
$admin->setUsername('admin');
Expand Down
15 changes: 15 additions & 0 deletions src/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}
2 changes: 2 additions & 0 deletions src/Repository/CommentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion tests/Controller/ConferenceControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]' => '[email protected]',
'comment_form[email]' => $email = '[email protected]',
'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")');
}
Expand Down

0 comments on commit 985e4b8

Please sign in to comment.