|
| 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 DateTime; |
| 12 | +use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct; |
| 13 | +use eZ\Publish\Core\FieldType\TextLine; |
| 14 | +use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct; |
| 15 | +use Ibexa\Tests\Integration\Core\RepositoryTestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * @covers \eZ\Publish\API\Repository\ContentService |
| 19 | + */ |
| 20 | +final class CopyTranslationsFromPublishedVersionTest extends RepositoryTestCase |
| 21 | +{ |
| 22 | + private const ENG_LANGUAGE_CODE = 'eng-GB'; |
| 23 | + private const GER_LANGUAGE_CODE = 'ger-DE'; |
| 24 | + private const US_LANGUAGE_CODE = 'eng-US'; |
| 25 | + private const CONTENT_TYPE_IDENTIFIER = 'custom'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @throws \eZ\Publish\API\Repository\Exceptions\Exception |
| 29 | + */ |
| 30 | + public function testCopyTranslationsFromPublishedVersionCopiesEmptyValues(): void |
| 31 | + { |
| 32 | + $this->createContentType(); |
| 33 | + |
| 34 | + $contentService = self::getContentService(); |
| 35 | + $contentTypeService = self::getContentTypeService(); |
| 36 | + $locationService = self::getLocationService(); |
| 37 | + |
| 38 | + // Creating and publishing content in eng-GB language |
| 39 | + $contentType = $contentTypeService->loadContentTypeByIdentifier(self::CONTENT_TYPE_IDENTIFIER); |
| 40 | + $mainLanguageCode = self::ENG_LANGUAGE_CODE; |
| 41 | + $contentCreateStruct = $contentService->newContentCreateStruct($contentType, $mainLanguageCode); |
| 42 | + $contentCreateStruct->setField('title', 'Test title'); |
| 43 | + |
| 44 | + $contentDraft = $contentService->createContent( |
| 45 | + $contentCreateStruct, |
| 46 | + [ |
| 47 | + $locationService->newLocationCreateStruct(2), |
| 48 | + ], |
| 49 | + ); |
| 50 | + $publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo()); |
| 51 | + |
| 52 | + // Creating a draft and publishing in ger-DE language with empty 'title' field |
| 53 | + $gerDraft = $contentService->createContentDraft($publishedContent->contentInfo); |
| 54 | + $usDraft = $contentService->createContentDraft($publishedContent->contentInfo); |
| 55 | + |
| 56 | + $contentUpdateStruct = new ContentUpdateStruct([ |
| 57 | + 'initialLanguageCode' => self::GER_LANGUAGE_CODE, |
| 58 | + ]); |
| 59 | + $contentUpdateStruct->setField('title', null); |
| 60 | + $gerContent = $contentService->updateContent($gerDraft->getVersionInfo(), $contentUpdateStruct); |
| 61 | + $contentService->publishVersion($gerContent->getVersionInfo(), [self::GER_LANGUAGE_CODE]); |
| 62 | + |
| 63 | + // Creating a draft and publishing in eng-US language with empty 'title' field |
| 64 | + $contentUpdateStruct = new ContentUpdateStruct([ |
| 65 | + 'initialLanguageCode' => self::US_LANGUAGE_CODE, |
| 66 | + ]); |
| 67 | + $contentUpdateStruct->setField('title', null); |
| 68 | + $usContent = $contentService->updateContent($usDraft->getVersionInfo(), $contentUpdateStruct); |
| 69 | + $publishedUsContent = $contentService->publishVersion($usContent->getVersionInfo(), [self::US_LANGUAGE_CODE]); |
| 70 | + |
| 71 | + $usFieldValueInUsContent = $publishedUsContent->getField('title', self::US_LANGUAGE_CODE)->getValue(); |
| 72 | + self::assertInstanceOf(TextLine\Value::class, $usFieldValueInUsContent); |
| 73 | + self::assertSame('', $usFieldValueInUsContent->text); |
| 74 | + |
| 75 | + $gerFieldValueInUsContent = $publishedUsContent->getField('title', self::GER_LANGUAGE_CODE)->getValue(); |
| 76 | + self::assertInstanceOf(TextLine\Value::class, $gerFieldValueInUsContent); |
| 77 | + self::assertSame('', $gerFieldValueInUsContent->text); |
| 78 | + } |
| 79 | + |
| 80 | + private function createContentType(): void |
| 81 | + { |
| 82 | + $permissionResolver = self::getPermissionResolver(); |
| 83 | + $contentTypeService = self::getContentTypeService(); |
| 84 | + |
| 85 | + $typeCreate = $contentTypeService->newContentTypeCreateStruct(self::CONTENT_TYPE_IDENTIFIER); |
| 86 | + |
| 87 | + $typeCreate->mainLanguageCode = 'eng-GB'; |
| 88 | + $typeCreate->remoteId = '1234567890abcdef'; |
| 89 | + $typeCreate->urlAliasSchema = '<title>'; |
| 90 | + $typeCreate->nameSchema = '<title>'; |
| 91 | + $typeCreate->names = [ |
| 92 | + self::ENG_LANGUAGE_CODE => 'Some content type', |
| 93 | + ]; |
| 94 | + $typeCreate->descriptions = [ |
| 95 | + self::ENG_LANGUAGE_CODE => '', |
| 96 | + ]; |
| 97 | + $typeCreate->creatorId = $permissionResolver->getCurrentUserReference()->getUserId(); |
| 98 | + $typeCreate->creationDate = new DateTime(); |
| 99 | + |
| 100 | + $typeCreate->addFieldDefinition( |
| 101 | + new FieldDefinitionCreateStruct( |
| 102 | + [ |
| 103 | + 'fieldTypeIdentifier' => 'ezstring', |
| 104 | + 'identifier' => 'title', |
| 105 | + 'names' => ['eng-GB' => 'Title'], |
| 106 | + 'isRequired' => false, |
| 107 | + 'isTranslatable' => true, |
| 108 | + ], |
| 109 | + ) |
| 110 | + ); |
| 111 | + |
| 112 | + $contentTypeDraft = $contentTypeService->createContentType( |
| 113 | + $typeCreate, |
| 114 | + [$contentTypeService->loadContentTypeGroupByIdentifier('Content')], |
| 115 | + ); |
| 116 | + $contentTypeService->publishContentTypeDraft($contentTypeDraft); |
| 117 | + } |
| 118 | +} |
0 commit comments