Skip to content

Commit 9c92395

Browse files
committed
MAGE-1084: add replica config change tests
1 parent bc47132 commit 9c92395

File tree

3 files changed

+260
-107
lines changed

3 files changed

+260
-107
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Integration\Product;
4+
5+
trait ReplicaAssertionsTrait
6+
{
7+
protected function assertSortToReplicaConfigParity(string $primaryIndexName, array $sorting, array $replicas): void
8+
{
9+
foreach ($sorting as $sortAttr) {
10+
$replicaIndexName = $sortAttr['name'];
11+
$isVirtual = array_key_exists('virtualReplica', $sortAttr) && $sortAttr['virtualReplica'];
12+
$needle = $isVirtual
13+
? "virtual($replicaIndexName)"
14+
: $replicaIndexName;
15+
$this->assertContains($needle, $replicas);
16+
17+
$replicaSettings = $this->assertReplicaIndexExists($primaryIndexName, $replicaIndexName);
18+
$sort = reset($sortAttr['ranking']);
19+
if ($isVirtual) {
20+
$this->assertVirtualReplicaRanking($replicaSettings, $sort);
21+
} else {
22+
$this->assertStandardReplicaRanking($replicaSettings, $sort);
23+
}
24+
}
25+
}
26+
27+
protected function assertReplicaIndexExists(
28+
string $primaryIndexName,
29+
string $replicaIndexName,
30+
?int $storeId = null
31+
): array
32+
{
33+
$replicaSettings = $this->algoliaHelper->getSettings($replicaIndexName, $storeId);
34+
$this->assertArrayHasKey('primary', $replicaSettings);
35+
$this->assertEquals($primaryIndexName, $replicaSettings['primary']);
36+
return $replicaSettings;
37+
}
38+
39+
protected function assertReplicaRanking(array $replicaSettings, string $rankingKey, string $sort) {
40+
$this->assertArrayHasKey($rankingKey, $replicaSettings);
41+
$this->assertEquals($sort, reset($replicaSettings[$rankingKey]));
42+
}
43+
44+
protected function assertStandardReplicaRanking(array $replicaSettings, string $sort): void
45+
{
46+
$this->assertReplicaRanking($replicaSettings, 'ranking', $sort);
47+
}
48+
49+
protected function assertVirtualReplicaRanking(array $replicaSettings, string $sort): void
50+
{
51+
$this->assertReplicaRanking($replicaSettings, 'customRanking', $sort);
52+
}
53+
54+
protected function assertStandardReplicaRankingOld(array $replicaSettings, string $sortAttr, string $sortDir): void
55+
{
56+
$this->assertArrayHasKey('ranking', $replicaSettings);
57+
$this->assertEquals("$sortDir($sortAttr)", array_shift($replicaSettings['ranking']));
58+
}
59+
60+
protected function assertVirtualReplicaRankingOld(array $replicaSettings, string $sortAttr, string $sortDir): void
61+
{
62+
$this->assertArrayHasKey('customRanking', $replicaSettings);
63+
$this->assertEquals("$sortDir($sortAttr)", array_shift($replicaSettings['customRanking']));
64+
}
65+
66+
/**
67+
* @param string[] $replicaSetting
68+
* @param string $replicaIndexName
69+
* @return bool
70+
*/
71+
protected function isVirtualReplica(array $replicaSetting, string $replicaIndexName): bool
72+
{
73+
return (bool) array_filter(
74+
$replicaSetting,
75+
function ($replica) use ($replicaIndexName) {
76+
return str_contains($replica, "virtual($replicaIndexName)");
77+
}
78+
);
79+
}
80+
81+
protected function isStandardReplica(array $replicaSetting, string $replicaIndexName): bool
82+
{
83+
return (bool) array_filter(
84+
$replicaSetting,
85+
function ($replica) use ($replicaIndexName) {
86+
$regex = '/^' . preg_quote($replicaIndexName) . '$/';
87+
return preg_match($regex, $replica);
88+
}
89+
);
90+
}
91+
92+
protected function hasSortingAttribute($sortAttr, $sortDir): bool
93+
{
94+
$sorting = $this->configHelper->getSorting();
95+
return (bool) array_filter(
96+
$sorting,
97+
function($sort) use ($sortAttr, $sortDir) {
98+
return $sort['attribute'] == $sortAttr
99+
&& $sort['sort'] == $sortDir;
100+
}
101+
);
102+
}
103+
104+
protected function assertSortingAttribute($sortAttr, $sortDir): void
105+
{
106+
$this->assertTrue($this->hasSortingAttribute($sortAttr, $sortDir));
107+
}
108+
109+
protected function assertNoSortingAttribute($sortAttr, $sortDir): void
110+
{
111+
$this->assertFalse($this->hasSortingAttribute($sortAttr, $sortDir));
112+
}
113+
}

0 commit comments

Comments
 (0)