Forums: Fix delete forum + add tests.

pull/4042/head
Julio 3 years ago
parent fd60f2f548
commit 93675bcf7d
  1. 2
      src/CoreBundle/Migrations/Schema/V200/Version20170625153000.php
  2. 41
      src/CoreBundle/Migrations/Schema/V200/Version20210205082253.php
  3. 7
      src/CourseBundle/Entity/CForum.php
  4. 7
      src/CourseBundle/Entity/CForumPost.php
  5. 2
      src/CourseBundle/Entity/CForumThread.php
  6. 14
      src/CourseBundle/Entity/CForumThreadQualify.php
  7. 17
      src/CourseBundle/Repository/CForumRepository.php
  8. 22
      tests/CourseBundle/Repository/CForumPostRepositoryTest.php
  9. 17
      tests/CourseBundle/Repository/CForumRepositoryTest.php
  10. 13
      tests/CourseBundle/Repository/CForumThreadRepositoryTest.php

@ -115,7 +115,7 @@ class Version20170625153000 extends AbstractMigrationChamilo
'ALTER TABLE c_forum_thread ADD CONSTRAINT FK_5DA7884C1BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE' 'ALTER TABLE c_forum_thread ADD CONSTRAINT FK_5DA7884C1BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE'
); );
$this->addSql( $this->addSql(
'ALTER TABLE c_forum_thread ADD CONSTRAINT FK_5DA7884C29CCBAD0 FOREIGN KEY (forum_id) REFERENCES c_forum_forum (iid) ON DELETE SET NULL' 'ALTER TABLE c_forum_thread ADD CONSTRAINT FK_5DA7884C29CCBAD0 FOREIGN KEY (forum_id) REFERENCES c_forum_forum (iid) ON DELETE CASCADE'
); );
$this->addSql('CREATE UNIQUE INDEX UNIQ_5DA7884C1BAD783F ON c_forum_thread (resource_node_id)'); $this->addSql('CREATE UNIQUE INDEX UNIQ_5DA7884C1BAD783F ON c_forum_thread (resource_node_id)');
} }

