Skip to content

Commit f675d31

Browse files
committed
MAGE-1044 Test sorting attribute update
1 parent 6ea433b commit f675d31

File tree

2 files changed

+85
-19
lines changed

2 files changed

+85
-19
lines changed

Helper/ConfigHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ public function getRawSortingValue(?int $storeId = null): string
11571157
* @param int|null $scopeId
11581158
* @return void
11591159
*/
1160-
public function setSorting(array $sorting, ?string $scope = null, ?int $scopeId = null): void
1160+
public function setSorting(array $sorting, string $scope = Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT, ?int $scopeId = null): void
11611161
{
11621162
$this->configWriter->save(
11631163
self::SORTING_INDICES,

Test/Integration/Product/ReplicaIndexingTest.php

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Algolia\AlgoliaSearch\Test\Integration\Product;
44

55
use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
6+
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
67
use Algolia\AlgoliaSearch\Model\Indexer\Product as ProductIndexer;
78
use Algolia\AlgoliaSearch\Model\IndicesConfigurator;
89
use Algolia\AlgoliaSearch\Test\Integration\IndexingTestCase;
@@ -16,16 +17,17 @@ class ReplicaIndexingTest extends IndexingTestCase
1617

1718
protected ?string $indexSuffix;
1819

20+
protected ?array $ogSortingState;
21+
1922
public function setUp(): void
2023
{
2124
parent::setUp();
2225
$this->productIndexer = $this->objectManager->get(ProductIndexer::class);
2326
$this->replicaManager = $this->objectManager->get(ReplicaManagerInterface::class);
24-
$this->indicesConfigurator = $this->getObjectManager()->get(IndicesConfigurator::class);
27+
$this->indicesConfigurator = $this->objectManager->get(IndicesConfigurator::class);
2528
$this->indexSuffix = 'products';
2629

27-
// Replicas will not get created if InstantSearch is not used
28-
$this->setConfig('algoliasearch_instant/instant/is_instant_enabled', 1);
30+
$this->ogSortingState = $this->configHelper->getSorting();
2931
}
3032

3133
protected function getIndexName(string $storeIndexPart): string
@@ -38,25 +40,40 @@ public function processFullReindexProducts(): void
3840
$this->processFullReindex($this->productIndexer, $this->indexSuffix);
3941
}
4042

41-
public function testReplicaConfig(): void
43+
protected function hasSortingAttribute($sortAttr, $sortDir): bool
4244
{
4345
$sorting = $this->configHelper->getSorting();
44-
$sortAttr = 'created_at';
45-
$sortDir = 'desc';
46-
47-
// Has created_at sort
48-
$this->assertTrue(
49-
(bool) array_filter(
50-
$sorting,
51-
function($sort) use ($sortAttr, $sortDir) {
52-
return $sort['attribute'] == $sortAttr
53-
&& $sort['sort'] == $sortDir;
54-
}
55-
)
46+
return (bool) array_filter(
47+
$sorting,
48+
function($sort) use ($sortAttr, $sortDir) {
49+
return $sort['attribute'] == $sortAttr
50+
&& $sort['sort'] == $sortDir;
51+
}
5652
);
53+
}
5754

58-
// Expected replica max
55+
protected function assertSortingAttribute($sortAttr, $sortDir): void
56+
{
57+
$this->assertTrue($this->hasSortingAttribute($sortAttr, $sortDir));
58+
}
59+
60+
protected function assertNoSortingAttribute($sortAttr, $sortDir): void
61+
{
62+
$this->assertTrue(!$this->hasSortingAttribute($sortAttr, $sortDir));
63+
}
64+
65+
public function testReplicaLimits() {
5966
$this->assertEquals(20, $this->replicaManager->getMaxVirtualReplicasPerIndex());
67+
}
68+
69+
/**
70+
* @magentoConfigFixture current_store algoliasearch_instant/instant/is_instant_enabled 1
71+
*/
72+
public function testStandardReplicaConfig(): void
73+
{
74+
$sortAttr = 'created_at';
75+
$sortDir = 'desc';
76+
$this->assertSortingAttribute($sortAttr, $sortDir);
6077

6178
$this->indicesConfigurator->saveConfigurationToAlgolia(1);
6279
$this->algoliaHelper->waitLastTask();
@@ -88,8 +105,57 @@ function($replica) use ($sortIndexName) {
88105

89106
}
90107

108+
/**
109+
* @magentoDbIsolation disabled
110+
*/
111+
public function testVirtualReplicaConfig(): void
112+
{
113+
$productHelper = $this->objectManager->get(ProductHelper::class);
114+
$sortAttr = 'color';
115+
$sortDir = 'asc';
116+
$attributes = $productHelper->getAllAttributes();
117+
$this->assertArrayHasKey($sortAttr, $attributes);
118+
119+
$this->assertNoSortingAttribute($sortAttr, $sortDir);
120+
121+
$sorting = $this->configHelper->getSorting();
122+
$sorting[] = [
123+
'attribute' => $sortAttr,
124+
'sort' => $sortDir,
125+
'sortLabel' => $sortAttr
126+
];
127+
$encoded = json_encode($sorting);
128+
// $this->setConfig('algoliasearch_instant/instant_sorts/sorts', $encoded);
129+
$this->configHelper->setSorting($sorting);
130+
131+
$connection = $this->objectManager->create(\Magento\Framework\App\ResourceConnection::class)
132+
->getConnection();
133+
// $connection->beginTransaction();
134+
// $this->objectManager->get(\Magento\Framework\App\Config\Storage\WriterInterface::class)->save(
135+
// \Algolia\AlgoliaSearch\Helper\ConfigHelper::SORTING_INDICES,
136+
// $encoded,
137+
// 'default'
138+
// );
139+
// $connection->commit();
140+
141+
142+
$select = $connection->select()
143+
->from('core_config_data', 'value')
144+
->where('path = ?', 'algoliasearch_instant/instant_sorts/sorts')
145+
->where('scope = ?', 'default')
146+
->where('scope_id = ?', 0);
147+
148+
$configValue = $connection->fetchOne($select);
149+
150+
// Assert that the correct value was written to the database
151+
$this->assertEquals($encoded, $configValue);
152+
153+
// $this->assertSortingAttribute($sortAttr, $sortDir);
154+
155+
}
156+
91157
public function tearDown(): void
92158
{
93-
$this->setConfig('algoliasearch_instant/instant/is_instant_enabled', 0);
159+
$this->configHelper->setSorting($this->ogSortingState);
94160
}
95161
}

0 commit comments

Comments
 (0)