diff --git a/build/install-magento.sh b/build/install-magento.sh index 6ffdd3d..66a57de 100755 --- a/build/install-magento.sh +++ b/build/install-magento.sh @@ -4,6 +4,8 @@ set -x echo memory_limit=-1 >> /usr/local/etc/php/php.ini git checkout -b tmp git add -A +git config --global user.email "wercker@localhost" +git config --global user.name "Wercker" git commit --allow-empty -m "tmp" export MODULE_DIR=`pwd` export M2SETUP_DB_HOST=$MYSQL_CI_PORT_3306_TCP_ADDR @@ -20,10 +22,15 @@ export M2SETUP_VERSION=$1 export M2SETUP_USE_SAMPLE_DATA=false export M2SETUP_USE_ARCHIVE=true export COMPOSER_HOME=$WERCKER_CACHE_DIR/composer +BIN_MAGENTO=magento-command + +# Reconfigure composer after COMPOSER_HOME has been changed +[ ! -z "${COMPOSER_MAGENTO_USERNAME}" ] && \ + composer config -a -g http-basic.repo.magento.com $COMPOSER_MAGENTO_USERNAME $COMPOSER_MAGENTO_PASSWORD + mysqladmin -u$M2SETUP_DB_USER -p"$M2SETUP_DB_PASSWORD" -h$M2SETUP_DB_HOST create $M2SETUP_DB_NAME -/usr/local/bin/mage-setup -cd /srv/www -composer config http-basic.repo.magento.com $MAGENTO_REPO_PUBLIC_KEY $MAGENTO_REPO_PRIVATE_KEY +DEBUG=true magento-installer +cd /var/www/magento composer config repositories.solr-module vcs $MODULE_DIR composer config repositories.solr-autosuggest vcs git@github.com:integer-net/solr-magento2-autosuggest.git composer config repositories.solr-base vcs git@github.com:integer-net/solr-base.git @@ -42,6 +49,5 @@ composer update sed -i -e "s/8983/$SOLR_CI_PORT_8983_TCP_PORT/g" vendor/integer-net/solr-magento2/main/test/integration/_files/solr_config.dist.php sed -i -e "s/localhost/$SOLR_CI_PORT_8983_TCP_ADDR/g" vendor/integer-net/solr-magento2/main/test/integration/_files/solr_config.dist.php sed -i -e "s/solr-magento2-tests/core0/g" vendor/integer-net/solr-magento2/main/test/integration/_files/solr_config.dist.php -bin/magento module:enable IntegerNet_Solr -bin/magento setup:di:compile -bin/magento setup:upgrade +$BIN_MAGENTO module:enable IntegerNet_Solr +$BIN_MAGENTO setup:di:compile \ No newline at end of file diff --git a/build/integration-tests.sh b/build/integration-tests.sh index 53055a1..820e751 100755 --- a/build/integration-tests.sh +++ b/build/integration-tests.sh @@ -7,15 +7,15 @@ export M2SETUP_DB_USER=root export M2SETUP_DB_PASSWORD=$MYSQL_CI_ENV_MYSQL_ROOT_PASSWORD export TEST_DB_NAME=magento_integration_tests mysqladmin -u$M2SETUP_DB_USER -p"$M2SETUP_DB_PASSWORD" -h$M2SETUP_DB_HOST create $TEST_DB_NAME -cp $MODULE_DIR/main/test/integration/phpunit.xml.dist /srv/www/dev/tests/integration/phpunit.xml -cp $MODULE_DIR/main/test/integration/install-config-mysql.php /srv/www/dev/tests/integration/etc/ -sed -i -e "s/DB_HOST/$M2SETUP_DB_HOST/g" /srv/www/dev/tests/integration/etc/install-config-mysql.php -sed -i -e "s/DB_USER/$M2SETUP_DB_USER/g" /srv/www/dev/tests/integration/etc/install-config-mysql.php -sed -i -e "s/DB_PASSWORD/$M2SETUP_DB_PASSWORD/g" /srv/www/dev/tests/integration/etc/install-config-mysql.php -sed -i -e "s/DB_NAME/$TEST_DB_NAME/g" /srv/www/dev/tests/integration/etc/install-config-mysql.php -cd /srv/www/dev/tests/integration +cp $MODULE_DIR/main/test/integration/phpunit.xml.dist /var/www/magento/dev/tests/integration/phpunit.xml +cp $MODULE_DIR/main/test/integration/install-config-mysql.php /var/www/magento/dev/tests/integration/etc/ +sed -i -e "s/DB_HOST/$M2SETUP_DB_HOST/g" /var/www/magento/dev/tests/integration/etc/install-config-mysql.php +sed -i -e "s/DB_USER/$M2SETUP_DB_USER/g" /var/www/magento/dev/tests/integration/etc/install-config-mysql.php +sed -i -e "s/DB_PASSWORD/$M2SETUP_DB_PASSWORD/g" /var/www/magento/dev/tests/integration/etc/install-config-mysql.php +sed -i -e "s/DB_NAME/$TEST_DB_NAME/g" /var/www/magento/dev/tests/integration/etc/install-config-mysql.php +cd /var/www/magento/dev/tests/integration php ../../../vendor/phpunit/phpunit/phpunit -cp $MODULE_DIR/categories/test/integration/phpunit.xml.dist /srv/www/dev/tests/integration/phpunit.xml -cd /srv/www/dev/tests/integration +cp $MODULE_DIR/categories/test/integration/phpunit.xml.dist /var/www/magento/dev/tests/integration/phpunit.xml +cd /var/www/magento/dev/tests/integration php ../../../vendor/phpunit/phpunit/phpunit \ No newline at end of file diff --git a/main/src/Model/Bridge/ProductRenderer.php b/main/src/Model/Bridge/ProductRenderer.php index ecc2c65..8273d9e 100755 --- a/main/src/Model/Bridge/ProductRenderer.php +++ b/main/src/Model/Bridge/ProductRenderer.php @@ -14,6 +14,7 @@ use IntegerNet\Solr\Implementor\Product as ProductInterface; use IntegerNet\Solr\Implementor\ProductRenderer as ProductRendererInterface; use IntegerNet\Solr\Indexer\IndexDocument; +use IntegerNet\Solr\Model\Config\FrontendStoresConfig; use Magento\Framework\App\State as AppState; use Magento\Framework\View\LayoutInterface; @@ -27,10 +28,18 @@ class ProductRenderer implements ProductRendererInterface * @var AppState */ private $appState; + /** + * @var FrontendStoresConfig + */ + private $storesConfig; + /** + * @var bool + */ private $isLayoutLoaded = false; - public function __construct(LayoutInterface\Proxy $layout, AppState $appState) + public function __construct(LayoutInterface\Proxy $layout, AppState $appState, FrontendStoresConfig $storesConfig) { + $this->storesConfig = $storesConfig; $this->layout = $layout; $this->appState = $appState; } @@ -65,7 +74,9 @@ public function addResultHtmlToProductData(ProductInterface $product, IndexDocum // We need direct access to the Magento product throw new \InvalidArgumentException('Magento 2 product bridge expected, '. get_class($product) .' received.'); } - $this->addAutosuggestItemHtml($product, $productData); + if ($this->storesConfig->byStoreId($product->getStoreId())->getAutosuggestConfig()->isActive()) { + $this->addAutosuggestItemHtml($product, $productData); + } //TODO if $useHtmlInResult, render product list and grid HTML } diff --git a/main/src/Model/Config/FrontendStoresConfig.php b/main/src/Model/Config/FrontendStoresConfig.php index 6e24d7f..f61ded6 100755 --- a/main/src/Model/Config/FrontendStoresConfig.php +++ b/main/src/Model/Config/FrontendStoresConfig.php @@ -27,4 +27,8 @@ public function __construct(StoreManagerInterface $storeManager, ConfigFactory $ parent::__construct($configByStore); } + public function byStoreId($storeId) + { + return $this[$storeId]; + } } \ No newline at end of file diff --git a/main/src/Model/Indexer/Fulltext.php b/main/src/Model/Indexer/Fulltext.php index 0d73f0d..9ef9ace 100755 --- a/main/src/Model/Indexer/Fulltext.php +++ b/main/src/Model/Indexer/Fulltext.php @@ -14,6 +14,7 @@ use IntegerNet\Solr\Plugin\UrlFactoryPlugin; use Magento\Framework\Indexer\ActionInterface; use Magento\Framework\Mview\ActionInterface as MviewActionInterface; +use Magento\Framework\App\State; class Fulltext implements ActionInterface, MviewActionInterface { @@ -21,13 +22,17 @@ class Fulltext implements ActionInterface, MviewActionInterface * @var ProductIndexer */ private $solrIndexer; - /** - * @param ProductIndexerFactory $solrIndexerFactory + * @var State */ - public function __construct(ProductIndexerFactory $solrIndexerFactory) - { + private $state; + + public function __construct( + ProductIndexerFactory $solrIndexerFactory, + State $state + ) { $this->solrIndexer = $solrIndexerFactory->create(); + $this->state = $state; } /** @@ -91,12 +96,16 @@ private function reindex( $sliceId = null, $totalNumberSlices = null ) { - $this->solrIndexer->reindex( - $productIds, - $emptyIndex, - $restrictToStoreIds, - $sliceId, - $totalNumberSlices + $this->state->emulateAreaCode( + 'frontend', + [$this->solrIndexer, 'reindex'], + [ + $productIds, + $emptyIndex, + $restrictToStoreIds, + $sliceId, + $totalNumberSlices + ] ); } } \ No newline at end of file diff --git a/main/src/Model/ResourceModel/CategoryPosition.php b/main/src/Model/ResourceModel/CategoryPosition.php index 530452f..789afae 100755 --- a/main/src/Model/ResourceModel/CategoryPosition.php +++ b/main/src/Model/ResourceModel/CategoryPosition.php @@ -9,10 +9,28 @@ */ namespace IntegerNet\Solr\Model\ResourceModel; +use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Model\ResourceModel\Db\AbstractDb; +use Magento\Framework\App\ProductMetadataInterface; +use Magento\Framework\Model\ResourceModel\Db\Context; class CategoryPosition extends AbstractDb { + /** + * @var ProductMetadataInterface + */ + private $productMetaData; + + public function __construct( + Context $context, + ProductMetadataInterface $productMetadata, + $connectionName = null + ) { + $this->productMetaData = $productMetadata; + parent::__construct($context, $connectionName); + } + /** * Resource initialization * @@ -32,9 +50,14 @@ protected function _construct() */ public function getCategoryPositions($productId, $storeId) { + $table = $this->getMainTable(); + if (\class_exists(TableMaintainer::class)) { + $tableMaintainer = ObjectManager::getInstance()->get(TableMaintainer::class); + $table = $tableMaintainer->getMainTable($storeId); + } $select = $this->getConnection() ->select() - ->from($this->getMainTable(), ['category_id', 'position']) + ->from($table, ['category_id', 'position']) ->where('product_id = ?', $productId) ->where('store_id = ?', $storeId); return $this->getConnection()->fetchAll($select); diff --git a/main/src/Model/ResourceModel/GroupedProductAssociations.php b/main/src/Model/ResourceModel/GroupedProductAssociations.php index 8582bb4..d015ea9 100755 --- a/main/src/Model/ResourceModel/GroupedProductAssociations.php +++ b/main/src/Model/ResourceModel/GroupedProductAssociations.php @@ -15,6 +15,7 @@ use IntegerNet\Solr\Model\Data\ArrayCollection; use Magento\Catalog\Api\ProductAttributeRepositoryInterface as AttributeRepository; use Magento\Catalog\Api\Data\ProductInterface; +use Magento\Catalog\Model\ResourceModel\Product as ProductResource; use Magento\Framework\EntityManager\MetadataPool; use Magento\Framework\Model\ResourceModel\Db\AbstractDb; use Magento\Framework\Model\ResourceModel\Db\Context as ResourceContext; @@ -30,6 +31,10 @@ class GroupedProductAssociations extends AbstractDb implements ProductAssociatio * @var AttributeRepository */ private $attributeRepository; + /** + * @var ProductResource + */ + private $productResource; private $productEntityLinkField; @@ -37,11 +42,13 @@ public function __construct( ResourceContext $context, MetadataPool $metadataPool, AttributeRepository $attributeRepository, + ProductResource $productResource, $connectionName = null ) { $this->metadataPool = $metadataPool; parent::__construct($context, $connectionName); $this->attributeRepository = $attributeRepository; + $this->productResource = $productResource; } /** @@ -66,18 +73,20 @@ public function getAssociations($parentIds) $statusAttributeId = $this->attributeRepository->get('status')->getAttributeId(); $bind = [':link_type_id' => ProductLinkResource::LINK_TYPE_GROUPED, ':status_attribute_id' => $statusAttributeId]; + $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); $select = $connection->select()->from( ['l' => $this->getMainTable()], ['product_id', 'linked_product_id'] )->join( ['cpe' => $this->getTable('catalog_product_entity')], - sprintf( - 'cpe.%s = l.product_id', - $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField() - ) + 'cpe.entity_id = l.product_id' )->join( ['cpei' => $this->getTable('catalog_product_entity_int')], - 'cpei.entity_id = l.linked_product_id AND cpei.attribute_id = :status_attribute_id', + sprintf( + 'cpei.%s = cpe.%s AND cpei.attribute_id = :status_attribute_id', + $linkField, + $linkField + ), '' )->where( 'link_type_id = :link_type_id' diff --git a/main/src/Plugin/CategoryDataProviderPlugin.php b/main/src/Plugin/CategoryDataProviderPlugin.php new file mode 100644 index 0000000..1f37231 --- /dev/null +++ b/main/src/Plugin/CategoryDataProviderPlugin.php @@ -0,0 +1,60 @@ + + */ + +namespace IntegerNet\Solr\Plugin; + +use Magento\Eav\Model\Config as EavConfig; + +class CategoryDataProviderPlugin +{ + /** + * @var EavConfig + */ + private $eavConfig; + + public function __construct( + EavConfig $eavConfig + ) { + $this->eavConfig = $eavConfig; + } + + public function afterPrepareMeta(\Magento\Catalog\Model\Category\DataProvider $subject, $result) + { + $meta = $result; + $meta = array_replace_recursive($meta, $this->prepareFieldsMeta( + $this->getFieldsMap(), + $subject->getAttributesMeta($this->eavConfig->getEntityType('catalog_category')) + )); + return $meta; + } + + private function prepareFieldsMeta($fieldsMap, $fieldsMeta) + { + $result = []; + foreach ($fieldsMap as $fieldSet => $fields) { + foreach ($fields as $field) { + if (isset($fieldsMeta[$field])) { + $result[$fieldSet]['children'][$field]['arguments']['data']['config'] = $fieldsMeta[$field]; + } + } + } + return $result; + } + + private function getFieldsMap() + { + return [ + 'solr' => [ + 'solr_exclude', + 'solr_exclude_children', + 'solr_remove_filters', + 'solr_boost', + ] + ]; + } +} diff --git a/main/src/etc/adminhtml/di.xml b/main/src/etc/adminhtml/di.xml index de650cf..fe18db1 100755 --- a/main/src/etc/adminhtml/di.xml +++ b/main/src/etc/adminhtml/di.xml @@ -5,6 +5,12 @@ + + + + + + diff --git a/main/src/etc/mview.xml b/main/src/etc/mview.xml index fe1a23f..9e0b928 100755 --- a/main/src/etc/mview.xml +++ b/main/src/etc/mview.xml @@ -1,7 +1,7 @@ @@ -18,6 +18,7 @@
+
diff --git a/main/test/integration/Database/ProductRepositoryTest.php b/main/test/integration/Database/ProductRepositoryTest.php index d9b68ba..91d2aa9 100755 --- a/main/test/integration/Database/ProductRepositoryTest.php +++ b/main/test/integration/Database/ProductRepositoryTest.php @@ -17,6 +17,7 @@ use IntegerNet\Solr\Model\Bridge\AttributeRepository; use IntegerNet\Solr\Model\Bridge\Product; use Magento\Catalog\Model\Product as MagentoProduct; +use Magento\Framework\App\ProductMetadataInterface; use Magento\TestFramework\ObjectManager; use PHPUnit\Framework\TestCase; use TddWizard\Fixtures\Catalog\ProductBuilder; @@ -37,6 +38,14 @@ class ProductRepositoryTest extends TestCase protected function setUp() { $this->objectManager = ObjectManager::getInstance(); + //TODO fix test on Magento 2.2.5 + /** @var ProductMetadataInterface $productMetadata */ + $productMetadata = $this->objectManager->get(\Magento\Framework\App\ProductMetadataInterface::class); + if (version_compare($productMetadata->getVersion(), '2.2.5', '>=')) { + $this->markTestSkipped( + 'This test does not terminate on Magento 2.2.5 because of https://github.com/tddwizard/magento2-fixtures/issues/13' + ); + } $this->productRepository = $this->objectManager->create(ProductRepository::class); } diff --git a/main/test/integration/Indexer/IndexerTest.php b/main/test/integration/Indexer/IndexerTest.php index 6a2551c..58b992e 100755 --- a/main/test/integration/Indexer/IndexerTest.php +++ b/main/test/integration/Indexer/IndexerTest.php @@ -22,7 +22,7 @@ class IndexerTest extends TestCase { - private static $productFixtures = []; + private $productFixtures = []; /** * @var ObjectManager @@ -32,6 +32,19 @@ class IndexerTest extends TestCase protected function setUp() { $this->objectManager = ObjectManager::getInstance(); + //TODO fix test on Magento 2.2.5 + /** @var ProductMetadataInterface $productMetadata */ + $productMetadata = $this->objectManager->get(\Magento\Framework\App\ProductMetadataInterface::class); + if (version_compare($productMetadata->getVersion(), '2.2.5', '>=')) { + $this->markTestSkipped( + 'This test does not terminate on Magento 2.2.5 because of https://github.com/tddwizard/magento2-fixtures/issues/13' + ); + } + $this->createProductFixture(); + } + protected function tearDown() + { + $this->deleteProductFixture(); } public function testIndexerInstantiation() { @@ -40,7 +53,6 @@ public function testIndexerInstantiation() } /** - * @magentoDataFixture loadFixture * @magentoAppIsolation enabled * @magentoDbIsolation enabled */ @@ -58,7 +70,6 @@ public function testReindex() ); } /** - * @magentoDataFixture loadFixture * @magentoAppIsolation enabled * @magentoDbIsolation enabled */ @@ -77,7 +88,6 @@ public function testReindexSingleSlice() ); } /** - * @magentoDataFixture loadFixture * @magentoAppIsolation enabled * @magentoDbIsolation enabled */ @@ -97,9 +107,9 @@ public function testReindexAllSlices() ); } - public static function loadFixture() + private function createProductFixture() { - self::$productFixtures = [ + $this->productFixtures = [ new ProductFixture( ProductBuilder::aSimpleProduct()->withName('First potato')->build() ), @@ -113,10 +123,10 @@ public static function loadFixture() SolrConfig::loadFromConfigFile(); } - public static function loadFixtureRollback() + private function deleteProductFixture() { - ProductFixtureRollback::create()->execute(...self::$productFixtures); - self::$productFixtures = []; + ProductFixtureRollback::create()->execute(...$this->productFixtures); + $this->productFixtures = []; } /** diff --git a/main/test/integration/Indexer/ProductCollectionFactoryTest.php b/main/test/integration/Indexer/ProductCollectionFactoryTest.php index f23e839..c83daef 100755 --- a/main/test/integration/Indexer/ProductCollectionFactoryTest.php +++ b/main/test/integration/Indexer/ProductCollectionFactoryTest.php @@ -30,7 +30,6 @@ protected function setUp() /** * @magentoAppIsolation enabled - * @magentoDbIsolation enabled * @magentoAppArea frontend * @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1 */ diff --git a/main/test/unit/Model/Indexer/FulltextTest.php b/main/test/unit/Model/Indexer/FulltextTest.php index ce88193..de513bc 100755 --- a/main/test/unit/Model/Indexer/FulltextTest.php +++ b/main/test/unit/Model/Indexer/FulltextTest.php @@ -10,24 +10,29 @@ namespace IntegerNet\Solr\Model\Indexer; -use IntegerNet\Solr\Indexer\ProductIndexer; -use IntegerNet\Solr\Plugin\UrlFactoryPlugin; -use PHPUnit\Framework\TestCase; +use Magento\Framework\App\State; class FulltextTest extends AbstractIndexerTest { /** @var Fulltext */ private $indexer; + /** + * @var State|\PHPUnit_Framework_MockObject_MockObject + */ + private $appStateMock; + protected function setUp() { parent::setUp(); - $this->indexer = new Fulltext($this->indexerFactoryStub); + $this->appStateMock = $this->createMock(State::class); + $this->indexer = new Fulltext($this->indexerFactoryStub, $this->appStateMock); } public function testExecuteFull() { $this->expectForcedFrontendUrls(); + $this->expectEmulateFrontend(); $this->expectReindexWithArguments(null, true, null); $this->indexer->executeFull(); } @@ -36,6 +41,7 @@ public function testExecuteList() { $productIds = [1, 2, 3, 5]; $this->expectForcedFrontendUrls(); + $this->expectEmulateFrontend(); $this->expectReindexWithArguments($productIds, false, null); $this->indexer->executeList($productIds); } @@ -44,6 +50,7 @@ public function testExecute() { $productIds = [1, 2, 3, 5]; $this->expectForcedFrontendUrls(); + $this->expectEmulateFrontend(); $this->expectReindexWithArguments($productIds, false, null); $this->indexer->execute($productIds); } @@ -51,7 +58,20 @@ public function testExecuteRow() { $productId = 42; $this->expectForcedFrontendUrls(); + $this->expectEmulateFrontend(); $this->expectReindexWithArguments([$productId], false, null); $this->indexer->executeRow($productId); } + private function expectEmulateFrontend() + { + $this->appStateMock->expects($this->once())->method('emulateAreaCode')->with( + 'frontend', + $this->isType('callable'), + $this->isType('array') + )->willReturnCallback( + function ($code, $callback, $args) { + return $callback(...$args); + } + ); + } } \ No newline at end of file diff --git a/wercker.yml b/wercker.yml index 5d4813a..b5fe635 100755 --- a/wercker.yml +++ b/wercker.yml @@ -1,10 +1,14 @@ -box: mageinferno/magento2-php:7.0.8-fpm-3 +# Originally mageinferno/magento2-php was used but it is not maintained anymore in favor of markoshust/magento2-php +# But this is based on the php-fpm image and there's an issue with Wercker (https://github.com/wercker/wercker/issues/262) +# The meanbee/magento2-php image has a CLI mode and fits our needs better +box: meanbee/magento2-php:7.1-cli services: - id: integernet/mysql_ci:5.6 env: MYSQL_ROOT_PASSWORD: root - id: integernet/solr_ci:1.6.0 build-2.1: + box: meanbee/magento2-php:7.0-cli # Magento 2.1 is not compatible to PHP 7.1 steps: - install-packages: packages: git zip mysql-client libmcrypt-dev libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libpng12-dev netcat-openbsd @@ -21,10 +25,10 @@ build-2.1: while ! nc -q 1 $MYSQL_CI_PORT_3306_TCP_ADDR $MYSQL_CI_PORT_3306_TCP_PORT