Skip to content
Snippets Groups Projects
Commit 06c96e9a authored by Andrii Kasian's avatar Andrii Kasian
Browse files

Merge pull request #506 from magento-dragons/PR

[Dragons] Bugfixes
parents 1f7a63dc 61814b02
Branches
No related merge requests found
Showing
with 138 additions and 89 deletions
...@@ -15,6 +15,7 @@ use Magento\Framework\Api\ImageContentValidatorInterface; ...@@ -15,6 +15,7 @@ use Magento\Framework\Api\ImageContentValidatorInterface;
use Magento\Framework\Api\ImageProcessorInterface; use Magento\Framework\Api\ImageProcessorInterface;
use Magento\Framework\Api\SortOrder; use Magento\Framework\Api\SortOrder;
use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException; use Magento\Framework\Exception\StateException;
use Magento\Framework\Exception\ValidatorException; use Magento\Framework\Exception\ValidatorException;
...@@ -546,6 +547,8 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa ...@@ -546,6 +547,8 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
); );
} catch (ValidatorException $e) { } catch (ValidatorException $e) {
throw new CouldNotSaveException(__($e->getMessage())); throw new CouldNotSaveException(__($e->getMessage()));
} catch (LocalizedException $e) {
throw $e;
} catch (\Exception $e) { } catch (\Exception $e) {
throw new \Magento\Framework\Exception\CouldNotSaveException(__('Unable to save product')); throw new \Magento\Framework\Exception\CouldNotSaveException(__('Unable to save product'));
} }
......
...@@ -6,45 +6,58 @@ ...@@ -6,45 +6,58 @@
namespace Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin; namespace Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin;
use Magento\Catalog\Model\ResourceModel\Product as ResourceProduct;
use Magento\Framework\Model\AbstractModel;
class Product extends AbstractPlugin class Product extends AbstractPlugin
{ {
/** /**
* Reindex on product save * Reindex on product save
* *
* @param \Magento\Catalog\Model\ResourceModel\Product $productResource * @param ResourceProduct $productResource
* @param \Closure $proceed * @param \Closure $proceed
* @param \Magento\Framework\Model\AbstractModel $product * @param AbstractModel $product
* @return \Magento\Catalog\Model\ResourceModel\Product * @return ResourceProduct
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/ */
public function aroundSave( public function aroundSave(ResourceProduct $productResource, \Closure $proceed, AbstractModel $product)
\Magento\Catalog\Model\ResourceModel\Product $productResource, {
\Closure $proceed, return $this->addCommitCallback($productResource, $proceed, $product);
\Magento\Framework\Model\AbstractModel $product
) {
$productResource->addCommitCallback(function () use ($product) {
$this->reindexRow($product->getEntityId());
});
return $proceed($product);
} }
/** /**
* Reindex on product delete * Reindex on product delete
* *
* @param \Magento\Catalog\Model\ResourceModel\Product $productResource * @param ResourceProduct $productResource
* @param \Closure $proceed * @param \Closure $proceed
* @param \Magento\Framework\Model\AbstractModel $product * @param AbstractModel $product
* @return \Magento\Catalog\Model\ResourceModel\Product * @return ResourceProduct
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/ */
public function aroundDelete( public function aroundDelete(ResourceProduct $productResource, \Closure $proceed, AbstractModel $product)
\Magento\Catalog\Model\ResourceModel\Product $productResource, {
\Closure $proceed, return $this->addCommitCallback($productResource, $proceed, $product);
\Magento\Framework\Model\AbstractModel $product }
) {
$productResource->addCommitCallback(function () use ($product) { /**
$this->reindexRow($product->getEntityId()); * @param ResourceProduct $productResource
}); * @param \Closure $proceed
return $proceed($product); * @param AbstractModel $product
* @return ResourceProduct
* @throws \Exception
*/
private function addCommitCallback(ResourceProduct $productResource, \Closure $proceed, AbstractModel $product)
{
try {
$productResource->beginTransaction();
$result = $proceed($product);
$productResource->addCommitCallback(function () use ($product) {
$this->reindexRow($product->getEntityId());
});
$productResource->commit();
} catch (\Exception $e) {
$productResource->rollBack();
throw $e;
}
return $result;
} }
} }
...@@ -6,22 +6,28 @@ ...@@ -6,22 +6,28 @@
namespace Magento\CatalogSearch\Test\Unit\Model\Indexer\Fulltext\Plugin; namespace Magento\CatalogSearch\Test\Unit\Model\Indexer\Fulltext\Plugin;
use Magento\Catalog\Model\Product as ProductModel;
use Magento\Catalog\Model\ResourceModel\Product as ProductResourceModel;
use \Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product; use \Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Indexer\IndexerInterface;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
class ProductTest extends \PHPUnit_Framework_TestCase class ProductTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Indexer\IndexerInterface * @var \PHPUnit_Framework_MockObject_MockObject|IndexerInterface
*/ */
protected $indexerMock; protected $indexerMock;
/** /**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\ResourceModel\Product * @var \PHPUnit_Framework_MockObject_MockObject|ProductResourceModel
*/ */
protected $subjectMock; protected $subjectMock;
/** /**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product * @var \PHPUnit_Framework_MockObject_MockObject|ProductModel
*/ */
protected $productMock; protected $productMock;
...@@ -31,7 +37,7 @@ class ProductTest extends \PHPUnit_Framework_TestCase ...@@ -31,7 +37,7 @@ class ProductTest extends \PHPUnit_Framework_TestCase
protected $proceed; protected $proceed;
/** /**
* @var \Magento\Framework\Indexer\IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject * @var IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject
*/ */
protected $indexerRegistryMock; protected $indexerRegistryMock;
...@@ -42,30 +48,34 @@ class ProductTest extends \PHPUnit_Framework_TestCase ...@@ -42,30 +48,34 @@ class ProductTest extends \PHPUnit_Framework_TestCase
protected function setUp() protected function setUp()
{ {
$this->productMock = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false); $this->productMock = $this->getMockBuilder(ProductModel::class)
$this->subjectMock = $this->getMock('Magento\Catalog\Model\ResourceModel\Product', [], [], '', false); ->disableOriginalConstructor()
$this->indexerMock = $this->getMockForAbstractClass( ->getMock();
'Magento\Framework\Indexer\IndexerInterface', $this->subjectMock = $this->getMockBuilder(ProductResourceModel::class)
[], ->disableOriginalConstructor()
'', ->getMock();
false, $connection = $this->getMockBuilder(AdapterInterface::class)
false, ->disableOriginalConstructor()
true, ->getMockForAbstractClass();
['getId', 'getState', '__wakeup'] $this->subjectMock->method('getConnection')->willReturn($connection);
);
$this->indexerRegistryMock = $this->getMock( $this->indexerMock = $this->getMockBuilder(IndexerInterface::class)
'Magento\Framework\Indexer\IndexerRegistry', ->disableOriginalConstructor()
['get'], ->setMethods(['getId', 'getState', '__wakeup'])
[], ->getMockForAbstractClass();
'', $this->indexerRegistryMock = $this->getMockBuilder(IndexerRegistry::class)
false ->disableOriginalConstructor()
); ->setMethods(['get'])
->getMock();
$this->proceed = function () { $this->proceed = function () {
return $this->subjectMock; return $this->subjectMock;
}; };
$this->model = new Product($this->indexerRegistryMock); $this->model = (new ObjectManager($this))->getObject(
Product::class,
['indexerRegistry' => $this->indexerRegistryMock]
);
} }
public function testAfterSaveNonScheduled() public function testAfterSaveNonScheduled()
......
...@@ -58,7 +58,7 @@ abstract class AbstractSave implements ObserverInterface ...@@ -58,7 +58,7 @@ abstract class AbstractSave implements ObserverInterface
{ {
$this->_initEntity($observer); $this->_initEntity($observer);
if ($this->_isGoogleExperimentActive()) { if ($this->_isGoogleExperimentActive() && $this->isDataAvailable()) {
$this->_processCode(); $this->_processCode();
} }
...@@ -112,11 +112,10 @@ abstract class AbstractSave implements ObserverInterface ...@@ -112,11 +112,10 @@ abstract class AbstractSave implements ObserverInterface
*/ */
protected function _initRequestParams() protected function _initRequestParams()
{ {
$params = $this->_request->getParam('google_experiment'); if (!$this->isDataAvailable()) {
if (!is_array($params) || !isset($params['experiment_script']) || !isset($params['code_id'])) {
throw new \InvalidArgumentException('Wrong request parameters'); throw new \InvalidArgumentException('Wrong request parameters');
} }
$this->_params = $params; $this->_params = $this->getRequestData();
} }
/** /**
...@@ -181,4 +180,21 @@ abstract class AbstractSave implements ObserverInterface ...@@ -181,4 +180,21 @@ abstract class AbstractSave implements ObserverInterface
{ {
$this->_modelCode->delete(); $this->_modelCode->delete();
} }
/**
* @return array
*/
private function isDataAvailable()
{
$params = $this->getRequestData();
return is_array($params) && isset($params['experiment_script']) && isset($params['code_id']);
}
/**
* @return mixed
*/
private function getRequestData()
{
return $this->_request->getParam('google_experiment');
}
} }
...@@ -85,7 +85,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -85,7 +85,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
); );
$this->_requestMock->expects( $this->_requestMock->expects(
$this->once() $this->exactly(3)
)->method( )->method(
'getParam' 'getParam'
)->with( )->with(
...@@ -113,8 +113,6 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -113,8 +113,6 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
/** /**
* @param array $params * @param array $params
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Wrong request parameters
* @dataProvider dataProviderWrongRequestForCreating * @dataProvider dataProviderWrongRequestForCreating
*/ */
public function testCreatingCodeIfRequestIsNotValid($params) public function testCreatingCodeIfRequestIsNotValid($params)
...@@ -174,7 +172,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -174,7 +172,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
); );
$this->_requestMock->expects( $this->_requestMock->expects(
$this->once() $this->exactly(3)
)->method( )->method(
'getParam' 'getParam'
)->with( )->with(
...@@ -223,7 +221,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -223,7 +221,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
); );
$this->_requestMock->expects( $this->_requestMock->expects(
$this->once() $this->exactly(3)
)->method( )->method(
'getParam' 'getParam'
)->with( )->with(
...@@ -254,7 +252,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -254,7 +252,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
); );
$this->_requestMock->expects( $this->_requestMock->expects(
$this->once() $this->exactly(3)
)->method( )->method(
'getParam' 'getParam'
)->with( )->with(
......
...@@ -70,7 +70,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -70,7 +70,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
$this->_helperMock->expects($this->once())->method('isGoogleExperimentActive')->will($this->returnValue(true)); $this->_helperMock->expects($this->once())->method('isGoogleExperimentActive')->will($this->returnValue(true));
$this->_requestMock->expects( $this->_requestMock->expects(
$this->once() $this->exactly(3)
)->method( )->method(
'getParam' 'getParam'
)->with( )->with(
...@@ -98,8 +98,6 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -98,8 +98,6 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
/** /**
* @param array $params * @param array $params
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Wrong request parameters
* @dataProvider dataProviderWrongRequestForCreating * @dataProvider dataProviderWrongRequestForCreating
*/ */
public function testCreatingCodeIfRequestIsNotValid($params) public function testCreatingCodeIfRequestIsNotValid($params)
...@@ -143,7 +141,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -143,7 +141,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
$this->_helperMock->expects($this->once())->method('isGoogleExperimentActive')->will($this->returnValue(true)); $this->_helperMock->expects($this->once())->method('isGoogleExperimentActive')->will($this->returnValue(true));
$this->_requestMock->expects( $this->_requestMock->expects(
$this->once() $this->exactly(3)
)->method( )->method(
'getParam' 'getParam'
)->with( )->with(
...@@ -184,7 +182,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -184,7 +182,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
$this->_helperMock->expects($this->once())->method('isGoogleExperimentActive')->will($this->returnValue(true)); $this->_helperMock->expects($this->once())->method('isGoogleExperimentActive')->will($this->returnValue(true));
$this->_requestMock->expects( $this->_requestMock->expects(
$this->once() $this->exactly(3)
)->method( )->method(
'getParam' 'getParam'
)->with( )->with(
...@@ -215,7 +213,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -215,7 +213,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
); );
$this->_requestMock->expects( $this->_requestMock->expects(
$this->once() $this->exactly(3)
)->method( )->method(
'getParam' 'getParam'
)->with( )->with(
......
...@@ -85,7 +85,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -85,7 +85,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
); );
$this->_requestMock->expects( $this->_requestMock->expects(
$this->once() $this->exactly(3)
)->method( )->method(
'getParam' 'getParam'
)->with( )->with(
...@@ -113,8 +113,6 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -113,8 +113,6 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
/** /**
* @param array $params * @param array $params
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Wrong request parameters
* @dataProvider dataProviderWrongRequestForCreating * @dataProvider dataProviderWrongRequestForCreating
*/ */
public function testCreatingCodeIfRequestIsNotValid($params) public function testCreatingCodeIfRequestIsNotValid($params)
...@@ -174,7 +172,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -174,7 +172,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
); );
$this->_requestMock->expects( $this->_requestMock->expects(
$this->once() $this->exactly(3)
)->method( )->method(
'getParam' 'getParam'
)->with( )->with(
...@@ -223,7 +221,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -223,7 +221,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
); );
$this->_requestMock->expects( $this->_requestMock->expects(
$this->once() $this->exactly(3)
)->method( )->method(
'getParam' 'getParam'
)->with( )->with(
...@@ -254,7 +252,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase ...@@ -254,7 +252,7 @@ class SaveGoogleExperimentScriptObserverTest extends \PHPUnit_Framework_TestCase
); );
$this->_requestMock->expects( $this->_requestMock->expects(
$this->once() $this->exactly(3)
)->method( )->method(
'getParam' 'getParam'
)->with( )->with(
......
...@@ -79,6 +79,5 @@ class Sidebar extends ListCompare ...@@ -79,6 +79,5 @@ class Sidebar extends ListCompare
} }
); );
$this->_rootElement->find($this->clearAll)->click(); $this->_rootElement->find($this->clearAll)->click();
$this->browser->acceptAlert();
} }
} }
...@@ -56,7 +56,7 @@ abstract class AbstractResource ...@@ -56,7 +56,7 @@ abstract class AbstractResource
/** /**
* Subscribe some callback to transaction commit * Subscribe some callback to transaction commit
* *
* @param array $callback * @param callable|array $callback
* @return $this * @return $this
* @api * @api
*/ */
......
...@@ -84,7 +84,7 @@ class Search implements SearchInterface ...@@ -84,7 +84,7 @@ class Search implements SearchInterface
*/ */
private function addFieldToFilter($field, $condition = null) private function addFieldToFilter($field, $condition = null)
{ {
if (!is_array($condition) || !in_array(key($condition), ['from', 'to'])) { if (!is_array($condition) || !in_array(key($condition), ['from', 'to'], true)) {
$this->requestBuilder->bind($field, $condition); $this->requestBuilder->bind($field, $condition);
} else { } else {
if (!empty($condition['from'])) { if (!empty($condition['from'])) {
......
...@@ -66,29 +66,24 @@ class SearchTest extends \PHPUnit_Framework_TestCase ...@@ -66,29 +66,24 @@ class SearchTest extends \PHPUnit_Framework_TestCase
{ {
$requestName = 'requestName'; $requestName = 'requestName';
$scopeId = 333; $scopeId = 333;
$filterField = 'filterField'; $filters = [
$filterValue = 'filterValue'; $this->createFilterMock('array_filter', ['arrayValue1', 'arrayValue2']),
$this->createFilterMock('simple_filter', 'filterValue'),
$this->createFilterMock('from_filter', ['from' => 30]),
$this->createFilterMock('to_filter', ['to' => 100]),
$this->createFilterMock('range_filter', ['from' => 60, 'to' => 82]),
];
$scope = $this->getMockBuilder('Magento\Framework\App\ScopeInterface') $scope = $this->getMockBuilder('Magento\Framework\App\ScopeInterface')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMockForAbstractClass(); ->getMockForAbstractClass();
$filter = $this->getMockBuilder('Magento\Framework\Api\Filter')
->disableOriginalConstructor()
->getMock();
$filter->expects($this->once())
->method('getField')
->willReturn($filterField);
$filter->expects($this->once())
->method('getValue')
->willReturn($filterValue);
$filterGroup = $this->getMockBuilder('Magento\Framework\Api\Search\FilterGroup') $filterGroup = $this->getMockBuilder('Magento\Framework\Api\Search\FilterGroup')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$filterGroup->expects($this->once()) $filterGroup->expects($this->once())
->method('getFilters') ->method('getFilters')
->willReturn([$filter]); ->willReturn($filters);
$searchCriteria = $this->getMockBuilder('Magento\Framework\Api\Search\SearchCriteriaInterface') $searchCriteria = $this->getMockBuilder('Magento\Framework\Api\Search\SearchCriteriaInterface')
->disableOriginalConstructor() ->disableOriginalConstructor()
...@@ -118,7 +113,7 @@ class SearchTest extends \PHPUnit_Framework_TestCase ...@@ -118,7 +113,7 @@ class SearchTest extends \PHPUnit_Framework_TestCase
$this->requestBuilder->expects($this->once()) $this->requestBuilder->expects($this->once())
->method('bindDimension') ->method('bindDimension')
->with('scope', $scopeId); ->with('scope', $scopeId);
$this->requestBuilder->expects($this->any()) $this->requestBuilder->expects($this->exactly(6))
->method('bind'); ->method('bind');
$this->requestBuilder->expects($this->once()) $this->requestBuilder->expects($this->once())
->method('create') ->method('create')
...@@ -146,4 +141,23 @@ class SearchTest extends \PHPUnit_Framework_TestCase ...@@ -146,4 +141,23 @@ class SearchTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('Magento\Framework\Api\Search\SearchResultInterface', $searchResult); $this->assertInstanceOf('Magento\Framework\Api\Search\SearchResultInterface', $searchResult);
} }
/**
* @param $field
* @param $value
* @return \Magento\Framework\Api\Filter|\PHPUnit_Framework_MockObject_MockObject
*/
private function createFilterMock($field, $value)
{
$filter = $this->getMockBuilder('Magento\Framework\Api\Filter')
->disableOriginalConstructor()
->getMock();
$filter->expects($this->once())
->method('getField')
->willReturn($field);
$filter->expects($this->once())
->method('getValue')
->willReturn($value);
return $filter;
}
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment