Repositories - Remove unused code, fix queries + add tests

pull/4020/head^2
Julio 3 years ago
parent 5e9101f0f8
commit 421268c348
  1. 16
      src/CourseBundle/Repository/CQuizRepository.php
  2. 33
      src/CourseBundle/Repository/CStudentPublicationRepository.php
  3. 15
      src/CourseBundle/Repository/CSurveyRepository.php
  4. 1
      tests/CoreBundle/Repository/ExtraFieldOptionsRepositoryTest.php
  5. 38
      tests/CoreBundle/Repository/SessionRepositoryTest.php
  6. 9
      tests/CoreBundle/Repository/SysAnnouncementRepositoryTest.php
  7. 2
      tests/CoreBundle/Tool/ToolChainTest.php
  8. 7
      tests/CourseBundle/Repository/CExerciseCategoryRepositoryTest.php
  9. 5
      tests/CourseBundle/Repository/CForumCategoryRepositoryTest.php
  10. 11
      tests/CourseBundle/Repository/CForumPostRepositoryTest.php
  11. 52
      tests/CourseBundle/Repository/CForumThreadRepositoryTest.php
  12. 4
      tests/CourseBundle/Repository/CGlossaryRepositoryTest.php
  13. 3
      tests/CourseBundle/Repository/CGroupRepositoryTest.php
  14. 29
      tests/CourseBundle/Repository/CQuizRepositoryTest.php
  15. 4
      tests/CourseBundle/Repository/CStudentPublicationRepositoryTest.php
  16. 10
      tests/CourseBundle/Repository/CSurveyRepositoryTest.php

