|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\CatalogGraphQl; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductAttributeRepositoryInterface; |
| 11 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 12 | +use Magento\Eav\Model\Entity\Attribute\FrontendLabel; |
| 13 | +use Magento\Framework\ObjectManagerInterface; |
| 14 | +use Magento\Store\Test\Fixture\Store as StoreFixture; |
| 15 | +use Magento\TestFramework\Fixture\DataFixture; |
| 16 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 17 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 20 | + |
| 21 | +/** |
| 22 | + * Test class to verify the translated price attribute option label based on the store view. |
| 23 | + */ |
| 24 | +class PriceAttributeOptionsLabelTranslateTest extends GraphQlAbstract |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var ObjectManagerInterface |
| 28 | + */ |
| 29 | + private $objectManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var DataFixtureStorage |
| 33 | + */ |
| 34 | + private $fixture; |
| 35 | + |
| 36 | + /** |
| 37 | + * Setup |
| 38 | + */ |
| 39 | + protected function setUp(): void |
| 40 | + { |
| 41 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 42 | + $this->fixture = DataFixtureStorageManager::getStorage(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 47 | + */ |
| 48 | + #[ |
| 49 | + DataFixture( |
| 50 | + StoreFixture::class, |
| 51 | + [ |
| 52 | + 'code' => 'view2', |
| 53 | + 'name' => 'view2' |
| 54 | + ], |
| 55 | + as: 'view2' |
| 56 | + ), |
| 57 | + DataFixture( |
| 58 | + ProductFixture::class, |
| 59 | + [ |
| 60 | + 'sku' => 'simple' |
| 61 | + ], |
| 62 | + as: 'product' |
| 63 | + ), |
| 64 | + ] |
| 65 | + |
| 66 | + public function testValidatePriceAttributeOptionsLabelTranslationForSecondStoreView(): void |
| 67 | + { |
| 68 | + $attributeCode = 'price'; |
| 69 | + $secondStoreViewFixtureName = 'view2'; |
| 70 | + $attributeStoreFrontLabelForSecondStoreView = 'Price View2'; |
| 71 | + |
| 72 | + //Updating price attribute storefront option label for the second store view. |
| 73 | + $attributeRepository = $this->objectManager->create(ProductAttributeRepositoryInterface::class); |
| 74 | + |
| 75 | + $priceAttribute = $attributeRepository->get($attributeCode); |
| 76 | + |
| 77 | + $frontendLabelAttribute = $this->objectManager->get(FrontendLabel::class); |
| 78 | + $frontendLabelAttribute->setStoreId( |
| 79 | + $this->fixture->get($secondStoreViewFixtureName)->getId() |
| 80 | + ); |
| 81 | + $frontendLabelAttribute->setLabel($attributeStoreFrontLabelForSecondStoreView); |
| 82 | + |
| 83 | + $frontendLabels = $priceAttribute->getFrontendLabels(); |
| 84 | + $frontendLabels[] = $frontendLabelAttribute; |
| 85 | + |
| 86 | + $priceAttribute->setFrontendLabels($frontendLabels); |
| 87 | + $attributeRepository->save($priceAttribute); |
| 88 | + |
| 89 | + $query = $this->getProductsQueryWithAggregations(); |
| 90 | + $headers = ['Store' => $secondStoreViewFixtureName]; |
| 91 | + $response = $this->graphQlQuery($query, [], '', $headers); |
| 92 | + $this->assertNotEmpty($response['products']['aggregations']); |
| 93 | + $aggregationAttributes = $response['products']['aggregations']; |
| 94 | + |
| 95 | + foreach ($aggregationAttributes as $attribute) { |
| 96 | + if ($attribute['attribute_code'] === $attributeCode) { |
| 97 | + $priceAttributeOptionLabel = $attribute['label']; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + $this->assertEquals($priceAttributeOptionLabel, 'Price View2'); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Prepare products query with aggregations |
| 106 | + * |
| 107 | + * @return string |
| 108 | + */ |
| 109 | + private function getProductsQueryWithAggregations() : string |
| 110 | + { |
| 111 | + return <<<QUERY |
| 112 | +{ |
| 113 | + products( |
| 114 | + currentPage: 1, |
| 115 | + pageSize:12, |
| 116 | + filter: { |
| 117 | + sku: { |
| 118 | + eq: "simple" |
| 119 | + } |
| 120 | + }, sort: { |
| 121 | + price: ASC |
| 122 | + }) { |
| 123 | +
|
| 124 | + aggregations { |
| 125 | + options { |
| 126 | + count, |
| 127 | + label, |
| 128 | + value |
| 129 | + }, attribute_code, count, label |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | +QUERY; |
| 134 | + } |
| 135 | +} |
0 commit comments