diff --git a/src/lib/Repository/ContentTypeService.php b/src/lib/Repository/ContentTypeService.php index e1773a3b7f..63ee29092c 100644 --- a/src/lib/Repository/ContentTypeService.php +++ b/src/lib/Repository/ContentTypeService.php @@ -297,6 +297,11 @@ public function deleteContentTypeGroup(APIContentTypeGroup $contentTypeGroup): v $this->repository->beginTransaction(); try { + $contentTypesDrafts = $this->contentTypeHandler->loadContentTypes($contentTypeGroup->id, SPIContentType::STATUS_DRAFT); + foreach ($contentTypesDrafts as $contentTypeDraft) { + $this->contentTypeHandler->delete($contentTypeDraft->id, SPIContentType::STATUS_DRAFT); + } + $this->contentTypeHandler->deleteGroup( $loadedContentTypeGroup->id ); diff --git a/tests/integration/Core/Repository/ContentTypeServiceTest.php b/tests/integration/Core/Repository/ContentTypeServiceTest.php index 7f125bedee..476cfdaf4c 100644 --- a/tests/integration/Core/Repository/ContentTypeServiceTest.php +++ b/tests/integration/Core/Repository/ContentTypeServiceTest.php @@ -600,6 +600,36 @@ public function testDeleteContentTypeGroup() $this->fail('Content type group not deleted.'); } + public function testDeleteContentTypeGroupWithOrphanedContentTypeDrafts(): void + { + $repository = $this->getRepository(); + + $contentTypeService = $repository->getContentTypeService(); + + $groupCreate = $contentTypeService->newContentTypeGroupCreateStruct( + 'new-group' + ); + $contentTypeService->createContentTypeGroup($groupCreate); + + $group = $contentTypeService->loadContentTypeGroupByIdentifier('new-group'); + for ($i = 0; $i < 3; ++$i) { + $contentTypeCreateStruct = $contentTypeService->newContentTypeCreateStruct('content_type_draft_' . $i); + $contentTypeCreateStruct->mainLanguageCode = 'eng-GB'; + $contentTypeCreateStruct->names = [ + 'eng-GB' => 'content_type_draft_' . $i, + ]; + + $contentTypeService->createContentType($contentTypeCreateStruct, [$group]); + } + + $contentTypeService->deleteContentTypeGroup($group); + + // loadContentTypeGroup should throw NotFoundException + $this->expectException(NotFoundException::class); + + $contentTypeService->loadContentTypeGroup($group->id); + } + /** * Test for the newContentTypeCreateStruct() method. *