diff --git a/src/CoreBundle/Entity/GradebookCategory.php b/src/CoreBundle/Entity/GradebookCategory.php index c3f46b05ad..bc2c8295d5 100644 --- a/src/CoreBundle/Entity/GradebookCategory.php +++ b/src/CoreBundle/Entity/GradebookCategory.php @@ -56,7 +56,7 @@ class GradebookCategory /** * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\GradebookCategory", inversedBy="subCategories") - * @ORM\JoinColumn(name="parent_id", referencedColumnName="id") + * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE") */ protected ?GradebookCategory $parent = null; diff --git a/src/CoreBundle/Entity/GradebookEvaluation.php b/src/CoreBundle/Entity/GradebookEvaluation.php index 0da2d5da3d..c400b39834 100644 --- a/src/CoreBundle/Entity/GradebookEvaluation.php +++ b/src/CoreBundle/Entity/GradebookEvaluation.php @@ -51,7 +51,7 @@ class GradebookEvaluation /** * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", inversedBy="gradebookEvaluations") - * @ORM\JoinColumn(name="c_id", referencedColumnName="id") + * @ORM\JoinColumn(name="c_id", referencedColumnName="id", onDelete="CASCADE") */ protected Course $course; diff --git a/src/CoreBundle/Entity/GradebookLink.php b/src/CoreBundle/Entity/GradebookLink.php index 32684badc1..2beb16b83d 100644 --- a/src/CoreBundle/Entity/GradebookLink.php +++ b/src/CoreBundle/Entity/GradebookLink.php @@ -53,7 +53,7 @@ class GradebookLink /** * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", inversedBy="gradebookLinks") - * @ORM\JoinColumn(name="c_id", referencedColumnName="id") + * @ORM\JoinColumn(name="c_id", referencedColumnName="id", onDelete="CASCADE") */ protected Course $course; diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20181025064351.php b/src/CoreBundle/Migrations/Schema/V200/Version20181025064351.php index 787474a35f..fa88273246 100644 --- a/src/CoreBundle/Migrations/Schema/V200/Version20181025064351.php +++ b/src/CoreBundle/Migrations/Schema/V200/Version20181025064351.php @@ -74,7 +74,7 @@ class Version20181025064351 extends AbstractMigrationChamilo $this->addSql('DELETE FROM gradebook_category WHERE session_id IS NOT NULL AND session_id NOT IN (SELECT id FROM session)'); if (false === $table->hasForeignKey('FK_96A4C705727ACA70')) { - $this->addSql('ALTER TABLE gradebook_category ADD CONSTRAINT FK_96A4C705727ACA70 FOREIGN KEY (parent_id) REFERENCES gradebook_category (id);'); + $this->addSql('ALTER TABLE gradebook_category ADD CONSTRAINT FK_96A4C705727ACA70 FOREIGN KEY (parent_id) REFERENCES gradebook_category (id) ON DELETE CASCADE'); } if (false === $table->hasForeignKey('FK_96A4C705613FECDF')) { @@ -139,12 +139,12 @@ class Version20181025064351 extends AbstractMigrationChamilo // Evaluation. $table = $schema->getTable('gradebook_evaluation'); - if (false === $table->hasColumn('c_id')) { + if (!$table->hasColumn('c_id')) { $this->addSql('ALTER TABLE gradebook_evaluation ADD c_id INT DEFAULT NULL'); $this->addSql('UPDATE gradebook_evaluation SET c_id = (SELECT id FROM course WHERE code = course_code)'); $this->addSql('ALTER TABLE gradebook_evaluation DROP course_code'); $this->addSql( - 'ALTER TABLE gradebook_evaluation ADD CONSTRAINT FK_DDDED80491D79BD3 FOREIGN KEY (c_id) REFERENCES course (id);' + 'ALTER TABLE gradebook_evaluation ADD CONSTRAINT FK_DDDED80491D79BD3 FOREIGN KEY (c_id) REFERENCES course (id) ON DELETE CASCADE' ); $this->addSql('CREATE INDEX IDX_DDDED80491D79BD3 ON gradebook_evaluation (c_id)'); //$this->addSql('ALTER TABLE gradebook_evaluation RENAME INDEX fk_ddded80491d79bd3 TO IDX_DDDED80491D79BD3;'); @@ -155,7 +155,7 @@ class Version20181025064351 extends AbstractMigrationChamilo $this->addSql('DELETE FROM gradebook_evaluation WHERE category_id NOT IN (SELECT id FROM gradebook_category) '); - if (false === $table->hasForeignKey('FK_DDDED80412469DE2')) { + if (!$table->hasForeignKey('FK_DDDED80412469DE2')) { $this->addSql('ALTER TABLE gradebook_evaluation ADD CONSTRAINT FK_DDDED80412469DE2 FOREIGN KEY (category_id) REFERENCES gradebook_category (id) ON DELETE CASCADE'); } @@ -191,7 +191,7 @@ class Version20181025064351 extends AbstractMigrationChamilo $this->addSql('UPDATE gradebook_link SET c_id = (SELECT id FROM course WHERE code = course_code)'); $this->addSql('ALTER TABLE gradebook_link DROP course_code'); $this->addSql( - 'ALTER TABLE gradebook_link ADD CONSTRAINT FK_4F0F595F91D79BD3 FOREIGN KEY (c_id) REFERENCES course (id);' + 'ALTER TABLE gradebook_link ADD CONSTRAINT FK_4F0F595F91D79BD3 FOREIGN KEY (c_id) REFERENCES course (id) ON DELETE CASCADE' ); $this->addSql('CREATE INDEX IDX_4F0F595F91D79BD3 ON gradebook_link (c_id);'); } diff --git a/tests/CoreBundle/Repository/GradeBookCategoryRepositoryTest.php b/tests/CoreBundle/Repository/GradeBookCategoryRepositoryTest.php index 1a19e6be7a..899e599d65 100644 --- a/tests/CoreBundle/Repository/GradeBookCategoryRepositoryTest.php +++ b/tests/CoreBundle/Repository/GradeBookCategoryRepositoryTest.php @@ -14,6 +14,7 @@ use Chamilo\CoreBundle\Entity\GradebookLink; use Chamilo\CoreBundle\Entity\GradebookResult; use Chamilo\CoreBundle\Entity\GradebookResultAttempt; use Chamilo\CoreBundle\Repository\GradeBookCategoryRepository; +use Chamilo\CoreBundle\Repository\Node\CourseRepository; use Chamilo\Tests\AbstractApiTest; use Chamilo\Tests\ChamiloTestTrait; @@ -23,9 +24,64 @@ class GradeBookCategoryRepositoryTest extends AbstractApiTest public function testCreate(): void { - self::bootKernel(); + $em = $this->getEntityManager(); + $courseRepo = self::getContainer()->get(CourseRepository::class); + $repo = self::getContainer()->get(GradeBookCategoryRepository::class); + + $course = $this->createCourse('new'); + + $category = (new GradebookCategory()) + ->setName('cat1') + ->setCourse($course) + ->setWeight(100.00) + ->setVisible(true) + ->setGenerateCertificates(true) + ; + $this->assertHasNoEntityViolations($category); + $evaluation = (new GradebookEvaluation()) + ->setName('eva') + ->setCategory($category) + ->setCourse($course) + ->setWeight(100.00) + ->setVisible(1) + ->setWeight(50.00) + ->setType('evaluation') + ->setMax(100.00) + ; + $this->assertHasNoEntityViolations($evaluation); + + $link = (new GradebookLink()) + ->setRefId(1) + ->setCategory($category) + ->setCourse($course) + ->setWeight(100.00) + ->setVisible(1) + ->setWeight(50.00) + ->setType(1) + ; + $this->assertHasNoEntityViolations($link); + + $category->getLinks()->add($link); + $category->getEvaluations()->add($evaluation); + + $em->persist($evaluation); + $em->persist($category); + $em->flush(); + + $this->assertSame(1, $courseRepo->count([])); + + $em->remove($course); + $em->flush(); + + $this->assertSame(0, $courseRepo->count([])); + $this->assertSame(0, $repo->count([])); + } + + public function testCreateWithEvaluationAndLinks(): void + { $em = $this->getEntityManager(); + $courseRepo = self::getContainer()->get(CourseRepository::class); $repo = self::getContainer()->get(GradeBookCategoryRepository::class); $course = $this->createCourse('new'); @@ -107,6 +163,8 @@ class GradeBookCategoryRepositoryTest extends AbstractApiTest $em->persist($resultAttempt); $em->flush(); + $this->assertSame(1, $courseRepo->count([])); + $em->remove($category); $em->flush();