|
| 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