Skip to content

Commit 222f62c

Browse files
ViniTouSteveb-p
andauthored
IBX-9262: Fixed Relation::Asset not being cleaned up on content deletion (#523)
For more details see https://issues.ibexa.co/browse/IBX-9262 and #523 Key changes: * Fixed Relation::Asset not being cleaned up on content deletion --------- Co-Authored-By: Paweł Niedzielski <[email protected]>
1 parent 42a1259 commit 222f62c

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

src/lib/Persistence/Legacy/Content/Gateway/DoctrineDatabase.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ public function removeReverseFieldRelations(int $contentId): void
11861186
)
11871187
)
11881188
->setParameter('content_id', $contentId, ParameterType::INTEGER)
1189-
->setParameter('relation_type', Relation::FIELD, ParameterType::INTEGER);
1189+
->setParameter('relation_type', Relation::FIELD | Relation::ASSET, ParameterType::INTEGER);
11901190

11911191
$statement = $query->execute();
11921192

@@ -1198,6 +1198,10 @@ public function removeReverseFieldRelations(int $contentId): void
11981198
if ($row['data_type_string'] === 'ezobjectrelationlist') {
11991199
$this->removeRelationFromRelationListField($contentId, $row);
12001200
}
1201+
1202+
if ($row['data_type_string'] === 'ezimageasset') {
1203+
$this->removeRelationFromAssetField($row);
1204+
}
12011205
}
12021206
}
12031207

@@ -1266,6 +1270,33 @@ private function removeRelationFromRelationField(array $row): void
12661270
$query->execute();
12671271
}
12681272

1273+
/**
1274+
* @param array{
1275+
* id: int|string,
1276+
* version: int|string,
1277+
* data_type_string: string,
1278+
* data_text: string|null
1279+
* } $row
1280+
*/
1281+
private function removeRelationFromAssetField(array $row): void
1282+
{
1283+
$query = $this->connection->createQueryBuilder();
1284+
$query
1285+
->update(self::CONTENT_FIELD_TABLE)
1286+
->set('data_text', ':data_text')
1287+
->set('data_int', ':data_int')
1288+
->set('sort_key_int', ':sort_key_int')
1289+
->setParameter('data_text', null, ParameterType::NULL)
1290+
->setParameter('data_int', null, ParameterType::NULL)
1291+
->setParameter('sort_key_int', 0, ParameterType::INTEGER)
1292+
->andWhere('id = :attribute_id')
1293+
->andWhere('version = :version_no')
1294+
->setParameter('attribute_id', (int)$row['id'], ParameterType::INTEGER)
1295+
->setParameter('version_no', (int)$row['version'], ParameterType::INTEGER);
1296+
1297+
$query->execute();
1298+
}
1299+
12691300
public function deleteField(int $fieldId): void
12701301
{
12711302
$query = $this->connection->createQueryBuilder();
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Integration\Core\Repository\ContentService;
10+
11+
use Ibexa\Contracts\Core\Repository\ContentService;
12+
use Ibexa\Contracts\Core\Repository\ContentTypeService;
13+
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
14+
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
15+
use Ibexa\Core\FieldType\Image\Value;
16+
use Ibexa\Core\FieldType\ImageAsset\Value as AssetValue;
17+
use Ibexa\Tests\Integration\Core\RepositoryTestCase;
18+
19+
final class ImageAssetTest extends RepositoryTestCase
20+
{
21+
private ContentService $contentService;
22+
23+
private ContentTypeService $contentTypeService;
24+
25+
protected function setUp(): void
26+
{
27+
parent::setUp();
28+
29+
$this->contentService = self::getContentService();
30+
$this->contentTypeService = self::getContentTypeService();
31+
}
32+
33+
public function testAssetRelationIsRemoved(): void
34+
{
35+
$contentType = $this->createContentTypeWithImageAsset();
36+
$destinationContent = $this->createImageContent();
37+
38+
$struct = $this->contentService->newContentCreateStruct($contentType, 'eng-US');
39+
$struct->setField('asset', new AssetValue(
40+
$destinationContent->getId(),
41+
));
42+
$assetContent = $this->contentService->publishVersion(
43+
$this->contentService->createContent($struct)->getVersionInfo()
44+
);
45+
46+
self::assertEquals(1, $this->contentService->countRelations($assetContent->getVersionInfo()));
47+
self::assertEquals(1, $this->contentService->countReverseRelations($destinationContent->getContentInfo()));
48+
49+
$this->contentService->deleteContent($destinationContent->getContentInfo());
50+
51+
self::assertEquals(0, $this->contentService->countRelations($assetContent->getVersionInfo()));
52+
self::assertEquals(0, $this->contentService->countReverseRelations($destinationContent->getContentInfo()));
53+
54+
$assetContent = $this->contentService->loadContentByContentInfo($assetContent->getContentInfo());
55+
56+
$value = $assetContent->getFieldValue('asset');
57+
self::assertInstanceOf(AssetValue::class, $value);
58+
self::assertNull($value->destinationContentId);
59+
}
60+
61+
private function createContentTypeWithImageAsset(): ContentType
62+
{
63+
$struct = $this->contentTypeService->newContentTypeCreateStruct('content_type_with_image_asset');
64+
$struct->names = ['eng-US' => 'Content Type with Image Asset'];
65+
66+
$struct->addFieldDefinition(
67+
$this->contentTypeService->newFieldDefinitionCreateStruct('asset', 'ezimageasset')
68+
);
69+
$struct->mainLanguageCode = 'eng-US';
70+
$contentType = $this->contentTypeService->createContentType(
71+
$struct,
72+
[$this->contentTypeService->loadContentTypeGroupByIdentifier('Content')],
73+
);
74+
$this->contentTypeService->publishContentTypeDraft($contentType);
75+
76+
return $this->contentTypeService->loadContentType($contentType->id);
77+
}
78+
79+
private function createImageContent(): Content
80+
{
81+
$path = __DIR__ . '/../_fixtures/image/square.png';
82+
83+
$imageContentType = $this->contentTypeService->loadContentTypeByIdentifier('image');
84+
$struct = $this->contentService->newContentCreateStruct($imageContentType, 'eng-US');
85+
$struct->setField('name', 'Image Name');
86+
$struct->setField('image', new Value(
87+
[
88+
'fileName' => 'square.jpg',
89+
'inputUri' => $path,
90+
'fileSize' => filesize($path),
91+
],
92+
));
93+
$content = $this->contentService->createContent(
94+
$struct
95+
);
96+
97+
return $this->contentService->publishVersion($content->getVersionInfo());
98+
}
99+
}

0 commit comments

Comments
 (0)