private function loadPosts(ObjectManager $manager) { $passwordEncoder = $this->container->get('security.password_encoder'); $user = new User(); $user->setUsername('vvasia'); $user->setDisplayName('Vasia Vasin'); $user->setEmail('*****@*****.**'); $user->setUuid('uuid'); $encodedPassword = $passwordEncoder->encodePassword($user, 'password'); $user->setPassword($encodedPassword); $user->setRoles(['ROLE_USER']); $manager->persist($user); $manager->flush(); /** @var User $author */ $author = $manager->getRepository('AppBundle:User')->findOneBy(['email' => '*****@*****.**']); foreach (range(1, 10) as $i) { $post = new Post(); $post->setTitle($this->getRandomPostTitle() . ' ' . uniqid())->setSummary($this->getRandomPostSummary())->setSlug($this->container->get('slugger')->slugify($post->getTitle()))->setContent($this->getPostContent())->setAuthor($author)->setPublishedAt(new \DateTime('now - ' . $i . 'days'))->setState($this->getRandomState())->setCategory($category); foreach (range(1, 5) as $j) { $comment = new Comment(); $comment->setUser($user)->setPublishedAt(new \DateTime('now + ' . ($i + $j) . 'seconds'))->setContent($this->getRandomCommentContent())->setPost($post); $manager->persist($comment); $post->addComment($comment); } if (rand(0, 1)) { $vote = new Vote(); $vote->setAuthorEmail(rand(0, 1) ? '*****@*****.**' : '*****@*****.**'); $vote->setPost($post); $vote->setVote(rand(0, 1)); } $manager->persist($post); $category->addPost($post); } $manager->flush(); }
/** * @ParamConverter("user", class="CoreUserBundle:User", options={"mapping":{"user_username" = "username"}}) * @ParamConverter("idea", class="AppBundle:Idea", options={"mapping":{"idea_slug" = "slug"}}) * @ParamConverter("comment", class="AppBundle:Comment", options={"mapping":{"comment_id" = "id"}}) */ public function addAction(Request $request, User $user, Idea $idea, Comment $comment) { if ($idea->getUserId() !== $user->getId() || $comment->getIdeaId() !== $idea->getId()) { throw new $this->createNotFoundException(); } $doctrine = $this->getDoctrine(); $manager = $doctrine->getManager(); $voteUser = $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY') ? $this->getUser() : null; $hash = $voteUser ? null : $this->get('core.base.form')->getUserHash($request); if ($voteUser && $voteUser->getId() === $comment->getUserId()) { $this->get('session')->getFlashBag()->add('error', "You can't vote for your own idea."); } else { $exist = $doctrine->getRepository('AppBundle:Vote')->findOneBy(['user' => $voteUser, 'hash' => $hash, 'comment' => $comment]); if ($exist) { $this->get('session')->getFlashBag()->add('error', 'You have already voted for this comment.'); } else { $vote = new Vote(); $vote->setComment($comment); if ($voteUser) { $vote->setUser($voteUser); } else { $vote->setHash($hash); } $manager->persist($vote); $manager->flush(); } } return $this->end($request, $user, $idea, $comment); }
public function testGetTotalScoreReturnNegativeScore() { $comment = new Comment(); $comment->addRating($this->badRating); $actual = $comment->getTotalScore(); $this->assertEquals(-1, $actual); }
private function canYouDoIt(Comment $comment, User $user) { $commentOwner = $this->doctrine->getRepository('AppBundle:User')->findOneBy(array('email' => $comment->getAuthorEmail())); if (in_array("ROLE_ADMIN", $commentOwner->getRoles()) || $comment->getPost()->getAuthorEmail() !== $user->getEmail()) { return false; } return true; }
/** * @test */ public function addSuccess() { $comment = new Comment(); $comment->setComment('test'); $this->repository->expects($this->once())->method('add'); $result = $this->comment_service->add($comment); $this->assertInstanceOf('AppBundle\\Entity\\Comment', $result); }
private function canEdit(Comment $comment, User $user) { // this assumes that the data object has a getOwner() method // to get the entity of the user who owns this data object if ($comment->getUser()->getIsAdmin() and !$user->getIsAdmin()) { return false; } return $user === $comment->getUser() or $user->getIsAdmin() or $user === $comment->getPost()->getAuthor(); }
/** * @covers AppBundle\Entity\Article::__construct * @covers AppBundle\Entity\Article::__toArray */ public function test__construct() { $testData = ['userName' => 'some user name', 'comment' => 'sample comment']; $article = new Article('some title', 'some description'); $comment = new Comment($article, $testData['userName'], $testData['comment']); $commentArray = $comment->__toArray(); $this->assertEquals($testData['userName'], $commentArray['userName']); $this->assertEquals($testData['comment'], $commentArray['comment']); }
private function canEdit(Comment $comment, UserInterface $user, TokenInterface $token) { if ($this->decisionManager->decide($token, ['ROLE_MANAGER']) && (!$comment->getUser() || !$this->roleHierarchyChecker->check($comment->getUser(), 'ROLE_ADMIN')) && $comment->getPost()->getOwner() == $user) { return true; } if ($comment->getUser() === $user) { return true; } return false; }
/** * @param ObjectManager $manager */ public function load(ObjectManager $manager) { $comment = new Comment(); $comment->setAuthor($this->getReference('user')); $comment->setContent('Commentaire babla bla bla Beer'); $comment->setArticle($this->getReference('article')); $manager->persist($comment); $manager->flush(); $this->addReference('comment', $comment); }
public function addNewCommentToArticle($article, Comment $comment, $author) { if ($author !== null) { $comment->setAuthor($author); } $comment->setArticle($article); $em = $this->doctrine->getManager(); $em->persist($comment); $em->flush(); }
/** * @Route("/create/comment", name="new_comment") */ public function newCommentAction($id) { if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) { throw $this->createAccessDeniedException(); } $comment = new Comment(); $comment->setPostId($id); $form = $this->createForm('app_comment', $comment, array('action' => $this->generateUrl('check_comment'), 'method' => 'POST')); return $this->render("AppBundle:Comment:form.html.twig", array("commentForm" => $form->createView())); }
/** * @Route("/comments/{id}", requirements={"id" = "\d+"}, options={"expose" = true}) * @Method("DELETE") */ public function removeAction(Comment $comment) { if ($comment->getAuthor() != $this->getUser() && !$this->isGranted('ROLE_ADMIN')) { return new JsonResponse(array('KO'), Response::HTTP_FORBIDDEN); } $em = $this->getDoctrine()->getManager(); $em->remove($comment); $em->flush(); return new JsonResponse(array('OK')); }
private function canEdit(Comment $comment, User $user) { if ($user === $comment->getArticle()->getUser() && !in_array('ROLE_ADMIN', $comment->getUser()->getRoles())) { return true; } if ($user === $comment->getUser()) { return true; } return false; }
/** * @ParamConverter("comment", class="AppBundle:Comment", options={"mapping":{"comment_id" = "id"}}) */ public function deleteAction(Request $request, Comment $comment) { $idea = $comment->getIdea(); $user = $idea->getUser(); $responseData = $this->get('app.model.comment')->delete($comment); if ($responseData['valid']) { //$this->get('session')->getFlashBag()->add('success', "Comment were deleted."); return $this->redirectToRoute('app_idea_view', ['user_username' => $user->getUsername(), 'idea_slug' => $idea->getSlug()]); } else { throw $this->createNotFoundException(); } }
public function addRating(Request $request, \AppBundle\Entity\Comment $comment) { if ($this->validateRequest($request)) { $userId = $request->request->get('user'); $thumbsUp = $request->request->get('rating'); $user = $this->userRepo->findOneById($userId); $comment->addRating($this->createRating($user, $thumbsUp)); $this->commentRepo->save($comment); return true; } return false; }
private function handleRequest(Comment $entity, Request $request) { $data = $request->getContent(); if (isset($data['answer'])) { $entity->setAnswer($this->findAnswer($data['answer'])); } if (isset($data['text'])) { $entity->setText($data['text']); } if (isset($data['created_by'])) { $entity->setCreatedBy($data['created_by']); } }
/** * @Route("/comment", name="Comment") */ public function commentAction(Request $request) { $user = $this->getUser(); $comment = new Comment(); $now = new DateTime(); $doodle_id = $request->get('id'); $text = $request->get('comment'); $doodle = $this->getDoctrine()->getRepository('AppBundle:Doodle')->find($doodle_id); $comment->setUser($user); $comment->setDoodle($doodle); $comment->setCreated($now); $comment->setText($text); if ($user) { $comment->setAuthor($user->username); } else { $comment->setAuthor('Anonymous'); } $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); $data = ['message' => 'Success!', 'id' => $doodle_id, 'text' => $text]; $response = new Response(json_encode(['response' => $data])); $response->headers->set('Content-Type', 'application/json'); return $response; }
/** * @Route("/createRandomComment/{messageLength}" , name="create post") */ function createRandomCommentAction($messageLength) { $generator = $this->get('app.data_generator'); $doctrine = $this->getDoctrine(); $em = $doctrine->getEntityManager(); $comment = new Comment(); $comment->setMessage($generator->generateRandomComment($messageLength)); $comment->setMember($generator->getRandomMember()); $thread = $generator->getRandomThread(); $comment->setThread($thread); $em->persist($comment); $em->flush(); return $this->redirect('/thread/' . $thread->getId()); }
/** * @Route("/comments/{id}/new", name="app_add_comment", requirements={ "id" = "\d+" }) * @Method("GET|POST") */ public function newAction(Request $request, Blog $blog) { $comment = new Comment(); $comment->setBlog($blog); $form = $this->createCommentForm($comment, $blog); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); return $this->redirect($this->generateUrl('app_blog_show', ['id' => $blog->getId()]) . '#comment-' . $comment->getId()); } return $this->render('comment/new.html.twig', ['comment' => $comment, 'form' => $form->createView()]); }
public function __construct() { $strength = new Comment(); $strength->setType('s'); $this->comments[] = $strength; $weakness = new Comment(); $weakness->setType('w'); $this->comments[] = $weakness; $opportunity = new Comment(); $opportunity->setType('o'); $this->comments[] = $opportunity; $threat = new Comment(); $threat->setType('t'); $this->comments[] = $threat; }
public function testSubmitValidData() { $formData = array('content' => 'Test content text'); $form = $this->factory->create(CommentType::class); $object = new Comment(); $object->setContent('Test content text'); $form->submit($formData); $this->assertTrue($form->isSynchronized()); $this->assertEquals($object, $form->getData()); $view = $form->createView(); $children = $view->children; foreach (array_keys($formData) as $key) { $this->assertArrayHasKey($key, $children); } }
public function createAction($blog_id) { $blog = $this->getBlog($blog_id); $comment = new Comment(); $comment->setBlog($blog); $request = $this->getRequest(); $form = $this->createForm(new CommentType(), $comment); $form->submit($request); if ($form->isValid()) { $em = $this->getDoctrine()->getEntityManager(); $em->persist($comment); $em->flush(); return $this->redirect($this->generateUrl('AppBundle_blog_show', array('id' => $comment->getBlog()->getId())) . '#comment-' . $comment->getId()); } return $this->render('AppBundle:Comment:create.html.twig', array('comment' => $comment, 'form' => $form->createView())); }
/** * Creates a new Comment entity. * * @Route("/new/{id}", name="comment_new") * @Method({"GET", "POST"}) */ public function newAction(Request $request, $id) { $em = $this->getDoctrine()->getManager(); $news = $em->getRepository('AppBundle:News')->find($id); if (!$news instanceof News) { throw new Exception("Error Processing Request", 1); } $comment = new Comment(); $comment->setNews($news); $form = $this->createForm(get_class(new CommentType()), $comment); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->get('event_dispatcher')->dispatch('pre_comment_created', new GenericEvent($comment)); $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); $this->get('event_dispatcher')->dispatch('post_comment_created', new GenericEvent($comment)); return $this->redirectToRoute('news_index'); } return $this->render('comment/new.html.twig', array('comment' => $comment, 'form' => $form->createView())); }
public function load(ObjectManager $manager) { for ($i = 0; $i < 40; $i++) { $article = new Article(); $article->setTitle('News' . $i); $article->setContent('This is default content'); $article->setDatetime(new \DateTime('- ' . rand(1, 4) . ' day')); $commentA = new Comment(); $commentA->setAuthor('Author'); $commentA->setContent('Comment .... default'); $manager->persist($commentA); $article->addComment($commentA); $commentB = new Comment(); $commentB->setAuthor('Author' . $i); $commentB->setContent('Comment .... default'); $manager->persist($commentB); $article->addComment($commentB); $manager->persist($article); } $manager->flush(); }
/** * @param Request $request * @param $slug * @return \Symfony\Component\HttpFoundation\RedirectResponse * @Route("/show/{slug}", name="add_comment") * @Method("POST") * @Template("AppBundle:Blog:article.html.twig") */ public function addCommentAction(Request $request, $slug) { if (!$this->get('security.authorization_checker')->isGranted('ROLE_USER')) { throw $this->createAccessDeniedException(); } $user = $this->getUser(); $repository = $this->getDoctrine()->getRepository('AppBundle:Article'); $article = $repository->findArticleBySlug($slug); $comment = new Comment(); $comment->setUser($user); $comment->setArticle($article[0]); $form = $this->createForm(CommentType::class, $comment, ['action' => $this->generateUrl('add_comment', ['slug' => $slug]), 'method' => 'POST'])->add('save', SubmitType::class, ['label' => 'Add comment']); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); return $this->redirect($this->generateUrl('show_article', ['slug' => $comment->getArticle()->getSlug()])); } return ['article' => $article, 'form' => $form->createView()]; }
public function load(ObjectManager $manager) { // TODO: Implement load() method. $category = new Category(); $category->setName('Default'); $manager->persist($category); foreach (range(1, 100) as $i) { $post = new Post(); $post->setTitle($this->getPostTitle()); $post->setSlug($this->container->get('slugger')->slugify($post->getTitle())); $post->setImage('post.jpeg'); $post->setContent($this->getPostContent()); $post->setAuthor('gunyem'); $post->setCreated(new \DateTime('now - ' . $i . 'days')); $post->setUpdated(new \DateTime('now - ' . $i . 'days')); $post->setCategory($category); foreach (range(1, 10) as $j) { $comment = new Comment(); $comment->setPost($post); $comment->setContent($this->getPostTitle()); $comment->setAuthor('gunyem'); $comment->setCreated(new \DateTime('now + ' . ($i + $j) . 'seconds')); $manager->persist($comment); $post->createComment($comment); } $manager->persist($post); } $manager->flush(); }
private function loadPosts(ObjectManager $manager) { foreach (range(1, 30) as $i) { $post = new Post(); $post->setTitle('Sed ut perspiciatis unde'); $post->setAlias('Sed ut perspiciatis unde'); $post->setIntrotext('Sed ut perspicantium, tocto beatae vitae dicta sunt explicabo. '); $post->setSlug($this->container->get('slugger')->slugify($post->getTitle())); $post->setBody('Sed ut is iste uasi architecto beatae vitae dicta sunt explicabo. '); $post->setAuthorEmail('*****@*****.**'); $post->setPublishedAt(new \DateTime('now - ' . $i . 'days')); $post->setState(1); $post->setImages('test.jpg'); foreach (range(1, 5) as $j) { $comment = new Comment(); $comment->setAuthorEmail('*****@*****.**'); $comment->setPublishedAt(new \DateTime('now + ' . ($i + $j) . 'seconds')); $comment->setContent('Sed ut perspiciatis undedasdadasd'); $comment->setPost($post); $manager->persist($comment); $post->addComment($comment); } $manager->persist($post); } $manager->flush(); }
public function load(ObjectManager $manager) { $faker = Factory::create(); for ($i = 0; $i < 50; $i++) { static $id = 1; $post = new Post(); $post->setTitle($faker->sentence); $post->setAuthorEmail('*****@*****.**'); $post->setImageName("images/post/foto{$id}.jpg"); $post->setContent($faker->realText($maxNbChars = 5000, $indexSize = 2)); $marks = array(); for ($q = 0; $q < rand(1, 10); $q++) { $marks[] = rand(1, 5); } $post->setMarks($marks); $post->addMark(5); $manager->persist($post); $this->addReference("{$id}", $post); $id = $id + 1; $rand = rand(3, 7); for ($j = 0; $j < $rand; $j++) { $comment = new Comment(); $comment->setAuthorEmail('*****@*****.**'); $comment->setCreatedBy('user_user'); $comment->setContent($faker->realText($maxNbChars = 500, $indexSize = 2)); $comment->setPost($post); $post->getComments()->add($comment); $manager->persist($comment); $manager->flush(); } } $manager->flush(); }
private function loadPosts(ObjectManager $manager) { $category = new Category(); $category->setName('Improvements'); foreach (range(1, 5) as $i) { $post = new Post(); $post->setTitle($this->getRandomPostTitle()); $post->setSummary($this->getRandomPostSummary()); $post->setSlug($this->container->get('slugger')->slugify($post->getTitle())); $post->setContent($this->getPostContent()); $post->setAuthorEmail('*****@*****.**'); $post->setPublishedAt(new \DateTime('now - ' . $i . 'days')); $post->setState($this->getRandomState()); $post->setCategory($category); foreach (range(1, 5) as $j) { $comment = new Comment(); $comment->setAuthorEmail('*****@*****.**'); $comment->setPublishedAt(new \DateTime('now + ' . ($i + $j) . 'seconds')); $comment->setContent($this->getRandomCommentContent()); $comment->setPost($post); $manager->persist($comment); $post->addComment($comment); } if (rand(0, 1)) { $vote = new Vote(); $vote->setAuthorEmail(rand(0, 1) ? '*****@*****.**' : '*****@*****.**'); $vote->setPost($post); $vote->setVote(rand(0, 1)); } $manager->persist($post); $category->addPost($post); } $manager->flush(); }
/** * @Security("has_role('ROLE_OKTOLAB_USER')") * @Route("/answer/{comment}", name="oktothek_answer_comment") * @Template() */ public function answerCommentAction(Request $request, Comment $comment) { $answer = new Comment(); $commentForm = $this->createForm(CommentType::class, $answer, ['action' => $this->generateUrl('oktothek_answer_comment', ['comment' => $comment->getId()])]); $commentForm->add('submit', SubmitType::class, ['label' => 'oktothek.comment_reply_button', 'attr' => ['class' => 'btn btn-primary']]); if ($request->getMethod() == "POST") { $commentForm->handleRequest($request); if ($commentForm->isValid()) { $answer->setUser($this->get('security.context')->getToken()->getUser()); $answer->setReferer($comment->getReferer()); $answer->setParent($comment); $comment->addChild($answer); $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->persist($answer); $em->flush(); $this->get('session')->getFlashBag()->add('success', 'oktothek.comment_create_success'); return $this->redirect($request->headers->get('referer')); } $this->get('session')->getFlashBag()->add('error', 'oktothek.comment_create_error'); } return ['commentForm' => $commentForm->createView()]; }