@ -4,10 +4,14 @@ declare(strict_types=1);
namespace Chamilo\CoreBundle\Migrations\Schema\V200; namespace Chamilo\CoreBundle\Migrations\Schema\V200;
use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Entity\AccessUrlRelUserGroup;
use Chamilo\CoreBundle\Entity\User; use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Entity\Usergroup; use Chamilo\CoreBundle\Entity\Usergroup;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository; use Chamilo\CoreBundle\Repository\Node\IllustrationRepository;
use Chamilo\CoreBundle\Repository\Node\UsergroupRepository;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
@ -22,8 +26,7 @@ final class Version20210205082253 extends AbstractMigrationChamilo
public function up(Schema $schema): void public function up(Schema $schema): void
{ {
$container = $this->getContainer(); $container = $this->getContainer();
$doctrine = $container->get('doctrine'); $em = $this->getEntityManager();
$em = $doctrine->getManager();
/** @var Connection $connection */ /** @var Connection $connection */
$connection = $em->getConnection(); $connection = $em->getConnection();
@ -72,21 +75,51 @@ final class Version20210205082253 extends AbstractMigrationChamilo
$em->flush(); $em->flush();
$em->clear(); $em->clear();
// Migrate Usergroup images. // Migrate Usergroup.
$counter = 1; $counter = 1;
$q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\Usergroup u'); $q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\Usergroup u');
$admin = $this->getAdmin(); $admin = $this->getAdmin();
$userGroupRepo = $container->get(UsergroupRepository::class);
$urlRepo = $container->get(AccessUrlRepository::class);
$urlList = $urlRepo->findAll();
/** @var AccessUrl $url */
$url = $urlList[0];
/** @var Usergroup $userGroup */ /** @var Usergroup $userGroup */
foreach ($q->toIterable() as $userGroup) { foreach ($q->toIterable() as $userGroup) {
if ($userGroup->hasResourceNode()) { if ($userGroup->hasResourceNode()) {
continue; continue;
} }
$id = $userGroup->getId();
$userGroup->setCreator($admin);
if (0 === $userGroup->getUrls()->count()) {
$accessUrlRelUserGroup = (new AccessUrlRelUserGroup())
->setUserGroup($userGroup)
->setUrl($url)
;
$userGroup->getUrls()->add($accessUrlRelUserGroup);
}
$userGroupRepo->addResourceNode($userGroup, $admin, $url);
$em->persist($userGroup);
$em->flush();
}
$em->clear();
// Migrate Usergroup images.
$q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\Usergroup u');
/** @var Usergroup $userGroup */
foreach ($q->toIterable() as $userGroup) {
if (!$userGroup->hasResourceNode()) {
continue;
}
$picture = $userGroup->getPicture(); $picture = $userGroup->getPicture();
if (empty($picture)) { if (empty($picture)) {
continue; continue;
} }
$id = $userGroup->getId();
$path = "groups/{$id}/"; $path = "groups/{$id}/";
if (!empty($setting) && 'true' === $setting['selected_value']) { if (!empty($setting) && 'true' === $setting['selected_value']) {
$path = 'groups/'.substr((string) $id, 0, 1).'/'.$id.'/'; $path = 'groups/'.substr((string) $id, 0, 1).'/'.$id.'/';

@ -209,12 +209,7 @@ class CForum extends AbstractResource implements ResourceInterface
return $this; return $this;
} }
/** public function getForumThreads(): ?int
* Get forumThreads.
*
* @return int
*/
public function getForumThreads()
{ {
return $this->forumThreads; return $this->forumThreads;
} }

@ -75,7 +75,7 @@ class CForumPost extends AbstractResource implements ResourceInterface
protected ?int $status = null; 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") * @ORM\JoinColumn(name="thread_id", referencedColumnName="iid", nullable=true, onDelete="SET NULL")
*/ */
protected ?CForumThread $thread = null; protected ?CForumThread $thread = null;
@ -108,10 +108,7 @@ class CForumPost extends AbstractResource implements ResourceInterface
/** /**
* @var Collection|CForumAttachment[] * @var Collection|CForumAttachment[]
* *
* @ORM\OneToMany( * @ORM\OneToMany(targetEntity="CForumAttachment", mappedBy="post", cascade={"persist", "remove"}, orphanRemoval=true)
* targetEntity="Chamilo\CourseBundle\Entity\CForumAttachment",
* mappedBy="post", cascade={"persist", "remove"}, orphanRemoval=true
* )
*/ */
protected Collection $attachments; protected Collection $attachments;

@ -42,7 +42,7 @@ class CForumThread extends AbstractResource implements ResourceInterface
/** /**
* @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CForum", inversedBy="threads") * @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CForum", inversedBy="threads")
* @ORM\JoinColumn(name="forum_id", referencedColumnName="iid", nullable=true, onDelete="SET NULL") * @ORM\JoinColumn(name="forum_id", referencedColumnName="iid", nullable=true, onDelete="CASCADE")
*/ */
protected ?CForum $forum = null; protected ?CForum $forum = null;

@ -11,8 +11,6 @@ use DateTime;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
/** /**
* CForumThreadQualify.
*
* @ORM\Table( * @ORM\Table(
* name="c_forum_thread_qualify", * name="c_forum_thread_qualify",
* indexes={ * indexes={
@ -119,4 +117,16 @@ class CForumThreadQualify
{ {
return $this->cId; return $this->cId;
} }
public function getThread(): CForumThread
{
return $this->thread;
}
public function setThread(CForumThread $thread): self
{
$this->thread = $thread;
return $this;
}
} }

@ -6,10 +6,8 @@ declare(strict_types=1);
namespace Chamilo\CourseBundle\Repository; namespace Chamilo\CourseBundle\Repository;
use Chamilo\CoreBundle\Entity\ResourceInterface;
use Chamilo\CoreBundle\Repository\ResourceRepository; use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CourseBundle\Entity\CForum; use Chamilo\CourseBundle\Entity\CForum;
use Chamilo\CourseBundle\Entity\CForumThread;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
final class CForumRepository extends ResourceRepository final class CForumRepository extends ResourceRepository
@ -18,19 +16,4 @@ final class CForumRepository extends ResourceRepository
{ {
parent::__construct($registry, CForum::class); parent::__construct($registry, CForum::class);
} }
public function delete(ResourceInterface $resource): void
{
/** @var CForum $resource */
$threads = $resource->getThreads();
$repo = $this->getEntityManager()->getRepository(CForumThread::class);
if (!empty($threads)) {
foreach ($threads as $thread) {
/** @var CForumThread $thread */
$repo->delete($thread);
}
}
parent::delete($resource);
}
} }

@ -60,6 +60,7 @@ class CForumPostRepositoryTest extends AbstractApiTest
->setThread($thread) ->setThread($thread)
->setForum($forum) ->setForum($forum)
->setUser($teacher) ->setUser($teacher)
->addCourseLink($course)
; ;
$postRepo->create($post); $postRepo->create($post);
@ -104,11 +105,26 @@ class CForumPostRepositoryTest extends AbstractApiTest
$this->assertSame(1, $forum->getThreads()->count()); $this->assertSame(1, $forum->getThreads()->count());
$this->assertSame(1, $forum->getPosts()->count()); $this->assertSame(1, $forum->getPosts()->count());
/*$forumRepo->delete($forum); $count = $postRepo->countCourseForumPosts($course);
$this->assertSame(1, $count);
$count = $postRepo->countUserForumPosts($teacher, $course);
$this->assertSame(1, $count);
$postRepo->delete($post);
$this->assertSame(0, $attachmentRepo->count([]));
$this->assertSame(0, $postRepo->count([])); $this->assertSame(0, $postRepo->count([]));
$this->assertSame(0, $attachmentRepo->count([]));
$this->assertSame(1, $threadRepo->count([]));
$this->assertSame(1, $forumRepo->count([]));
$this->getEntityManager()->clear();
/** @var CForum $forum */
$forum = $forumRepo->find($forum->getIid());
$forumRepo->delete($forum);
$this->assertSame(0, $threadRepo->count([])); $this->assertSame(0, $threadRepo->count([]));
$this->assertSame(0, $forumRepo->count([]));*/ $this->assertSame(0, $forumRepo->count([]));
} }
} }

