Skip to content

Commit

Permalink
minor #15581 [API] Resolve update of translations (Wojdylak)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.13 branch.

Discussion
----------

| Q               | A                                                            |
|-----------------|--------------------------------------------------------------|
| Branch?         | 1.13
| Bug fix?        | no
| New feature?    | yes
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | N/A
| License         | MIT

#15535

Commits
-------
  [AttributeBundle] Add unique entity constraint to AttributeTranslation
  [PaymentBundle] Add unique entity constraint to PaymentMethodTranslation
  [ProductBundle] Add unique entity constraint to ProductAssociationTypeTranslation
  [ProductBundle] Add unique entity constraint to ProductOptionTranslation and ProductOptionValueTranslation
  [ProductBundle] Add unique entity constraint to ProductTranslation
  [ProductBundle] Add unique entity constraint to ProductVariantTranslation
  [PromotionBundle] Add unique entity constraint to CatalogPromotionTranslation
  [PromotionBundle] Add unique entity constraint to PromotionTranslation
  [ShippingBundle] Add unique entity constraint to ShippingMethodTranslation
  [TaxonBundle] Add unique entity constraint to TaxonTranslation
  [Behat] Rename language to localeCode for improved clarity
  [Api][Test] Improve spec test of throwing an exception if locale is not the same as key
  Change message of unique entity constraint of translations
  Rename unique constraint error message in translation
  • Loading branch information
NoResponseMate committed Dec 6, 2023
2 parents f9fac11 + a61c57b commit ff7ba12
Show file tree
Hide file tree
Showing 43 changed files with 657 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ public function iWantToModifyTheProductAssociationType(ProductAssociationTypeInt
}

