From 25bbcfddc3b4a15e77ad8f28224829d14431b7ea Mon Sep 17 00:00:00 2001 From: Julio Date: Thu, 21 Oct 2021 14:44:03 +0200 Subject: [PATCH] CStudentPublication: Add tests --- public/main/work/view.php | 6 +- public/main/work/work.lib.php | 6 +- .../Repository/CShortcutRepository.php | 1 - ...entPublicationAssignmentRepositoryTest.php | 63 ++++++++++++++++++ ...tudentPublicationCommentRepositoryTest.php | 64 ++++++++++++++++++ ...entPublicationCorrectionRepositoryTest.php | 66 +++++++++++++++++++ .../CStudentPublicationRepositoryTest.php | 46 +++++++++++-- .../Repository/CSurveyRepositoryTest.php | 56 +++++++++++++--- 8 files changed, 289 insertions(+), 19 deletions(-) create mode 100644 tests/CourseBundle/Repository/CStudentPublicationAssignmentRepositoryTest.php create mode 100644 tests/CourseBundle/Repository/CStudentPublicationCommentRepositoryTest.php create mode 100644 tests/CourseBundle/Repository/CStudentPublicationCorrectionRepositoryTest.php diff --git a/public/main/work/view.php b/public/main/work/view.php index f1c85cf836..83d48a8940 100644 --- a/public/main/work/view.php +++ b/public/main/work/view.php @@ -160,10 +160,10 @@ if (($isDrhOfCourse || $allowEdition || $isDrhOfSession || user_is_author($id)) if (null !== $file) { $em = Database::getManager(); - $correction = new CStudentPublicationCorrection(); - $correction + $correction = (new CStudentPublicationCorrection()) ->setParent($work) - ->setTitle($file->getClientOriginalName()); + ->setTitle($file->getClientOriginalName()) + ; // @todo improve file upload. $correctionRepo = Container::getStudentPublicationCorrectionRepository(); $correctionRepo->create($correction); diff --git a/public/main/work/work.lib.php b/public/main/work/work.lib.php index 0fc572cbc5..149dcb8cd9 100644 --- a/public/main/work/work.lib.php +++ b/public/main/work/work.lib.php @@ -4150,8 +4150,7 @@ function addWorkComment($courseInfo, $userId, $parentWork, CStudentPublication $ } $em = Database::getManager(); - $comment = new CStudentPublicationComment(); - $comment + $comment = (new CStudentPublicationComment()) ->setComment($data['comment']) ->setUser(api_get_user_entity($userId)) ->setPublication($studentPublication) @@ -4160,7 +4159,8 @@ function addWorkComment($courseInfo, $userId, $parentWork, CStudentPublication $ $courseEntity, api_get_session_entity(), api_get_group_entity() - ); + ) + ; $repo = Container::getStudentPublicationCommentRepository(); $repo->create($comment); diff --git a/src/CourseBundle/Repository/CShortcutRepository.php b/src/CourseBundle/Repository/CShortcutRepository.php index 88be694b70..b746e66467 100644 --- a/src/CourseBundle/Repository/CShortcutRepository.php +++ b/src/CourseBundle/Repository/CShortcutRepository.php @@ -6,7 +6,6 @@ declare(strict_types=1); namespace Chamilo\CourseBundle\Repository; -use Chamilo\CoreBundle\Entity\AbstractResource; use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Entity\ResourceInterface; use Chamilo\CoreBundle\Entity\Session; diff --git a/tests/CourseBundle/Repository/CStudentPublicationAssignmentRepositoryTest.php b/tests/CourseBundle/Repository/CStudentPublicationAssignmentRepositoryTest.php new file mode 100644 index 0000000000..659d01aafb --- /dev/null +++ b/tests/CourseBundle/Repository/CStudentPublicationAssignmentRepositoryTest.php @@ -0,0 +1,63 @@ +getEntityManager(); + $publicationRepo = self::getContainer()->get(CStudentPublicationRepository::class); + $assignmentRepo = self::getContainer()->get(CStudentPublicationAssignmentRepository::class); + + $course = $this->createCourse('new'); + $teacher = $this->createUser('teacher'); + + $publication = (new CStudentPublication()) + ->setTitle('publi') + ->setDescription('desc') + ->setParent($course) + ->setFiletype('folder') + ->setWeight(100) + ->setCreator($teacher) + ; + $em->persist($publication); + + $assignment = (new CStudentPublicationAssignment()) + ->setAddToCalendar(0) + ->setEnableQualification(true) + ->setEndsOn(new DateTime()) + ->setExpiresOn(new DateTime()) + ->setPublication($publication) + ; + $em->persist($assignment); + $em->flush(); + $em->clear(); + + /** @var CStudentPublication $publication */ + $publication = $publicationRepo->find($publication->getIid()); + + $this->assertNotNull($publication->getAssignment()); + $this->assertSame(1, $assignmentRepo->count([])); + + $em->remove($publication); + $em->flush(); + + $this->assertSame(0, $assignmentRepo->count([])); + $this->assertSame(0, $publicationRepo->count([])); + } +} diff --git a/tests/CourseBundle/Repository/CStudentPublicationCommentRepositoryTest.php b/tests/CourseBundle/Repository/CStudentPublicationCommentRepositoryTest.php new file mode 100644 index 0000000000..85be6f3fdb --- /dev/null +++ b/tests/CourseBundle/Repository/CStudentPublicationCommentRepositoryTest.php @@ -0,0 +1,64 @@ +getEntityManager(); + $publicationRepo = self::getContainer()->get(CStudentPublicationRepository::class); + $commentRepo = self::getContainer()->get(CStudentPublicationCommentRepository::class); + + $course = $this->createCourse('new'); + $teacher = $this->createUser('teacher'); + + $publication = (new CStudentPublication()) + ->setTitle('publi') + ->setDescription('desc') + ->setParent($course) + ->setFiletype('folder') + ->setWeight(100) + ->setCreator($teacher) + ; + $em->persist($publication); + + $comment = (new CStudentPublicationComment()) + ->setComment('comment') + ->setUser($teacher) + ->setPublication($publication) + ->setParent($publication) + ->setCreator($teacher) + ->addCourseLink($course) + ; + $em->persist($comment); + $em->flush(); + $em->clear(); + + /** @var CStudentPublication $publication */ + $publication = $publicationRepo->find($publication->getIid()); + + $this->assertSame(1, $publication->getComments()->count()); + $this->assertSame(1, $publicationRepo->count([])); + $this->assertSame(1, $commentRepo->count([])); + + $em->remove($publication); + $em->flush(); + + $this->assertSame(0, $publicationRepo->count([])); + $this->assertSame(0, $commentRepo->count([])); + } +} diff --git a/tests/CourseBundle/Repository/CStudentPublicationCorrectionRepositoryTest.php b/tests/CourseBundle/Repository/CStudentPublicationCorrectionRepositoryTest.php new file mode 100644 index 0000000000..55a7aefe0b --- /dev/null +++ b/tests/CourseBundle/Repository/CStudentPublicationCorrectionRepositoryTest.php @@ -0,0 +1,66 @@ +getEntityManager(); + $publicationRepo = self::getContainer()->get(CStudentPublicationRepository::class); + $correctionRepo = self::getContainer()->get(CStudentPublicationCorrectionRepository::class); + + $course = $this->createCourse('new'); + $teacher = $this->createUser('teacher'); + + $publication = (new CStudentPublication()) + ->setTitle('publi') + ->setDescription('desc') + ->setParent($course) + ->setFiletype('folder') + ->setWeight(100) + ->setCreator($teacher) + ; + $em->persist($publication); + + $file = $this->getUploadedFile(); + + $correction = (new CStudentPublicationCorrection()) + ->setParent($publication) + ->setCreator($teacher) + ->setTitle($file->getClientOriginalName()) + ; + $correctionRepo->create($correction); + $correctionRepo->addFile($correction, $file); + $correctionRepo->update($correction); + + $em->flush(); + $em->clear(); + + /** @var CStudentPublication $publication */ + $publication = $publicationRepo->find($publication->getIid()); + + $this->assertNotNull($publication->getCorrection()); + $this->assertSame(1, $publicationRepo->count([])); + $this->assertSame(1, $correctionRepo->count([])); + + $em->remove($publication); + $em->flush(); + + $this->assertSame(0, $publicationRepo->count([])); + $this->assertSame(0, $correctionRepo->count([])); + } +} diff --git a/tests/CourseBundle/Repository/CStudentPublicationRepositoryTest.php b/tests/CourseBundle/Repository/CStudentPublicationRepositoryTest.php index 9b9fd53b0e..ee2edda455 100644 --- a/tests/CourseBundle/Repository/CStudentPublicationRepositoryTest.php +++ b/tests/CourseBundle/Repository/CStudentPublicationRepositoryTest.php @@ -10,6 +10,7 @@ use Chamilo\CourseBundle\Entity\CStudentPublication; use Chamilo\CourseBundle\Repository\CStudentPublicationRepository; use Chamilo\Tests\AbstractApiTest; use Chamilo\Tests\ChamiloTestTrait; +use DateTime; class CStudentPublicationRepositoryTest extends AbstractApiTest { @@ -25,6 +26,21 @@ class CStudentPublicationRepositoryTest extends AbstractApiTest $item = (new CStudentPublication()) ->setTitle('publi') + ->setDescription('desc') + ->setAuthor('author') + ->setAccepted(false) + ->setPostGroupId(0) + ->setSentDate(new DateTime()) + ->setHasProperties(0) + ->setViewProperties(false) + ->setQualification(0) + ->setDateOfQualification(new DateTime()) + ->setQualificatorId(0) + ->setAllowTextAssignment(0) + ->setContainsFile(0) + ->setDocumentId(0) + ->setFileSize(0) + ->setAssignment(null) ->setParent($course) ->setFiletype('folder') ->setWeight(100) @@ -38,6 +54,28 @@ class CStudentPublicationRepositoryTest extends AbstractApiTest $this->assertSame(1, $repo->count([])); } + public function testCreateWithAssignment(): void + { + $em = $this->getEntityManager(); + $repo = self::getContainer()->get(CStudentPublicationRepository::class); + + $course = $this->createCourse('new'); + $teacher = $this->createUser('teacher'); + + $item = (new CStudentPublication()) + ->setTitle('publi') + ->setDescription('desc') + ->setParent($course) + ->setFiletype('folder') + ->setWeight(100) + ->setCreator($teacher) + ; + $em->persist($item); + $em->flush(); + + $this->assertSame(1, $repo->count([])); + } + public function testFindAllByCourse(): void { $em = $this->getEntityManager(); @@ -46,7 +84,7 @@ class CStudentPublicationRepositoryTest extends AbstractApiTest $course = $this->createCourse('new'); $qb = $repo->findAllByCourse($course); - $this->assertSame(0, \count($qb->getQuery()->getResult())); + $this->assertCount(0, $qb->getQuery()->getResult()); $teacher = $this->createUser('teacher'); @@ -62,7 +100,7 @@ class CStudentPublicationRepositoryTest extends AbstractApiTest $em->flush(); $qb = $repo->findAllByCourse($course); - $this->assertSame(1, \count($qb->getQuery()->getResult())); + $this->assertCount(1, $qb->getQuery()->getResult()); } public function testGetStudentAssignments(): void @@ -86,7 +124,7 @@ class CStudentPublicationRepositoryTest extends AbstractApiTest $em->flush(); $qb = $repo->getStudentAssignments($item, $course); - $this->assertSame(0, \count($qb->getQuery()->getResult())); + $this->assertCount(0, $qb->getQuery()->getResult()); $studentResult = (new CStudentPublication()) ->setTitle('work from student') @@ -102,7 +140,7 @@ class CStudentPublicationRepositoryTest extends AbstractApiTest $em->flush(); $qb = $repo->getStudentAssignments($item, $course); - $this->assertSame(1, \count($qb->getQuery()->getResult())); + $this->assertCount(1, $qb->getQuery()->getResult()); //$this->assertSame(1, $repo->countUserPublications($student, $course)); //$this->assertSame(1, $repo->findWorksByTeacher($teacher, $course)); diff --git a/tests/CourseBundle/Repository/CSurveyRepositoryTest.php b/tests/CourseBundle/Repository/CSurveyRepositoryTest.php index 124dea0181..7be9add67d 100644 --- a/tests/CourseBundle/Repository/CSurveyRepositoryTest.php +++ b/tests/CourseBundle/Repository/CSurveyRepositoryTest.php @@ -10,6 +10,7 @@ use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Repository\Node\CourseRepository; use Chamilo\CourseBundle\Entity\CSurvey; use Chamilo\CourseBundle\Entity\CSurveyAnswer; +use Chamilo\CourseBundle\Entity\CSurveyInvitation; use Chamilo\CourseBundle\Entity\CSurveyQuestion; use Chamilo\CourseBundle\Entity\CSurveyQuestionOption; use Chamilo\CourseBundle\Repository\CSurveyAnswerRepository; @@ -17,6 +18,7 @@ use Chamilo\CourseBundle\Repository\CSurveyQuestionRepository; use Chamilo\CourseBundle\Repository\CSurveyRepository; use Chamilo\Tests\AbstractApiTest; use Chamilo\Tests\ChamiloTestTrait; +use DateTime; class CSurveyRepositoryTest extends AbstractApiTest { @@ -33,6 +35,21 @@ class CSurveyRepositoryTest extends AbstractApiTest $survey = (new CSurvey()) ->setTitle('survey') ->setCode('survey') + ->setSubtitle('subtitle') + ->setSurveythanks('thanks') + ->setIsMandatory(false) + ->setSurveyType(1) + ->setSurveyVersion('v1') + ->setAccessCondition('condition') + ->setShuffle(false) + ->setTemplate('tpl') + ->setAnonymous('0') + ->setVisibleResults(1) + ->setReminderMail('reminder') + ->setRgt(1) + ->setMailSubject('subject') + ->setInviteMail('invite') + ->setLang('lang') ->setParent($course) ->setCreator($teacher) ; @@ -53,9 +70,12 @@ class CSurveyRepositoryTest extends AbstractApiTest $surveyRepo = self::getContainer()->get(CSurveyRepository::class); $surveyQuestionRepo = self::getContainer()->get(CSurveyQuestionRepository::class); $surveyAnswerRepo = self::getContainer()->get(CSurveyAnswerRepository::class); + $surveyInvitationRepo = $em->getRepository(CSurveyInvitation::class); + $surveyOptionRepo = $em->getRepository(CSurveyQuestionOption::class); $course = $this->createCourse('new'); $teacher = $this->createUser('teacher'); + $student = $this->createUser('student'); $survey = (new CSurvey()) ->setTitle('survey') @@ -82,17 +102,20 @@ class CSurveyRepositoryTest extends AbstractApiTest ; $this->assertHasNoEntityViolations($question); $em->persist($question); - $em->flush(); - $questionOption = (new CSurveyQuestionOption()) + $option = (new CSurveyQuestionOption()) + ->setSurvey($survey) ->setQuestion($question) + ->setOptionText('text') ->setValue(1) - ->setSurvey($survey) ->setSort(1) - ->setOptionText('option text') ; - $this->assertHasNoEntityViolations($questionOption); - $em->persist($questionOption); + $em->persist($option); + $this->assertHasNoEntityViolations($option); + $em->flush(); + + $this->assertSame('hola?', $question->getSurveyQuestion()); + $em->flush(); $answer = (new CSurveyAnswer()) @@ -104,8 +127,21 @@ class CSurveyRepositoryTest extends AbstractApiTest ; $this->assertHasNoEntityViolations($answer); $em->persist($answer); - $em->flush(); + $invitation = (new CSurveyInvitation()) + ->setCourse($course) + ->setUser($student) + ->setGroup(null) + ->setSession(null) + ->setSurvey($survey) + ->setInvitationCode('code') + ->setInvitationDate(new DateTime()) + ->setAnswered(0) + ->setReminderDate(new DateTime()) + ; + $em->persist($invitation); + + $em->flush(); $em->clear(); /** @var CSurvey $survey */ @@ -113,6 +149,7 @@ class CSurveyRepositoryTest extends AbstractApiTest /** @var CSurveyQuestion $question */ $question = $surveyQuestionRepo->find($question->getIid()); + $this->assertSame(1, $survey->getInvitations()->count()); $this->assertSame(1, $survey->getQuestions()->count()); $this->assertSame(1, $question->getOptions()->count()); $this->assertSame(1, $question->getAnswers()->count()); @@ -120,7 +157,8 @@ class CSurveyRepositoryTest extends AbstractApiTest $this->assertSame(1, $surveyRepo->count([])); $this->assertSame(1, $surveyQuestionRepo->count([])); $this->assertSame(1, $surveyAnswerRepo->count([])); - + $this->assertSame(1, $surveyInvitationRepo->count([])); + $this->assertSame(1, $surveyOptionRepo->count([])); $this->assertSame(1, $courseRepo->count([])); /** @var Course $course */ @@ -131,5 +169,7 @@ class CSurveyRepositoryTest extends AbstractApiTest $this->assertSame(0, $surveyRepo->count([])); $this->assertSame(0, $surveyQuestionRepo->count([])); $this->assertSame(0, $surveyAnswerRepo->count([])); + $this->assertSame(0, $surveyInvitationRepo->count([])); + $this->assertSame(0, $surveyOptionRepo->count([])); } }