@ -6,6 +6,7 @@ declare(strict_types=1);
namespace Chamilo\Tests\CourseBundle\Repository; namespace Chamilo\Tests\CourseBundle\Repository;
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
use Chamilo\CourseBundle\Entity\CForum; use Chamilo\CourseBundle\Entity\CForum;
use Chamilo\CourseBundle\Entity\CLp; use Chamilo\CourseBundle\Entity\CLp;
use Chamilo\CourseBundle\Repository\CForumRepository; use Chamilo\CourseBundle\Repository\CForumRepository;
@ -20,7 +21,8 @@ class CForumRepositoryTest extends AbstractApiTest
public function testCreate(): void public function testCreate(): void
{ {
$repo = self::getContainer()->get(CForumRepository::class); $courseRepo = self::getContainer()->get(CourseRepository::class);
$forumRepo = self::getContainer()->get(CForumRepository::class);
$course = $this->createCourse('new'); $course = $this->createCourse('new');
$teacher = $this->createUser('teacher'); $teacher = $this->createUser('teacher');
@ -48,11 +50,20 @@ class CForumRepositoryTest extends AbstractApiTest
->setCreator($teacher) ->setCreator($teacher)
; ;
$this->assertHasNoEntityViolations($forum); $this->assertHasNoEntityViolations($forum);
$repo->create($forum); $forumRepo->create($forum);
$this->assertSame('forum', (string) $forum); $this->assertSame('forum', (string) $forum);
$this->assertSame(1, $repo->count([]));
$this->assertSame($forum->getIid(), $forum->getResourceIdentifier());
$this->assertSame(1, $forum->getAllowAnonymous());
$this->assertSame(1, $forumRepo->count([]));
$this->assertSame(0, $forum->getForumPosts()); $this->assertSame(0, $forum->getForumPosts());
$courseRepo->delete($course);
$this->assertSame(0, $forumRepo->count([]));
$this->assertSame(0, $courseRepo->count([]));
} }
public function testCreateWithAttachment(): void public function testCreateWithAttachment(): void

@ -8,6 +8,7 @@ namespace Chamilo\Tests\CourseBundle\Repository;
use Chamilo\CourseBundle\Entity\CForum; use Chamilo\CourseBundle\Entity\CForum;
use Chamilo\CourseBundle\Entity\CForumThread; use Chamilo\CourseBundle\Entity\CForumThread;
use Chamilo\CourseBundle\Entity\CForumThreadQualify;
use Chamilo\CourseBundle\Repository\CForumRepository; use Chamilo\CourseBundle\Repository\CForumRepository;
use Chamilo\CourseBundle\Repository\CForumThreadRepository; use Chamilo\CourseBundle\Repository\CForumThreadRepository;
use Chamilo\Tests\AbstractApiTest; use Chamilo\Tests\AbstractApiTest;
@ -26,6 +27,8 @@ class CForumThreadRepositoryTest extends AbstractApiTest
$forumRepo = self::getContainer()->get(CForumRepository::class); $forumRepo = self::getContainer()->get(CForumRepository::class);
$threadRepo = self::getContainer()->get(CForumThreadRepository::class); $threadRepo = self::getContainer()->get(CForumThreadRepository::class);
$em = $this->getEntityManager();
$forum = (new CForum()) $forum = (new CForum())
->setForumTitle('forum') ->setForumTitle('forum')
->setParent($course) ->setParent($course)
@ -53,6 +56,15 @@ class CForumThreadRepositoryTest extends AbstractApiTest
; ;
$threadRepo->create($thread); $threadRepo->create($thread);
$qualify = (new CForumThreadQualify())
->setQualify(100)
->setQualifyTime(new DateTime())
->setThread($thread)
->setCId($course->getId())
;
$em->persist($qualify);
$em->flush();
/** @var CForum $forum */ /** @var CForum $forum */
$forum = $forumRepo->find($forum->getIid()); $forum = $forumRepo->find($forum->getIid());
@ -66,6 +78,7 @@ class CForumThreadRepositoryTest extends AbstractApiTest
$this->assertTrue($forum->hasThread($thread)); $this->assertTrue($forum->hasThread($thread));
$this->assertNull($threadRepo->getForumThread('title', $course)); $this->assertNull($threadRepo->getForumThread('title', $course));
$this->assertSame($thread->getIid(), $thread->getResourceIdentifier());
$this->assertSame(0, $thread->getThreadViews()); $this->assertSame(0, $thread->getThreadViews());
$threadRepo->increaseView($thread); $threadRepo->increaseView($thread);
$this->assertSame(1, $thread->getThreadViews()); $this->assertSame(1, $thread->getThreadViews());

Loading…
Cancel
Save