|
| 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\CatalogGraphQl\Model\Config; |
| 9 | + |
| 10 | +use Magento\CatalogGraphQl\Model\Resolver\Products\Attributes\Collection as ProductsAttributesCollection; |
| 11 | +use Magento\Framework\Search\Request\Config as SearchRequestConfig; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | +use Magento\TestFramework\Helper\CacheCleaner; |
| 14 | + |
| 15 | +/** |
| 16 | + * @magentoDbIsolation disabled |
| 17 | + * @magentoAppArea graphql |
| 18 | + */ |
| 19 | +class ConfigTest extends \PHPUnit\Framework\TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var bool|null $isFixtureLoaded |
| 23 | + */ |
| 24 | + private ?bool $isFixtureLoaded = null; |
| 25 | + |
| 26 | + /** |
| 27 | + * @inheritDoc |
| 28 | + */ |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + $this->isFixtureLoaded = false; |
| 32 | + CacheCleaner::cleanAll(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritDoc |
| 37 | + */ |
| 38 | + protected function tearDown(): void |
| 39 | + { |
| 40 | + if ($this->isFixtureLoaded) { |
| 41 | + $rollBackPath = __DIR__ |
| 42 | + . '/../../../Catalog/_files/products_with_layered_navigation_attribute_store_options_rollback.php'; |
| 43 | + require $rollBackPath; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Test to confirm that we don't load cached configuration before attribute existed |
| 49 | + * |
| 50 | + * @covers \Magento\Framework\Search\Request\Config |
| 51 | + * @return void |
| 52 | + * @magentoApiDataFixture Magento/Store/_files/store.php |
| 53 | + */ |
| 54 | + public function testAttributesChangeCleansSearchRequestConfigCache(): void |
| 55 | + { |
| 56 | + $objectManager = Bootstrap::getObjectManager(); |
| 57 | + /** @var SearchRequestConfig $configInstance1 */ |
| 58 | + $configInstance1 = $objectManager->create(SearchRequestConfig::class); |
| 59 | + $aggregations1 = $configInstance1->get('graphql_product_search_with_aggregation/aggregations'); |
| 60 | + $this->assertArrayNotHasKey('test_configurable_bucket', $aggregations1); |
| 61 | + require __DIR__ . '/../../../Catalog/_files/products_with_layered_navigation_attribute_store_options.php'; |
| 62 | + $this->isFixtureLoaded = true; |
| 63 | + /** @var SearchRequestConfig $configInstance2 */ |
| 64 | + $configInstance2 = $objectManager->create(SearchRequestConfig::class); |
| 65 | + $aggregations2 = $configInstance2->get('graphql_product_search_with_aggregation/aggregations'); |
| 66 | + $this->assertArrayHasKey('test_configurable_bucket', $aggregations2); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Test to confirm that we don't load cached configuration before attribute existed |
| 71 | + * |
| 72 | + * @return void |
| 73 | + * @magentoApiDataFixture Magento/Store/_files/store.php |
| 74 | + */ |
| 75 | + public function testAttributesChangeCleansGraphQlConfigCache(): void |
| 76 | + { |
| 77 | + $objectManager = Bootstrap::getObjectManager(); |
| 78 | + $this->resetStateProductsAttributesCollection($objectManager); |
| 79 | + $configInstance1 = $objectManager->create('Magento\Framework\GraphQl\Config\Data'); |
| 80 | + $aggregations1 = $configInstance1->get('SimpleProduct/fields'); |
| 81 | + $this->assertArrayNotHasKey('test_configurable', $aggregations1); |
| 82 | + require __DIR__ . '/../../../Catalog/_files/products_with_layered_navigation_attribute_store_options.php'; |
| 83 | + $this->isFixtureLoaded = true; |
| 84 | + $this->resetStateProductsAttributesCollection($objectManager); |
| 85 | + $configInstance2 = $objectManager->create('Magento\Framework\GraphQl\Config\Data'); |
| 86 | + $aggregations2 = $configInstance2->get('SimpleProduct/fields'); |
| 87 | + $this->assertArrayHasKey('test_configurable', $aggregations2); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test to confirm that we don't load cached configuration before attribute existed |
| 92 | + * |
| 93 | + * @return void |
| 94 | + * @magentoApiDataFixture Magento/Store/_files/store.php |
| 95 | + * @magentoAppArea global |
| 96 | + */ |
| 97 | + public function testGraphQlConfigCacheShouldntBreakWhenLoadedByGlobalArea(): void |
| 98 | + { |
| 99 | + /* |
| 100 | + * When Magento\Framework\GraphQl\Config\Data is loaded from area outside of graphql, and its cache doesn't |
| 101 | + * currently exist, then it will load incorrect data and save it in its cache, which will then be used by |
| 102 | + * Magento\Framework\GraphQl\Config\Data when it actually is in the graphql area. |
| 103 | + * See AC-10465 for more details on this bug. |
| 104 | + */ |
| 105 | + $this->markTestSkipped('Fix this in AC-10465'); |
| 106 | + $this->testAttributesChangeCleansGraphQlConfigCache(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Magento\CatalogGraphQl\Model\Config\AttributeReader has mutable state in |
| 111 | + * Magento\CatalogGraphQl\Model\Resolver\Products\Attributes\Collection $collection, so we must reset it. |
| 112 | + * |
| 113 | + * @param $objectManager |
| 114 | + * @return void |
| 115 | + */ |
| 116 | + private function resetStateProductsAttributesCollection($objectManager) : void |
| 117 | + { |
| 118 | + /** @var ProductsAttributesCollection $productsAttributesCollection */ |
| 119 | + $productsAttributesCollection = $objectManager->get(ProductsAttributesCollection::class); |
| 120 | + $productsAttributesCollection->_resetState(); |
| 121 | + } |
| 122 | +} |
0 commit comments