Skip to content

Commit

Permalink
BB-13617: Checkout Storefront API (#39688)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmytro Khrysiev <dkhrysev@oroinc.com>
Co-authored-by: Yefim Yevtushenko <yyevtushenko@oroinc.com>
  • Loading branch information
3 people authored Jan 27, 2025
1 parent 9952696 commit 6c18a7e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ private function assertHateoasLinksForAssociations(array $metadata, string $enti
if (null === $subresource) {
continue;
}
if ($subresource->isExcludedAction(ApiAction::GET_SUBRESOURCE)) {
continue;
}
self::assertArrayHasKey(
'relationship_links',
$association,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ services:
class: Oro\Bundle\WorkflowBundle\Validator\Constraints\TransitionIsAllowedValidator
arguments:
- '@oro_workflow.registry'
- '@translator'
tags:
- { name: validator.constraint_validator }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@
use Oro\Bundle\WorkflowBundle\Validator\Constraints\TransitionIsAllowedValidator;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Contracts\Translation\TranslatorInterface;

class TransitionIsAllowedValidatorTest extends ConstraintValidatorTestCase
{
private WorkflowRegistry|MockObject $workflowRegistry;
private TranslatorInterface|MockObject $translator;

#[\Override]
protected function setUp(): void
{
$this->workflowRegistry = $this->createMock(WorkflowRegistry::class);
$this->translator = $this->createMock(TranslatorInterface::class);
parent::setUp();
}

#[\Override]
protected function createValidator(): TransitionIsAllowedValidator
{
return new TransitionIsAllowedValidator($this->workflowRegistry);
return new TransitionIsAllowedValidator($this->workflowRegistry, $this->translator);
}

private function getWorkflowStep(string $name): WorkflowStep
Expand Down Expand Up @@ -103,20 +106,26 @@ public function testValidateExceptions(
->method('getWorkflow')
->with($workflowName)
->willReturn($workflow);
$this->translator->expects(self::any())
->method('trans')
->with($expectedMessage, $expectedMessageParameters)
->willReturn($expectedMessage . ' TR');

$value = new WorkflowData();

$constraint = new TransitionIsAllowed($workflowItem, $transitionName);
$this->validator->validate($value, $constraint);

$expectedTranslatedMessage = $expectedMessage === $constraint->someConditionsNotMetMessage
? $expectedMessage
: $expectedMessage . ' TR';

if ($expectedMessage !== $constraint->someConditionsNotMetMessage) {
$this->buildViolation($constraint->someConditionsNotMetMessage)
->buildNextViolation($expectedMessage)
->setParameters($expectedMessageParameters)
->buildNextViolation($expectedTranslatedMessage)
->assertRaised();
} else {
$this->buildViolation($expectedMessage)
->setParameters($expectedMessageParameters)
$this->buildViolation($expectedTranslatedMessage)
->assertRaised();
}
}
Expand Down Expand Up @@ -152,7 +161,7 @@ public function validateExceptionsDataProvider(): array
'workflowException' => new InvalidTransitionException(),
'expectedMessage' => $constraint->someConditionsNotMetMessage,
'expectedMessageParameters' => []
],
]
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Validate that workflow transition is allowed.
*/
class TransitionIsAllowedValidator extends ConstraintValidator
{
public function __construct(
private readonly WorkflowRegistry $workflowRegistry
private readonly WorkflowRegistry $workflowRegistry,
private readonly TranslatorInterface $translator
) {
}

Expand Down Expand Up @@ -51,7 +53,11 @@ public function validate($value, Constraint $constraint): void
$this->context->addViolation($constraint->someConditionsNotMetMessage);
if ($errors->count()) {
foreach ($errors as $error) {
$this->context->addViolation($error['message'], $error['parameters'] ?? []);
// Translate errors here to use "messages" translation domains, because if we will add raw
// key to the constraint violation object it will be later translated with the "validation" domain
$this->context->addViolation(
$this->translator->trans($error['message'], $error['parameters'] ?? [])
);
}
}
}
Expand Down

0 comments on commit 6c18a7e

Please sign in to comment.