@ -58,22 +58,6 @@ final class CQuizRepository extends ResourceRepository implements ResourceWithLi
return $router->generate('legacy_main', $params); return $router->generate('legacy_main', $params);
} }
public function deleteAllByCourse(Course $course): void
{
$qb = $this->getResourcesByCourse($course);
$resources = $qb->getQuery()->getResult();
$em = $this->getEntityManager();
/*foreach ($resources as $quiz) {
$questions = $quiz->getQuestions();
foreach ($questions as $question) {
//$em->remove($question);
}
$em->remove($quiz);
}*/
//$em->flush();
}
private function addDateFilterQueryBuilder(DateTime $dateTime, QueryBuilder $qb = null): QueryBuilder private function addDateFilterQueryBuilder(DateTime $dateTime, QueryBuilder $qb = null): QueryBuilder
{ {
$qb = $this->getOrCreateQueryBuilder($qb); $qb = $this->getOrCreateQueryBuilder($qb);

@ -12,9 +12,7 @@ use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Repository\ResourceRepository; use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CourseBundle\Entity\CGroup; use Chamilo\CourseBundle\Entity\CGroup;
use Chamilo\CourseBundle\Entity\CStudentPublication; use Chamilo\CourseBundle\Entity\CStudentPublication;
use Chamilo\CourseBundle\Entity\CStudentPublicationAssignment;
use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
@ -78,6 +76,7 @@ final class CStudentPublicationRepository extends ResourceRepository
public function countUserPublications(User $user, Course $course, Session $session = null, CGroup $group = null): int public function countUserPublications(User $user, Course $course, Session $session = null, CGroup $group = null): int
{ {
$qb = $this->getResourcesByCourseLinkedToUser($user, $course, $session); $qb = $this->getResourcesByCourseLinkedToUser($user, $course, $session);
$qb->andWhere('resource.publicationParent IS NOT NULL');
return $this->getCount($qb); return $this->getCount($qb);
} }
@ -94,35 +93,13 @@ final class CStudentPublicationRepository extends ResourceRepository
/** /**
* Find all the works registered by a teacher. * Find all the works registered by a teacher.
*/ */
public function findWorksByTeacher(User $user, Course $course, Session $session = null, int $groupId = 0): array public function findWorksByTeacher(User $user, Course $course, Session $session = null)
{ {
$qb = $this->createQueryBuilder('w'); $qb = $this->getResourcesByCourseLinkedToUser($user, $course, $session);
$qb->andWhere('resource.publicationParent IS NOT NULL');
return $qb return $qb
->leftJoin( ->orderBy('resource.sentDate', Criteria::ASC)
CStudentPublicationAssignment::class,
'a',
Join::WITH,
'a.publicationId = w.iid AND a.cId = w.cId'
)
->where(
$qb->expr()->andX(
$qb->expr()->eq('w.cId', ':course'),
$qb->expr()->eq('w.session', ':session'),
$qb->expr()->in('w.active', [0, 1]),
$qb->expr()->eq('w.parentId', 0),
$qb->expr()->eq('w.postGroupId', ':group'),
$qb->expr()->eq('w.userId', ':user')
)
)
->orderBy('w.sentDate', Criteria::ASC)
->setParameters([
'course' => $course->getId(),
'session' => $session,
'group' => $groupId,
'user' => $user->getId(),
])
->getQuery() ->getQuery()
->getResult() ->getResult()
; ;

@ -42,21 +42,6 @@ final class CSurveyRepository extends ResourceRepository
return $qb; return $qb;
} }
public function getTreeByCourse(Course $course, Session $session = null): void
{
/*$qb = $this->getResourcesByCourse($course, $session);
$this->getEntityManager()->getConfiguration()->addCustomHydrationMode(
'tree',
TreeObjectHydrator::class
);
/*$options = ['decorate' => true, 'html' => false];
$tree = $this->buildTree($qb->getQuery()->getArrayResult(), $options);*/
//return $qb->getQuery()->setHint(Query::HINT_INCLUDE_META_COLUMNS, true)->getResult('tree');
}
protected function addTitleQueryBuilder(?string $title, QueryBuilder $qb = null): QueryBuilder protected function addTitleQueryBuilder(?string $title, QueryBuilder $qb = null): QueryBuilder
{ {
$qb = $this->getOrCreateQueryBuilder($qb); $qb = $this->getOrCreateQueryBuilder($qb);

@ -64,7 +64,6 @@ class ExtraFieldOptionsRepositoryTest extends AbstractApiTest
$this->testCreate(); $this->testCreate();
$em = $this->getEntityManager(); $em = $this->getEntityManager();
$extraFieldRepo = static::getContainer()->get(ExtraFieldRepository::class);
$extraFieldOptionsRepo = static::getContainer()->get(ExtraFieldOptionsRepository::class); $extraFieldOptionsRepo = static::getContainer()->get(ExtraFieldOptionsRepository::class);
/** @var ExtraFieldOptions $extraFieldOption */ /** @var ExtraFieldOptions $extraFieldOption */

@ -226,11 +226,47 @@ class SessionRepositoryTest extends AbstractApiTest
$this->assertResponseStatusCodeSame(422); $this->assertResponseStatusCodeSame(422);
} }
public function testAddUserToSessionInactive(): void
{
$sessionRepo = self::getContainer()->get(SessionRepository::class);
$userRepo = self::getContainer()->get(UserRepository::class);
// Add session
$session = $this->createSession('session');
$course = $this->createCourse('new');
$student = $this->createUser('student');
$sessionAdmin = $this->createUser('session_admin');
$generalCoach = $this->createUser('general_coach');
// Add session admin + add course to session.
$session
->setAccessStartDate(new DateTime('now +30 days'))
->addCourse($course)
->addSessionAdmin($sessionAdmin)
->addGeneralCoach($generalCoach)
;
$sessionRepo->update($session);
$this->assertSame(1, $session->getCourses()->count());
$this->expectException(Exception::class);
$sessionRepo->addUserInCourse(Session::STUDENT, $student, $course, $session);
$session->setAccessStartDate(new DateTime());
$sessionRepo->update($session);
$student->setActive(false);
$userRepo->update($student);
$this->expectException(Exception::class);
$sessionRepo->addUserInCourse(Session::STUDENT, $student, $course, $session);
}
public function testAddUserToSession(): void public function testAddUserToSession(): void
{ {
$em = $this->getEntityManager(); $em = $this->getEntityManager();
$sessionRepo = self::getContainer()->get(SessionRepository::class); $sessionRepo = self::getContainer()->get(SessionRepository::class);
$courseRepo = self::getContainer()->get(CourseRepository::class);
$userRepo = self::getContainer()->get(UserRepository::class); $userRepo = self::getContainer()->get(UserRepository::class);
// Add session // Add session

@ -83,6 +83,8 @@ class SysAnnouncementRepositoryTest extends WebTestCase
$items = $repo->getAnnouncements($user, $this->getAccessUrl(), ''); $items = $repo->getAnnouncements($user, $this->getAccessUrl(), '');
$this->assertCount(1, $items); $this->assertCount(1, $items);
$session = $this->createSession('session');
$career = (new Career()) $career = (new Career())
->setName('Doctor') ->setName('Doctor')
; ;
@ -93,9 +95,14 @@ class SysAnnouncementRepositoryTest extends WebTestCase
->setCareer($career) ->setCareer($career)
->setStatus(1) ->setStatus(1)
; ;
$promotion->getSessions()->add($session);
$em->persist($promotion); $em->persist($promotion);
$em->flush(); $em->flush();
$this->assertSame(1, $promotion->getSessions()->count());
$sysAnnouncement = (new SysAnnouncement()) $sysAnnouncement = (new SysAnnouncement())
->setTitle('Welcome to Chamilo!') ->setTitle('Welcome to Chamilo!')
->setContent('content') ->setContent('content')
@ -111,6 +118,6 @@ class SysAnnouncementRepositoryTest extends WebTestCase
$em->flush(); $em->flush();
$items = $repo->getAnnouncements($user, $this->getAccessUrl(), ''); $items = $repo->getAnnouncements($user, $this->getAccessUrl(), '');
$this->assertSame(1, \count($items)); $this->assertCount(1, $items);
} }
} }

