Forum: Fix delete forum

pull/3984/head
Julio 4 years ago
parent a121f57936
commit 3ea3fd8dbf
  1. 13
      public/main/forum/forumfunction.inc.php
  2. 22
      src/CoreBundle/Repository/ResourceRepository.php
  3. 3
      src/CourseBundle/Entity/CForum.php
  4. 11
      src/CourseBundle/Entity/CForumPost.php
  5. 9
      src/CourseBundle/Entity/CForumThread.php
  6. 8
      tests/CourseBundle/Repository/CDocumentRepositoryTest.php
  7. 29
      tests/CourseBundle/Repository/CForumCategoryRepositoryTest.php
  8. 76
      tests/CourseBundle/Repository/CForumPostRepositoryTest.php
  9. 4
      tests/CourseBundle/Repository/CForumRepositoryTest.php
  10. 57
      tests/CourseBundle/Repository/CForumThreadRepositoryTest.php
  11. 49
      tests/CourseBundle/Repository/CQuizRepositoryTest.php

@ -2471,13 +2471,19 @@ function saveThread(
WHERE
iid = '".$thread->getIid()."'";
Database::query($sql);
$message = get_lang('The new thread has been added');
$message = '';
if ($showMessage) {
Display::addFlash(Display::return_message(get_lang('The new thread has been added'), 'success', false));
}
// Overwrite default message.
if ($forum->isModerated() &&
!api_is_allowed_to_edit(null, true)
) {
$message = get_lang('Your message has to be approved before people can view it.');
if ($showMessage) {
Display::addFlash(Display::return_message(get_lang('Your message has to be approved before people can view it.'), 'success', false));
}
}
add_forum_attachment_file(
@ -2510,8 +2516,7 @@ function saveThread(
send_notification_mails(
$forum,
$thread,
$reply_info,
$courseInfo['code']
$reply_info
);
Session::erase('formelements');

@ -6,7 +6,6 @@ declare(strict_types=1);
namespace Chamilo\CoreBundle\Repository;
use Chamilo\CoreBundle\Component\Resource\Template;
use Chamilo\CoreBundle\Component\Utils\CreateUploadedFile;
use Chamilo\CoreBundle\Entity\AbstractResource;
use Chamilo\CoreBundle\Entity\Course;
@ -42,21 +41,8 @@ abstract class ResourceRepository extends ServiceEntityRepository
use NonResourceRepository;
use RepositoryQueryBuilderTrait;
protected Template $templates;
protected ?ResourceType $resourceType = null;
public function setTemplates(Template $templates): self
{
$this->templates = $templates;
return $this;
}
public function getTemplates(): Template
{
return $this->templates;
}
public function getCount(QueryBuilder $qb): int
{
$qb->select('count(resource)');
@ -452,18 +438,20 @@ abstract class ResourceRepository extends ServiceEntityRepository
public function delete(ResourceInterface $resource): void
{
$em = $this->getEntityManager();
$children = $resource->getResourceNode()->getChildren();
foreach ($children as $child) {
if ($child->hasResourceFile()) {
$this->getEntityManager()->remove($child->getResourceFile());
$em->remove($child->getResourceFile());
}
$resourceNode = $this->getResourceFromResourceNode($child->getId());
if (null !== $resourceNode) {
$this->delete($resourceNode);
}
}
$this->getEntityManager()->remove($resource);
$this->getEntityManager()->flush();
$em->remove($resource);
$em->flush();
}
/**

@ -243,6 +243,9 @@ class CForum extends AbstractResource implements ResourceInterface
public function setForumCategory(CForumCategory $forumCategory = null): self
{
if (null !== $forumCategory) {
$forumCategory->getForums()->add($this);
}
$this->forumCategory = $forumCategory;
return $this;

@ -55,6 +55,7 @@ class CForumPost extends AbstractResource implements ResourceInterface
/**
* @ORM\Column(name="post_date", type="datetime", nullable=false)
*/
#[Assert\NotBlank]
protected DateTime $postDate;
/**
@ -65,6 +66,7 @@ class CForumPost extends AbstractResource implements ResourceInterface
/**
* @ORM\Column(name="visible", type="boolean", nullable=false)
*/
#[Assert\NotBlank]
protected bool $visible;
/**
@ -73,7 +75,7 @@ class CForumPost extends AbstractResource implements ResourceInterface
protected ?int $status = null;
/**
* @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CForumThread", inversedBy="posts")
* @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CForumThread", inversedBy="posts", )
* @ORM\JoinColumn(name="thread_id", referencedColumnName="iid", nullable=true, onDelete="SET NULL")
*/
protected ?CForumThread $thread = null;
@ -88,6 +90,7 @@ class CForumPost extends AbstractResource implements ResourceInterface
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User")
* @ORM\JoinColumn(name="poster_id", referencedColumnName="id")
*/
#[Assert\NotBlank]
protected ?User $user = null;
/**
@ -114,6 +117,7 @@ class CForumPost extends AbstractResource implements ResourceInterface
public function __construct()
{
$this->postDate = new DateTime();
$this->visible = false;
$this->attachments = new ArrayCollection();
$this->children = new ArrayCollection();
@ -150,6 +154,9 @@ class CForumPost extends AbstractResource implements ResourceInterface
public function setThread(CForumThread $thread = null): self
{
if (null !== $thread) {
$thread->getPosts()->add($this);
}
$this->thread = $thread;
return $this;
@ -253,6 +260,8 @@ class CForumPost extends AbstractResource implements ResourceInterface
public function setForum(?CForum $forum): self
{
$forum->getPosts()->add($this);
$this->forum = $forum;
return $this;

@ -81,26 +81,31 @@ class CForumThread extends AbstractResource implements ResourceInterface
/**
* @ORM\Column(name="thread_date", type="datetime", nullable=false)
*/
#[Assert\NotBlank]
protected DateTime $threadDate;
/**
* @ORM\Column(name="thread_replies", type="integer", nullable=false, options={"unsigned":true, "default":0})
*/
#[Assert\NotBlank]
protected int $threadReplies;
/**
* @ORM\Column(name="thread_views", type="integer", nullable=false, options={"unsigned":true, "default":0})
*/
#[Assert\NotBlank]
protected int $threadViews;
/**
* @ORM\Column(name="thread_sticky", type="boolean", nullable=false)
*/
#[Assert\NotBlank]
protected bool $threadSticky;
/**
* @ORM\Column(name="locked", type="integer", nullable=false)
*/
#[Assert\NotBlank]
protected int $locked;
/**
@ -132,6 +137,7 @@ class CForumThread extends AbstractResource implements ResourceInterface
{
$this->posts = new ArrayCollection();
$this->qualifications = new ArrayCollection();
$this->threadDate = new DateTime();
$this->threadPeerQualify = false;
$this->threadReplies = 0;
$this->threadViews = 0;
@ -172,6 +178,9 @@ class CForumThread extends AbstractResource implements ResourceInterface
public function setForum(CForum $forum = null): self
{
if (null !== $forum) {
$forum->getThreads()->add($this);
}
$this->forum = $forum;
return $this;

@ -588,6 +588,10 @@ class CDocumentRepositoryTest extends AbstractApiTest
$documentRepo->setVisibilityDeleted($document);
$link = $document->getFirstResourceLink();
$this->assertSame(ResourceLink::VISIBILITY_DELETED, $link->getVisibility());
$documentRepo->softDelete($document);
$link = $document->getFirstResourceLink();
$this->assertSame(ResourceLink::VISIBILITY_DELETED, $link->getVisibility());
}
public function testGetTotalSpaceByCourse(): void
@ -614,5 +618,9 @@ class CDocumentRepositoryTest extends AbstractApiTest
$total = $documentRepo->getTotalSpaceByCourse($course);
$this->assertSame($this->getUploadedFile()->getSize(), $total);
$documentRepo->delete($document);
$this->assertSame(0, $documentRepo->count([]));
}
}

@ -6,8 +6,10 @@ declare(strict_types=1);
namespace Chamilo\Tests\CourseBundle\Repository;
use Chamilo\CourseBundle\Entity\CForum;
use Chamilo\CourseBundle\Entity\CForumCategory;
use Chamilo\CourseBundle\Repository\CForumCategoryRepository;
use Chamilo\CourseBundle\Repository\CForumRepository;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
@ -19,22 +21,33 @@ class CForumCategoryRepositoryTest extends AbstractApiTest
{
self::bootKernel();
$em = $this->getEntityManager();
$repo = self::getContainer()->get(CForumCategoryRepository::class);
$categoryRepo = self::getContainer()->get(CForumCategoryRepository::class);
$forumRepo = self::getContainer()->get(CForumRepository::class);
$course = $this->createCourse('new');
$teacher = $this->createUser('teacher');
$item = (new CForumCategory())
$category = (new CForumCategory())
->setCatTitle('cat')
->setParent($course)
->setCreator($teacher)
;
$this->assertHasNoEntityViolations($item);
$em->persist($item);
$em->flush();
$this->assertHasNoEntityViolations($category);
$categoryRepo->create($category);
$this->assertSame('cat', (string) $item);
$this->assertSame(1, $repo->count([]));
$this->assertSame('cat', (string) $category);
$this->assertSame(1, $categoryRepo->count([]));
$forum = (new CForum())
->setForumTitle('forum')
->setParent($course)
->setCreator($teacher)
->setForumCategory($category)
;
$forumRepo->create($forum);
/** @var CForumCategory $category */
$category = $categoryRepo->find($category->getIid());
$this->assertSame(1, $category->getForums()->count());
}
}

@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CourseBundle\Repository;
use Chamilo\CourseBundle\Entity\CForum;
use Chamilo\CourseBundle\Entity\CForumPost;
use Chamilo\CourseBundle\Entity\CForumThread;
use Chamilo\CourseBundle\Repository\CForumPostRepository;
use Chamilo\CourseBundle\Repository\CForumRepository;
use Chamilo\CourseBundle\Repository\CForumThreadRepository;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
class CForumPostRepositoryTest extends AbstractApiTest
{
use ChamiloTestTrait;
public function testCreate(): void
{
self::bootKernel();
$course = $this->createCourse('new');
$teacher = $this->createUser('teacher');
$forumRepo = self::getContainer()->get(CForumRepository::class);
$threadRepo = self::getContainer()->get(CForumThreadRepository::class);
$postRepo = self::getContainer()->get(CForumPostRepository::class);
$forum = (new CForum())
->setForumTitle('forum')
->setParent($course)
->setCreator($teacher)
;
$forumRepo->create($forum);
$thread = (new CForumThread())
->setThreadTitle('thread title')
->setForum($forum)
->setParent($course)
->setCreator($teacher)
;
$threadRepo->create($thread);
$post = (new CForumPost())
->setPostTitle('post')
->setParent($course)
->setCreator($teacher)
->setThread($thread)
->setForum($forum)
->setUser($teacher)
;
$postRepo->create($post);
$this->assertSame('post', (string) $post);
$this->assertSame(1, $postRepo->count([]));
$this->assertSame(1, $threadRepo->count([]));
$this->assertSame(1, $forumRepo->count([]));
/** @var CForumThread $thread */
$thread = $threadRepo->find($thread->getIid());
/** @var CForum $forum */
$forum = $forumRepo->find($forum->getIid());
$this->assertSame(1, $thread->getPosts()->count());
$this->assertSame(1, $forum->getThreads()->count());
$this->assertSame(1, $forum->getPosts()->count());
$forumRepo->delete($forum);
$this->assertSame(0, $postRepo->count([]));
$this->assertSame(0, $threadRepo->count([]));
$this->assertSame(0, $forumRepo->count([]));
}
}

@ -19,7 +19,6 @@ class CForumRepositoryTest extends AbstractApiTest
{
self::bootKernel();
$em = $this->getEntityManager();
$repo = self::getContainer()->get(CForumRepository::class);
$course = $this->createCourse('new');
@ -31,8 +30,7 @@ class CForumRepositoryTest extends AbstractApiTest
->setCreator($teacher)
;
$this->assertHasNoEntityViolations($item);
$em->persist($item);
$em->flush();
$repo->create($item);
$this->assertSame('forum', (string) $item);
$this->assertSame(1, $repo->count([]));

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CourseBundle\Repository;
use Chamilo\CourseBundle\Entity\CForum;
use Chamilo\CourseBundle\Entity\CForumThread;
use Chamilo\CourseBundle\Repository\CForumRepository;
use Chamilo\CourseBundle\Repository\CForumThreadRepository;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
class CForumThreadRepositoryTest extends AbstractApiTest
{
use ChamiloTestTrait;
public function testCreate(): void
{
self::bootKernel();
$course = $this->createCourse('new');
$teacher = $this->createUser('teacher');
$forumRepo = self::getContainer()->get(CForumRepository::class);
$threadRepo = self::getContainer()->get(CForumThreadRepository::class);
$forum = (new CForum())
->setForumTitle('forum')
->setParent($course)
->setCreator($teacher)
;
$forumRepo->create($forum);
$thread = (new CForumThread())
->setThreadTitle('thread title')
->setForum($forum)
->setParent($course)
->setCreator($teacher)
;
$threadRepo->create($thread);
/** @var CForum $forum */
$forum = $forumRepo->find($forum->getIid());
$this->assertSame('thread title', (string) $thread);
$this->assertSame(1, $threadRepo->count([]));
$this->assertSame(1, $forumRepo->count([]));
$this->assertSame(1, $forum->getThreads()->count());
$forumRepo->delete($forum);
$this->assertSame(0, $threadRepo->count([]));
$this->assertSame(0, $forumRepo->count([]));
}
}

@ -37,10 +37,38 @@ class CQuizRepositoryTest extends AbstractApiTest
$this->assertSame('exercise', (string) $item);
$this->assertSame(1, $repo->count([]));
$repo->updateNodeForResource($item);
$link = $repo->getLink($item, $this->getContainer()->get('router'));
$this->assertSame('/main/exercise/overview.php?exerciseId='.$item->getIid(), $link);
}
public function testUpdateNodeForResource(): void
{
self::bootKernel();
$repo = self::getContainer()->get(CQuizRepository::class);
$course = $this->createCourse('new');
$teacher = $this->createUser('teacher');
$item = (new CQuiz())
->setTitle('exercise')
->setParent($course)
->setCreator($teacher)
;
$repo->create($item);
$this->assertSame(1, $repo->count([]));
$item->setTitle('exercise modified');
$repo->updateNodeForResource($item);
/** @var CQuiz $newExercise */
$newExercise = $repo->find($item->getIid());
$this->assertSame('exercise modified', $newExercise->getTitle());
}
public function testFindAllByCourse(): void
{
self::bootKernel();
@ -60,5 +88,26 @@ class CQuizRepositoryTest extends AbstractApiTest
$qb = $repo->findAllByCourse($course);
$this->assertSame(1, \count($qb->getQuery()->getResult()));
$found = $repo->findCourseResourceByTitle('exercise', $course->getResourceNode(), $course);
$this->assertNotNull($found);
$foundList = $repo->findCourseResourcesByTitle('exercise', $course->getResourceNode(), $course);
$this->assertSame(1, \count($foundList));
$found = $repo->getResourceByCreatorFromTitle('exercise', $teacher, $course->getResourceNode());
$this->assertNotNull($found);
$qb = $repo->getResourcesByCreator($teacher, $course->getResourceNode());
$this->assertSame(1, \count($qb->getQuery()->getResult()));
$qb = $repo->getResourcesByCourseLinkedToUser($teacher, $course);
$this->assertSame(1, \count($qb->getQuery()->getResult()));
$qb = $repo->getResourcesByLinkedUser($teacher, $course->getResourceNode());
$this->assertSame(0, \count($qb->getQuery()->getResult()));
$node = $repo->getResourceFromResourceNode($exercise->getResourceNode()->getId());
$this->assertNotNull($node);
}
}

Loading…
Cancel
Save