|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Test\Integration\Product; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface; |
| 6 | +use Algolia\AlgoliaSearch\Helper\ConfigHelper; |
| 7 | +use Algolia\AlgoliaSearch\Test\Integration\MultiStoreTestCase; |
| 8 | +use Magento\Framework\Serialize\SerializerInterface; |
| 9 | +use Magento\Store\Api\Data\StoreInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php |
| 13 | + */ |
| 14 | +class MultiStoreReplicaTest extends MultiStoreTestCase |
| 15 | +{ |
| 16 | + use ReplicaAssertionsTrait; |
| 17 | + |
| 18 | + const COLOR_ATTR = 'color'; |
| 19 | + const CREATED_AT_ATTR = 'created_at'; |
| 20 | + const ASC_DIR = 'asc'; |
| 21 | + const DESC_DIR = 'desc'; |
| 22 | + |
| 23 | + protected ?ReplicaManagerInterface $replicaManager = null; |
| 24 | + |
| 25 | + protected ?SerializerInterface $serializer = null; |
| 26 | + |
| 27 | + protected function setUp(): void |
| 28 | + { |
| 29 | + parent::setUp(); |
| 30 | + $this->replicaManager = $this->objectManager->get(ReplicaManagerInterface::class); |
| 31 | + $this->serializer = $this->objectManager->get(SerializerInterface::class); |
| 32 | + |
| 33 | + $stores = $this->storeManager->getStores(); |
| 34 | + |
| 35 | + foreach ($stores as $store) { |
| 36 | + $this->setupStore($store, true); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public function testMultiStoreReplicaConfig() |
| 41 | + { |
| 42 | + $defaultStore = $this->storeRepository->get('default'); |
| 43 | + $fixtureSecondStore = $this->storeRepository->get('fixture_second_store'); |
| 44 | + |
| 45 | + // Check replica config for created_at desc |
| 46 | + $this->checkReplicaIsStandard($defaultStore, self::CREATED_AT_ATTR, self::DESC_DIR); |
| 47 | + $this->checkReplicaIsStandard($fixtureSecondStore, self::CREATED_AT_ATTR, self::DESC_DIR); |
| 48 | + |
| 49 | + // add color asc sorting (virtual on a single store) |
| 50 | + $this->addSortingByStore($defaultStore, self::COLOR_ATTR, self::ASC_DIR); |
| 51 | + $this->addSortingByStore($fixtureSecondStore, self::COLOR_ATTR, self::ASC_DIR,true); |
| 52 | + |
| 53 | + // Check replica config for color asc |
| 54 | + $this->checkReplicaIsStandard($defaultStore, self::COLOR_ATTR, self::ASC_DIR); |
| 55 | + $this->checkReplicaIsVirtual($fixtureSecondStore, self::COLOR_ATTR, self::ASC_DIR); |
| 56 | + } |
| 57 | + |
| 58 | + protected function checkReplicaIsStandard(StoreInterface $store, $sortAttr, $sortDir) |
| 59 | + { |
| 60 | + $this->checkReplicaConfigByStore($store, $sortAttr, $sortDir, 'standard'); |
| 61 | + } |
| 62 | + |
| 63 | + protected function checkReplicaIsVirtual(StoreInterface $store, $sortAttr, $sortDir) |
| 64 | + { |
| 65 | + $this->checkReplicaConfigByStore($store, $sortAttr, $sortDir, 'virtual'); |
| 66 | + } |
| 67 | + |
| 68 | + protected function checkReplicaConfigByStore(StoreInterface $store, $sortAttr, $sortDir, $type) |
| 69 | + { |
| 70 | + $indexName = $this->indexPrefix . $store->getCode() . '_products'; |
| 71 | + |
| 72 | + $settings = $this->algoliaHelper->getSettings($indexName, $store->getId()); |
| 73 | + |
| 74 | + $this->assertArrayHasKey('replicas', $settings); |
| 75 | + |
| 76 | + $replicaIndexName = $indexName . '_' . $sortAttr . '_' . $sortDir; |
| 77 | + |
| 78 | + $type === 'virtual' ? |
| 79 | + $this->assertTrue($this->isVirtualReplica($settings['replicas'], $replicaIndexName)) : |
| 80 | + $this->assertTrue($this->isStandardReplica($settings['replicas'], $replicaIndexName)); |
| 81 | + |
| 82 | + $replicaSettings = $this->assertReplicaIndexExists($indexName, $replicaIndexName, $store->getId()); |
| 83 | + |
| 84 | + $type === 'virtual' ? |
| 85 | + $this->assertVirtualReplicaRanking($replicaSettings, "$sortDir($sortAttr)"): |
| 86 | + $this->assertStandardReplicaRanking($replicaSettings, "$sortDir($sortAttr)"); |
| 87 | + } |
| 88 | + |
| 89 | + protected function addSortingByStore(StoreInterface $store, $attr, $dir, $isVirtual = false) |
| 90 | + { |
| 91 | + $sorting = $this->configHelper->getSorting($store->getId()); |
| 92 | + $newSorting = [ |
| 93 | + 'attribute' => $attr, |
| 94 | + 'sort' => $dir, |
| 95 | + 'sortLabel' => $attr, |
| 96 | + ]; |
| 97 | + |
| 98 | + if ($isVirtual) { |
| 99 | + $newSorting['virtualReplica'] = 1; |
| 100 | + } |
| 101 | + |
| 102 | + $sorting[] = $newSorting; |
| 103 | + |
| 104 | + $this->setConfig( |
| 105 | + ConfigHelper::SORTING_INDICES, |
| 106 | + $this->serializer->serialize($sorting), |
| 107 | + $store->getCode() |
| 108 | + ); |
| 109 | + |
| 110 | + $this->assertSortingAttribute($attr, $dir); |
| 111 | + $this->indicesConfigurator->saveConfigurationToAlgolia($store->getId()); |
| 112 | + $this->algoliaHelper->waitLastTask($store->getId()); |
| 113 | + } |
| 114 | + |
| 115 | + protected function tearDown(): void |
| 116 | + { |
| 117 | + $stores = $this->storeManager->getStores(); |
| 118 | + |
| 119 | + foreach ($stores as $store) { |
| 120 | + $this->setConfig( |
| 121 | + ConfigHelper::SORTING_INDICES, |
| 122 | + [ |
| 123 | + [ |
| 124 | + 'attribute' => 'price', |
| 125 | + 'sort' => 'asc', |
| 126 | + 'sortLabel' => 'Lowest Price' |
| 127 | + ], |
| 128 | + [ |
| 129 | + 'attribute' => 'price', |
| 130 | + 'sort' => 'desc', |
| 131 | + 'sortLabel' => 'Highest Price' |
| 132 | + ], |
| 133 | + [ |
| 134 | + 'attribute' => 'created_at', |
| 135 | + 'sort' => 'desc', |
| 136 | + 'sortLabel' => 'Newest first' |
| 137 | + ] |
| 138 | + ], |
| 139 | + $store->getCode() |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + parent::tearDown(); |
| 144 | + } |
| 145 | +} |
0 commit comments