@ -91,6 +91,8 @@ class ToolChainTest extends AbstractApiTest
$this->assertNotEmpty($name); $this->assertNotEmpty($name);
$link = $tool->getLink(); $link = $tool->getLink();
$this->assertNotEmpty($tool->getCategory());
$this->assertNotEmpty($tool->getIcon());
$this->assertNotEmpty($link, sprintf('Link for tool %s is empty', $name)); $this->assertNotEmpty($link, sprintf('Link for tool %s is empty', $name));
$types = $tool->getResourceTypes(); $types = $tool->getResourceTypes();

@ -25,16 +25,21 @@ class CExerciseCategoryRepositoryTest extends AbstractApiTest
$item = (new CExerciseCategory()) $item = (new CExerciseCategory())
->setName('cat') ->setName('cat')
->setDescription('des') ->setDescription('desc')
->setCourse($course) ->setCourse($course)
->setParent($course) ->setParent($course)
->setCreator($teacher) ->setCreator($teacher)
->setPosition(1)
; ;
$this->assertHasNoEntityViolations($item); $this->assertHasNoEntityViolations($item);
$em->persist($item); $em->persist($item);
$em->flush(); $em->flush();
$this->assertSame('cat', (string) $item); $this->assertSame('cat', (string) $item);
$this->assertSame($item->getId(), $item->getResourceIdentifier());
$this->assertSame('desc', $item->getDescription());
$this->assertSame('cat', $item->getName());
$this->assertSame(1, $repo->count([])); $this->assertSame(1, $repo->count([]));
$this->assertCount(1, $repo->getCategories($course->getId())); $this->assertCount(1, $repo->getCategories($course->getId()));
} }

@ -39,6 +39,11 @@ class CForumCategoryRepositoryTest extends AbstractApiTest
$this->assertHasNoEntityViolations($category); $this->assertHasNoEntityViolations($category);
$categoryRepo->create($category); $categoryRepo->create($category);
$this->assertSame($category->getIid(), $category->getResourceIdentifier());
$this->assertSame(1, $category->getLocked());
$this->assertSame(1, $category->getCatOrder());
$this->assertSame('comment', $category->getCatComment());
$this->assertSame('cat 1', (string) $category); $this->assertSame('cat 1', (string) $category);
$forum = (new CForum()) $forum = (new CForum())

@ -46,11 +46,11 @@ class CForumPostRepositoryTest extends AbstractApiTest
->setCreator($teacher) ->setCreator($teacher)
; ;
$threadRepo->create($thread); $threadRepo->create($thread);
$date = new DateTime();
$post = (new CForumPost()) $post = (new CForumPost())
->setPostTitle('post') ->setPostTitle('post')
->setPostText('text') ->setPostText('text')
->setPostDate(new DateTime()) ->setPostDate($date)
->setPostNotification(true) ->setPostNotification(true)
->setVisible(true) ->setVisible(true)
->setStatus(1) ->setStatus(1)
@ -64,6 +64,13 @@ class CForumPostRepositoryTest extends AbstractApiTest
; ;
$postRepo->create($post); $postRepo->create($post);
$this->assertSame('text', $post->getPostText());
$this->assertSame($date, $post->getPostDate());
$this->assertTrue($post->getVisible());
$this->assertTrue($post->getPostNotification());
$this->assertSame(1, $post->getStatus());
$this->assertSame($post->getIid(), $post->getResourceIdentifier());
$file = $this->getUploadedFile(); $file = $this->getUploadedFile();
$attachment = (new CForumAttachment()) $attachment = (new CForumAttachment())

