From fe53d23be76519528e6d43aceca83b8fdb7b4ef2 Mon Sep 17 00:00:00 2001 From: Dmytro Androshchuk Date: Tue, 11 Jun 2019 14:24:43 +0300 Subject: [PATCH 1/4] add missing semicolon --- .../view/frontend/web/js/attribute-filter.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/module-elasticsuite-catalog/view/frontend/web/js/attribute-filter.js b/src/module-elasticsuite-catalog/view/frontend/web/js/attribute-filter.js index 602f7d41d..55674516e 100644 --- a/src/module-elasticsuite-catalog/view/frontend/web/js/attribute-filter.js +++ b/src/module-elasticsuite-catalog/view/frontend/web/js/attribute-filter.js @@ -211,7 +211,8 @@ define([ */ addItemId: function (item) { item.id = _.uniqueId(this.index + "_option_"); - item.displayProductCount = this.displayProductCount && (item.count >= 1) + item.displayProductCount = this.displayProductCount && (item.count >= 1); + return item; }, }); From 9e56837a12a4b6bdb900007181d99366961887a9 Mon Sep 17 00:00:00 2001 From: Richard BAYET Date: Thu, 20 Jun 2019 12:12:51 +0200 Subject: [PATCH 2/4] Allow access to the current tracker visitor or session id --- .../Helper/Data.php | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/module-elasticsuite-tracker/Helper/Data.php b/src/module-elasticsuite-tracker/Helper/Data.php index 16a5cc9b7..573bea73f 100644 --- a/src/module-elasticsuite-tracker/Helper/Data.php +++ b/src/module-elasticsuite-tracker/Helper/Data.php @@ -76,21 +76,29 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper */ private $sessionManager; + /** + * @var \Magento\Framework\Stdlib\CookieManagerInterface + */ + private $cookieManager; + /** * PHP Constructor * * @param \Magento\Framework\App\Helper\Context $context The current context * @param \Magento\Store\Model\StoreManagerInterface $storeManager The Store Manager * @param \Magento\Framework\Session\SessionManagerInterface $sessionManager Session Manager + * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager Cookie manager */ public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, - \Magento\Framework\Session\SessionManagerInterface $sessionManager + \Magento\Framework\Session\SessionManagerInterface $sessionManager, + \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager ) { $this->urlBuilder = $context->getUrlBuilder(); $this->storeManager = $storeManager; $this->sessionManager = $sessionManager; + $this->cookieManager = $cookieManager; parent::__construct($context); } @@ -168,4 +176,40 @@ public function getRetentionDelay() { return (int) $this->scopeConfig->getValue(self::CONFIG_RETENTION_DELAY_XPATH); } + + /** + * Return the current tracker visitor id + * + * @return null|string + */ + public function getCurrentVisitorId() + { + $visitorId = null; + + $cookieConfig = $this->getCookieConfig(); + if (array_key_exists('visitor_cookie_name', $cookieConfig)) { + $visitorCookieName = $cookieConfig['visitor_cookie_name']; + $visitorId = $this->cookieManager->getCookie($visitorCookieName); + } + + return $visitorId; + } + + /** + * Return the current tracker session id + * + * @return null|string + */ + public function getCurrentSessionId() + { + $visitorId = null; + + $cookieConfig = $this->getCookieConfig(); + if (array_key_exists('visit_cookie_name', $cookieConfig)) { + $sessionCookieName = $cookieConfig['visit_cookie_name']; + $visitorId = $this->cookieManager->getCookie($sessionCookieName); + } + + return $visitorId; + } } From 1a75375f2a065b36242a5403f3a34b84bcadf78f Mon Sep 17 00:00:00 2001 From: Dmytro Androshchuk Date: Fri, 21 Jun 2019 10:39:17 +0300 Subject: [PATCH 3/4] add check empty array in optimizer preview, code optimization and add tests --- .../Model/Optimizer/Preview.php | 38 +- .../Test/Unit/Model/Optimizer/PreviewTest.php | 462 ++++++++++++++++++ 2 files changed, 485 insertions(+), 15 deletions(-) create mode 100644 src/module-elasticsuite-catalog-optimizer/Test/Unit/Model/Optimizer/PreviewTest.php diff --git a/src/module-elasticsuite-catalog-optimizer/Model/Optimizer/Preview.php b/src/module-elasticsuite-catalog-optimizer/Model/Optimizer/Preview.php index be3870cd2..4840c5474 100644 --- a/src/module-elasticsuite-catalog-optimizer/Model/Optimizer/Preview.php +++ b/src/module-elasticsuite-catalog-optimizer/Model/Optimizer/Preview.php @@ -161,25 +161,33 @@ private function getOptimizedResults() } /** - * Indicates if the current optmizer can be applied to the search context. + * Indicates if the current optimizer can be applied to the search context. * * @return boolean */ - private function canApply() + private function canApply() : bool { - $canApply = in_array($this->containerConfiguration->getName(), $this->optimizer->getSearchContainer()); - - if ($canApply && $this->containerConfiguration->getName() == 'quick_search_container') { - $config = $this->optimizer->getQuickSearchContainer(); - if ($config['apply_to'] == 1) { - $queries = array_column($config['query_ids'], 'query_text'); - $canApply = in_array($this->queryText, $queries); - } - } elseif ($canApply && $this->containerConfiguration->getName() == 'catalog_view_container') { - $config = $this->optimizer->getCatalogViewContainer(); - if ($config['apply_to'] == 1) { - $categoryIds = array_filter($config['category_ids']); - $canApply = in_array($this->category->getId(), $categoryIds); + $canApply = false; + if (!empty($this->optimizer->getSearchContainer()) + && in_array($this->containerConfiguration->getName(), $this->optimizer->getSearchContainer(), true)) { + switch ($this->containerConfiguration->getName()) { + case 'catalog_product_autocomplete': + $canApply = true; + break; + case 'quick_search_container': + $config = $this->optimizer->getQuickSearchContainer(); + if ((int) $config['apply_to'] === 1 && !empty($config['query_ids'])) { + $queries = array_column($config['query_ids'], 'query_text'); + $canApply = in_array($this->queryText, $queries, true); + } + break; + case 'catalog_view_container': + $config = $this->optimizer->getCatalogViewContainer(); + if ((int) $config['apply_to'] === 1 && !empty($config['category_ids'])) { + $categoryIds = array_filter($config['category_ids']); + $canApply = in_array($this->category->getId(), $categoryIds, true); + } + break; } } diff --git a/src/module-elasticsuite-catalog-optimizer/Test/Unit/Model/Optimizer/PreviewTest.php b/src/module-elasticsuite-catalog-optimizer/Test/Unit/Model/Optimizer/PreviewTest.php new file mode 100644 index 000000000..f36310e4a --- /dev/null +++ b/src/module-elasticsuite-catalog-optimizer/Test/Unit/Model/Optimizer/PreviewTest.php @@ -0,0 +1,462 @@ + + * @copyright 2019 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +namespace Smile\ElasticsuiteCore\Test\Unit\Model; + +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use PHPUnit_Framework_MockObject_MockObject; +use ReflectionClass; +use Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\ApplierListFactory; +use Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Preview; +use Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Preview\ResultsBuilder; +use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface; +use Magento\Catalog\Api\Data\CategoryInterface; +use Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Collection\ProviderFactory; +use Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer; +use Smile\ElasticsuiteCore\Search\Request\ContainerConfiguration; + +/** + * Optimiser Preview unit testing. + * + * @category Smile + * @package Smile\ElasticsuiteCatalogOptimizer + * @author Dmytro ANDROSHCHUK + */ +class PreviewTest extends TestCase +{ + /** + * @var Preview\ItemFactory + */ + private $previewItemFactory; + + /** + * @var \Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer + */ + private $optimizer; + + /** + * @var ContainerConfiguration + */ + private $containerConfiguration; + + /** + * @var \Magento\Catalog\Api\Data\CategoryInterface + */ + private $category; + + /** + * @var \Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Collection\ProviderFactory + */ + private $providerFactory; + + /** + * @var \Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Preview\ResultsBuilder + */ + private $previewResultsBuilder; + + /** + * @var \Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Preview + */ + private $preview; + + /** + * @var ApplierListFactory + */ + private $applierListFactory; + + /** + * {@inheritDoc} + */ + protected function setUp() + { + $this->optimizer = $this->getOptimizerMock(); + $this->previewItemFactory = $this->getPreviewItemFactoryMock(); + $this->applierListFactory = $this->getApplierListFactoryMock(); + $this->providerFactory = $this->getProviderFactoryMock(); + $this->containerConfiguration = $this->getContainerConfigMock(); + $this->previewResultsBuilder = $this->getResultsBuilderMock(); + $this->category = $this->getCategoryMock(); + $this->preview = new Preview( + $this->optimizer, + $this->previewItemFactory, + $this->applierListFactory, + $this->providerFactory, + $this->containerConfiguration, + $this->previewResultsBuilder, + $this->category + ); + } + + /** + * Test can apply. + * + * @return void. + * @dataProvider dataProvider + * + * @param array $searchContainers Search Containers + * @param array $name Container Name + * @param array $quickSearchContainer Quick Search Container + * @param array $catalogViewContainer Catalog View Container + * @param string $queryText Query Text + * @param integer $category Category + * @param boolean $expectedResult Expected result + */ + public function testCanApply( + $searchContainers, + $name, + $quickSearchContainer, + $catalogViewContainer, + $queryText, + $category, + $expectedResult + ) : void { + $class = new ReflectionClass(Preview::class); + $method = $class->getMethod('canApply'); + $method->setAccessible(true); + + $this->optimizer->method('getSearchContainer')->willReturn($searchContainers); + $this->optimizer->method('getQuickSearchContainer')->willReturn($quickSearchContainer); + $this->optimizer->method('getCatalogViewContainer')->willReturn($catalogViewContainer); + $this->category->method('getId')->willReturn($category); + $this->containerConfiguration->method('getName')->willReturn($name); + + $reflection = new ReflectionClass($this->preview); + $property = $reflection->getProperty('queryText'); + $property->setAccessible(true); + $property->setValue($this->preview, $queryText); + + $result = $method->invoke($this->preview); + + $this->assertEquals($expectedResult, $result); + } + + /** + * Can apply test data provider. + * + * @return array + */ + public function dataProvider(): array + { + $data = [ + [['quick_search_container'], 'catalog_product_autocomplete', null, null, null, null, false], + [ + ['catalog_view_container', 'catalog_product_autocomplete'], + 'catalog_product_autocomplete', + null, + null, + null, + null, + true, + ], + [ + ['catalog_view_container'], + 'catalog_product_autocomplete', + null, + null, + null, + null, + false, + ], + [null, 'quick_search_container', null, null, null, null, false], + [[], 'quick_search_container', null, null, null, null, false], + [['quick_search_container'], 'quick_search_container', null, null, null, null, false], + [ + ['catalog_view_container', 'quick_search_container', 'catalog_product_autocomplete'], + 'quick_search_container', + [ + 'apply_to' => 1, + 'query_ids' => [ + [ + 'id' => 1, + 'query_text' => 'watch', + ], + [ + 'id' => 2, + 'query_text' => 'skirt', + ], + ], + ], + [ + 'apply_to' => 1, + 'category_ids' => [ + 7, + 3, + 9, + ], + ], + 'skirt', + 3, + true, + ], + [ + ['catalog_view_container', 'quick_search_container', 'catalog_product_autocomplete'], + 'quick_search_container', + [ + 'apply_to' => false, + 'query_ids' => [ + [ + 'id' => 1, + 'query_text' => 'watch', + ], + [ + 'id' => 2, + 'query_text' => 'skirt', + ], + ], + ], + [ + 'apply_to' => 1, + 'category_ids' => [ + 7, 3, 9, + ], + ], + 'skirt', + 3, + false, + ], + [ + ['catalog_view_container', 'quick_search_container', 'catalog_product_autocomplete'], + 'catalog_view_container', + [ + 'apply_to' => 1, + 'query_ids' => [ + [ + 'id' => 1, + 'query_text' => 'watch', + ], + [ + 'id' => 2, + 'query_text' => 'skirt', + ], + ], + ], + [ + 'apply_to' => false, + 'category_ids' => [ + 7, 3, 9, + ], + ], + 'skirt', + 3, + false, + ], + [ + ['catalog_view_container', 'catalog_product_autocomplete'], + 'catalog_view_container', + [ + 'apply_to' => 1, + 'query_ids' => [ + [ + 'id' => 1, + 'query_text' => 'watch', + ], + [ + 'id' => 2, + 'query_text' => 'skirt', + ], + ], + ], + [ + 'apply_to' => 1, + 'category_ids' => [ + 7, 3, 9, + ], + ], + 'skirt', + 3, + true, + ], + [ + ['catalog_view_container', 'quick_search_container', 'catalog_product_autocomplete'], + 'quick_search_container', + [ + 'apply_to' => 1, + 'query_ids' => [ + [ + 'id' => 1, + 'query_text' => 'watch', + ], + [ + 'id' => 2, + 'query_text' => 'skirt', + ], + ], + ], + [ + 'apply_to' => 1, + 'category_ids' => [ + 7, 3, 9, + ], + ], + 'jacket', + 3, + false, + ], + [ + ['catalog_view_container', 'quick_search_container', 'catalog_product_autocomplete'], + 'quick_search_container', + [ + 'apply_to' => 1, + 'query_ids' => [ + [ + 'id' => 1, + 'query_text' => 'watch', + ], + [ + 'id' => 2, + 'query_text' => 'skirt', + ], + ], + ], + [ + 'apply_to' => 1, + 'category_ids' => [ + 7, 3, 9, + ], + ], + 'jacket', + 4, + false, + ], + [ + ['catalog_view_container', 'quick_search_container'], + 'catalog_product_autocomplete', + [ + 'apply_to' => 1, + ], + [ + 'apply_to' => 1, + 'category_ids' => [ + 7, 3, 9, + ], + ], + 'skirt', + 4, + false, + ], + [ + ['catalog_view_container', 'quick_search_container'], + 'catalog_product_autocomplete', + [ + 'apply_to' => 1, + 'query_ids' => [ + [ + 'id' => 1, + 'query_text' => 'watch', + ], + [ + 'id' => 2, + 'query_text' => 'skirt', + ], + ], + ], + [ + 'apply_to' => 1, + ], + 'skirt', + 4, + false, + ], + ]; + + return $data; + } + + /** + * Generate results builder mock. + * + * @return \PHPUnit\Framework\MockObject\MockObject + */ + private function getResultsBuilderMock() : MockObject + { + return $this->getMockBuilder(ResultsBuilder::class)->disableOriginalConstructor()->getMock(); + } + + /** + * Generate container config mock. + * + * @return PHPUnit_Framework_MockObject_MockObject + */ + private function getContainerConfigMock(): PHPUnit_Framework_MockObject_MockObject + { + return $this->getMockBuilder(ContainerConfigurationInterface::class) + ->getMock(); + } + + /** + * Generate category mock. + * + * @return PHPUnit_Framework_MockObject_MockObject + */ + private function getCategoryMock(): PHPUnit_Framework_MockObject_MockObject + { + return $this + ->getMockBuilder(CategoryInterface::class) + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * Generate optimizer mock. + * + * @return PHPUnit_Framework_MockObject_MockObject + */ + private function getOptimizerMock(): PHPUnit_Framework_MockObject_MockObject + { + return $this + ->getMockBuilder(Optimizer::class) + ->disableOriginalConstructor() + ->setMethods(['getSearchContainer', 'getQuickSearchContainer', 'getCatalogViewContainer']) + ->getMock(); + } + + /** + * Generate item factory mock. + * + * @return PHPUnit_Framework_MockObject_MockObject + */ + private function getPreviewItemFactoryMock(): PHPUnit_Framework_MockObject_MockObject + { + return $this + ->getMockBuilder('\Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Preview\ItemFactory') + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * Generate applier list factory mock. + * + * @return PHPUnit_Framework_MockObject_MockObject + */ + private function getApplierListFactoryMock(): PHPUnit_Framework_MockObject_MockObject + { + return $this + ->getMockBuilder('\Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\ApplierListFactory') + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * Generate provider factory mock. + * + * @return PHPUnit_Framework_MockObject_MockObject + */ + private function getProviderFactoryMock(): PHPUnit_Framework_MockObject_MockObject + { + return $this + ->getMockBuilder(ProviderFactory::class) + ->disableOriginalConstructor() + ->getMock(); + } +} From 5a6bb6630d27ea59828f5234f6546924e9d0266b Mon Sep 17 00:00:00 2001 From: Dmytro Androshchuk Date: Fri, 21 Jun 2019 15:45:13 +0300 Subject: [PATCH 4/4] suppress php mess detector warnings --- .../Model/Optimizer/Preview.php | 1 + .../Test/Unit/Model/Optimizer/PreviewTest.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/module-elasticsuite-catalog-optimizer/Model/Optimizer/Preview.php b/src/module-elasticsuite-catalog-optimizer/Model/Optimizer/Preview.php index 4840c5474..241b6aaae 100644 --- a/src/module-elasticsuite-catalog-optimizer/Model/Optimizer/Preview.php +++ b/src/module-elasticsuite-catalog-optimizer/Model/Optimizer/Preview.php @@ -164,6 +164,7 @@ private function getOptimizedResults() * Indicates if the current optimizer can be applied to the search context. * * @return boolean + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ private function canApply() : bool { diff --git a/src/module-elasticsuite-catalog-optimizer/Test/Unit/Model/Optimizer/PreviewTest.php b/src/module-elasticsuite-catalog-optimizer/Test/Unit/Model/Optimizer/PreviewTest.php index f36310e4a..c70f05f39 100644 --- a/src/module-elasticsuite-catalog-optimizer/Test/Unit/Model/Optimizer/PreviewTest.php +++ b/src/module-elasticsuite-catalog-optimizer/Test/Unit/Model/Optimizer/PreviewTest.php @@ -32,6 +32,7 @@ * @category Smile * @package Smile\ElasticsuiteCatalogOptimizer * @author Dmytro ANDROSHCHUK + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class PreviewTest extends TestCase { @@ -145,6 +146,7 @@ public function testCanApply( * Can apply test data provider. * * @return array + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function dataProvider(): array {