/**
* @When I rename it to :name in :language
* @When I rename it to :name in :localeCode
*/
public function iRenameItToIn(string $name, string $language): void
public function iRenameItToIn(string $name, string $localeCode): void
{
$this->client->updateRequestData(['translations' => [$language => ['name' => $name]]]);
$this->client->updateRequestData(['translations' => [$localeCode => ['name' => $name]]]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,33 @@ public function iWantToModifyProductOption(ProductOptionInterface $productOption
}

/**
* @When I name it :name in :language
* @When I name it :name in :localeCode
* @When I do not name it
*/
public function iNameItInLanguage(?string $name = null, ?string $language = 'en_US'): void
public function iNameItInLanguage(?string $name = null, ?string $localeCode = 'en_US'): void
{
$data = ['translations' => [$language => []]];
$data = ['translations' => [$localeCode => []]];
if ($name !== null) {
$data['translations'][$language]['name'] = $name;
$data['translations'][$localeCode]['name'] = $name;
}

$this->client->updateRequestData($data);
}

/**
* @When I rename it to :name in :language
* @When I rename it to :name in :localeCode
*/
public function iRenameItInLanguage(string $name, string $language): void
public function iRenameItInLanguage(string $name, string $localeCode): void
{
$this->client->updateRequestData(['translations' => [$language => ['name' => $name]]]);
$this->client->updateRequestData(['translations' => [$localeCode => ['name' => $name]]]);
}

/**
* @When I remove its name from :language translation
* @When I remove its name from :localeCode translation
*/
public function iRemoveItsNameFromTranslation(string $language): void
public function iRemoveItsNameFromTranslation(string $localeCode): void
{
$this->client->updateRequestData(['translations' => [$language => ['name' => '']]]);
$this->client->updateRequestData(['translations' => [$localeCode => ['name' => '']]]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</attribute>
<attribute name="locale">
<group>admin:product:create</group>
<group>admin:product:update</group>
</attribute>
<attribute name="name">
<group>admin:product:read</group>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function it_throws_an_exception_if_locale_is_not_the_same_as_key(

$this
->shouldThrow(TranslationLocaleMismatchException::class)
->during('denormalize', [['translations' => ['de_DE' => ['locale' => 'locale']]], TranslatableInterface::class])
->during('denormalize', [['translations' => ['de_DE' => ['locale' => 'en_US']]], TranslatableInterface::class])
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Component\Attribute\Model\AttributeTranslation">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">
<value>locale</value>
<value>translatable</value>
</option>
<option name="groups">sylius</option>
<option name="message">sylius.translation.locale.unique</option>
</constraint>
<property name="name">
<constraint name="NotBlank">
<option name="message">sylius.attribute.name.not_blank</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ sylius:
locale:
not_blank: Please enter the locale.
invalid: This value is not a valid locale.
unique: A translation for the {{ value }} locale code already exists.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Component\Payment\Model\PaymentMethodTranslation">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">
<value>locale</value>
<value>translatable</value>
</option>
<option name="groups">sylius</option>
<option name="message">sylius.translation.locale.unique</option>
</constraint>
<property name="name">
<constraint name="NotBlank">
<option name="message">sylius.payment_method.name.not_blank</option>
Expand All @@ -26,5 +34,15 @@
<option name="groups">sylius</option>
</constraint>
</property>
<property name="locale">
<constraint name="NotBlank">
<option name="groups">sylius</option>
<option name="message">sylius.translation.locale.not_blank</option>
</constraint>
<constraint name="Locale">
<option name="groups">sylius</option>
<option name="message">sylius.translation.locale.invalid</option>
</constraint>
</property>
</class>
</constraint-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ sylius:
not_blank: 'Please enter payment method code.'
regex: 'Payment method code can only be comprised of letters, numbers, dashes and underscores.'
unique: 'The payment method with given code already exists.'
translation:
locale:
not_blank: Please enter the locale.
invalid: This value is not a valid locale.
unique: A translation for the {{ value }} locale code already exists.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Component\Product\Model\ProductAssociationTypeTranslation">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">
<value>locale</value>
<value>translatable</value>
</option>
<option name="groups">sylius</option>
<option name="message">sylius.translation.locale.unique</option>
</constraint>
<property name="name">
<constraint name="NotBlank">
<option name="message">sylius.association_type.name.not_blank</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Component\Product\Model\ProductOptionTranslation">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">
<value>locale</value>
<value>translatable</value>
</option>
<option name="groups">sylius</option>
<option name="message">sylius.translation.locale.unique</option>
</constraint>
<property name="name">
<constraint name="NotBlank">
<option name="message">sylius.option.name.not_blank</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Component\Product\Model\ProductOptionValueTranslation">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">
<value>locale</value>
<value>translatable</value>
</option>
<option name="groups">sylius</option>
<option name="message">sylius.translation.locale.unique</option>
</constraint>
<property name="value">
<constraint name="NotBlank">
<option name="message">sylius.option_value.value.not_blank</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Component\Product\Model\ProductTranslation">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">
<value>locale</value>
<value>translatable</value>
</option>
<option name="groups">sylius</option>
<option name="message">sylius.translation.locale.unique</option>
</constraint>
<property name="name">
<constraint name="NotBlank">
<option name="message">sylius.product.name.not_blank</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Component\Product\Model\ProductVariantTranslation">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">
<value>locale</value>
<value>translatable</value>
</option>
<option name="groups">sylius</option>
<option name="message">sylius.translation.locale.unique</option>
</constraint>
<property name="locale">
<constraint name="NotBlank">
<option name="groups">sylius</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ sylius:
locale:
not_blank: Please enter the locale.
invalid: This value is not a valid locale.
unique: A translation for the {{ value }} locale code already exists.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Component\Promotion\Model\CatalogPromotionTranslation">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">
<value>locale</value>
<value>translatable</value>
</option>
<option name="groups">sylius</option>
<option name="message">sylius.translation.locale.unique</option>
</constraint>
<property name="locale">
<constraint name="NotBlank">
<option name="groups">sylius</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Component\Promotion\Model\PromotionTranslation">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">
<value>locale</value>
<value>translatable</value>
</option>
<option name="groups">sylius</option>
<option name="message">sylius.translation.locale.unique</option>
</constraint>
<property name="label">
<constraint name="Length">
<option name="max">255</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ sylius:
locale:
not_blank: Please enter the locale.
invalid: This value is not a valid locale.
unique: A translation for the {{ value }} locale code already exists.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Component\Shipping\Model\ShippingMethodTranslation">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">
<value>locale</value>
<value>translatable</value>
</option>
<option name="groups">sylius</option>
<option name="message">sylius.translation.locale.unique</option>
</constraint>
<property name="name">
<constraint name="NotBlank">
<option name="message">sylius.shipping_method.name.not_blank</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ sylius:
locale:
not_blank: 'Please enter the locale.'
invalid: 'This value is not a valid locale.'
unique: 'A translation for the {{ value }} locale code already exists.'
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Component\Taxonomy\Model\TaxonTranslation">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">
<value>locale</value>
<value>translatable</value>
</option>
<option name="groups">sylius</option>
<option name="message">sylius.translation.locale.unique</option>
</constraint>
<property name="name">
<constraint name="NotBlank">
<option name="message">sylius.taxon.name.not_blank</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ sylius:
locale:
not_blank: Please enter the locale.
invalid: This value is not a valid locale.
unique: A translation for the {{ value }} locale code already exists.
27 changes: 27 additions & 0 deletions tests/Api/Admin/CatalogPromotionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,33 @@ public function it_updates_catalog_promotion(): void
);
}

/** @test */
public function it_does_not_update_a_catalog_promotion_with_duplicate_locale_translation(): void
{
$catalogPromotion = $this->loadFixturesAndGetCatalogPromotion();
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);

$this->client->request(
method: 'PUT',
uri: sprintf('/api/v2/admin/catalog-promotions/%s', $catalogPromotion->getCode()),
server: $header,
content: json_encode([
'translations' => [
'en_US' => [
'slug' => 'caps/cap',
'name' => 'Cap',
]
],
], JSON_THROW_ON_ERROR),
);

$this->assertResponse(
$this->client->getResponse(),
'admin/catalog_promotion/put_catalog_promotion_with_duplicate_locale_translation',
Response::HTTP_UNPROCESSABLE_ENTITY,
);
}

private function loadFixturesAndGetCatalogPromotion(): CatalogPromotionInterface
{
$fixtures = $this->loadFixturesFromFiles([
Expand Down
35 changes: 35 additions & 0 deletions tests/Api/Admin/PaymentMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,39 @@ public function it_updates_a_payment_method(): void
Response::HTTP_OK,
);
}

/** @test */
public function it_does_not_update_a_payment_method_with_duplicate_locale_translation(): void
{
$fixtures = $this->loadFixturesFromFiles([
'authentication/api_administrator.yaml',
'channel.yaml',
'payment_method.yaml',
]);

$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);

/** @var PaymentMethodInterface $paymentMethod */
$paymentMethod = $fixtures['paypal_payment_method'];

$this->client->request(
method: 'PUT',
uri: sprintf('/api/v2/admin/payment-methods/%s', $paymentMethod->getCode()),
server: $header,
content: json_encode([
'translations' => [
'en_US' => [
'name' => 'Different name',
'description' => 'Different description',
],
],
], JSON_THROW_ON_ERROR),
);

$this->assertResponse(
$this->client->getResponse(),
'admin/payment_method/put_payment_method_with_duplicate_locale_translation',
Response::HTTP_UNPROCESSABLE_ENTITY,
);
}
}
29 changes: 29 additions & 0 deletions tests/Api/Admin/ProductAssociationTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,33 @@ public function it_updates_product_association_type(): void
Response::HTTP_OK,
);
}

/** @test */
public function it_does_not_update_a_product_association_type_with_duplicate_locale_translation(): void
{
$fixtures = $this->loadFixturesFromFiles(['product/product_with_many_locales.yaml', 'authentication/api_administrator.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);

/** @var ProductAssociationTypeInterface $associationType */
$associationType = $fixtures['product_association_type'];
$this->client->request(
method: 'PUT',
uri: sprintf('/api/v2/admin/product-association-types/%s', $associationType->getCode()),
server: $header,
content: json_encode([
'code' => 'TEST',
'translations' => [
'en_US' => [
'name' => 'Similar products',
],
]
], JSON_THROW_ON_ERROR),
);

$this->assertResponse(
$this->client->getResponse(),
'admin/product_association_type/put_product_association_type_with_duplicate_locale_translation',
Response::HTTP_UNPROCESSABLE_ENTITY,
);
}
}
Loading

0 comments on commit ff7ba12

Please sign in to comment.