Skip to content

Commit 3faa844

Browse files
authored
MAGE-1057: Fix/add product tests (#1626)
* MAGE-1057: fix specialPrice test * MAGE-1057: add new fixtures * MAGE-1057: add first product tests for multistore scenarios * MAGE-1057: add Rules test * MAGE-1057: fix flakiness when all tests are run at once * MAGE-1057: changes after review * MAGE-1057: handle EE and CE expected values * MAGE-1057: merge fix * MAGE-1057: fix rule creation flakiness * MAGE-1057: make sure to use AlgoliaHelper when managing Rules to be sure to use waitForLastTask() efficiently * MAGE-1057: remove unused client instanciation
1 parent dc21e43 commit 3faa844

17 files changed

+565
-110
lines changed

Helper/AlgoliaHelper.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Algolia\AlgoliaSearch\Configuration\SearchConfig;
77
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
88
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
9+
use Algolia\AlgoliaSearch\Model\Search\SearchRulesResponse;
910
use Algolia\AlgoliaSearch\Response\AbstractResponse;
1011
use Algolia\AlgoliaSearch\Response\BatchIndexingResponse;
1112
use Algolia\AlgoliaSearch\Response\MultiResponse;
@@ -453,6 +454,19 @@ public function saveRule(array $rule, string $indexName, bool $forwardToReplicas
453454
self::setLastOperationInfo($indexName, $res);
454455
}
455456

457+
/**
458+
* @param string $indexName
459+
* @param array $rules
460+
* @param bool $forwardToReplicas
461+
* @return void
462+
*/
463+
public function saveRules(string $indexName, array $rules, bool $forwardToReplicas = false): void
464+
{
465+
$res = $this->client->saveRules($indexName, $rules, $forwardToReplicas);
466+
467+
self::setLastOperationInfo($indexName, $res);
468+
}
469+
456470

457471
/**
458472
* @param string $indexName
@@ -521,6 +535,21 @@ public function copyQueryRules(string $fromIndexName, string $toIndexName): void
521535
self::setLastOperationInfo($fromIndexName, $response);
522536
}
523537

538+
/**
539+
* @param string $indexName
540+
* @param array|null $searchRulesParams
541+
*
542+
* @return SearchRulesResponse|mixed[]
543+
*
544+
* @throws AlgoliaException
545+
*/
546+
public function searchRules(string $indexName, array$searchRulesParams = null)
547+
{
548+
$this->checkClient(__FUNCTION__);
549+
550+
return $this->client->searchRules($indexName, $searchRulesParams);
551+
}
552+
524553
/**
525554
* @param $methodName
526555
* @return void

Helper/Entity/ProductHelper.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,12 @@ public function setSettings(string $indexName, string $indexNameTmp, int $storeI
359359
$this->logger->log('Pushing the same settings to TMP index as well');
360360
}
361361

362-
$this->setFacetsQueryRules($indexName);
362+
$this->setFacetsQueryRules($indexName, $storeId);
363+
$this->algoliaHelper->waitLastTask();
364+
363365
if ($saveToTmpIndicesToo) {
364-
$this->setFacetsQueryRules($indexNameTmp);
366+
$this->setFacetsQueryRules($indexNameTmp, $storeId);
367+
$this->algoliaHelper->waitLastTask();
365368
}
366369

367370
$this->replicaManager->syncReplicasToAlgolia($storeId, $indexSettings);
@@ -1204,17 +1207,16 @@ protected function getAttributesForFaceting($storeId)
12041207

12051208
/**
12061209
* @param $indexName
1210+
* @param $storeId
12071211
* @return void
12081212
* @throws AlgoliaException
12091213
*/
1210-
protected function setFacetsQueryRules($indexName)
1214+
protected function setFacetsQueryRules($indexName, $storeId = null)
12111215
{
1212-
$client = $this->algoliaHelper->getClient();
1213-
12141216
$this->clearFacetsQueryRules($indexName);
12151217

12161218
$rules = [];
1217-
$facets = $this->configHelper->getFacets();
1219+
$facets = $this->configHelper->getFacets($storeId);
12181220
foreach ($facets as $facet) {
12191221
if (!array_key_exists('create_rule', $facet) || $facet['create_rule'] !== '1') {
12201222
continue;
@@ -1245,7 +1247,7 @@ protected function setFacetsQueryRules($indexName)
12451247

12461248
if ($rules) {
12471249
$this->logger->log('Setting facets query rules to "' . $indexName . '" index: ' . json_encode($rules));
1248-
$client->saveRules($indexName, $rules, true);
1250+
$this->algoliaHelper->saveRules($indexName, $rules, true);
12491251
}
12501252
}
12511253

@@ -1260,8 +1262,7 @@ protected function clearFacetsQueryRules($indexName): void
12601262
$hitsPerPage = 100;
12611263
$page = 0;
12621264
do {
1263-
$client = $this->algoliaHelper->getClient();
1264-
$fetchedQueryRules = $client->searchRules($indexName, [
1265+
$fetchedQueryRules = $this->algoliaHelper->searchRules($indexName, [
12651266
'context' => 'magento_filters',
12661267
'page' => $page,
12671268
'hitsPerPage' => $hitsPerPage,
@@ -1273,7 +1274,7 @@ protected function clearFacetsQueryRules($indexName): void
12731274
}
12741275

12751276
foreach ($fetchedQueryRules['hits'] as $hit) {
1276-
$client->deleteRule($indexName, $hit[AlgoliaHelper::ALGOLIA_API_OBJECT_ID], true);
1277+
$this->algoliaHelper->deleteRule($indexName, $hit[AlgoliaHelper::ALGOLIA_API_OBJECT_ID], true);
12771278
}
12781279

12791280
$page++;

Helper/Logger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function isEnable()
3535

3636
public function getStoreName($storeId)
3737
{
38-
if ($storeId === null) {
38+
if ($storeId === null || !isset($this->stores[$storeId])) {
3939
return 'undefined store';
4040
}
4141

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Integration\AssertValues;
4+
5+
class Magento246CE extends Magento24CE
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Integration\AssertValues;
4+
5+
class Magento246EE extends Magento24EE
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Integration\AssertValues;
4+
5+
class Magento247CE extends Magento24CE
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Integration\AssertValues;
4+
5+
class Magento247EE extends Magento24EE
6+
{
7+
8+
}

Test/Integration/AssertValues/Magento246.php renamed to Test/Integration/AssertValues/Magento24CE.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Algolia\AlgoliaSearch\Test\Integration\AssertValues;
44

5-
class Magento246
5+
abstract class Magento24CE
66
{
77
public $productsOnStockCount = 180;
88
public $productsOutOfStockCount = 181;
9+
public $productsCountWithoutGiftcards = 181;
910
public $lastJobDataSize = 13;
1011
public $expectedCategory = 16;
1112
public $attributesForFaceting = 5;

Test/Integration/AssertValues/Magento247.php renamed to Test/Integration/AssertValues/Magento24EE.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Algolia\AlgoliaSearch\Test\Integration\AssertValues;
44

5-
class Magento247
5+
abstract class Magento24EE
66
{
77
public $productsOnStockCount = 180;
88
public $productsOutOfStockCount = 183;
9+
public $productsCountWithoutGiftcards = 181;
910
public $lastJobDataSize = 13;
1011
public $expectedCategory = 17;
1112
public $attributesForFaceting = 5;

Test/Integration/Config/MultiStoreConfigTest.php

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public function testMultiStoreIndicesCreation()
1919
$stores = $this->storeManager->getStores();
2020

2121
// Check that stores and websites are properly created
22-
$this->assertEquals(count($websites), 2);
23-
$this->assertEquals(count($stores), 3);
22+
$this->assertEquals(2, count($websites));
23+
$this->assertEquals(3, count($stores));
2424

2525
foreach ($stores as $store) {
26-
$this->setupStore($store);
26+
$this->setupStore($store, true);
2727
}
2828

2929
$indicesCreatedByTest = 0;
@@ -37,9 +37,8 @@ public function testMultiStoreIndicesCreation()
3737
}
3838
}
3939

40-
// Check that the configuration created the appropriate number of indices (4 per store => 3*4=12)
41-
$this->assertEquals($indicesCreatedByTest, 12);
42-
40+
// Check that the configuration created the appropriate number of indices (7 (4 mains + 3 replicas per store => 3*7=21)
41+
$this->assertEquals(21, $indicesCreatedByTest);
4342

4443
$defaultStore = $this->storeRepository->get('default');
4544
$fixtureSecondStore = $this->storeRepository->get('fixture_second_store');
@@ -73,17 +72,48 @@ public function testMultiStoreIndicesCreation()
7372
$fixtureSecondStore->getCode())
7473
;
7574

75+
// Query rules check (activate one QR on the fixture store)
76+
$facetsFromConfig = $this->configHelper->getFacets($defaultStore->getId());
77+
$facetsFromConfigAlt = $facetsFromConfig;
78+
foreach ($facetsFromConfigAlt as $key => $facet) {
79+
if ($facet['attribute'] === "color") {
80+
$facetsFromConfigAlt[$key]['create_rule'] = "1";
81+
break;
82+
}
83+
}
84+
85+
$this->setConfig(
86+
ConfigHelper::FACETS,
87+
json_encode($facetsFromConfigAlt),
88+
$fixtureSecondStore->getCode()
89+
);
90+
7691
$this->indicesConfigurator->saveConfigurationToAlgolia($fixtureSecondStore->getId());
92+
$this->algoliaHelper->waitLastTask();
7793

78-
$defaultIndexSettings = $this->algoliaHelper->getSettings($this->indexPrefix . 'default_categories');
79-
$fixtureIndexSettings = $this->algoliaHelper->getSettings($this->indexPrefix . 'fixture_second_store_categories');
94+
$defaultCategoryIndexSettings = $this->algoliaHelper->getSettings($this->indexPrefix . 'default_categories');
95+
$fixtureCategoryIndexSettings = $this->algoliaHelper->getSettings($this->indexPrefix . 'fixture_second_store_categories');
8096

8197
$attributeFromConfig = 'unordered(' . self::ADDITIONAL_ATTRIBUTE . ')';
82-
$this->assertNotContains($attributeFromConfig, $defaultIndexSettings['searchableAttributes']);
83-
$this->assertContains($attributeFromConfig, $fixtureIndexSettings['searchableAttributes']);
98+
$this->assertNotContains($attributeFromConfig, $defaultCategoryIndexSettings['searchableAttributes']);
99+
$this->assertContains($attributeFromConfig, $fixtureCategoryIndexSettings['searchableAttributes']);
84100

85101
$rankingFromConfig = 'desc(' . self::ADDITIONAL_ATTRIBUTE . ')';
86-
$this->assertNotContains($rankingFromConfig, $defaultIndexSettings['customRanking']);
87-
$this->assertContains($rankingFromConfig, $fixtureIndexSettings['customRanking']);
102+
$this->assertNotContains($rankingFromConfig, $defaultCategoryIndexSettings['customRanking']);
103+
$this->assertContains($rankingFromConfig, $fixtureCategoryIndexSettings['customRanking']);
104+
105+
$defaultProductIndexRules = $this->algoliaHelper->searchRules($this->indexPrefix . 'default_products');
106+
$fixtureProductIndexRules = $this->algoliaHelper->searchRules($this->indexPrefix . 'fixture_second_store_products');
107+
108+
// Check that the Rule has only been created for the fixture store
109+
$this->assertEquals(0, $defaultProductIndexRules['nbHits']);
110+
$this->assertEquals(1, $fixtureProductIndexRules['nbHits']);
111+
}
112+
113+
public function tearDown(): void
114+
{
115+
parent::tearDown();
116+
117+
$this->setConfig(ConfigHelper::IS_INSTANT_ENABLED, 0);
88118
}
89119
}

0 commit comments

Comments
 (0)