7
7
namespace Magento \GroupedProduct \Api ;
8
8
9
9
use Magento \TestFramework \Helper \Bootstrap ;
10
+ use Magento \Indexer \Model \Config ;
11
+ use Magento \Framework \Indexer \IndexerRegistry ;
12
+ use Magento \Framework \Webapi \Rest \Request ;
10
13
11
14
class ProductLinkRepositoryTest extends \Magento \TestFramework \TestCase \WebapiAbstract
12
15
{
13
16
const SERVICE_NAME = 'catalogProductLinkRepositoryV1 ' ;
14
17
const SERVICE_VERSION = 'V1 ' ;
15
18
const RESOURCE_PATH = '/V1/products/ ' ;
19
+ const SERVICE_NAME_SEARCH = 'searchV1 ' ;
20
+ const RESOURCE_PATH_SEARCH = '/V1/search/ ' ;
16
21
17
22
/**
18
23
* @var \Magento\Framework\ObjectManagerInterface
19
24
*/
20
25
protected $ objectManager ;
21
26
27
+ /**
28
+ * @var array
29
+ */
30
+ private $ indexersState ;
31
+
32
+ /**
33
+ * @var mixed
34
+ */
35
+ private $ indexerRegistry ;
36
+
22
37
protected function setUp (): void
23
38
{
24
39
$ this ->objectManager = Bootstrap::getObjectManager ();
40
+ $ this ->indexerRegistry = $ this ->objectManager ->get (IndexerRegistry::class);
25
41
}
26
42
27
43
/**
28
44
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_duplicated.php
29
45
* @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped.php
30
46
*/
31
- public function testSave ()
47
+ public function testSave (): void
32
48
{
33
49
$ productSku = 'grouped-product ' ;
34
50
$ linkType = 'associated ' ;
@@ -46,7 +62,7 @@ public function testSave()
46
62
$ serviceInfo = [
47
63
'rest ' => [
48
64
'resourcePath ' => self ::RESOURCE_PATH . $ productSku . '/links ' ,
49
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_PUT ,
65
+ 'httpMethod ' => Request::HTTP_METHOD_PUT ,
50
66
],
51
67
'soap ' => [
52
68
'service ' => self ::SERVICE_NAME ,
@@ -64,4 +80,106 @@ public function testSave()
64
80
});
65
81
$ this ->assertEquals ($ productData , $ actual [2 ]);
66
82
}
83
+
84
+ /**
85
+ * @magentoApiDataFixture Magento/Catalog/_files/product_simple_duplicated.php
86
+ * @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped.php
87
+ */
88
+ public function testLinkWithScheduledIndex (): void
89
+ {
90
+ $ this ->setIndexScheduled ();
91
+ $ productSkuGrouped = 'grouped-product ' ;
92
+ $ productSimple = 'simple-1 ' ;
93
+ $ linkType = 'associated ' ;
94
+ $ productData = [
95
+ 'sku ' => $ productSkuGrouped ,
96
+ 'link_type ' => $ linkType ,
97
+ 'linked_product_type ' => 'simple ' ,
98
+ 'linked_product_sku ' => $ productSimple ,
99
+ 'position ' => 3 ,
100
+ 'extension_attributes ' => [
101
+ 'qty ' => (float ) 300.0000 ,
102
+ ],
103
+ ];
104
+ $ serviceInfo = [
105
+ 'rest ' => [
106
+ 'resourcePath ' => self ::RESOURCE_PATH . $ productSkuGrouped . '/links ' ,
107
+ 'httpMethod ' => Request::HTTP_METHOD_PUT ,
108
+ ],
109
+ 'soap ' => [
110
+ 'service ' => self ::SERVICE_NAME ,
111
+ 'serviceVersion ' => self ::SERVICE_VERSION ,
112
+ 'operation ' => self ::SERVICE_NAME . 'Save ' ,
113
+ ],
114
+ ];
115
+ $ this ->_webApiCall ($ serviceInfo , ['entity ' => $ productData ]);
116
+
117
+ $ searchCriteria = $ this ->buildSearchCriteria ($ productSimple );
118
+ $ serviceInfo = $ this ->buildSearchServiceInfo ($ searchCriteria );
119
+ $ response = $ this ->_webApiCall ($ serviceInfo , $ searchCriteria );
120
+ $ this ->assertArrayHasKey ('search_criteria ' , $ response );
121
+ $ this ->assertArrayHasKey ('items ' , $ response );
122
+ $ this ->assertGreaterThan (1 , count ($ response ['items ' ]));
123
+ $ this ->assertGreaterThan (0 , $ response ['items ' ][0 ]['id ' ]);
124
+ $ this ->restoreIndexMode ();
125
+ }
126
+
127
+ /**
128
+ * @param string $productSku
129
+ * @return array
130
+ */
131
+ private function buildSearchCriteria (string $ productSku ): array
132
+ {
133
+ return [
134
+ 'searchCriteria ' => [
135
+ 'request_name ' => 'quick_search_container ' ,
136
+ 'filter_groups ' => [
137
+ [
138
+ 'filters ' => [
139
+ [
140
+ 'field ' => 'search_term ' ,
141
+ 'value ' => $ productSku ,
142
+ ]
143
+ ]
144
+ ]
145
+ ]
146
+ ]
147
+ ];
148
+ }
149
+
150
+ /**
151
+ * @param array $searchCriteria
152
+ * @return array
153
+ */
154
+ private function buildSearchServiceInfo (array $ searchCriteria ): array
155
+ {
156
+ return [
157
+ 'rest ' => [
158
+ 'resourcePath ' => self ::RESOURCE_PATH_SEARCH . '? ' . http_build_query ($ searchCriteria ),
159
+ 'httpMethod ' => Request::HTTP_METHOD_GET
160
+ ],
161
+ 'soap ' => [
162
+ 'service ' => self ::SERVICE_NAME_SEARCH ,
163
+ 'serviceVersion ' => self ::SERVICE_VERSION ,
164
+ 'operation ' => self ::SERVICE_NAME_SEARCH . 'Search '
165
+ ]
166
+ ];
167
+ }
168
+
169
+ private function setIndexScheduled (): void
170
+ {
171
+ $ indexerListIds = $ this ->objectManager ->get (Config::class)->getIndexers ();
172
+ foreach ($ indexerListIds as $ indexerId ) {
173
+ $ indexer = $ this ->indexerRegistry ->get ($ indexerId ['indexer_id ' ]);
174
+ $ this ->indexersState [$ indexerId ['indexer_id ' ]] = $ indexer ->isScheduled ();
175
+ $ indexer ->setScheduled (true );
176
+ }
177
+ }
178
+
179
+ private function restoreIndexMode (): void
180
+ {
181
+ foreach ($ this ->indexersState as $ indexerId => $ state ) {
182
+ $ this ->indexerRegistry ->get ($ indexerId )->setScheduled ($ state );
183
+ }
184
+ }
67
185
}
0 commit comments