Gradebook: Fix delete + add test

pull/4032/head
Julio 4 years ago
parent d747b57ba8
commit 2310c78d35
  1. 2
      src/CoreBundle/Entity/GradebookCategory.php
  2. 2
      src/CoreBundle/Entity/GradebookEvaluation.php
  3. 2
      src/CoreBundle/Entity/GradebookLink.php
  4. 10
      src/CoreBundle/Migrations/Schema/V200/Version20181025064351.php
  5. 60
      tests/CoreBundle/Repository/GradeBookCategoryRepositoryTest.php

@ -56,7 +56,7 @@ class GradebookCategory
/** /**
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\GradebookCategory", inversedBy="subCategories") * @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; protected ?GradebookCategory $parent = null;

@ -51,7 +51,7 @@ class GradebookEvaluation
/** /**
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", inversedBy="gradebookEvaluations") * @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; protected Course $course;

@ -53,7 +53,7 @@ class GradebookLink
/** /**
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", inversedBy="gradebookLinks") * @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; protected Course $course;

@ -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)'); $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')) { 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')) { if (false === $table->hasForeignKey('FK_96A4C705613FECDF')) {
@ -139,12 +139,12 @@ class Version20181025064351 extends AbstractMigrationChamilo
// Evaluation. // Evaluation.
$table = $schema->getTable('gradebook_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('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('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 DROP course_code');
$this->addSql( $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('CREATE INDEX IDX_DDDED80491D79BD3 ON gradebook_evaluation (c_id)');
//$this->addSql('ALTER TABLE gradebook_evaluation RENAME INDEX fk_ddded80491d79bd3 TO IDX_DDDED80491D79BD3;'); //$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) '); $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'); $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('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 DROP course_code');
$this->addSql( $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);'); $this->addSql('CREATE INDEX IDX_4F0F595F91D79BD3 ON gradebook_link (c_id);');
} }

@ -14,6 +14,7 @@ use Chamilo\CoreBundle\Entity\GradebookLink;
use Chamilo\CoreBundle\Entity\GradebookResult; use Chamilo\CoreBundle\Entity\GradebookResult;
use Chamilo\CoreBundle\Entity\GradebookResultAttempt; use Chamilo\CoreBundle\Entity\GradebookResultAttempt;
use Chamilo\CoreBundle\Repository\GradeBookCategoryRepository; use Chamilo\CoreBundle\Repository\GradeBookCategoryRepository;
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
use Chamilo\Tests\AbstractApiTest; use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait; use Chamilo\Tests\ChamiloTestTrait;
@ -23,9 +24,64 @@ class GradeBookCategoryRepositoryTest extends AbstractApiTest
public function testCreate(): void 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(); $em = $this->getEntityManager();
$courseRepo = self::getContainer()->get(CourseRepository::class);
$repo = self::getContainer()->get(GradeBookCategoryRepository::class); $repo = self::getContainer()->get(GradeBookCategoryRepository::class);
$course = $this->createCourse('new'); $course = $this->createCourse('new');
@ -107,6 +163,8 @@ class GradeBookCategoryRepositoryTest extends AbstractApiTest
$em->persist($resultAttempt); $em->persist($resultAttempt);
$em->flush(); $em->flush();
$this->assertSame(1, $courseRepo->count([]));
$em->remove($category); $em->remove($category);
$em->flush(); $em->flush();

Loading…
Cancel
Save