diff --git a/src/CoreBundle/Entity/Ticket.php b/src/CoreBundle/Entity/Ticket.php index 3e8881767f..dc508bcb23 100644 --- a/src/CoreBundle/Entity/Ticket.php +++ b/src/CoreBundle/Entity/Ticket.php @@ -10,8 +10,6 @@ use DateTime; use Doctrine\ORM\Mapping as ORM; /** - * Ticket. - * * @ORM\Table(name="ticket_ticket") * @ORM\Entity */ @@ -59,13 +57,13 @@ class Ticket /** * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course") - * @ORM\JoinColumn(name="course_id", referencedColumnName="id") + * @ORM\JoinColumn(name="course_id", referencedColumnName="id", onDelete="CASCADE") */ protected Course $course; /** * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session") - * @ORM\JoinColumn(name="session_id", referencedColumnName="id") + * @ORM\JoinColumn(name="session_id", referencedColumnName="id", onDelete="CASCADE") */ protected Session $session; @@ -79,7 +77,7 @@ class Ticket */ /** * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User") - * @ORM\JoinColumn(name="assigned_last_user", referencedColumnName="id") + * @ORM\JoinColumn(name="assigned_last_user", referencedColumnName="id", onDelete="CASCADE") */ protected ?User $assignedLastUser = null; diff --git a/src/CoreBundle/Entity/TicketAssignedLog.php b/src/CoreBundle/Entity/TicketAssignedLog.php index c5aba0447a..c6ac8797e6 100644 --- a/src/CoreBundle/Entity/TicketAssignedLog.php +++ b/src/CoreBundle/Entity/TicketAssignedLog.php @@ -10,8 +10,6 @@ use DateTime; use Doctrine\ORM\Mapping as ORM; /** - * Class AssignedLog. - * * @ORM\Table( * name="ticket_assigned_log", * ) @@ -28,7 +26,7 @@ class TicketAssignedLog /** * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Ticket") - * @ORM\JoinColumn(name="ticket_id", referencedColumnName="id") + * @ORM\JoinColumn(name="ticket_id", referencedColumnName="id", onDelete="CASCADE") */ protected Ticket $ticket; diff --git a/src/CoreBundle/Entity/TicketMessage.php b/src/CoreBundle/Entity/TicketMessage.php index 49501b2e47..de45114654 100644 --- a/src/CoreBundle/Entity/TicketMessage.php +++ b/src/CoreBundle/Entity/TicketMessage.php @@ -11,8 +11,6 @@ use Doctrine\ORM\Mapping as ORM; use Gedmo\Mapping\Annotation as Gedmo; /** - * Ticket messages. - * * @ORM\Table(name="ticket_message") * @ORM\Entity */ @@ -47,7 +45,7 @@ class TicketMessage /** * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Ticket") - * @ORM\JoinColumn(name="ticket_id", referencedColumnName="id") + * @ORM\JoinColumn(name="ticket_id", referencedColumnName="id", onDelete="CASCADE") */ protected Ticket $ticket; diff --git a/src/CoreBundle/Entity/TicketMessageAttachment.php b/src/CoreBundle/Entity/TicketMessageAttachment.php index fe7641b2b0..c725cdd390 100644 --- a/src/CoreBundle/Entity/TicketMessageAttachment.php +++ b/src/CoreBundle/Entity/TicketMessageAttachment.php @@ -24,13 +24,13 @@ class TicketMessageAttachment extends AbstractResource implements ResourceInterf /** * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Ticket") - * @ORM\JoinColumn(name="ticket_id", referencedColumnName="id") + * @ORM\JoinColumn(name="ticket_id", referencedColumnName="id", onDelete="CASCADE") */ protected Ticket $ticket; /** * @ORM\ManyToOne(targetEntity="TicketMessage") - * @ORM\JoinColumn(name="message_id", referencedColumnName="id") + * @ORM\JoinColumn(name="message_id", referencedColumnName="id", onDelete="CASCADE") */ protected TicketMessage $message; diff --git a/src/CoreBundle/Entity/TrackEAttempt.php b/src/CoreBundle/Entity/TrackEAttempt.php index c746a20b18..d7ac44f405 100644 --- a/src/CoreBundle/Entity/TrackEAttempt.php +++ b/src/CoreBundle/Entity/TrackEAttempt.php @@ -40,7 +40,7 @@ class TrackEAttempt /** * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\TrackExercise", inversedBy="attempts") - * @ORM\JoinColumn(name="exe_id", referencedColumnName="exe_id", nullable=false) + * @ORM\JoinColumn(name="exe_id", referencedColumnName="exe_id", nullable=false, onDelete="CASCADE") */ #[Assert\NotNull] protected TrackExercise $trackExercise; diff --git a/src/CoreBundle/Entity/TrackExercise.php b/src/CoreBundle/Entity/TrackExercise.php index c92b1fd2b5..96dcd6c37b 100644 --- a/src/CoreBundle/Entity/TrackExercise.php +++ b/src/CoreBundle/Entity/TrackExercise.php @@ -34,21 +34,21 @@ class TrackExercise /** * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User") - * @ORM\JoinColumn(name="exe_user_id", referencedColumnName="id", nullable=false) + * @ORM\JoinColumn(name="exe_user_id", referencedColumnName="id", nullable=false, onDelete="CASCADE") */ #[Assert\NotNull] protected User $user; /** * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course") - * @ORM\JoinColumn(name="c_id", referencedColumnName="id", nullable=false) + * @ORM\JoinColumn(name="c_id", referencedColumnName="id", nullable=false, onDelete="CASCADE") */ #[Assert\NotNull] protected Course $course; /** * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session") - * @ORM\JoinColumn(name="session_id", referencedColumnName="id") + * @ORM\JoinColumn(name="session_id", referencedColumnName="id", onDelete="CASCADE") */ protected ?Session $session = null; diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20.php b/src/CoreBundle/Migrations/Schema/V200/Version20.php index 91d5d6c828..8e60418588 100644 --- a/src/CoreBundle/Migrations/Schema/V200/Version20.php +++ b/src/CoreBundle/Migrations/Schema/V200/Version20.php @@ -388,24 +388,41 @@ class Version20 extends AbstractMigrationChamilo 'columns' => ['hotspot_course_code'], 'index' => [], ], + 'c_item_property' => [ + 'fks' => [ + 'FK_1D84C18129F6EE60', + 'FK_1D84C181330D47E9', + 'FK_1D84C181613FECDF', + 'FK_1D84C18191D79BD3', + 'FK_1D84C1819C859CC3', + ], + ], ]; foreach ($dropColumnsAndIndex as $tableName => $data) { if ($schema->hasTable($tableName)) { - $indexList = $data['index']; + $table = $schema->getTable($tableName); + + $indexList = $data['index'] ?? []; foreach ($indexList as $index) { if ($table->hasIndex($index)) { $table->dropIndex($index); } } - $columns = $data['columns']; - $table = $schema->getTable($tableName); + $columns = $data['columns'] ?? []; foreach ($columns as $column) { if ($table->hasColumn($column)) { $table->dropColumn($column); } } + + $fks = $data['fks'] ?? []; + foreach ($fks as $fk) { + if ($table->hasForeignKey($fk)) { + $table->removeForeignKey($fk); + } + } } } diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20180904175500.php b/src/CoreBundle/Migrations/Schema/V200/Version20180904175500.php index 146b47464b..d0cc376457 100644 --- a/src/CoreBundle/Migrations/Schema/V200/Version20180904175500.php +++ b/src/CoreBundle/Migrations/Schema/V200/Version20180904175500.php @@ -99,7 +99,6 @@ class Version20180904175500 extends AbstractMigrationChamilo $table = $schema->getTable('track_e_exercises'); $this->addSql('ALTER TABLE track_e_exercises CHANGE session_id session_id INT DEFAULT NULL'); - $this->addSql('ALTER TABLE track_e_attempt CHANGE session_id session_id INT DEFAULT NULL'); $this->addSql('UPDATE track_e_attempt SET session_id = NULL WHERE session_id = 0'); $this->addSql('DELETE FROM track_e_attempt WHERE session_id IS NOT NULL AND session_id NOT IN (select id FROM session)'); @@ -111,15 +110,15 @@ class Version20180904175500 extends AbstractMigrationChamilo $this->addSql('DELETE FROM track_e_exercises WHERE exe_user_id IS NOT NULL AND exe_user_id NOT IN (select id FROM user)'); if (!$table->hasForeignKey('FK_AA0DA082613FECDF')) { - $this->addSql('ALTER TABLE track_e_exercises ADD CONSTRAINT FK_AA0DA082613FECDF FOREIGN KEY (session_id) REFERENCES session (id)'); + $this->addSql('ALTER TABLE track_e_exercises ADD CONSTRAINT FK_AA0DA082613FECDF FOREIGN KEY (session_id) REFERENCES session (id) ON DELETE CASCADE'); } if (!$table->hasForeignKey('FK_AA0DA082613FECDF')) { - $this->addSql('ALTER TABLE track_e_exercises ADD CONSTRAINT FK_AA0DA08291D79BD3 FOREIGN KEY (c_id) REFERENCES course (id) '); + $this->addSql('ALTER TABLE track_e_exercises ADD CONSTRAINT FK_AA0DA08291D79BD3 FOREIGN KEY (c_id) REFERENCES course (id) ON DELETE CASCADE'); } if (!$table->hasForeignKey('FK_AA0DA082F6A6790')) { - $this->addSql('ALTER TABLE track_e_exercises ADD CONSTRAINT FK_AA0DA082F6A6790 FOREIGN KEY (exe_user_id) REFERENCES user (id) '); + $this->addSql('ALTER TABLE track_e_exercises ADD CONSTRAINT FK_AA0DA082F6A6790 FOREIGN KEY (exe_user_id) REFERENCES user (id) ON DELETE CASCADE '); } if ($table->hasColumn('exe_weighting')) { @@ -167,7 +166,7 @@ class Version20180904175500 extends AbstractMigrationChamilo if (!$table->hasForeignKey('FK_F8C342C3B5A18F57')) { $this->addSql( - 'ALTER TABLE track_e_attempt ADD CONSTRAINT FK_F8C342C3B5A18F57 FOREIGN KEY (exe_id) REFERENCES track_e_exercises (exe_id)' + 'ALTER TABLE track_e_attempt ADD CONSTRAINT FK_F8C342C3B5A18F57 FOREIGN KEY (exe_id) REFERENCES track_e_exercises (exe_id) ON DELETE CASCADE' ); } diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20211005153900.php b/src/CoreBundle/Migrations/Schema/V200/Version20211005153900.php index 5e691da015..b26136ba0a 100644 --- a/src/CoreBundle/Migrations/Schema/V200/Version20211005153900.php +++ b/src/CoreBundle/Migrations/Schema/V200/Version20211005153900.php @@ -22,20 +22,52 @@ class Version20211005153900 extends AbstractMigrationChamilo 'UPDATE ticket_ticket SET assigned_last_user = NULL WHERE assigned_last_user NOT IN (SELECT id FROM user)' ); - if (false === $table->hasForeignKey('FK_EDE2C7686219A7B7')) { + $this->addSql('UPDATE ticket_ticket SET session_id = NULL WHERE session_id = 0'); + $this->addSql('UPDATE ticket_ticket SET course_id = NULL WHERE course_id = 0'); + + $this->addSql('DELETE FROM ticket_ticket WHERE session_id IS NOT NULL AND session_id NOT IN (SELECT id FROM session)'); + $this->addSql('DELETE FROM ticket_ticket WHERE course_id IS NOT NULL AND course_id NOT IN (SELECT id FROM course)'); + + if (!$table->hasForeignKey('FK_EDE2C7686219A7B7')) { $this->addSql( - 'ALTER TABLE ticket_ticket ADD CONSTRAINT FK_EDE2C7686219A7B7 FOREIGN KEY (assigned_last_user) REFERENCES user (id)' + 'ALTER TABLE ticket_ticket ADD CONSTRAINT FK_EDE2C7686219A7B7 FOREIGN KEY (assigned_last_user) REFERENCES user (id) ON DELETE CASCADE' ); } + if (!$table->hasForeignKey('FK_EDE2C768591CC992')) { + $this->addSql('ALTER TABLE ticket_ticket ADD CONSTRAINT FK_EDE2C768591CC992 FOREIGN KEY (course_id) REFERENCES course (id) ON DELETE CASCADE;'); + } + + if (!$table->hasForeignKey('FK_EDE2C768613FECDF')) { + $this->addSql('ALTER TABLE ticket_ticket ADD CONSTRAINT FK_EDE2C768613FECDF FOREIGN KEY (session_id) REFERENCES session (id) ON DELETE CASCADE;'); + } + if (false === $table->hasIndex('IDX_EDE2C7686219A7B7')) { $this->addSql('CREATE INDEX IDX_EDE2C7686219A7B7 ON ticket_ticket (assigned_last_user)'); } + $table = $schema->getTable('ticket_assigned_log'); + if (!$table->hasForeignKey('FK_54B65868700047D2')) { + $this->addSql('ALTER TABLE ticket_assigned_log ADD CONSTRAINT FK_54B65868700047D2 FOREIGN KEY (ticket_id) REFERENCES ticket_ticket (id) ON DELETE CASCADE;'); + } + + $table = $schema->getTable('ticket_message'); + if (!$table->hasForeignKey('FK_BA71692D700047D2')) { + $this->addSql('ALTER TABLE ticket_message ADD CONSTRAINT FK_BA71692D700047D2 FOREIGN KEY (ticket_id) REFERENCES ticket_ticket (id) ON DELETE CASCADE;'); + } + // Attachments $table = $schema->getTable('ticket_message_attachments'); - if (false === $table->hasColumn('resource_node_id')) { + if (!$table->hasForeignKey('FK_70BF9E26537A1329')) { + $this->addSql('ALTER TABLE ticket_message_attachments ADD CONSTRAINT FK_70BF9E26537A1329 FOREIGN KEY (message_id) REFERENCES ticket_message (id) ON DELETE CASCADE;'); + } + + if (!$table->hasForeignKey('FK_70BF9E26700047D2')) { + $this->addSql('ALTER TABLE ticket_message_attachments ADD CONSTRAINT FK_70BF9E26700047D2 FOREIGN KEY (ticket_id) REFERENCES ticket_ticket (id) ON DELETE CASCADE;'); + } + + if (!$table->hasColumn('resource_node_id')) { $this->addSql('ALTER TABLE ticket_message_attachments ADD resource_node_id BIGINT DEFAULT NULL;'); $this->addSql( 'ALTER TABLE ticket_message_attachments ADD CONSTRAINT FK_70BF9E261BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE;' diff --git a/tests/CoreBundle/Repository/TrackExerciseRepositoryTest.php b/tests/CoreBundle/Repository/TrackExerciseRepositoryTest.php index 7d6381cf1c..f79323cc16 100644 --- a/tests/CoreBundle/Repository/TrackExerciseRepositoryTest.php +++ b/tests/CoreBundle/Repository/TrackExerciseRepositoryTest.php @@ -12,6 +12,7 @@ use Chamilo\CoreBundle\Entity\AttemptFile; use Chamilo\CoreBundle\Entity\TrackEAttempt; use Chamilo\CoreBundle\Entity\TrackExercise; use Chamilo\CoreBundle\Repository\AssetRepository; +use Chamilo\CoreBundle\Repository\Node\CourseRepository; use Chamilo\CoreBundle\Repository\TrackExerciseRepository; use Chamilo\CourseBundle\Entity\CQuiz; use Chamilo\CourseBundle\Repository\CQuizRepository; @@ -29,11 +30,16 @@ class TrackExerciseRepositoryTest extends AbstractApiTest $em = $this->getEntityManager(); $course = $this->createCourse('new'); + $teacher = $this->createUser('teacher'); $student = $this->createUser('student'); - $repo = self::getContainer()->get(TrackExerciseRepository::class); + $courseRepo = self::getContainer()->get(CourseRepository::class); + $trackExerciseRepo = self::getContainer()->get(TrackExerciseRepository::class); $exerciseRepo = self::getContainer()->get(CQuizRepository::class); + + $this->assertSame(1, $courseRepo->count([])); + $exercise = (new CQuiz()) ->setTitle('exercise') ->setParent($course) @@ -65,13 +71,22 @@ class TrackExerciseRepositoryTest extends AbstractApiTest $em->persist($trackExercise); $this->assertHasNoEntityViolations($trackExercise); $em->flush(); - $this->assertSame(1, $repo->count([])); + $this->assertSame(1, $trackExerciseRepo->count([])); $this->assertSame(1, $exerciseRepo->count([])); + $this->assertSame(1, $courseRepo->count([])); - $repo->delete($trackExercise); + $trackExerciseRepo->delete($trackExercise); - $this->assertSame(0, $repo->count([])); + $this->assertSame(1, $courseRepo->count([])); + $this->assertSame(0, $trackExerciseRepo->count([])); $this->assertSame(1, $exerciseRepo->count([])); + + $em->remove($course); + $em->flush(); + + $this->assertSame(0, $trackExerciseRepo->count([])); + $this->assertSame(0, $exerciseRepo->count([])); + $this->assertSame(0, $courseRepo->count([])); } public function testCreateWithAttempt(): void @@ -79,7 +94,9 @@ class TrackExerciseRepositoryTest extends AbstractApiTest self::bootKernel(); $em = $this->getEntityManager(); - $repo = self::getContainer()->get(TrackExerciseRepository::class); + + $courseRepo = self::getContainer()->get(CourseRepository::class); + $trackExerciseRepo = self::getContainer()->get(TrackExerciseRepository::class); $exerciseRepo = self::getContainer()->get(CQuizRepository::class); $course = $this->createCourse('new'); @@ -128,7 +145,6 @@ class TrackExerciseRepositoryTest extends AbstractApiTest $this->assertHasNoEntityViolations($trackExercise); $em->flush(); - //$trackExercise = $repo->find($trackExercise->getExeId()); $this->assertSame(1, $trackExercise->getAttempts()->count()); $this->assertInstanceOf(TrackEAttempt::class, $trackExercise->getAttemptByQuestionId(1)); @@ -190,8 +206,16 @@ class TrackExerciseRepositoryTest extends AbstractApiTest $this->assertSame(1, $trackExercise->getAttempts()->count()); $this->assertSame(1, $attempt->getAttemptFeedbacks()->count()); $this->assertSame(1, $attempt->getAttemptFiles()->count()); - - $this->assertSame(1, $repo->count([])); + $this->assertSame(1, $trackExerciseRepo->count([])); $this->assertSame(1, $exerciseRepo->count([])); + $this->assertSame(1, $courseRepo->count([])); + + // Delete course. + $em->remove($course); + $em->flush(); + + $this->assertSame(0, $courseRepo->count([])); + $this->assertSame(0, $exerciseRepo->count([])); + $this->assertSame(0, $trackExerciseRepo->count([])); } }