update fixtures

This commit is contained in:
Tykayn 2021-04-26 13:47:44 +02:00 committed by tykayn
parent 2634228af0
commit dd1de88635
3 changed files with 75 additions and 56 deletions

View File

@ -38,41 +38,44 @@ class PollController extends EmailsController {
$data = $pollRepository->findAll(); $data = $pollRepository->findAll();
$polls = $data; $polls = $data;
$titles=[]; $titles = [];
$pollData = [ $pollData = [
'message' => 'here are your polls', 'message' => 'here are your polls',
'count' => count($polls), 'count' => count( $polls ),
]; ];
$debug=1; $debug = 1;
if($debug){ if ( $debug ) {
foreach ( $polls as $poll ) { foreach ( $polls as $poll ) {
$titles[] = ['title' => $poll->getTitle(), $titles[] = [
'slug' => $poll->getCustomUrl() 'title' => $poll->getTitle(),
'slug' => $poll->getCustomUrl(),
]; ];
} }
$pollData['polls'] = $titles; $pollData[ 'polls' ] = $titles;
} }
return $this->json( $pollData); return $this->json( $pollData );
} }
/** /**
* @param $id * @param $id
* message when the poll is not found * message when the poll is not found
*
* @return JsonResponse * @return JsonResponse
*/ */
public function notFoundPoll($id): Response{ public function notFoundPoll( $id ): Response {
return $this->json( [ return $this->json( [
'message' => $id . ' : poll not found', 'message' => $id . ' : poll not found',
], ],
404 ); 404 );
} }
/** /**
* get a poll config by its custom URL, we do not want polls to be reachable by their numeric id * get a poll config by its custom URL, we do not want polls to be reachable by their numeric id
* @Get( * @Get(
@ -89,7 +92,7 @@ class PollController extends EmailsController {
SerializerInterface $serializer, SerializerInterface $serializer,
$customUrl, $customUrl,
Request $request Request $request
): Response { ) {
$repository = $this->getDoctrine()->getRepository( Poll::class ); $repository = $this->getDoctrine()->getRepository( Poll::class );
$poll = $repository->findOneByCustomUrl( $customUrl ); $poll = $repository->findOneByCustomUrl( $customUrl );
@ -109,7 +112,7 @@ class PollController extends EmailsController {
'stacks' => $poll->getStacksOfVotes(), 'stacks' => $poll->getStacksOfVotes(),
'choices_count' => $poll->computeAnswers(), 'choices_count' => $poll->computeAnswers(),
'choices' => $poll->getChoices(), 'choices' => $poll->getChoices(),
'comments' => $comments, 'comments' => $comments,
'comments_count' => count( $comments ), 'comments_count' => count( $comments ),
]; ];
@ -119,13 +122,15 @@ class PollController extends EmailsController {
if ( $pass ) { if ( $pass ) {
// no password possibly given by this route // no password possibly given by this route
return $this->json( [ return $this->json( [
'message' => 'this is protected by a password,but you did not provide the encoded password parameter, and you should feel bad. ' , 'message' => 'this is protected by a password,but you did not provide the encoded password parameter, and you should feel bad. ',
], ],
403 ); 403 );
} else { } else {
// free access to poll // free access to poll
return $this->returnPollData( $poll, $serializer ); // return $this->returnPollData( $poll, $serializer );
// return $this->json($returnedPoll);
return $this->json($poll);
} }
} }
@ -142,22 +147,22 @@ class PollController extends EmailsController {
* *
* @return JsonResponse|Response * @return JsonResponse|Response
*/ */
function getProtectedPoll($customUrl,$md5, SerializerInterface $serializer){ function getProtectedPoll( $customUrl, $md5, SerializerInterface $serializer ) {
$repository = $this->getDoctrine()->getRepository( Poll::class ); $repository = $this->getDoctrine()->getRepository( Poll::class );
$poll = $repository->findOneByCustomUrl( $customUrl ); $poll = $repository->findOneByCustomUrl( $customUrl );
if ( ! $poll ) { if ( ! $poll ) {
return $this->notFoundPoll($customUrl); return $this->notFoundPoll( $customUrl );
} }
if ( $poll->getPassword() === $md5 ) { if ( $poll->getPassword() === $md5 ) {
// good matching pass // good matching pass
return $this->returnPollData( $poll, $serializer ); return $this->returnPollData( $poll, $serializer );
} else { } else {
// wrong pass // wrong pass
return $this->json( [ return $this->json( [
'message' => 'this is protected by a password, your password "' . $md5 . '" is wrong, and you should feel bad', 'message' => 'this is protected by a password, your password hash "' . $md5 . '" is wrong, and you should feel bad',
'md5' => md5($md5), 'md5' => md5( $md5 ),
'data' => null, 'data' => null,
], ],
403 ); 403 );
@ -203,6 +208,7 @@ class PollController extends EmailsController {
return $this->json( [ return $this->json( [
'message' => 'you updated the poll ' . $poll->getTitle(), 'message' => 'you updated the poll ' . $poll->getTitle(),
"poll" => $poll,
], ],
200 ); 200 );
} }

View File

@ -14,6 +14,9 @@ use Doctrine\Persistence\ObjectManager;
class AppPollFixtures extends Fixture { class AppPollFixtures extends Fixture {
public const POLL_FIXTURE_ONE = 'citron-poll-fixture'; public const POLL_FIXTURE_ONE = 'citron-poll-fixture';
public const POLL_FIXTURE_TWO = 'aujourdhui-ou-demain';
public const POLL_FIXTURE_THREE = 'citron';
public const POLL_FIXTURE_FOUR = 'demo';
public function load( ObjectManager $manager ) { public function load( ObjectManager $manager ) {
@ -41,7 +44,6 @@ class AppPollFixtures extends Fixture {
$pollCitronOrange->setTitle( 'citron ou orange' ) $pollCitronOrange->setTitle( 'citron ou orange' )
->setCustomUrl('citron') ->setCustomUrl('citron')
->setDescription( 'votre sorbert préféré' ) ->setDescription( 'votre sorbert préféré' )
->setAdminKey( uniqid() )
->setModificationPolicy( 'nobody' ) ->setModificationPolicy( 'nobody' )
->setPassword('le pass woute woute'); ->setPassword('le pass woute woute');
@ -120,7 +122,6 @@ class AppPollFixtures extends Fixture {
->setCustomUrl('demo') ->setCustomUrl('demo')
->setDescription( 'description du sondage 2' ); ->setDescription( 'description du sondage 2' );
$pollCitronOrange->setAdminKey( uniqid() );
$pollCitronOrange->setModificationPolicy( 'self' ); $pollCitronOrange->setModificationPolicy( 'self' );
$pollCitronOrange->setMailOnComment( true ); $pollCitronOrange->setMailOnComment( true );
@ -154,16 +155,16 @@ class AppPollFixtures extends Fixture {
->addChoice( $choice ) ->addChoice( $choice )
->addChoice( $choice2 ) ->addChoice( $choice2 )
->addChoice( $choice3 ) ->addChoice( $choice3 )
->setModificationPolicy( 'self' ); ->setModificationPolicy( 'everybody' );
$manager->persist( $pollCitronOrange ); $manager->persist( $pollCitronOrange );
// poll with cartoon choices // poll with cartoon choices
$pollCitronOrange = new Poll(); $pollCartoons = new Poll();
$pollCitronOrange->setTitle( 'dessin animé préféré' ) $pollCartoons->setTitle( 'dessin animé préféré' )
->setCustomUrl('dessin-anime') ->setCustomUrl('dessin-anime')
->setDescription( 'choisissez votre animé préféré' ) ->setDescription( 'choisissez votre animé préféré' )
->setOwner( $owner ) ->setOwner( $owner )
->setModificationPolicy( 'self' ) ->setModificationPolicy( 'nobody' )
->addTextChoiceArray( [ ->addTextChoiceArray( [
"Vic le viking", "Vic le viking",
"Boumbo petite automobile", "Boumbo petite automobile",
@ -179,61 +180,72 @@ class AppPollFixtures extends Fixture {
->setPseudo('un gens qui commente') ->setPseudo('un gens qui commente')
->setText( "allez boumbo!" ) ->setText( "allez boumbo!" )
->setOwner( $commenterMan ); ->setOwner( $commenterMan );
$pollCitronOrange->addComment( $someoneComment ); $pollCartoons->addComment( $someoneComment );
$someoneComment2 = new Comment(); $someoneComment2 = new Comment();
$someoneComment2 $someoneComment2
->setPseudo('un gens qui commente') ->setPseudo('un gens qui commente')
->setText( "je suis pour la team rocket de digimon" ) ->setText( "je suis pour la team rocket de digimon" )
->setOwner( $owner ); ->setOwner( $owner );
$pollCitronOrange->addComment( $someoneComment2 ); $pollCartoons->addComment( $someoneComment2 );
$manager->persist( $pollCitronOrange ); $manager->persist( $pollCartoons );
$stack = new StackOfVotes(); $stack = new StackOfVotes();
$stack->setPseudo( 'Wulfila' ); $stack->setPseudo( 'Wulfila' );
$stack $stack
->setPoll( $pollCitronOrange ) ->setPoll( $pollCartoons )
->setOwner( $voter ); ->setOwner( $voter );
$pollCartoons->addStackOfVote($stack);
$vote = new Vote(); $vote = new Vote();
$vote $vote
->setPoll( $pollCitronOrange ) ->setPoll( $pollCartoons )
->setStacksOfVotes( $stack ) ->setStacksOfVotes( $stack )
->setValue( "yes" ) ->setValue( "yes" )
->setChoice( $pollCitronOrange->getChoices()[ 2 ] ); ->setChoice( $pollCartoons->getChoices()[ 2 ] );
$pollCartoons->addVote($vote);
$vote = new Vote(); $vote = new Vote();
$vote $vote
->setPoll( $pollCitronOrange ) ->setPoll( $pollCartoons )
->setStacksOfVotes( $stack ) ->setStacksOfVotes( $stack )
->setValue( "maybe" ) ->setValue( "maybe" )
->setChoice( $pollCitronOrange->getChoices()[ 1 ] ); ->setChoice( $pollCartoons->getChoices()[ 1 ] );
$pollCartoons->addVote($vote);
$manager->persist( $stack ); $manager->persist( $stack );
$stack = new StackOfVotes(); $stack = new StackOfVotes();
$stack->setPseudo( 'Tykayn' ); $stack->setPseudo( 'Tykayn' );
$stack $stack
->setPoll( $pollCitronOrange ) ->setPoll( $pollCartoons )
->setOwner( $voter ); ->setOwner( $voter );
$vote = new Vote(); $vote = new Vote();
$vote $vote
->setPoll( $pollCitronOrange ) ->setPoll( $pollCartoons )
->setStacksOfVotes( $stack ) ->setStacksOfVotes( $stack )
->setValue( "yes" ) ->setValue( "yes" )
->setChoice( $pollCitronOrange->getChoices()[ 1 ] ); ->setChoice( $pollCartoons->getChoices()[ 1 ] );
$pollCartoons->addVote($vote);
$vote = new Vote(); $vote = new Vote();
$vote $vote
->setPoll( $pollCitronOrange ) ->setPoll( $pollCartoons )
->setStacksOfVotes( $stack ) ->setStacksOfVotes( $stack )
->setValue( "yes" ) ->setValue( "yes" )
->setChoice( $pollCitronOrange->getChoices()[ 2 ] ); ->setChoice( $pollCartoons->getChoices()[ 2 ] );
$pollCartoons->addVote($vote);
$vote = new Vote(); $vote = new Vote();
$vote $vote
->setPoll( $pollCitronOrange ) ->setPoll( $pollCartoons )
->setStacksOfVotes( $stack ) ->setStacksOfVotes( $stack )
->setValue( "no" ) ->setValue( "no" )
->setChoice( $pollCitronOrange->getChoices()[ 2 ] ); ->setChoice( $pollCartoons->getChoices()[ 2 ] );
$pollCartoons->addVote($vote);
$manager->persist( $pollCartoons );
$manager->persist( $stack ); $manager->persist( $stack );

View File

@ -79,7 +79,7 @@ class Poll {
* @Serializer\Type("boolean") * @Serializer\Type("boolean")
* @Serializer\Expose() * @Serializer\Expose()
*/ */
public $votesAllowed; public $votesAllowed = true;
/** /**
* max number of stack of votes possible. * max number of stack of votes possible.
* limits the number of people who can answer. as long as you trust the people to give only one answer with a reliable system. * limits the number of people who can answer. as long as you trust the people to give only one answer with a reliable system.
@ -87,7 +87,7 @@ class Poll {
* @Serializer\Type("smallint") * @Serializer\Type("smallint")
* @Serializer\Expose() * @Serializer\Expose()
*/ */
public $votesMax; public $votesMax = 1024;
/** /**
* max number of choices people can answer in a stack of vote. for text polls only, not date kind. * max number of choices people can answer in a stack of vote. for text polls only, not date kind.
@ -97,7 +97,7 @@ class Poll {
* @Serializer\Type("smallint") * @Serializer\Type("smallint")
* @Serializer\Expose() * @Serializer\Expose()
*/ */
public $choicesMax; public $choicesMax = -1;
/** /**
* people can add comments * people can add comments
@ -105,7 +105,7 @@ class Poll {
* @Serializer\Type("boolean") * @Serializer\Type("boolean")
* @Serializer\Expose() * @Serializer\Expose()
*/ */
public $commentsAllowed; public $commentsAllowed = true;
/** /**
* kind of way the people can modify the poll * kind of way the people can modify the poll
@ -116,35 +116,35 @@ class Poll {
* @Serializer\Type("string") * @Serializer\Type("string")
* @Serializer\Expose() * @Serializer\Expose()
*/ */
public $modificationPolicy = 'nobody'; public $modificationPolicy = 'everybody';
/** /**
* send a mail on a new comment * send a mail on a new comment
* @ORM\Column(type="boolean", nullable=true) * @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean") * @Serializer\Type("boolean")
* @Serializer\Expose() * @Serializer\Expose()
*/ */
public $mailOnComment; public $mailOnComment = true;
/** /**
* send a mail on a new vote * send a mail on a new vote
* @ORM\Column(type="boolean", nullable=true) * @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean") * @Serializer\Type("boolean")
* @Serializer\Expose() * @Serializer\Expose()
*/ */
public $mailOnVote; public $mailOnVote = false;
/** /**
* hide publicly results * hide publicly results
* @ORM\Column(type="boolean", nullable=true) * @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean") * @Serializer\Type("boolean")
* @Serializer\Expose() * @Serializer\Expose()
*/ */
public $hideResults; public $hideResults = false;
/** /**
* show publicly results even if there is a password to access the vote * show publicly results even if there is a password to access the vote
* @ORM\Column(type="boolean", nullable=true) * @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean") * @Serializer\Type("boolean")
* @Serializer\Expose() * @Serializer\Expose()
*/ */
public $showResultEvenIfPasswords; public $showResultEvenIfPasswords = false;
/** /**
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"}) * @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"})
* @Serializer\Type("ArrayCollection<App\Entity\Vote>") * @Serializer\Type("ArrayCollection<App\Entity\Vote>")
@ -247,22 +247,23 @@ class Poll {
} }
public function __construct() { public function __construct() {
$this->votes = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
$this->choices = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->initiate(); $this->initiate();
} }
private function initiate() { private function initiate() {
$this->adminKey = $this->generateAdminKey(); $this->votes = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
$this->choices = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->setAdminKey( $this->generateAdminKey() );
$this->setCreationDate( new \DateTime() ); $this->setCreationDate( new \DateTime() );
$this->setExpiracyDate( $this->addDaysToDate( $this->setExpiracyDate( $this->addDaysToDate(
new \DateTime(), new \DateTime(),
$this->defaultExpiracyDaysFromNow $this->defaultExpiracyDaysFromNow
) ); ) );
$this->setAllowedAnswers( [ 'yes' ] ); $this->setAllowedAnswers( [ 'yes', 'maybe', 'no' ] );
} }
@ -324,7 +325,7 @@ class Poll {
} }
public function addDaysToDate( \DateTime $date, int $days ) { public function addDaysToDate( \DateTime $date, int $days ) {
$st = strtotime( $date->getTimestamp() . ' + ' . $days . ' days' ); $st = strtotime( $date->getTimestamp() );
return new \DateTime( $st ); return new \DateTime( $st );
} }