Skip to content

Commit bf02891

Browse files
committed
MAGE-1085: Add customer group replicas test
1 parent 0834fc2 commit bf02891

File tree

3 files changed

+70
-24
lines changed

3 files changed

+70
-24
lines changed

Test/Integration/Config/MultiStoreConfigTest.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
66
use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
77
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
8+
use Algolia\AlgoliaSearch\Test\Integration\Config\Traits\ConfigAssertionsTrait;
89
use Algolia\AlgoliaSearch\Test\Integration\MultiStoreTestCase;
910

1011
/**
1112
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
1213
*/
1314
class MultiStoreConfigTest extends MultiStoreTestCase
1415
{
16+
use ConfigAssertionsTrait;
1517

1618
const ADDITIONAL_ATTRIBUTE = 'additional_attribute';
1719

@@ -30,11 +32,13 @@ public function testMultiStoreIndicesCreation()
3032

3133
$defaultStore = $this->storeRepository->get('default');
3234
$fixtureSecondStore = $this->storeRepository->get('fixture_second_store');
35+
$fixtureThirdStore = $this->storeRepository->get('fixture_third_store');
3336

3437
$indicesCreatedByTest = 0;
3538

36-
$indicesCreatedByTest += $this->countStoreIndices($defaultStore->getId());
37-
$indicesCreatedByTest += $this->countStoreIndices($fixtureSecondStore->getId());
39+
$indicesCreatedByTest += $this->countStoreIndices($defaultStore);
40+
$indicesCreatedByTest += $this->countStoreIndices($fixtureSecondStore);
41+
$indicesCreatedByTest += $this->countStoreIndices($fixtureThirdStore);
3842

3943
// Check that the configuration created the appropriate number of indices (7 (4 mains + 3 replicas per store => 3*7=21)
4044
$this->assertEquals(21, $indicesCreatedByTest);
@@ -121,28 +125,6 @@ public function testMultiStoreIndicesCreation()
121125
$this->assertEquals(1, $fixtureProductIndexRules['nbHits']);
122126
}
123127

124-
/**
125-
* @param int|null $storeId
126-
* @return int
127-
* @throws AlgoliaException
128-
*/
129-
protected function countStoreIndices(int $storeId = null): int
130-
{
131-
$indices = $this->algoliaHelper->listIndexes($storeId);
132-
133-
$indicesCreatedByTest = 0;
134-
135-
foreach ($indices['items'] as $index) {
136-
$name = $index['name'];
137-
138-
if (mb_strpos($name, $this->indexPrefix) === 0) {
139-
$indicesCreatedByTest++;
140-
}
141-
}
142-
143-
return $indicesCreatedByTest;
144-
}
145-
146128
protected function tearDown(): void
147129
{
148130
parent::tearDown();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Integration\Config\Traits;
4+
5+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
6+
use Magento\Store\Api\Data\StoreInterface;
7+
8+
trait ConfigAssertionsTrait
9+
{
10+
/**
11+
* @param StoreInterface|null $store
12+
* @return int
13+
* @throws AlgoliaException
14+
*/
15+
protected function countStoreIndices(StoreInterface $store = null): int
16+
{
17+
$indices = $this->algoliaHelper->listIndexes($store->getId());
18+
19+
$indicesCreatedByTest = 0;
20+
21+
foreach ($indices['items'] as $index) {
22+
$name = $index['name'];
23+
24+
if (mb_strpos($name, $this->indexPrefix . $store->getCode()) === 0) {
25+
$indicesCreatedByTest++;
26+
}
27+
}
28+
29+
return $indicesCreatedByTest;
30+
}
31+
}

Test/Integration/Product/MultiStoreReplicaTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Algolia\AlgoliaSearch\Console\Command\ReplicaSyncCommand;
88
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
99
use Algolia\AlgoliaSearch\Service\Product\SortingTransformer;
10+
use Algolia\AlgoliaSearch\Test\Integration\Config\Traits\ConfigAssertionsTrait;
1011
use Algolia\AlgoliaSearch\Test\Integration\MultiStoreTestCase;
1112
use Algolia\AlgoliaSearch\Test\Integration\Product\Traits\ReplicaAssertionsTrait;
1213
use Magento\Framework\Serialize\SerializerInterface;
@@ -20,6 +21,7 @@
2021
class MultiStoreReplicaTest extends MultiStoreTestCase
2122
{
2223
use ReplicaAssertionsTrait;
24+
use ConfigAssertionsTrait;
2325

2426
const COLOR_ATTR = 'color';
2527
const CREATED_AT_ATTR = 'created_at';
@@ -63,6 +65,35 @@ public function testMultiStoreReplicaConfig()
6365
$this->resetAllSortings();
6466
}
6567

68+
public function testCustomerGroupsConfig()
69+
{
70+
$defaultStore = $this->storeRepository->get('default');
71+
$fixtureSecondStore = $this->storeRepository->get('fixture_second_store');
72+
$fixtureThirdStore = $this->storeRepository->get('fixture_third_store');
73+
74+
// Enable customer groups for second fixture store and save configuration
75+
$this->setConfig( ConfigHelper::CUSTOMER_GROUPS_ENABLE, 1, $fixtureSecondStore->getCode());
76+
$this->indicesConfigurator->saveConfigurationToAlgolia($fixtureSecondStore->getId());
77+
$this->algoliaHelper->waitLastTask($fixtureSecondStore->getId());
78+
79+
// 7 indices for default store and third store:
80+
// - 1 for categories
81+
// - 1 for pages
82+
// - 1 for suggestions
83+
// - 4 for products (1 main and 3 for sorting replicas)
84+
$this->assertEquals(7, $this->countStoreIndices($defaultStore));
85+
$this->assertEquals(7, $this->countStoreIndices($fixtureThirdStore));
86+
87+
// 13 indices for second store:
88+
// - 1 for categories
89+
// - 1 for pages
90+
// - 1 for suggestions
91+
// - 10 for products (1 main and 9 replicas (2 prices sortings * 4 customer groups + 1 other sorting (created_at))
92+
$this->assertEquals(13, $this->countStoreIndices($fixtureSecondStore));
93+
94+
$this->resetAllSortings();
95+
}
96+
6697
public function testReplicaCommands()
6798
{
6899
$defaultStore = $this->storeRepository->get('default');
@@ -218,6 +249,8 @@ protected function resetAllSortings()
218249
]),
219250
$store->getCode()
220251
);
252+
253+
$this->setConfig( ConfigHelper::CUSTOMER_GROUPS_ENABLE, 0, $store->getCode());
221254
}
222255
}
223256

0 commit comments

Comments
 (0)