3
3
namespace Algolia \AlgoliaSearch \Test \Integration \Product ;
4
4
5
5
use Algolia \AlgoliaSearch \Api \Product \ReplicaManagerInterface ;
6
+ use Algolia \AlgoliaSearch \Helper \Entity \ProductHelper ;
6
7
use Algolia \AlgoliaSearch \Model \Indexer \Product as ProductIndexer ;
7
8
use Algolia \AlgoliaSearch \Model \IndicesConfigurator ;
8
9
use Algolia \AlgoliaSearch \Test \Integration \IndexingTestCase ;
@@ -16,16 +17,17 @@ class ReplicaIndexingTest extends IndexingTestCase
16
17
17
18
protected ?string $ indexSuffix ;
18
19
20
+ protected ?array $ ogSortingState ;
21
+
19
22
public function setUp (): void
20
23
{
21
24
parent ::setUp ();
22
25
$ this ->productIndexer = $ this ->objectManager ->get (ProductIndexer::class);
23
26
$ this ->replicaManager = $ this ->objectManager ->get (ReplicaManagerInterface::class);
24
- $ this ->indicesConfigurator = $ this ->getObjectManager () ->get (IndicesConfigurator::class);
27
+ $ this ->indicesConfigurator = $ this ->objectManager ->get (IndicesConfigurator::class);
25
28
$ this ->indexSuffix = 'products ' ;
26
29
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 ();
29
31
}
30
32
31
33
protected function getIndexName (string $ storeIndexPart ): string
@@ -38,25 +40,40 @@ public function processFullReindexProducts(): void
38
40
$ this ->processFullReindex ($ this ->productIndexer , $ this ->indexSuffix );
39
41
}
40
42
41
- public function testReplicaConfig ( ): void
43
+ protected function hasSortingAttribute ( $ sortAttr , $ sortDir ): bool
42
44
{
43
45
$ 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
+ }
56
52
);
53
+ }
57
54
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 () {
59
66
$ 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 );
60
77
61
78
$ this ->indicesConfigurator ->saveConfigurationToAlgolia (1 );
62
79
$ this ->algoliaHelper ->waitLastTask ();
@@ -88,8 +105,57 @@ function($replica) use ($sortIndexName) {
88
105
89
106
}
90
107
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
+
91
157
public function tearDown (): void
92
158
{
93
- $ this ->setConfig ( ' algoliasearch_instant/instant/is_instant_enabled ' , 0 );
159
+ $ this ->configHelper -> setSorting ( $ this -> ogSortingState );
94
160
}
95
161
}
0 commit comments