Skip to content

Commit

Permalink
Merge pull request #1523 from magento-performance/MAGETWO-75935
Browse files Browse the repository at this point in the history
[PERFORMANCE] - PAT updates
  • Loading branch information
vzabaznov committed Sep 29, 2017
2 parents 9c14af7 + 3bbb6b3 commit 29bc9ad
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
/**
* @var string|null
*/
private $order = null;
private $relevanceOrderDirection = null;

/**
* @var string
Expand Down Expand Up @@ -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();
}

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\Product\Visibility;
use Magento\TestFramework\Helper\Bootstrap;

Bootstrap::getInstance()->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 <b>shorts</b>')
->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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** @var \Magento\Framework\Registry $registry */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$registry = $objectManager->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);

0 comments on commit 29bc9ad

Please sign in to comment.