|
| 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\Persistence\Legacy; |
| 10 | + |
| 11 | +use Ibexa\Contracts\Core\Repository\Values\Content\Content; |
| 12 | +use Ibexa\Contracts\Core\Repository\Values\Content\ContentUpdateStruct; |
| 13 | +use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentTypeCreateStruct; |
| 14 | +use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinitionCreateStruct; |
| 15 | +use Ibexa\Tests\Integration\Core\RepositoryTestCase; |
| 16 | + |
| 17 | +final class ContentUpdateAfterAddingFieldDefinitionTest extends RepositoryTestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\Exception |
| 21 | + */ |
| 22 | + public function testUpdateFields(): void |
| 23 | + { |
| 24 | + $contentService = self::getContentService(); |
| 25 | + $contentTypeService = self::getContentTypeService(); |
| 26 | + |
| 27 | + // Create new ContentType |
| 28 | + $fieldDefCreateStruct = $this->createFieldDefinitionStruct('name', 'Name', true); |
| 29 | + |
| 30 | + $contentTypeCreateStruct = $this->createTypeCreateStruct(); |
| 31 | + $contentTypeCreateStruct->addFieldDefinition($fieldDefCreateStruct); |
| 32 | + |
| 33 | + $contentType = $contentTypeService->createContentType($contentTypeCreateStruct, [ |
| 34 | + $contentTypeService->loadContentTypeGroupByIdentifier('Content'), |
| 35 | + ]); |
| 36 | + |
| 37 | + $contentTypeService->publishContentTypeDraft($contentType); |
| 38 | + |
| 39 | + // Create content, with two translations |
| 40 | + $content = $this->createNewContent('Some Content', ['eng-US', 'ger-DE']); |
| 41 | + |
| 42 | + // Create draft in language with higher id ( later in the $contentLanguageService->loadLanguages() list than 'eng-US' ) |
| 43 | + $content = $contentService->loadContent($content->getId(), ['eng-US']); |
| 44 | + $engUpdateStruct = $this->createUpdateStruct($content, '', ['eng-US']); |
| 45 | + $engDraft = $this->createContentDraft($content, 'eng-US'); |
| 46 | + $engDraft = $this->updateContent($engDraft, $engUpdateStruct); |
| 47 | + |
| 48 | + // Create new non-translatable field |
| 49 | + $contentType = $contentTypeService->loadContentTypeByIdentifier('multi_lang_drafts'); |
| 50 | + $contentTypeDraft = $contentTypeService->createContentTypeDraft($contentType); |
| 51 | + $fieldDefCreateStruct = $this->createFieldDefinitionStruct('non_trans_field', 'Non translatable field', false); |
| 52 | + $contentTypeService->addFieldDefinition($contentTypeDraft, $fieldDefCreateStruct); |
| 53 | + |
| 54 | + $contentTypeService->publishContentTypeDraft($contentTypeDraft); |
| 55 | + |
| 56 | + // Update eng-US draft |
| 57 | + $engUpdateStruct->setField('non_trans_field', '', 'eng-US'); |
| 58 | + $this->updateContent($engDraft, $engUpdateStruct); |
| 59 | + } |
| 60 | + |
| 61 | + private function createFieldDefinitionStruct(string $identifier, string $name, bool $isTranslatable): FieldDefinitionCreateStruct |
| 62 | + { |
| 63 | + $contentTypeService = self::getContentTypeService(); |
| 64 | + |
| 65 | + $fieldDefCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct( |
| 66 | + $identifier, |
| 67 | + 'ezstring' |
| 68 | + ); |
| 69 | + |
| 70 | + $fieldDefCreateStruct->names = ['eng-US' => $name]; |
| 71 | + $fieldDefCreateStruct->descriptions = [ |
| 72 | + 'eng-US' => '', |
| 73 | + ]; |
| 74 | + $fieldDefCreateStruct->isTranslatable = $isTranslatable; |
| 75 | + |
| 76 | + return $fieldDefCreateStruct; |
| 77 | + } |
| 78 | + |
| 79 | + private function createTypeCreateStruct(): ContentTypeCreateStruct |
| 80 | + { |
| 81 | + $contentTypeService = self::getContentTypeService(); |
| 82 | + $typeCreateStruct = $contentTypeService->newContentTypeCreateStruct('multi_lang_drafts'); |
| 83 | + $typeCreateStruct->mainLanguageCode = 'eng-US'; |
| 84 | + $typeCreateStruct->names = ['eng-US' => 'Multi lang drafts']; |
| 85 | + |
| 86 | + return $typeCreateStruct; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param string[] $languages |
| 91 | + */ |
| 92 | + protected function createNewContent(string $name, array $languages = ['eng-US'], int $parentLocationId = 2): Content |
| 93 | + { |
| 94 | + $contentTypeService = self::getContentTypeService(); |
| 95 | + $contentService = self::getContentService(); |
| 96 | + $locationService = self::getLocationService(); |
| 97 | + |
| 98 | + $contentType = $contentTypeService->loadContentTypeByIdentifier('multi_lang_drafts'); |
| 99 | + $createStruct = $contentService->newContentCreateStruct($contentType, $languages[0]); |
| 100 | + |
| 101 | + foreach ($languages as $language) { |
| 102 | + $createStruct->setField('name', "[$language]" . $name, $language); |
| 103 | + } |
| 104 | + $locationCreateStruct = $locationService->newLocationCreateStruct($parentLocationId); |
| 105 | + |
| 106 | + $draft = $contentService->createContent($createStruct, [$locationCreateStruct]); |
| 107 | + |
| 108 | + return $contentService->publishVersion($draft->versionInfo); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param string[] $languages |
| 113 | + */ |
| 114 | + protected function createUpdateStruct(Content $content, string $translatedName, array $languages): ContentUpdateStruct |
| 115 | + { |
| 116 | + $contentService = self::getContentService(); |
| 117 | + |
| 118 | + $updateStruct = $contentService->newContentUpdateStruct(); |
| 119 | + $updateStruct->initialLanguageCode = $languages[0]; |
| 120 | + |
| 121 | + if ($translatedName === '') { |
| 122 | + $translatedNameOrg = $content->getName(); |
| 123 | + } else { |
| 124 | + $translatedNameOrg = $translatedName; |
| 125 | + } |
| 126 | + |
| 127 | + foreach ($languages as $language) { |
| 128 | + $translatedName = "[$language]" . $translatedNameOrg; |
| 129 | + |
| 130 | + $updateStruct->setField('name', $translatedName, $language); |
| 131 | + } |
| 132 | + |
| 133 | + return $updateStruct; |
| 134 | + } |
| 135 | + |
| 136 | + protected function createContentDraft(Content $content, string $languageCode): Content |
| 137 | + { |
| 138 | + $contentLanguageService = self::getLanguageService(); |
| 139 | + |
| 140 | + $language = $contentLanguageService->loadLanguage($languageCode); |
| 141 | + |
| 142 | + return self::getContentService()->createContentDraft($content->contentInfo, null, null, $language); |
| 143 | + } |
| 144 | + |
| 145 | + protected function updateContent(Content $draft, ContentUpdateStruct $updateStruct): Content |
| 146 | + { |
| 147 | + return self::getContentService()->updateContent($draft->versionInfo, $updateStruct); |
| 148 | + } |
| 149 | +} |
0 commit comments