Skip to content

IBX-9446: [BO] Fatal error after non-translatable field added to Content Type #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: 4.6
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/Persistence/Legacy/Content/FieldHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,10 @@ public function updateFields(Content $content, UpdateStruct $updateStruct, Type
$field->versionNo = $content->versionInfo->versionNo;
if (isset($field->id) && array_key_exists($field->languageCode, $existingLanguageCodes)) {
$this->updateField($field, $content);
$updatedFields[$fieldDefinition->id][$languageCode] = $field;
} else {
$this->createNewField($field, $content);
}
$updatedFields[$fieldDefinition->id][$languageCode] = $field;
} elseif (!isset($existingLanguageCodes[$languageCode])) {
// If field is not set for new language
if ($fieldDefinition->isTranslatable) {
Expand Down
150 changes: 150 additions & 0 deletions tests/integration/Core/Persistence/Legacy/FileHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Integration\Core\Persistence\Legacy;

use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentUpdateStruct;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentTypeCreateStruct;
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinitionCreateStruct;
use Ibexa\Tests\Integration\Core\RepositoryTestCase;

class FileHandlerTest extends RepositoryTestCase
{
public function testUpdateFields(): void
{
$contentService = self::getContentService();
$contentTypeService = self::getContentTypeService();

// Create new ContentType
$fieldDefCreateStruct = $this->createFieldDefinitionStruct('name', 'Name', true);

$contentTypeCreateStruct = $this->createTypeCreateStruct();
$contentTypeCreateStruct->addFieldDefinition($fieldDefCreateStruct);

$contentType = $contentTypeService->createContentType($contentTypeCreateStruct, [
$contentTypeService->loadContentTypeGroupByIdentifier('Content'),
]);

$contentTypeService->publishContentTypeDraft($contentType);

// Create content, with two translations
$content = $this->createNewContent('Some Content', ['eng-GB', 'ger-DE']);

// Create draft in each translation
$content = $contentService->loadContent($content->getId(), ['eng-GB']);
$engUpdateStruct = $this->createUpdateStruct($content, '', ['eng-GB']);
$engDraft = $this->createContentDraft($content, 'eng-GB');
$engDraft = $this->updateContent($engDraft, $engUpdateStruct);

// Create new non-translatable field
$contentType = $contentTypeService->loadContentTypeByIdentifier('multi_lang_drafts');
$contentTypeDraft = $contentTypeService->createContentTypeDraft($contentType);
$fieldDefCreateStruct = $this->createFieldDefinitionStruct('non_trans_field', 'Non translatable field', false);
$contentTypeService->addFieldDefinition($contentTypeDraft, $fieldDefCreateStruct);

$contentTypeService->publishContentTypeDraft($contentTypeDraft);

// Update eng-GB draft
$engUpdateStruct->setField('non_trans_field', '', 'eng-GB');
$this->updateContent($engDraft, $engUpdateStruct);
}

private function createFieldDefinitionStruct(string $identifier, string $name, bool $isTranslatable): FieldDefinitionCreateStruct
{
$contentTypeService = self::getContentTypeService();

$fieldDefCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct(
$identifier,
'ezstring'
);

$fieldDefCreateStruct->names = ['eng-GB' => $name];
$fieldDefCreateStruct->descriptions = [
'eng-GB' => '',
];
$fieldDefCreateStruct->isTranslatable = $isTranslatable;

return $fieldDefCreateStruct;
}

private function createTypeCreateStruct(): ContentTypeCreateStruct
{
$contentTypeService = self::getContentTypeService();
$typeCreateStruct = $contentTypeService->newContentTypeCreateStruct('multi_lang_drafts');
$typeCreateStruct->mainLanguageCode = 'eng-GB';
$typeCreateStruct->names = ['eng-GB' => 'Multi lang drafts'];

return $typeCreateStruct;
}

protected function createNewContent(string $name, array $languages = ['eng-GB'], int $parentLocationId = 2): Content

Check failure on line 86 in tests/integration/Core/Persistence/Legacy/FileHandlerTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Method Ibexa\Tests\Integration\Core\Persistence\Legacy\FileHandlerTest::createNewContent() has parameter $languages with no value type specified in iterable type array.

Check failure on line 86 in tests/integration/Core/Persistence/Legacy/FileHandlerTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Method Ibexa\Tests\Integration\Core\Persistence\Legacy\FileHandlerTest::createNewContent() has parameter $languages with no value type specified in iterable type array.

Check failure on line 86 in tests/integration/Core/Persistence/Legacy/FileHandlerTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Method Ibexa\Tests\Integration\Core\Persistence\Legacy\FileHandlerTest::createNewContent() has parameter $languages with no value type specified in iterable type array.
{
$contentTypeService = self::getContentTypeService();
$contentService = self::getContentService();
$locationService = self::getLocationService();

$contentType = $contentTypeService->loadContentTypeByIdentifier('multi_lang_drafts');
$createStruct = $contentService->newContentCreateStruct($contentType, $languages[0]);

foreach ($languages as $language) {
$createStruct->setField('name', "[$language]" . $name, $language);
}
$locationCreateStruct = $locationService->newLocationCreateStruct($parentLocationId);

$draft = $contentService->createContent($createStruct, [$locationCreateStruct]);

return $contentService->publishVersion($draft->versionInfo);
}

protected function createUpdateStruct(Content $content, string $translatedName, array $languages)

Check failure on line 105 in tests/integration/Core/Persistence/Legacy/FileHandlerTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Method Ibexa\Tests\Integration\Core\Persistence\Legacy\FileHandlerTest::createUpdateStruct() has no return type specified.

Check failure on line 105 in tests/integration/Core/Persistence/Legacy/FileHandlerTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Method Ibexa\Tests\Integration\Core\Persistence\Legacy\FileHandlerTest::createUpdateStruct() has parameter $languages with no value type specified in iterable type array.

Check failure on line 105 in tests/integration/Core/Persistence/Legacy/FileHandlerTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Method Ibexa\Tests\Integration\Core\Persistence\Legacy\FileHandlerTest::createUpdateStruct() has no return type specified.

Check failure on line 105 in tests/integration/Core/Persistence/Legacy/FileHandlerTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Method Ibexa\Tests\Integration\Core\Persistence\Legacy\FileHandlerTest::createUpdateStruct() has parameter $languages with no value type specified in iterable type array.

Check failure on line 105 in tests/integration/Core/Persistence/Legacy/FileHandlerTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Method Ibexa\Tests\Integration\Core\Persistence\Legacy\FileHandlerTest::createUpdateStruct() has no return type specified.

Check failure on line 105 in tests/integration/Core/Persistence/Legacy/FileHandlerTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Method Ibexa\Tests\Integration\Core\Persistence\Legacy\FileHandlerTest::createUpdateStruct() has parameter $languages with no value type specified in iterable type array.
{
$contentService = self::getContentService();

$updateStruct = $contentService->newContentUpdateStruct();
$updateStruct->initialLanguageCode = $languages[0];

if ($translatedName === '') {
$translatedNameOrg = $content->getName();
} else {
$translatedNameOrg = $translatedName;
}

foreach ($languages as $language) {
$translatedName = "[$language]" . $translatedNameOrg;

$updateStruct->setField('name', $translatedName, $language);
}

return $updateStruct;
}

protected function createContentDraft(Content $content, string $languageCode): Content
{
$contentService = self::getContentService();
$contentLanguageService = self::getLanguageService();

$language = $contentLanguageService->loadLanguage($languageCode);
$draft = $contentService->createContentDraft($content->contentInfo, null, null, $language);

return $draft;
}

protected function updateContent(Content $draft, ContentUpdateStruct $updateStruct/*, array $languages*/): Content
{
$contentService = self::getContentService();

// At this point, $updateStruct is correct. However, when it reaches Ibexa\Core\Persistence\Legacy\Content::updateFields(),
// it will have an additional field (in ger-DE). This does not happen when running the same code in
// a controller inside the application (you then need to replace 'self::get*Service()' with DI).
// It seems like for instance decoration Ibexa\Core\Event\ContentService is not used inside tests, maybe other decorations to?
$updatedDraft = $contentService->updateContent($draft->versionInfo, $updateStruct);

return $updatedDraft;
}
}
Loading