diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20211008133540.php b/src/CoreBundle/Migrations/Schema/V200/Version20211008133540.php index 75ebbafa54..1a3c15f80f 100644 --- a/src/CoreBundle/Migrations/Schema/V200/Version20211008133540.php +++ b/src/CoreBundle/Migrations/Schema/V200/Version20211008133540.php @@ -21,7 +21,7 @@ final class Version20211008133540 extends AbstractMigrationChamilo if ($table->hasColumn('c_tool_id')) { if (!$table->hasForeignKey('FK_D705267B1DF6B517')) { $this->addSql( - 'ALTER TABLE c_tool_intro ADD CONSTRAINT FK_D705267B1DF6B517 FOREIGN KEY (c_tool_id) REFERENCES c_tool (iid);' + 'ALTER TABLE c_tool_intro ADD CONSTRAINT FK_D705267B1DF6B517 FOREIGN KEY (c_tool_id) REFERENCES c_tool (iid) ON DELETE CASCADE' ); } if (!$table->hasIndex('IDX_D705267B1DF6B517')) { diff --git a/src/CourseBundle/Entity/CToolIntro.php b/src/CourseBundle/Entity/CToolIntro.php index b018e7d2e3..4d6bea1cd1 100644 --- a/src/CourseBundle/Entity/CToolIntro.php +++ b/src/CourseBundle/Entity/CToolIntro.php @@ -78,7 +78,7 @@ class CToolIntro extends AbstractResource implements ResourceInterface, Resource /** * @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CTool") - * @ORM\JoinColumn(name="c_tool_id", referencedColumnName="iid", nullable=false) + * @ORM\JoinColumn(name="c_tool_id", referencedColumnName="iid", nullable=false, onDelete="CASCADE") */ #[Assert\NotNull] #[Groups(['c_tool_intro:read', 'c_tool_intro:write'])] diff --git a/tests/CourseBundle/Repository/CToolIntroRepositoryTest.php b/tests/CourseBundle/Repository/CToolIntroRepositoryTest.php index 3cc57fd3df..d4bd1f70a8 100644 --- a/tests/CourseBundle/Repository/CToolIntroRepositoryTest.php +++ b/tests/CourseBundle/Repository/CToolIntroRepositoryTest.php @@ -7,6 +7,7 @@ declare(strict_types=1); namespace Chamilo\Tests\CourseBundle\Repository; use Chamilo\CoreBundle\Entity\ResourceLink; +use Chamilo\CoreBundle\Repository\Node\CourseRepository; use Chamilo\CourseBundle\Entity\CTool; use Chamilo\CourseBundle\Entity\CToolIntro; use Chamilo\CourseBundle\Repository\CToolIntroRepository; @@ -19,9 +20,9 @@ class CToolIntroRepositoryTest extends AbstractApiTest public function testCreate(): void { - self::bootKernel(); - $em = $this->getEntityManager(); + + $courseRepo = self::getContainer()->get(CourseRepository::class); $repo = self::getContainer()->get(CToolIntroRepository::class); $this->assertSame(0, $repo->count([])); @@ -46,16 +47,49 @@ class CToolIntroRepositoryTest extends AbstractApiTest $this->assertNotEmpty($intro->getIntroText()); $this->assertNotNull($intro->getIid()); $this->assertNotEmpty($intro->getResourceName()); - $this->assertSame(1, $repo->count([])); + + // Delete intro. $repo->delete($intro); $this->assertSame(0, $repo->count([])); + $this->assertSame(1, $courseRepo->count([])); } - public function testCreateInSession(): void + public function testCreateAndDeleteCourse(): void { - self::bootKernel(); + $em = $this->getEntityManager(); + + $courseRepo = self::getContainer()->get(CourseRepository::class); + $repo = self::getContainer()->get(CToolIntroRepository::class); + + $course = $this->createCourse('new'); + $teacher = $this->createUser('teacher'); + + /** @var CTool $courseTool */ + $courseTool = $course->getTools()->first(); + + $intro = (new CToolIntro()) + ->setIntroText('test') + ->setCourseTool($courseTool) + ->setParent($course) + ->setCreator($teacher) + ->addCourseLink($course) + ; + $this->assertHasNoEntityViolations($intro); + $em->persist($intro); + $em->flush(); + $this->assertSame(1, $courseRepo->count([])); + $this->assertSame(1, $repo->count([])); + + // Delete course. + $courseRepo->delete($course); + $this->assertSame(0, $repo->count([])); + $this->assertSame(0, $courseRepo->count([])); + } + + public function testCreateInSession(): void + { $em = $this->getEntityManager(); $repo = self::getContainer()->get(CToolIntroRepository::class); @@ -84,7 +118,6 @@ class CToolIntroRepositoryTest extends AbstractApiTest $this->assertSame(1, $repo->count([])); // Create intro in session. - $intro2 = (new CToolIntro()) ->setIntroText('test in session') ->setCourseTool($courseTool) @@ -218,7 +251,6 @@ class CToolIntroRepositoryTest extends AbstractApiTest public function testUpdateIntroApi(): void { $course = $this->createCourse('new'); - $token = $this->getUserToken(); $resourceNodeId = $course->getResourceNode()->getId();