From cb0efb38b1cb54b6c1a5ca91403dedbed814db1e Mon Sep 17 00:00:00 2001 From: Stanislav Lopukhov Date: Fri, 15 Dec 2017 16:44:15 +0200 Subject: [PATCH] MAGETWO-85581: Enable metrics validation and run benchmark in multithread mode for PAT --- .../ResourceModel/Fulltext/Collection.php | 22 ++++++--- .../ResourceModel/Fulltext/CollectionTest.php | 39 ++++++++++++++++ .../products_with_the_same_search_score.php | 45 +++++++++++++++++++ ...ts_with_the_same_search_score_rollback.php | 21 +++++++++ 4 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Framework/Search/_files/products_with_the_same_search_score.php create mode 100644 dev/tests/integration/testsuite/Magento/Framework/Search/_files/products_with_the_same_search_score_rollback.php diff --git a/app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php b/app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php index fce0eb3871d63..379b21813860a 100644 --- a/app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php +++ b/app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php @@ -61,7 +61,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection /** * @var string|null */ - private $order = null; + private $relevanceOrderDirection = null; /** * @var string @@ -361,9 +361,19 @@ protected function _renderFiltersBefore() [] ); - if ($this->order && 'relevance' === $this->order['field']) { - $this->getSelect()->order('search_result.'. TemporaryStorage::FIELD_SCORE . ' ' . $this->order['dir']); + if ($this->relevanceOrderDirection) { + $this->getSelect()->order( + 'search_result.'. TemporaryStorage::FIELD_SCORE . ' ' . $this->relevanceOrderDirection + ); } + + /* + * This order is required to force search results be the same + * for the same requests and products with the same relevance + * NOTE: this does not replace existing orders but ADDs one more + */ + $this->setOrder('entity_id'); + return parent::_renderFiltersBefore(); } @@ -385,10 +395,12 @@ protected function _renderFilters() */ public function setOrder($attribute, $dir = Select::SQL_DESC) { - $this->order = ['field' => $attribute, 'dir' => $dir]; - if ($attribute !== 'relevance') { + if ($attribute === 'relevance') { + $this->relevanceOrderDirection = $dir; + } else { parent::setOrder($attribute, $dir); } + return $this; } diff --git a/dev/tests/integration/testsuite/Magento/CatalogSearch/Model/ResourceModel/Fulltext/CollectionTest.php b/dev/tests/integration/testsuite/Magento/CatalogSearch/Model/ResourceModel/Fulltext/CollectionTest.php index e9b5fd6d15766..93df194080b69 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogSearch/Model/ResourceModel/Fulltext/CollectionTest.php +++ b/dev/tests/integration/testsuite/Magento/CatalogSearch/Model/ResourceModel/Fulltext/CollectionTest.php @@ -31,6 +31,45 @@ public function testLoadWithFilterSearch($request, $filters, $expectedCount) $this->assertCount($expectedCount, $items); } + /** + * @magentoDataFixture Magento/Framework/Search/_files/products_with_the_same_search_score.php + */ + public function testSearchResultsAreTheSameForSameRequests() + { + $howManySearchRequests = 3; + $previousResult = null; + + $objManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + + foreach (range(1, $howManySearchRequests) as $i) { + /** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $fulltextCollection */ + $fulltextCollection = $objManager->create( + \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection::class, + ['searchRequestName' => 'quick_search_container'] + ); + + $fulltextCollection->addFieldToFilter('search_term', 'shorts'); + $fulltextCollection->setOrder('relevance'); + $fulltextCollection->load(); + $items = $fulltextCollection->getItems(); + $this->assertGreaterThan( + 0, + count($items), + sprintf("Search #%s result must not be empty", $i) + ); + + if ($previousResult) { + $this->assertEquals( + $previousResult, + array_keys($items), + "Search result must be the same for the same requests" + ); + } + + $previousResult = array_keys($items); + } + } + public function filtersDataProviderSearch() { return [ diff --git a/dev/tests/integration/testsuite/Magento/Framework/Search/_files/products_with_the_same_search_score.php b/dev/tests/integration/testsuite/Magento/Framework/Search/_files/products_with_the_same_search_score.php new file mode 100644 index 0000000000000..08925d7f503f7 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Framework/Search/_files/products_with_the_same_search_score.php @@ -0,0 +1,45 @@ +reinitialize(); + +/** @var ProductRepositoryInterface $productRepository */ +$productRepository = Bootstrap::getObjectManager() + ->create(ProductRepositoryInterface::class); + +$howManyProducts = 5; + +foreach (range(1, $howManyProducts) as $productId) { + $product = Bootstrap::getObjectManager()->create(Product::class); + $product->setTypeId(Type::TYPE_SIMPLE) + ->setAttributeSetId(4) + ->setWebsiteIds([1]) + ->setName('Cool short' . $productId) + ->setSku('cool_shorts_' . $productId) + ->setPrice(42) + ->setShortDescription("Some description about shorts") + ->setTaxClassId(0) + ->setDescription('Some description about shorts') + ->setVisibility(Visibility::VISIBILITY_BOTH) + ->setStatus(Status::STATUS_ENABLED) + ->setStockData( + [ + 'use_config_manage_stock' => 1, + 'qty' => 100, + 'is_qty_decimal' => 0, + 'is_in_stock' => 1, + ] + ); + + $productRepository->save($product); +} diff --git a/dev/tests/integration/testsuite/Magento/Framework/Search/_files/products_with_the_same_search_score_rollback.php b/dev/tests/integration/testsuite/Magento/Framework/Search/_files/products_with_the_same_search_score_rollback.php new file mode 100644 index 0000000000000..af3dbd121447a --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Framework/Search/_files/products_with_the_same_search_score_rollback.php @@ -0,0 +1,21 @@ +get(\Magento\Framework\Registry::class); + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', true); + +/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */ +$collection = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Product\Collection::class); +$collection->addAttributeToSelect('id')->load(); +if ($collection->count() > 0) { + $collection->delete(); +} + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', false);