@ -21,13 +21,14 @@ class CForumThreadRepositoryTest extends AbstractApiTest
public function testCreate(): void public function testCreate(): void
{ {
$em = $this->getEntityManager();
$course = $this->createCourse('new'); $course = $this->createCourse('new');
$teacher = $this->createUser('teacher'); $teacher = $this->createUser('teacher');
$forumRepo = self::getContainer()->get(CForumRepository::class); $forumRepo = self::getContainer()->get(CForumRepository::class);
$threadRepo = self::getContainer()->get(CForumThreadRepository::class); $threadRepo = self::getContainer()->get(CForumThreadRepository::class);
$qualifyRepo = $em->getRepository(CForumThreadQualify::class);
$em = $this->getEntityManager();
$forum = (new CForum()) $forum = (new CForum())
->setForumTitle('forum') ->setForumTitle('forum')
@ -54,6 +55,7 @@ class CForumThreadRepositoryTest extends AbstractApiTest
->setCreator($teacher) ->setCreator($teacher)
->addCourseLink($course) ->addCourseLink($course)
; ;
$this->assertHasNoEntityViolations($thread);
$threadRepo->create($thread); $threadRepo->create($thread);
$qualify = (new CForumThreadQualify()) $qualify = (new CForumThreadQualify())
@ -87,5 +89,51 @@ class CForumThreadRepositoryTest extends AbstractApiTest
$this->assertSame(0, $threadRepo->count([])); $this->assertSame(0, $threadRepo->count([]));
$this->assertSame(0, $forumRepo->count([])); $this->assertSame(0, $forumRepo->count([]));
$this->assertSame(0, $qualifyRepo->count([]));
}
public function testDelete(): void
{
$em = $this->getEntityManager();
$course = $this->createCourse('new');
$teacher = $this->createUser('teacher');
$qualifyRepo = $em->getRepository(CForumThreadQualify::class);
$forumRepo = self::getContainer()->get(CForumRepository::class);
$threadRepo = self::getContainer()->get(CForumThreadRepository::class);
$forum = (new CForum())
->setForumTitle('forum')
->setParent($course)
->setCreator($teacher)
->addCourseLink($course)
;
$forumRepo->create($forum);
$thread = (new CForumThread())
->setThreadTitle('thread title')
->setForum($forum)
->setParent($course)
->setCreator($teacher)
->addCourseLink($course)
;
$threadRepo->create($thread);
$qualify = (new CForumThreadQualify())
->setQualify(100)
->setQualifyTime(new DateTime())
->setThread($thread)
->setCId($course->getId())
;
$em->persist($qualify);
$em->flush();
$thread = $threadRepo->find($thread->getIid());
$threadRepo->delete($thread);
$this->assertSame(0, $qualifyRepo->count([]));
$this->assertSame(0, $threadRepo->count([]));
$this->assertSame(1, $forumRepo->count([]));
} }
} }

@ -48,6 +48,10 @@ class CGlossaryRepositoryTest extends AbstractApiTest
$link = $glossaryRepo->getLink($glossary, $router); $link = $glossaryRepo->getLink($glossary, $router);
$this->assertSame($link, '/main/glossary/index.php?glossary_id='.$glossary->getIid()); $this->assertSame($link, '/main/glossary/index.php?glossary_id='.$glossary->getIid());
$link = $glossaryRepo->getLink($glossary, $router, ['extra' => 'extra']);
$this->assertSame($link, '/main/glossary/index.php?glossary_id='.$glossary->getIid().'&extra=extra');
$this->assertSame(1, $glossaryRepo->count([])); $this->assertSame(1, $glossaryRepo->count([]));
$courseRepo->delete($course); $courseRepo->delete($course);

@ -202,5 +202,8 @@ class CGroupRepositoryTest extends AbstractApiTest
$qb = $repo->findAllByCourse($course); $qb = $repo->findAllByCourse($course);
$this->assertCount(1, $qb->getQuery()->getResult()); $this->assertCount(1, $qb->getQuery()->getResult());
$qb = $repo->findAllByCourse($course, null, null, 1);
$this->assertCount(1, $qb->getQuery()->getResult());
} }
} }

