From a46adbc3fb7607df33524900a7ccb7dd4d2d593a Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Thu, 27 Jun 2024 11:03:57 +0200 Subject: [PATCH 1/3] [PHPDoc] Fixed ContentService publishing method `@throws` doc --- src/contracts/Repository/ContentService.php | 12 ++++++----- src/lib/Repository/ContentService.php | 23 +++++---------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/contracts/Repository/ContentService.php b/src/contracts/Repository/ContentService.php index 4db6c57b7f..6a90733779 100644 --- a/src/contracts/Repository/ContentService.php +++ b/src/contracts/Repository/ContentService.php @@ -340,11 +340,6 @@ public function updateContent(VersionInfo $versionInfo, ContentUpdateStruct $con * Publishes a content version and deletes archive versions if they overflow max archive versions. * Max archive versions are currently a configuration for default max limit, by default set to 5. * - * @todo Introduce null|int ContentType->versionArchiveLimit to be able to let admins override this per type. - * - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the user is not allowed to publish this version - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException if the version is not a draft - * * @param \Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo $versionInfo * @param string[] $translations List of language codes of translations which will be included * in a published version. @@ -354,6 +349,13 @@ public function updateContent(VersionInfo $versionInfo, ContentUpdateStruct $con * overriding those in the current version. * * @return \Ibexa\Contracts\Core\Repository\Values\Content\Content + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the user is not allowed to publish this version + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException if the version is not a draft + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException */ public function publishVersion(VersionInfo $versionInfo, array $translations = Language::ALL): Content; diff --git a/src/lib/Repository/ContentService.php b/src/lib/Repository/ContentService.php index a2388e97da..9cd24b9438 100644 --- a/src/lib/Repository/ContentService.php +++ b/src/lib/Repository/ContentService.php @@ -1449,22 +1449,6 @@ protected function internalUpdateContent( ); } - /** - * Publishes a content version. - * - * Publishes a content version and deletes archive versions if they overflow max archive versions. - * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. - * - * @param \Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo $versionInfo - * @param string[] $translations - * - * @return \Ibexa\Contracts\Core\Repository\Values\Content\Content - * - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException if the version is not a draft - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException - */ public function publishVersion(APIVersionInfo $versionInfo, array $translations = Language::ALL): APIContent { $content = $this->internalLoadContentById( @@ -1721,13 +1705,16 @@ protected function fieldValuesAreEqual(FieldType $fieldType, Value $value1, Valu * Publishes a content version and deletes archive versions if they overflow max archive versions. * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. * - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException if the version is not a draft - * * @param \Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo $versionInfo * @param int|null $publicationDate If null existing date is kept if there is one, otherwise current time is used. * @param string[] $translations * * @return \Ibexa\Contracts\Core\Repository\Values\Content\Content + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException if the version is not a draft + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException */ protected function internalPublishVersion(APIVersionInfo $versionInfo, $publicationDate = null, array $translations = Language::ALL) { From 181f4548cb4212acef6c851494ea61f07e4c411f Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Thu, 27 Jun 2024 13:04:25 +0200 Subject: [PATCH 2/3] Added previous exception arg. into ContentFieldValidationException ctor --- .../Exceptions/ContentFieldValidationException.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/lib/Base/Exceptions/ContentFieldValidationException.php b/src/lib/Base/Exceptions/ContentFieldValidationException.php index 0b8091dca4..89ac061ca6 100644 --- a/src/lib/Base/Exceptions/ContentFieldValidationException.php +++ b/src/lib/Base/Exceptions/ContentFieldValidationException.php @@ -9,6 +9,7 @@ use Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException as APIContentFieldValidationException; use Ibexa\Core\Base\Translatable; use Ibexa\Core\Base\TranslatableBase; +use Throwable; /** * This Exception is thrown on create or update content one or more given fields are not valid. @@ -42,11 +43,11 @@ class ContentFieldValidationException extends APIContentFieldValidationException * * @param array> $errors */ - public function __construct(array $errors) + public function __construct(array $errors, ?Throwable $previous = null) { $this->errors = $errors; $this->setMessageTemplate('Content fields did not validate'); - parent::__construct($this->getBaseTranslation()); + parent::__construct($this->getBaseTranslation(), 0, $previous); } /** @@ -54,15 +55,15 @@ public function __construct(array $errors) * * @param array> $errors */ - public static function createNewWithMultiline(array $errors, ?string $contentName = null): self + public static function createNewWithMultiline(array $errors, ?string $contentName = null, ?Throwable $previous = null): self { - $exception = new self($errors); + $exception = new self($errors, $previous); $exception->contentName = $contentName; $exception->setMessageTemplate('Content "%contentName%" fields did not validate: %errors%'); $exception->setParameters([ '%errors%' => $exception->generateValidationErrorsMessages(), - '%contentName%' => $exception->contentName !== null ? $exception->contentName : '', + '%contentName%' => $exception->contentName ?? '', ]); $exception->message = $exception->getBaseTranslation(); From 9610657c53f74203e816da6adf3937d8f5b39573 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Thu, 27 Jun 2024 13:25:41 +0200 Subject: [PATCH 3/3] [Tests] Added unit coverage for ContentFieldValidationException --- .../ContentFieldValidationExceptionTest.php | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/lib/Base/Exception/ContentFieldValidationExceptionTest.php diff --git a/tests/lib/Base/Exception/ContentFieldValidationExceptionTest.php b/tests/lib/Base/Exception/ContentFieldValidationExceptionTest.php new file mode 100644 index 0000000000..e5ea72f6e5 --- /dev/null +++ b/tests/lib/Base/Exception/ContentFieldValidationExceptionTest.php @@ -0,0 +1,72 @@ + [ + 'eng-GB' => [ + new ValidationError('error 1'), new ValidationError('error 2'), + ], + 'pol-PL' => [ + new ValidationError('error 1'), new ValidationError('error 2'), + ], + ], + 456 => [ + 'pol-PL' => [ + new ValidationError('error 3'), new ValidationError('error 4'), + ], + 'eng-GB' => [ + new ValidationError('error 3'), new ValidationError('error 4'), + ], + ], + ]; + + $exception = new ContentFieldValidationException($errors); + + self::assertSame($errors, $exception->getFieldErrors()); + + return $exception; + } + + /** + * @depends testGetFieldErrors + */ + public function testCreateNewWithMultiline(ContentFieldValidationException $exception): void + { + $newException = ContentFieldValidationException::createNewWithMultiline( + $exception->getFieldErrors(), + 'My Content' + ); + + $expectedExceptionMessage = <<getMessage()); + } +}