diff --git a/app/code/Magento/Ui/Component/MassAction/Filter.php b/app/code/Magento/Ui/Component/MassAction/Filter.php index c24b77198030d7ebe1889ed3586b01becf6677ad..c23466ef0844e5237e2ae577cada08dd19086390 100644 --- a/app/code/Magento/Ui/Component/MassAction/Filter.php +++ b/app/code/Magento/Ui/Component/MassAction/Filter.php @@ -11,6 +11,7 @@ use Magento\Framework\View\Element\UiComponentFactory; use Magento\Framework\App\RequestInterface; use Magento\Framework\View\Element\UiComponentInterface; use Magento\Framework\Data\Collection\AbstractDb; +use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface; /** * Class Filter @@ -43,6 +44,11 @@ class Filter */ protected $filterBuilder; + /** + * @var DataProviderInterface + */ + private $dataProvider; + /** * @param UiComponentFactory $factory * @param RequestInterface $request @@ -74,23 +80,33 @@ class Filter } /** + * Adds filters to collection using DataProvider filter results + * * @param AbstractDb $collection * @return AbstractDb * @throws LocalizedException */ public function getCollection(AbstractDb $collection) { - $component = $this->getComponent(); - $this->prepareComponent($component); - $dataProvider = $component->getContext()->getDataProvider(); - $dataProvider->setLimit(0, false); - $ids = []; - foreach ($dataProvider->getSearchResult()->getItems() as $document) { - $ids[] = $document->getId(); - } + $selected = $this->request->getParam(static::SELECTED_PARAM); + $excluded = $this->request->getParam(static::EXCLUDED_PARAM); - $collection->addFieldToFilter($collection->getIdFieldName(), ['in' => $ids]); - return $this->applySelection($collection); + $isExcludedIdsValid = (is_array($excluded) && !empty($excluded)); + $isSelectedIdsValid = (is_array($selected) && !empty($selected)); + + if ('false' !== $excluded) { + if (!$isExcludedIdsValid && !$isSelectedIdsValid) { + throw new LocalizedException(__('Please select item(s).')); + } + } + $idsArray = $this->getFilterIds(); + if (!empty($idsArray)) { + $collection->addFieldToFilter( + $collection->getIdFieldName(), + ['in' => $idsArray] + ); + } + return $collection; } /** @@ -106,9 +122,7 @@ class Filter if ('false' === $excluded) { return; } - $component = $this->getComponent(); - $this->prepareComponent($component); - $dataProvider = $component->getContext()->getDataProvider(); + $dataProvider = $this->getDataProvider(); try { if (is_array($excluded) && !empty($excluded)) { $this->filterBuilder->setConditionType('nin') @@ -127,6 +141,8 @@ class Filter } /** + * Applies selection to collection from POST parameters + * * @param AbstractDb $collection * @return AbstractDb * @throws LocalizedException @@ -169,7 +185,7 @@ class Filter } /** - * Returns RefererUrl + * Returns Referrer Url * * @return string|null */ @@ -178,4 +194,33 @@ class Filter $data = $this->getComponent()->getContext()->getDataProvider()->getConfigData(); return (isset($data['referer_url'])) ? $data['referer_url'] : null; } + + /** + * Get data provider + * + * @return DataProviderInterface + */ + private function getDataProvider() + { + if (!$this->dataProvider) { + $component = $this->getComponent(); + $this->prepareComponent($component); + $this->dataProvider = $component->getContext()->getDataProvider(); + } + return $this->dataProvider; + } + + /** + * Get filter ids as array + * + * @return int[] + */ + private function getFilterIds() + { + $this->applySelectionOnTargetProvider(); + if ($this->getDataProvider()->getSearchResult()) { + return $this->getDataProvider()->getSearchResult()->getAllIds(); + } + return []; + } } diff --git a/app/code/Magento/Ui/Test/Unit/Component/MassAction/FilterTest.php b/app/code/Magento/Ui/Test/Unit/Component/MassAction/FilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..604febecde041df3e62aeae3cf135bb5e57e5398 --- /dev/null +++ b/app/code/Magento/Ui/Test/Unit/Component/MassAction/FilterTest.php @@ -0,0 +1,289 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Ui\Test\Unit\Component\MassAction; + +use Magento\Ui\Component\MassAction\Filter; +use Magento\Framework\Api\Filter as ApiFilter; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Framework\Data\Collection\AbstractDb; +use Magento\Framework\Api\FilterBuilder; +use Magento\Framework\View\Element\UiComponentFactory; +use Magento\Framework\App\RequestInterface; +use Magento\Framework\View\Element\UiComponentInterface; +use Magento\Framework\Api\Search\SearchResultInterface; +use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface; +use Magento\Framework\View\Element\UiComponent\ContextInterface; + +class FilterTest extends \PHPUnit_Framework_TestCase +{ + /** + *\PHPUnit_Framework_MockObject_MockObject + */ + private $requestMock; + + /** + * \PHPUnit_Framework_MockObject_MockObject + */ + private $uiComponentFactoryMock; + + /** + * \PHPUnit_Framework_MockObject_MockObject + */ + private $filterBuilderMock; + + /** @var \Magento\Ui\Component\MassAction\Filter */ + private $filter; + + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + + /** + *\PHPUnit_Framework_MockObject_MockObject + */ + private $dataProviderMock; + + /** + *\PHPUnit_Framework_MockObject_MockObject + */ + private $abstractDbMock; + + /** + * \PHPUnit_Framework_MockObject_MockObject + */ + private $searchResultMock; + + /** + * \PHPUnit_Framework_MockObject_MockObject + */ + private $uiComponentMock; + + /** + * Set up + */ + protected function setUp() + { + $this->objectManager = new ObjectManager($this); + $this->uiComponentFactoryMock = $this->getMock(UiComponentFactory::class, [], [], '', false); + $this->filterBuilderMock = $this->getMock(FilterBuilder::class, [], [], '', false); + $this->requestMock = $this->getMock(RequestInterface::class); + $this->dataProviderMock = $this->getMock(DataProviderInterface::class); + $this->uiComponentMock = $this->getMock(UiComponentInterface::class); + $this->abstractDbMock = $this->getMock(AbstractDb::class, [], [], '', false); + $contextMock = $this->getMock(ContextInterface::class); + $this->searchResultMock = $this->getMock(SearchResultInterface::class); + $uiComponentMockTwo = $this->getMock(UiComponentInterface::class); + $this->filter = $this->objectManager->getObject( + Filter::class, + [ + 'factory' => $this->uiComponentFactoryMock, + 'request' => $this->requestMock, + 'filterBuilder' => $this->filterBuilderMock + ] + ); + $this->uiComponentFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($this->uiComponentMock); + $this->uiComponentMock->expects($this->any()) + ->method('getChildComponents') + ->willReturn([$uiComponentMockTwo]); + $uiComponentMockTwo->expects($this->any()) + ->method('getChildComponents') + ->willReturn([]); + $this->uiComponentMock->expects($this->any()) + ->method('getContext') + ->willReturn($contextMock); + $contextMock->expects($this->any()) + ->method('getDataProvider') + ->willReturn($this->dataProviderMock); + $this->dataProviderMock->expects($this->any()) + ->method('setLimit'); + $this->dataProviderMock->expects($this->any()) + ->method('searchResultMock') + ->willReturn($this->searchResultMock); + $this->searchResultMock->expects($this->any()) + ->method('getAllIds') + ->willReturn([]); + } + + /** + * Run test for applySelectionOnTargetProvider method + * + * @param int[]|bool $selectedIds + * @param int[]|bool $excludedIds + * @param int $filterExpected + * @param string $conditionExpected + * @dataProvider applySelectionOnTargetProviderDataProvider + */ + public function testApplySelectionOnTargetProvider($selectedIds, $excludedIds, $filterExpected, $conditionExpected) + { + $this->setUpApplySelection($selectedIds, $excludedIds, $filterExpected, $conditionExpected); + $this->filter->applySelectionOnTargetProvider(); + } + + /** + * Data provider for testApplySelectionOnTargetProvider + */ + public function applySelectionOnTargetProviderDataProvider() + { + return [ + [[1, 2, 3], 'false' , 0, 'in'], + [[1, 2, 3], [1, 2, 3] , 1, 'nin'], + ['false', [1, 2, 3] , 1, 'nin'], + ['false', 'false' , 0, ''] + ]; + } + + /** + * @throws \Exception + * @expectedException \Magento\Framework\Exception\LocalizedException + */ + public function testApplySelectionOnTargetProviderException() + { + $filterMock = $this->getMock(ApiFilter::class, [], [], '', false); + $this->filterBuilderMock->expects($this->any()) + ->method('setConditionType') + ->willReturn($this->filterBuilderMock); + $this->filterBuilderMock->expects($this->any()) + ->method('create') + ->willReturn($filterMock); + $this->filterBuilderMock->expects($this->any()) + ->method('setField') + ->willReturn($this->filterBuilderMock); + $this->requestMock->expects($this->at(0)) + ->method('getParam') + ->with(Filter::SELECTED_PARAM) + ->willReturn([1]); + $this->requestMock->expects($this->at(1)) + ->method('getParam') + ->with(Filter::EXCLUDED_PARAM) + ->willReturn([]); + $this->dataProviderMock->expects($this->any()) + ->method('addFilter') + ->with($filterMock) + ->willThrowException(new \Exception('exception')); + $this->filter->applySelectionOnTargetProvider(); + } + + /** + * Run test for getCollection method + * + * @param int[]|bool $selectedIds + * @param int[]|bool $excludedIds + * @param int $filterExpected + * @param string $conditionExpected + * @dataProvider applySelectionOnTargetProviderDataProvider + */ + public function testGetCollection($selectedIds, $excludedIds, $filterExpected, $conditionExpected) + { + $this->setUpApplySelection($selectedIds, $excludedIds, $filterExpected, $conditionExpected); + $this->requestMock->expects($this->at(4)) + ->method('getParam') + ->with('namespace') + ->willReturn(''); + $this->requestMock->expects($this->at(2)) + ->method('getParam') + ->with(Filter::SELECTED_PARAM) + ->willReturn($selectedIds); + $this->requestMock->expects($this->at(3)) + ->method('getParam') + ->with(Filter::EXCLUDED_PARAM) + ->willReturn($excludedIds); + $this->assertEquals($this->abstractDbMock, $this->filter->getCollection($this->abstractDbMock)); + } + + /** + * This tests the method prepareComponent() + */ + public function testPrepareComponent() + { + $this->filter->prepareComponent($this->uiComponentMock); + } + + /** + * This tests the method getComponent() + */ + public function testGetComponent() + { + $this->requestMock->expects($this->at(0)) + ->method('getParam') + ->with('namespace') + ->willReturn(''); + $this->assertEquals($this->uiComponentMock, $this->filter->getComponent()); + } + + /** + * This tests the method getComponentRefererUrl() + */ + public function testGetComponentRefererUrlIsNotNull() + { + $returnArray = [ + 'referer_url' => 'referer_url' + ]; + $this->dataProviderMock->expects($this->once()) + ->method('getConfigData') + ->willReturn($returnArray); + $this->assertEquals('referer_url', $this->filter->getComponentRefererUrl()); + } + + /** + * This tests the method getComponentRefererUrl() + */ + public function testGetComponentRefererUrlIsNull() + { + $this->assertNull($this->filter->getComponentRefererUrl()); + } + + /** + * Apply mocks for current parameters from datasource + * + * @param int[]|bool $selectedIds + * @param int[]|bool $excludedIds + * @param int $filterExpected + * @param string $conditionExpected + */ + private function setUpApplySelection($selectedIds, $excludedIds, $filterExpected, $conditionExpected) + { + $filterMock = $this->getMock(ApiFilter::class, [], [], '', false); + $this->requestMock->expects($this->at(0)) + ->method('getParam') + ->with(Filter::SELECTED_PARAM) + ->willReturn($selectedIds); + $this->requestMock->expects($this->at(1)) + ->method('getParam') + ->with(Filter::EXCLUDED_PARAM) + ->willReturn($excludedIds); + $this->dataProviderMock->expects($this->exactly($filterExpected)) + ->method('addFilter') + ->with($filterMock); + $this->filterBuilderMock->expects($this->exactly($filterExpected)) + ->method('setConditionType') + ->with($conditionExpected) + ->willReturnSelf(); + $this->filterBuilderMock->expects($this->any()) + ->method('setField') + ->willReturnSelf(); + $this->filterBuilderMock->expects($this->any()) + ->method('value') + ->willReturnSelf(); + $this->filterBuilderMock->expects($this->any()) + ->method('create') + ->willReturn($filterMock); + $this->filterBuilderMock->expects($this->any()) + ->method('setConditionType') + ->willReturnSelf(); + $this->filterBuilderMock->expects($this->any()) + ->method('setField') + ->willReturnSelf(); + $this->filterBuilderMock->expects($this->any()) + ->method('value') + ->willReturnSelf(); + $this->filterBuilderMock->expects($this->any()) + ->method('create') + ->willReturn($filterMock); + } +} diff --git a/lib/internal/Magento/Framework/Api/Search/SearchResult.php b/lib/internal/Magento/Framework/Api/Search/SearchResult.php index f637ef27d7b2cb4889e509aac154997ff1454aa9..f4ece5c84dde049468b76b357ecb09e2e86c2230 100644 --- a/lib/internal/Magento/Framework/Api/Search/SearchResult.php +++ b/lib/internal/Magento/Framework/Api/Search/SearchResult.php @@ -85,4 +85,18 @@ class SearchResult extends AbstractSimpleObject implements SearchResultInterface { return $this->setData(self::TOTAL_COUNT, $totalCount); } + + /** + * Retrieve ids of all items + * + * @return array + */ + public function getAllIds() + { + $ids = []; + foreach ($this->getItems() as $item) { + $ids[] = $item->getId(); + } + return $ids; + } } diff --git a/lib/internal/Magento/Framework/Api/Search/SearchResultInterface.php b/lib/internal/Magento/Framework/Api/Search/SearchResultInterface.php index ffa4c0fe43cf2422efbefc5a2c1605ec78281711..d44a8be3fb0ecff9a5031d07d66a50785c31c9d0 100644 --- a/lib/internal/Magento/Framework/Api/Search/SearchResultInterface.php +++ b/lib/internal/Magento/Framework/Api/Search/SearchResultInterface.php @@ -7,6 +7,11 @@ namespace Magento\Framework\Api\Search; use Magento\Framework\Api\SearchResultsInterface; +/** + * Interface SearchResultInterface + * + * @api + */ interface SearchResultInterface extends SearchResultsInterface { /**#@+ @@ -48,4 +53,11 @@ interface SearchResultInterface extends SearchResultsInterface * @return \Magento\Framework\Api\Search\SearchCriteriaInterface */ public function getSearchCriteria(); + + /** + * Retrieve all ids from list + * + * @return int[] + */ + public function getAllIds(); } diff --git a/lib/internal/Magento/Framework/Api/Test/Unit/Search/SearchResultTest.php b/lib/internal/Magento/Framework/Api/Test/Unit/Search/SearchResultTest.php new file mode 100644 index 0000000000000000000000000000000000000000..5d289291996906e4b60b0940da8fafd893a720fc --- /dev/null +++ b/lib/internal/Magento/Framework/Api/Test/Unit/Search/SearchResultTest.php @@ -0,0 +1,72 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Framework\Api\Test\Unit\Search; + +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Framework\Api\Search\SearchResult; +use Magento\Framework\Api\Search\DocumentInterface; + +class SearchResultTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var SearchResult + */ + private $search; + + /** + * @var DocumentInterface[] + */ + private $items; + + /** + * @var ObjectManager + */ + private $objectManager; + + /** + * Set up + */ + protected function setUp() + { + $document1 = $this->getMock(DocumentInterface::class); + $document2 = $this->getMock(DocumentInterface::class); + + $this->items = [ $document1, $document2]; + $document1->expects($this->any()) + ->method('getId') + ->willReturn(1); + $document2->expects($this->any()) + ->method('getId') + ->willReturn(2); + + $data = [ + 'items' => $this->items + ]; + $this->objectManager = new ObjectManager($this); + $this->search = $this->objectManager->getObject( + SearchResult::class, + [ + 'data' => $data + ] + ); + } + + /** + * Test getAllIds + */ + public function testGetAllIds() + { + $this->assertEquals([1, 2], $this->search->getAllIds()); + } + + /** + * Test getItems + */ + public function testGetItems() + { + $this->assertEquals($this->items, $this->search->getItems()); + } +} diff --git a/lib/internal/Magento/Framework/HTTP/Adapter/Curl.php b/lib/internal/Magento/Framework/HTTP/Adapter/Curl.php index 181783c7b2115d8a216c681077e116e98344d15c..35d7b3ac5ee20ae669ccc66775498b3c0d9348b4 100644 --- a/lib/internal/Magento/Framework/HTTP/Adapter/Curl.php +++ b/lib/internal/Magento/Framework/HTTP/Adapter/Curl.php @@ -28,6 +28,7 @@ class Curl implements \Zend_Http_Client_Adapter_Interface ), 'verifypeer' => true, 'verifyhost' => 2, + 'sslversion' => 6 ]; /** @@ -53,6 +54,7 @@ class Curl implements \Zend_Http_Client_Adapter_Interface 'protocols' => CURLOPT_PROTOCOLS, 'verifypeer' => CURLOPT_SSL_VERIFYPEER, 'verifyhost' => CURLOPT_SSL_VERIFYHOST, + 'sslversion' => CURLOPT_SSLVERSION, ]; /** diff --git a/lib/internal/Magento/Framework/HTTP/Client/Curl.php b/lib/internal/Magento/Framework/HTTP/Client/Curl.php index 67525f62f6336cf7de844e866a153be223dc0b5c..5ebc92abf70d9063af63683f5f530dd01470865a 100644 --- a/lib/internal/Magento/Framework/HTTP/Client/Curl.php +++ b/lib/internal/Magento/Framework/HTTP/Client/Curl.php @@ -9,14 +9,17 @@ namespace Magento\Framework\HTTP\Client; * Class to work with HTTP protocol using curl library * * @author Magento Core Team <core@magentocommerce.com> + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ class Curl implements \Magento\Framework\HTTP\ClientInterface { + const SSL_VERSION = 6; + /** * Max supported protocol by curl CURL_SSLVERSION_TLSv1_2 * @var int */ - private static $sslVersion = 6; + private $sslVersion; /** * Hostname @@ -86,7 +89,7 @@ class Curl implements \Magento\Framework\HTTP\ClientInterface /** * Curl - * @var object + * @var resource */ protected $_ch; @@ -117,10 +120,11 @@ class Curl implements \Magento\Framework\HTTP\ClientInterface } /** - * Constructor + * @param int|null $sslVersion */ - public function __construct() + public function __construct($sslVersion = self::SSL_VERSION) { + $this->sslVersion = $sslVersion; } /** @@ -377,10 +381,9 @@ class Curl implements \Magento\Framework\HTTP\ClientInterface $this->curlOption(CURLOPT_PORT, $this->_port); } - //$this->curlOption(CURLOPT_HEADER, 1); $this->curlOption(CURLOPT_RETURNTRANSFER, 1); $this->curlOption(CURLOPT_HEADERFUNCTION, [$this, 'parseHeaders']); - $this->curlOption(CURLOPT_SSLVERSION, self::$sslVersion); + $this->curlOption(CURLOPT_SSLVERSION, $this->sslVersion); if (count($this->_curlUserOptions)) { foreach ($this->_curlUserOptions as $k => $v) { @@ -415,6 +418,7 @@ class Curl implements \Magento\Framework\HTTP\ClientInterface * @param resource $ch curl handle, not needed * @param string $data * @return int + * @throws \Exception * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ protected function parseHeaders($ch, $data) @@ -422,11 +426,10 @@ class Curl implements \Magento\Framework\HTTP\ClientInterface if ($this->_headerCount == 0) { $line = explode(" ", trim($data), 3); if (count($line) != 3) { - return $this->doError("Invalid response line returned from server: " . $data); + $this->doError("Invalid response line returned from server: " . $data); } $this->_responseStatus = intval($line[1]); } else { - //var_dump($data); $name = $value = ''; $out = explode(": ", trim($data), 2); if (count($out) == 2) {