@ -6,6 +6,7 @@ declare(strict_types=1);
namespace Chamilo\Tests\CourseBundle\Repository; namespace Chamilo\Tests\CourseBundle\Repository;
use Chamilo\CourseBundle\Entity\CExerciseCategory;
use Chamilo\CourseBundle\Entity\CQuiz; use Chamilo\CourseBundle\Entity\CQuiz;
use Chamilo\CourseBundle\Repository\CQuizRepository; use Chamilo\CourseBundle\Repository\CQuizRepository;
use Chamilo\Tests\AbstractApiTest; use Chamilo\Tests\AbstractApiTest;
@ -91,18 +92,34 @@ class CQuizRepositoryTest extends AbstractApiTest
/** @var CQuiz $newExercise */ /** @var CQuiz $newExercise */
$newExercise = $repo->find($item->getIid()); $newExercise = $repo->find($item->getIid());
$this->assertSame('exercise modified', $newExercise->getTitle()); $this->assertSame('exercise modified', $newExercise->getTitle());
$this->assertSame(0, $repo->count([]));
} }
public function testFindAllByCourse(): void public function testFindAllByCourse(): void
{ {
$em = $this->getEntityManager();
$repo = self::getContainer()->get(CQuizRepository::class); $repo = self::getContainer()->get(CQuizRepository::class);
$course = $this->createCourse('new'); $course = $this->createCourse('new');
$teacher = $this->createUser('teacher'); $teacher = $this->createUser('teacher');
$category = (new CExerciseCategory())
->setName('cat')
->setDescription('desc')
->setCourse($course)
->setParent($course)
->setCreator($teacher)
->setPosition(1)
;
$this->assertHasNoEntityViolations($category);
$em->persist($category);
$em->flush();
$exercise = (new CQuiz()) $exercise = (new CQuiz())
->setTitle('exercise 1') ->setTitle('exercise 1')
->setParent($course) ->setParent($course)
->setExerciseCategory($category)
->setCreator($teacher) ->setCreator($teacher)
->addCourseLink($course) ->addCourseLink($course)
; ;
@ -121,6 +138,18 @@ class CQuizRepositoryTest extends AbstractApiTest
$qb = $repo->findAllByCourse($course); $qb = $repo->findAllByCourse($course);
$this->assertCount(2, $qb->getQuery()->getResult()); $this->assertCount(2, $qb->getQuery()->getResult());
$qb = $repo->findAllByCourse($course, null, 'exercise 1');
$this->assertCount(2, $qb->getQuery()->getResult());
$qb = $repo->findAllByCourse($course, null, null, 0);
$this->assertCount(0, $qb->getQuery()->getResult());
$qb = $repo->findAllByCourse($course, null, null, 1);
$this->assertCount(2, $qb->getQuery()->getResult());
$qb = $repo->findAllByCourse($course, null, null, null, true, $category->getId());
$this->assertCount(1, $qb->getQuery()->getResult());
$found = $repo->findCourseResourceByTitle('exercise 1', $course->getResourceNode(), $course); $found = $repo->findCourseResourceByTitle('exercise 1', $course->getResourceNode(), $course);
$this->assertNotNull($found); $this->assertNotNull($found);

@ -178,8 +178,8 @@ class CStudentPublicationRepositoryTest extends AbstractApiTest
$qb = $repo->getStudentAssignments($item, $course); $qb = $repo->getStudentAssignments($item, $course);
$this->assertCount(1, $qb->getQuery()->getResult()); $this->assertCount(1, $qb->getQuery()->getResult());
//$this->assertSame(1, $repo->countUserPublications($student, $course)); $this->assertSame(1, $repo->countUserPublications($student, $course));
//$this->assertSame(1, $repo->findWorksByTeacher($teacher, $course)); $this->assertCount(1, $repo->findWorksByTeacher($teacher, $course));
} }
public function testGetStudentPublicationByUser(): void public function testGetStudentPublicationByUser(): void

@ -66,8 +66,18 @@ class CSurveyRepositoryTest extends AbstractApiTest
$qb = $surveyRepo->findAllByCourse($course); $qb = $surveyRepo->findAllByCourse($course);
$this->assertCount(1, $qb->getQuery()->getResult()); $this->assertCount(1, $qb->getQuery()->getResult());
$qb = $surveyRepo->findAllByCourse($course, null, 'survey');
$this->assertCount(1, $qb->getQuery()->getResult());
$qb = $surveyRepo->findAllByCourse($course, null, null, 'lang');
$this->assertCount(1, $qb->getQuery()->getResult());
$qb = $surveyRepo->findAllByCourse($course, null, null, null, $teacher);
$this->assertCount(1, $qb->getQuery()->getResult());
$courseRepo->delete($course); $courseRepo->delete($course);
$this->assertSame(0, $surveyRepo->count([])); $this->assertSame(0, $surveyRepo->count([]));
$this->assertSame(0, $courseRepo->count([]));
} }
public function testCreateWithQuestions(): void public function testCreateWithQuestions(): void

Loading…
Cancel
Save