Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexHandlerTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Catalog\Model\Product\Action as ProductAction;
16 use Magento\Elasticsearch\Model\Client\Elasticsearch as ElasticsearchClient;
20 
30 class IndexHandlerTest extends \PHPUnit\Framework\TestCase
31 {
35  private $productRepository;
36 
40  private $client;
41 
45  private $storeManager;
46 
50  private $storeIds;
51 
55  private $entityType;
56 
60  private $indexer;
61 
65  private $searchIndexNameResolver;
66 
70  protected function setUp()
71  {
72  $connectionManager = Bootstrap::getObjectManager()->create(ConnectionManager::class);
73  $this->client = $connectionManager->getConnection();
74 
75  $this->storeManager = Bootstrap::getObjectManager()->create(StoreManagerInterface::class);
76  $this->storeIds = array_keys($this->storeManager->getStores());
77 
78  $clientConfig = Bootstrap::getObjectManager()->create(Config::class);
79  $this->entityType = $clientConfig->getEntityType();
80 
81  $this->indexer = Bootstrap::getObjectManager()->create(Indexer::class);
82  $this->indexer->load(CatalogSearchFulltextIndexer::INDEXER_ID);
83  $this->indexer->reindexAll();
84 
85  $this->searchIndexNameResolver = Bootstrap::getObjectManager()->create(SearchIndexNameResolver::class);
86  $this->productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
87  }
88 
94  public function testReindexAll(): void
95  {
96  $productApple = $this->productRepository->get('fulltext-1');
97  foreach ($this->storeIds as $storeId) {
98  $products = $this->searchByName('Apple', $storeId);
99  $this->assertCount(1, $products);
100  $this->assertEquals($productApple->getId(), $products[0]['_id']);
101 
102  $products = $this->searchByName('Simple Product', $storeId);
103  $this->assertCount(5, $products);
104  }
105  }
106 
113  public function testReindexRowAfterEdit(): void
114  {
115  $this->storeManager->setCurrentStore('admin');
116  $productApple = $this->productRepository->get('fulltext-1');
117  $productApple->setName('Simple Product Cucumber');
118  $this->productRepository->save($productApple);
119 
120  foreach ($this->storeIds as $storeId) {
121  $products = $this->searchByName('Apple', $storeId);
122  $this->assertCount(0, $products);
123 
124  $products = $this->searchByName('Cucumber', $storeId);
125  $this->assertCount(1, $products);
126  $this->assertEquals($productApple->getId(), $products[0]['_id']);
127 
128  $products = $this->searchByName('Simple Product', $storeId);
129  $this->assertCount(5, $products);
130  }
131  }
132 
138  public function testReindexRowAfterMassAction(): void
139  {
140  $productApple = $this->productRepository->get('fulltext-1');
141  $productBanana = $this->productRepository->get('fulltext-2');
142  $productIds = [
143  $productApple->getId(),
144  $productBanana->getId(),
145  ];
146  $attrData = [
147  'name' => 'Simple Product Common',
148  ];
150  $action = Bootstrap::getObjectManager()->get(ProductAction::class);
151 
152  foreach ($this->storeIds as $storeId) {
153  $action->updateAttributes($productIds, $attrData, $storeId);
154 
155  $products = $this->searchByName('Apple', $storeId);
156  $this->assertCount(0, $products);
157 
158  $products = $this->searchByName('Banana', $storeId);
159  $this->assertCount(0, $products);
160 
161  $products = $this->searchByName('Unknown', $storeId);
162  $this->assertCount(0, $products);
163 
164  $products = $this->searchByName('Common', $storeId);
165  $this->assertCount(2, $products);
166 
167  $products = $this->searchByName('Simple Product', $storeId);
168  $this->assertCount(5, $products);
169  }
170  }
171 
178  public function testReindexRowAfterDelete(): void
179  {
180  $productBanana = $this->productRepository->get('fulltext-2');
181  $this->productRepository->delete($productBanana);
182 
183  foreach ($this->storeIds as $storeId) {
184  $products = $this->searchByName('Banana', $storeId);
185  $this->assertEmpty($products);
186 
187  $products = $this->searchByName('Simple Product', $storeId);
188  $this->assertCount(4, $products);
189  }
190  }
191 
200  public function testReindexRowAfterUpdateStockStatus(): void
201  {
202  foreach ($this->storeIds as $storeId) {
203  $products = $this->searchByName('ProductOption1', $storeId);
204  $this->assertNotEmpty($products);
205  }
206  $product = $this->productRepository->get('simple_10');
208  $stockRegistry = Bootstrap::getObjectManager()->create(StockRegistryInterface::class);
209  $stockItem = $stockRegistry->getStockItem($product->getId());
210  $stockItem->setIsInStock(false);
212  $stockRepository = Bootstrap::getObjectManager()->create(StockItemRepositoryInterface::class);
214 
215  foreach ($this->storeIds as $storeId) {
216  $products = $this->searchByName('ProductOption1', $storeId);
217  $this->assertEmpty($products);
218 
219  $products = $this->searchByName('Configurable', $storeId);
220  $this->assertNotEmpty($products);
221  }
222  }
223 
231  private function searchByName(string $text, int $storeId): array
232  {
233  $index = $this->searchIndexNameResolver->getIndexName($storeId, $this->indexer->getId());
234  $searchQuery = [
235  'index' => $index,
236  'type' => $this->entityType,
237  'body' => [
238  'query' => [
239  'bool' => [
240  'minimum_should_match' => 1,
241  'should' => [
242  [
243  'match' => [
244  'name' => $text,
245  ],
246  ],
247  ],
248  ],
249  ],
250  ],
251  ];
252  $queryResult = $this->client->query($searchQuery);
253  $products = isset($queryResult['hits']['hits']) ? $queryResult['hits']['hits'] : [];
254 
255  return $products;
256  }
257 }
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$stockRepository
Definition: stock.php:19
$index
Definition: list.phtml:44