Surveys: Fix delete course + add tests

pull/4032/head
Julio 5 years ago
parent 9d17f7c651
commit c41b352486
  1. 2
      src/CoreBundle/Migrations/Schema/V200/Version20180319145700.php
  2. 2
      src/CourseBundle/Entity/CSurveyAnswer.php
  3. 19
      src/CourseBundle/Repository/CSurveyAnswerRepository.php
  4. 4
      tests/CoreBundle/Repository/TrackExerciseRepositoryTest.php
  5. 107
      tests/CourseBundle/Repository/CSurveyRepositoryTest.php

@ -78,7 +78,7 @@ class Version20180319145700 extends AbstractMigrationChamilo
}
if (!$table->hasForeignKey('FK_8A897DD1E27F6BF')) {
$this->addSql('ALTER TABLE c_survey_answer ADD CONSTRAINT FK_8A897DD1E27F6BF FOREIGN KEY (question_id) REFERENCES c_survey_question (iid);');
$this->addSql('ALTER TABLE c_survey_answer ADD CONSTRAINT FK_8A897DD1E27F6BF FOREIGN KEY (question_id) REFERENCES c_survey_question (iid) ON DELETE CASCADE');
}
/*if (!$table->hasForeignKey('FK_8A897DDA7C41D6F')) {

@ -27,7 +27,7 @@ class CSurveyAnswer
/**
* @ORM\ManyToOne(targetEntity="CSurvey")
* @ORM\JoinColumn(name="survey_id", referencedColumnName="iid")
* @ORM\JoinColumn(name="survey_id", referencedColumnName="iid", onDelete="CASCADE")
*/
protected CSurvey $survey;

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CourseBundle\Repository;
use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CourseBundle\Entity\CSurveyAnswer;
use Doctrine\Persistence\ManagerRegistry;
final class CSurveyAnswerRepository extends ResourceRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, CSurveyAnswer::class);
}
}

@ -26,7 +26,6 @@ class TrackExerciseRepositoryTest extends AbstractApiTest
public function testCreate(): void
{
self::bootKernel();
$em = $this->getEntityManager();
$course = $this->createCourse('new');
@ -91,7 +90,6 @@ class TrackExerciseRepositoryTest extends AbstractApiTest
public function testCreateInSession(): void
{
self::bootKernel();
$em = $this->getEntityManager();
$course = $this->createCourse('new');
@ -160,8 +158,6 @@ class TrackExerciseRepositoryTest extends AbstractApiTest
public function testCreateWithAttempt(): void
{
self::bootKernel();
$em = $this->getEntityManager();
$courseRepo = self::getContainer()->get(CourseRepository::class);

@ -6,7 +6,14 @@ declare(strict_types=1);
namespace Chamilo\Tests\CourseBundle\Repository;
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\CSurveyQuestion;
use Chamilo\CourseBundle\Entity\CSurveyQuestionOption;
use Chamilo\CourseBundle\Repository\CSurveyAnswerRepository;
use Chamilo\CourseBundle\Repository\CSurveyQuestionRepository;
use Chamilo\CourseBundle\Repository\CSurveyRepository;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
@ -17,26 +24,112 @@ class CSurveyRepositoryTest extends AbstractApiTest
public function testCreate(): void
{
self::bootKernel();
$em = $this->getEntityManager();
$surveyRepo = self::getContainer()->get(CSurveyRepository::class);
$course = $this->createCourse('new');
$teacher = $this->createUser('teacher');
$survey = (new CSurvey())
->setTitle('survey')
->setCode('survey')
->setParent($course)
->setCreator($teacher)
;
$this->assertHasNoEntityViolations($survey);
$em->persist($survey);
$em->flush();
$this->assertSame('survey', (string) $survey);
$this->assertSame(1, $surveyRepo->count([]));
}
public function testCreateWithQuestions(): void
{
$em = $this->getEntityManager();
$repo = self::getContainer()->get(CSurveyRepository::class);
$courseRepo = self::getContainer()->get(CourseRepository::class);
$surveyRepo = self::getContainer()->get(CSurveyRepository::class);
$surveyQuestionRepo = self::getContainer()->get(CSurveyQuestionRepository::class);
$surveyAnswerRepo = self::getContainer()->get(CSurveyAnswerRepository::class);
$course = $this->createCourse('new');
$teacher = $this->createUser('teacher');
$item = (new CSurvey())
$survey = (new CSurvey())
->setTitle('survey')
->setCode('survey')
->setParent($course)
->setCreator($teacher)
;
$em->persist($survey);
$em->flush();
$question = (new CSurveyQuestion())
->setSurvey($survey)
->setSurveyQuestion('hola?')
->setIsMandatory(false)
->setDisplay('display')
->setSort(0)
->setSharedQuestionId(0)
->setSurveyQuestionComment('comment')
->setMaxValue(100)
->setSurveyGroupPri(0)
->setSurveyGroupSec1(0)
->setSurveyGroupSec2(0)
->setType('type')
;
$this->assertHasNoEntityViolations($question);
$em->persist($question);
$em->flush();
$this->assertHasNoEntityViolations($item);
$em->persist($item);
$questionOption = (new CSurveyQuestionOption())
->setQuestion($question)
->setValue(1)
->setSurvey($survey)
->setSort(1)
->setOptionText('option text')
;
$this->assertHasNoEntityViolations($questionOption);
$em->persist($questionOption);
$em->flush();
$this->assertSame('survey', (string) $item);
$this->assertSame(1, $repo->count([]));
$answer = (new CSurveyAnswer())
->setSurvey($survey)
->setUser('1')
->setValue(1)
->setOptionId('1')
->setQuestion($question)
;
$this->assertHasNoEntityViolations($answer);
$em->persist($answer);
$em->flush();
$em->clear();
/** @var CSurvey $survey */
$survey = $surveyRepo->find($survey->getIid());
/** @var CSurveyQuestion $question */
$question = $surveyQuestionRepo->find($question->getIid());
$this->assertSame(1, $survey->getQuestions()->count());
$this->assertSame(1, $question->getOptions()->count());
$this->assertSame(1, $question->getAnswers()->count());
$this->assertSame(1, $surveyRepo->count([]));
$this->assertSame(1, $surveyQuestionRepo->count([]));
$this->assertSame(1, $surveyAnswerRepo->count([]));
$this->assertSame(1, $courseRepo->count([]));
/** @var Course $course */
$course = $courseRepo->find($course->getId());
$courseRepo->delete($course);
$this->assertSame(0, $courseRepo->count([]));
$this->assertSame(0, $surveyRepo->count([]));
$this->assertSame(0, $surveyQuestionRepo->count([]));
$this->assertSame(0, $surveyAnswerRepo->count([]));
}
}

Loading…
Cancel
Save